/*Matrix Chain Multiplication
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1067 Accepted Submission(s): 720
Problem Description
Matrix multiplication problem is a typical example of dynamical programming.
Suppose you have to evaluate an expression like A*B*C*D*E where A,B,C,D and E are matrices. Since matrix multiplication is associative, the order
in which multiplications are performed is arbitrary. However, the number of elementary multiplications needed strongly depends on the evaluation
order you choose.
For example, let A be a 50*10 matrix, B a 10*20 matrix and C a 20*5 matrix.
There are two different strategies to compute A*B*C, namely (A*B)*C and A*(B*C).
The first one takes 15000 elementary multiplications, but the second one only 3500.
Your job is to write a program that determines the number of elementary multiplications needed for a given evaluation strategy.
Input
Input consists of two parts: a list of matrices and a list of expressions.
The first line of the input file contains one integer n (1 <= n <= 26), representing the number of matrices in the first part. The next n lines
each contain one capital letter, specifying the name of the matrix, and two integers, specifying the number of rows and columns of the matrix.
The second part of the input file strictly adheres to the following syntax (given in EBNF):
SecondPart = Line { Line } <EOF>
Line = Expression <CR>
Expression = Matrix | "(" Expression Expression ")"
Matrix = "A" | "B" | "C" | ... | "X" | "Y" | "Z"
Output
For each expression found in the second part of the input file, print one line containing the word "error" if evaluation of the expression leads
to an error due to non-matching matrices. Otherwise print one line containing the number of elementary multiplications needed to evaluate the
expression in the way specified by the parentheses.
Sample Input
9
A 50 10
B 10 20
C 20 5
D 30 35
E 35 15
F 15 5
G 5 10
H 10 20
I 20 25
A
B
C
(AA)
(AB)
(AC)
(A(BC))
((AB)C)
(((((DE)F)G)H)I)
(D(E(F(G(HI)))))
((D(EF))((GH)I))
Sample Output
0
0
0
error
10000
error
3500
15000
40500
47500
15125
Source
University of Ulm Local Contest 1996
*/
#include<stdio.h>
#include<string.h>
struct matrix
{
int num1;
int num2;
}mat[27], t;
int main()
{
int n, m, i, j, b, c, f[1000][2], e;
char s[1000], a, d[1000];
scanf("%d", &n);
getchar();
for(i =0 ;i < n; i++)
{
scanf("%c %d %d", &a, &b, &c);
mat[a-64].num1 = b;
mat[a-64].num2 = c;
getchar();
}
while(scanf("%s", s) != EOF)
{
int sum = 0;
t.num1 = 0;
t.num2 = 0;
memset(d, ' ', sizeof(d));
memset(f, 0, sizeof(f));
m = strlen(s);
if(m == 1)
printf("0\n");
else
{
j = 0;
e = 0;
for(i = 0; i < m; i++)
{
if(s[i] == '(') //左括号保存
d[j++] = '(';
else if( s[i] == ')' ) // 右括号
{
if(d[j-1] == '(') // 仅抵消左括号
{
d[--j] = ' ';
if(j == 1 )
{
f[e][0] = t.num1;
f[e][1] = t.num2;
e++;
t.num1 = 0;
t.num2 = 0;
}
// printf("%d ", j);
}
else // 字母 // 抵消字母 & 左括号
{
if(mat[d[j-1] - 64].num2 == t.num1)
{
sum += mat[ d[j-1] - 64].num1 * mat[d[j-1] - 64].num2 * t.num2;
t.num1 = mat[ d[j-1] - 64].num1;
d[j-1] = ' ';
d[j-1] = ' ';
j -= 2;
if(j == 1)
{
f[e][0] = t.num1;
f[e][1] = t.num2;
e++;
// printf("%d %d", f[e][0], f[e][1]);
t.num1 = 0;
t.num2 = 0;
}
}
else
{
printf("error\n");
break;
}
}
}
else //字母
{
if( d[j-1] == '(') // 若是左括号
{
if(j == 1) // 若没有前矩阵,保存字母
{
f[e][0] = mat[s[i] - 64].num1;
f[e][1] = mat[s[i] - 64].num2;
e++;
}
else
{
if(t.num1 != 0)
{
if(t.num2 == mat[s[i]-64].num1) //否则计算
{
sum += t.num1 * t.num2 * mat[s[i]-64].num2;
t.num2 = mat[s[i]-64].num2;
}
else
{
printf("error\n");
break;
}
}
else
d[j++] = s[i];
}
}
else //若是字母, 计算
{
if(mat[d[j-1] - 64].num2 == mat[s[i] - 64].num1)
{
sum += mat[d[j-1] - 64].num1 * mat[d[j-1] - 64].num2 * mat[s[i] - 64].num2;
t.num1 = mat[d[j-1] - 64].num1;
t.num2 = mat[s[i] - 64].num2;
// printf("%d %d", t.num1, t.num2);
d[--j] = ' ';
}
else
{
printf("error\n");
break;
}
}
}//printf("%c%d %d", d[j-1], t.num1, t.num2);
}
// 输出
if(i == m)
{
if(e > 1)
{
for(i = 1; i < e; i++) // 最后一个右括号内的小项相乘
{
if(f[0][1] == f[i][0])
{
// printf("%d %d ",f[0][0],f[0][1]);
sum += f[0][0]*f[0][1] * f[i][1];
f[0][1] = f[i][1];
}
else
{
// printf("%d %d ",f[0][1],f[0][0]);
printf("error\n");
break;
}
}
if(i == e)
printf("%d\n", sum);
}
else
printf("%d\n", sum);
}
}
}
return 0;
}
题意:计算矩阵相乘所进行的乘法操作次数。
用栈来存储,技术不好,只会用数组作为栈来进行操作。
难点:判断括号与字母之间的各种关系,以及各种情况,以使矩阵相乘能够正常进行。