6-3 矩阵链成 uva 442

时间:2021-05-11 04:19:32

较为简单的栈题

思路比较好

一次ac

1、char word ;word=A;直接  a[word]=xxxx,不用 a[‘word’]=xxxx

#include<bits/stdc++.h>

using namespace std;

struct aaa
{ int x;
int y; }a[]; int main()
{
stack<aaa>k;
int n;
cin>>n;getchar();
for(int i=;i<=n;i++)
{
char word;cin>>word;
cin>>a[word].x>>a[word].y; } string s;
while(getline(cin,s))
{
if(s=="")continue;
if(s.size()==){printf("0\n");continue;}
int sum=;int ok=;
for(int i=;i<s.size();i++)
{
if(isalpha(s[i])){k.push(a[ s[i] ]);}
else if(s[i]==')')
{
struct aaa x1=k.top();k.pop();
struct aaa x2=k.top();k.pop();
struct aaa x3;
if(x1.x!=x2.y){ok=;printf("error\n");break;}
sum+=x1.x*x1.y*x2.x;
x3.x=x2.x;x3.y=x1.y;
k.push(x3); } }
if(ok)printf("%d\n",sum); } return ;
}