-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path442.cpp
More file actions
67 lines (57 loc) · 1.52 KB
/
442.cpp
File metadata and controls
67 lines (57 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<bits/stdc++.h>
/*
Iterate through each expression. Ignore all left parentheses.
If you hit a character, push that matrix onto the stack. If you hit a right parenthesis,
pop the top two matrices from the stack. If their inner dimensions match,
add the number of multiplications to the running count and push back the new matrix. If not, output "error"
*/
using namespace std;
#define MAXN 28
struct martix
{
int row;
int col;
} Martix[MAXN],temp[MAXN];
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
char t[2];
int r,c;
scanf("%s %d %d",t,&r,&c);
Martix[t[0]-65].row=r;
Martix[t[0]-65].col=c;
}
char s[2*MAXN];
while(scanf("%s",s)!=EOF)
{
int len,ans=0,flag=1;
len=strlen(s);
int top=0;
for(int i=0;i<len;i++)
{
if(s[i]>='A'&&s[i]<='Z')
{
temp[top].row=Martix[s[i]-65].row;
temp[top].col=Martix[s[i]-65].col;
++top;
}
else if(s[i]==')'&&top>1) // pop 2 matrices
{
if(temp[top-2].col!=temp[top-1].row)
{
flag=0; // checking
break;
}
ans+=temp[top-2].row*temp[top-2].col*temp[top-1].col;
temp[top-2].col=temp[top-1].col;
top=top-1; //calculate
}
}
if(flag) printf("%d\n",ans);
else printf("error\n");
}
return 0;
}