-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathZOJ1094(AC).cpp
103 lines (91 loc) · 1.37 KB
/
ZOJ1094(AC).cpp
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Matrix{
char name;
long row;
long col;
}Matrix;
int main()
{
long len;
long i;
long j;
long n;
long top;
Matrix stack[1000];
Matrix matrices[1000];
char line[1000];
long multiplication;
long error;
gets(line);
sscanf(line, "%ld", &n);
for(i = 0; i < n; i++)
{
gets(line);
sscanf(line, "%c %ld %ld", &matrices[i].name, &matrices[i].row, &matrices[i].col);
}
while(1)
{
if(gets(line) == NULL)
{
break;
}
len = strlen(line);
top = -1;
error = 0;
multiplication = 0;
for(i = 0; i < len && error == 0; i++)
{
if(isupper(line[i]))
{
for(j = 0; j < n; j++)
{
if(matrices[j].name == line[i])
{
break;
}
}
if(j >= 0 && j <= n - 1)
{
top++;
stack[top] = matrices[j];
}
}
else if(line[i] == '(')
{
}
else if(line[i] == ')')
{
if(top < 1)
{
error = 1;
}
else if(stack[top - 1].col != stack[top].row)
{
error = 1;
}
else
{
multiplication = multiplication + stack[top - 1].row * stack[top - 1].col * stack[top].col;
top--;
stack[top].col = stack[top + 1].col;
}
}
}
if(top != 0)
{
error = 1;
}
if(error == 1)
{
printf("error\n");
}
else
{
printf("%ld\n", multiplication);
}
}
return 0;
}