#include<stdio.h>
#include<string.h>
#include<math.h>
struct
{
int row,col;
}a[30];
int main()
{
int n,i,r,w,top1,top2,sr[1000],sc[1000],ans;
char c,s[1000],s1[1000];
while(scanf("%d",&n)!=EOF)
{
getchar();
for(i=0;i<n;i++)
{
c=getchar();
scanf("%d%d",&a[c-'A'].row,&a[c-'A'].col);
getchar();
}
while(gets(s))
{
int len=strlen(s);
for(i=0,top1=0,top2=0,ans=0;i<len;i++)
if(s[i]=='('||s[i]==')')
{
if(!top1)
s1[top1++]=s[i];
else if(s[i]==')')
{
top1--;
if(sc[top2-2]==sr[top2-1])
{
ans+=sc[top2-2]*sc[top2-1]*sr[top2-2];
top2--;
sc[top2-1]=sc[top2];
}
else
break;
}
else
s1[top1++]=s[i];
}
else
{
sr[top2]=a[s[i]-'A'].row;
sc[top2]=a[s[i]-'A'].col;
top2++;
}
if(i!=len)
printf("error\n");
else
printf("%d\n",ans);
}
}
return 0;
}
UVa 442 Matrix Chain Multiplication
最新推荐文章于 2022-02-25 19:38:17 发布