UVa 442 Matrix Chain Multiplication(矩阵链,模拟栈)
意甲冠军 由于矩阵乘法计算链表达的数量,需要的计算 后的电流等于行的矩阵的矩阵的列数 他们乘足够的人才 非法输出error
输入是严格合法的 即使仅仅有两个相乘也会用括号括起来 并且括号中最多有两个 那么就非常easy了 遇到字母直接入栈 遇到反括号计算后入栈 然后就得到结果了
- #include<cstdio>
- #include<cctype>
- #include<cstring>
- using namespace std;
- const int N = 1000;
- int st[N], row[N], col[N], r[N], c[N];
- int main()
- {
- int n, ans, top;
- scanf("%d", &n);
- char na[3], s[N];
- for(int i = 1; i <= n; ++i)
- {
- scanf("%s", na);
- int j = na[0] - 'A';
- scanf("%d%d", &row[j], &col[j]);
- }
- while(~scanf("%s", &s))
- {
- int i;
- for(i = 0 ; i < 26; ++i)
- c[i] = col[i], r[i] = row[i];
- ans = top = 0;
- for(i = 0; s[i] != '\0'; ++i)
- {
- if(isalpha(s[i]))
- {
- int j = s[i] - 'A';
- st[++top] = j;
- }
- else if(s[i] == ')')
- {
- if(r[st[top]] != c[st[top - 1]]) break;
- else
- {
- --top;
- c[st[top]] = c[st[top + 1]];
- ans += (r[st[top]] * c[st[top]] * r[st[top + 1]]);
- }
- }
- }
- if(s[i] == '\0') printf("%d\n", ans);
- else printf("error\n");
- }
- return 0;
- }
Matrix Chain Multiplication |
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 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
UVa 442 Matrix Chain Multiplication(矩阵链,模拟栈)的更多相关文章
- UVA——442 Matrix Chain Multiplication
442 Matrix Chain MultiplicationSuppose you have to evaluate an expression like A*B*C*D*E where A,B,C ...
- UVA - 442 Matrix Chain Multiplication(栈模拟水题+专治自闭)
题目: 给出一串表示矩阵相乘的字符串,问这字符串中的矩阵相乘中所有元素相乘的次数. 思路: 遍历字符串遇到字母将其表示的矩阵压入栈中,遇到‘)’就将栈中的两个矩阵弹出来,然后计算这两个矩阵的元素相乘的 ...
- UVa 442 Matrix Chain Multiplication(栈的应用)
题目链接: https://cn.vjudge.net/problem/UVA-442 /* 问题 输入有括号表示优先级的矩阵链乘式子,计算该式进行的乘法次数之和 解题思路 栈的应用,直接忽视左括号, ...
- stack UVA 442 Matrix Chain Multiplication
题目传送门 题意:给出每个矩阵的行列,计算矩阵的表达式,如果错误输出error,否则输出答案 分析:表达式求值,stack 容器的应用:矩阵的表达式求值A 矩阵是a * b,B 矩阵是b * c,则A ...
- UVA442 Matrix Chain Multiplication 矩阵运算量计算(栈的简单应用)
栈的练习,如此水题竟然做了两个小时... 题意:给出矩阵大小和矩阵的运算顺序,判断能否相乘并求运算量. 我的算法很简单:比如(((((DE)F)G)H)I),遇到 (就cnt累计加一,字母入栈,遇到) ...
- 例题6-3 Matrix Chain Multiplication ,Uva 442
这个题思路没有任何问题,但还是做了近三个小时,其中2个多小时调试 得到的经验有以下几点: 一定学会调试,掌握输出中间量的技巧,加强gdb调试的学习 有时候代码不对,得到的结果却是对的(之后总结以下常见 ...
- UVA 442 二十 Matrix Chain Multiplication
Matrix Chain Multiplication Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %l ...
- UVa442 Matrix Chain Multiplication
// UVa442 Matrix Chain Multiplication // 题意:输入n个矩阵的维度和一些矩阵链乘表达式,输出乘法的次数.假定A和m*n的,B是n*p的,那么AB是m*p的,乘法 ...
- ACM学习历程——UVA442 Matrix Chain Multiplication(栈)
Description Matrix Chain Multiplication Matrix Chain Multiplication Suppose you have to evaluate ...
随机推荐
- 一个解析RTSP 的URL函数
写了一个解析URL的函数,可以提取URL中的IP 和 port. 如:url = "rtsp://192.168.1.43:2554/realmp3.mp3"; url = &qu ...
- isHiden和isVisible的区别(isVisible更可靠)
之前一直对isHiden和isVisible的区别比较模糊,都是乱用的.今天因需要仔细看了一下. 1.isHiden只是返回部件的隐藏属性,并不能表示部件当前的真实状态.比如A部件有个子部件B,而A处 ...
- c 有意思的数组初始化
c 有意思的数组初始化 #include <stdio.h> int main() { int i = 0; char a[1024]; char a0[10] = {}; char a1 ...
- uva 10196 Check The Check
题目:10196 - Check The Check 思路:水题..模拟 这个代码,前半部分是在数统机房上课的时候写的,挫了点,懒得改了. #include <cstdio> #inclu ...
- XML SelectSingleNode的使用 根据节点属性获取该节点
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Form ...
- navicat for mysql 如何将表ID排序重1开始?
如果是自增字段,删除数据,他是根据原来的继续往后排的 1.你可以删除这个字段,重新建立个自增字段就可以了 2.好像也可以重新设置排序起始 alter table table_name AUTO_INC ...
- Java Runtime.getRuntime().exec() 执行带空格命令
可执行文件路径如果包含空格,则在java中不能被获取到. 此时Debug一下,会发现 project=null. project.waitFor 的返回值为1.但是去源路径单击bat文件是可以正常运行 ...
- PS2: 这篇文章中的图片绘图工具使用的是Dia (sudo apt-get install dia)。据说yEd也很不错。
SBCL编译过程 - O.Nixie的专栏 - 博客频道 - CSDN.NET PS2: 这篇文章中的图片绘图工具使用的是Dia (sudo apt-get install dia).据说yEd也很不 ...
- ACM-简单题之Factorial——poj1401
转载请注明出处:http://blog.csdn.net/lttree Factorial Time Limit: 1500MS Memory Limit: 65536K Total Submis ...
- Android 表格布局<TableLayout>
表格布局即,tableLayout,表格布局通过行.列的形式来管理UI组件,TablelLayout并不需要明确地声明包含多少行.多少列,而是通过TableRow,以及其他组件来控制表格的行数和列数, ...