Suppose you have to evaluate an expression like A*B*C*D*E where A,B,C,D and E are matrices. Sincematrix multiplicationisassociative, theorderin whichmultiplicationsareperformedis 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
Foreachexpressionfoundinthesecondpartoftheinputfile,printonelinecontainingtheword‘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
这题题意就是给你一个矩阵链乘表达式 并且只有两个矩阵(例如A,B)A的列数等于B的行数才能进行乘法 否则输出error
如AB的乘法的计算公式为 (假定A是m*n, B是n*p) m*n*p;
这题因为保证输入合法 所以用一个栈就能实现 大体思路如下
首先定义一个结构体数组 数组下标代表给定字母编号 结构体里存行,列和乘法次数 遇见‘)’取出栈中前两个值进行乘法 将乘法之后得到的新矩阵压入栈中 如果遇见非法数字标记后直接跳出
#include <cstdio>
#include <cstring>
#include <stack>
const int MAXN = 30;
using namespace std;
struct node
{
int x, y, cnt = 0;
node () {}
node (int _x, int _y, int _cnt) : x(_x), y(_y), cnt(_cnt) {}
}nod[MAXN];
int main()
{
int n;
scanf("%d", &n);
getchar();
for (int i = 0; i < n; i++)//输入矩阵值
{
char c;
scanf("%c", &c);
scanf("%d %d", &nod[c - 'A'].x, &nod[c - 'A'].y);
getchar();
}
char tmp[MAXN];
stack<node> S;
while (scanf("%s", tmp) != EOF)//输入矩阵表达式
{
int len = strlen(tmp);
int flag = 1;
for (int i = 0; i < len; i++)
{
if (tmp[i] == ')')
{
node b = S.top(); S.pop();
node a = S.top(); S.pop();
if (a.y == b.x)//将得到的新矩阵压入栈
S.push(node(a.x, b.y, a.cnt + b.cnt + (a.x * a.y *b.y)));//得到A矩阵的乘法次数加得到B矩阵的乘法次数再加AB的乘法次数就是得到新矩阵所需的乘法次数
else//非法情况标记
{ flag = 0; break; }
}
else if (tmp[i] >= 'A' && tmp[i] <= 'Z')//进行输入压栈操作
S.push(nod[tmp[i] - 'A']);
}
node ans = S.top();
if (flag)
printf("%d\n", ans.cnt);
else
printf("error\n");
}
return 0;
}
/*
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))
*/