UVa 442 矩阵链乘

Input Specification

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 (  ), 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 Specification

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

栈的运用。
 1 #include<iostream>
 2 #include<stack>
 3 #include<string>
 4 using namespace std;
 5
 6 struct Node
 7 {
 8     int m, n;
 9     Node(int m = 0, int n = 0) :m(m), n(n){}
10 }Matrix[30];
11
12 stack<Node> chain;
13
14 void clear()
15 {
16     while (!chain.empty())
17         chain.pop();
18 }
19
20 void Multiplication(string s)
21 {
22     clear();
23     int count = 0;
24     if (s[0] != ‘(‘)    {cout << "0" << endl; return;}
25     for (int i = 0; i < s.length(); i++)
26     {
27         if (s[i] == ‘)‘)
28         {
29             Node x2 = chain.top();
30             chain.pop();
31             Node x1 = chain.top();
32             chain.pop();
33             if (x1.n != x2.m)  { cout << "error" << endl; return; }
34             else
35             {
36                 count += x1.m * x1.n * x2.n;
37                 chain.push(Node(x1.m,x2.n));
38             }
39         }
40         else if (s[i] == ‘(‘)  { ; }
41         else  chain.push(Matrix[s[i]-‘A‘]);
42     }
43     cout << count << endl;
44 }
45
46 int main()
47 {
48     int t, m, n;
49     char name;
50     cin >> t;
51     while (t--)
52     {
53         cin >> name >> m >> n;
54         Matrix[name - ‘A‘].m = m;
55         Matrix[name - ‘A‘].n = n;
56     }
57     string s;
58     while (cin >> s)
59     {
60         Multiplication(s);
61     }
62     return 0;
63 }
时间: 2024-07-30 20:36:12

UVa 442 矩阵链乘的相关文章

UVa 442 矩阵链乘及scanf说明符中的\n

题目:计算题给矩阵相乘次序所需的相乘次数.   我们已知的m*n和n*k矩阵相乘,得到的是m*k矩阵,但需要的相乘次数是m*n*k(开始当成了m*k %>_<%).evaluate,求值 思路:每个矩阵用结构体表示,有名字.行.列.需要计算的次数.矩阵相乘的过程用栈来模拟.遇到左括号(,压栈这是自然的.遇到一个矩阵时,检查栈顶,如果栈顶元素是左括号,则压栈,否则就是矩阵,则比较栈顶矩阵和输入矩阵是否匹配,如果匹配则修改栈顶矩阵的列.计算次数,这样输入矩阵对栈顶进行了修改,相当于完成了相乘,这时

UVA 348 矩阵链乘

UVA 348 题意: 给出 N 个矩阵(A1,A2,...,An),求完全括号化方案,使得计算乘积(A1A2...An)所需乘法次数最少.并输出方案. 解题: 算法导论是个好东西   讲的很详细~ 假设矩阵 A 和 B 相乘,那 A 的列数必须要和 B 的行数相同,即 若 A 的行列数为(x,y),则 B 须为(y,z), 它们相乘得到一个(x,z)的矩阵:需要乘 xyz 次.题目把相乘的顺序给出了,我们做的就是添加括号了.把所有矩阵用一个序列 P 表示,第 i 个矩阵的行列数为 P(i-1)

UVa 442 Matrix Chain Multiplication(矩阵链乘,模拟栈)

题意  计算给定矩阵链乘表达式需要计算的次数  当前一个矩阵的列数等于后一个矩阵的行数时  他们才可以相乘  不合法输出error 输入是严格合法的  即使只有两个相乘也会用括号括起来  而且括号里最多有两个 那么就很简单了 遇到字母直接入栈  遇到反括号计算后入栈  然后就得到结果了 #include<cstdio> #include<cctype> #include<cstring> using namespace std; const int N = 1000;

UVA 348 &amp; ZOJ 1276 Optimal Array Multiplication Sequence(dp , 矩阵链相乘问题)

Optimal Array Multiplication Sequence Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description Given two arrays A and B, we can determine the array C = AB using the standard definition of matrix multiplication: The number of

stack UVA 442 Matrix Chain Multiplication

题目传送门 /* stack 容器的应用:矩阵的表达式求值 A 矩阵是a * b,B 矩阵是b * c,则A * B 是a * c */ #include <cstdio> #include <iostream> #include <algorithm> #include <stack> #include <cmath> #include <cstring> #include <string> using namespac

[2016-02-05][UVA][442][Matrix Chain Multiplication]

[2016-02-05][UVA][442][Matrix Chain Multiplication] UVA - 442 Matrix Chain Multiplication Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Suppose you have to evaluate an expression like A*B*C*D*E where

uva348Optimal Array Multiplication Sequence (最优矩阵链乘+路径输出)

Optimal Array Multiplication Sequence Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Practice UVA 348 Appoint description: Description Download as PDF Given two arrays A and B, we can determine the array C = AB using the

矩阵链乘(Matrix Chain Multiplication)

输入n个矩阵的维度和一些矩阵链乘表达式,输出乘法的次数.如果乘法无法进行,则输出error.假定A是m*n矩阵,B是n*p矩阵,那么A*B是m*p矩阵,乘法次数为m*n*p.如果A的列数不等于B的行数,则乘法无法进行. 例如,A是50*10的,B是10*20的,C是20*5的,则(A(BC))的乘法次数为10*20*5(BC的乘法次数) +50*10*5(A(BC)的乘法次数) = 3500. #include<cstdio> #include<stack> #include<

矩阵链乘

矩阵链乘之结构体构造函数 struct构造函数,和构造函数的重载函数长这个样子,和C++的构造函数差不过. struct Dog { Dog() { name = "wangwang"; age = 10; } string name; int age; }; struct Dog { Dog() { name = "Ao di"; age = 2; } Dog(string n) { name = n; age = 10; } Car(string n, int