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 栈的运用。
#include<iostream>
#include<stack>
#include<string>
using namespace std; struct Node
{
int m, n;
Node(int m = , int n = ) :m(m), n(n){}
}Matrix[]; stack<Node> chain; void clear()
{
while (!chain.empty())
chain.pop();
} void Multiplication(string s)
{
clear();
int count = ;
if (s[] != '(') {cout << "" << endl; return;}
for (int i = ; i < s.length(); i++)
{
if (s[i] == ')')
{
Node x2 = chain.top();
chain.pop();
Node x1 = chain.top();
chain.pop();
if (x1.n != x2.m) { cout << "error" << endl; return; }
else
{
count += x1.m * x1.n * x2.n;
chain.push(Node(x1.m,x2.n));
}
}
else if (s[i] == '(') { ; }
else chain.push(Matrix[s[i]-'A']);
}
cout << count << endl;
} int main()
{
int t, m, n;
char name;
cin >> t;
while (t--)
{
cin >> name >> m >> n;
Matrix[name - 'A'].m = m;
Matrix[name - 'A'].n = n;
}
string s;
while (cin >> s)
{
Multiplication(s);
}
return ;
}
UVa 442 矩阵链乘(栈)的更多相关文章
- UVa 442 Matrix Chain Multiplication(矩阵链,模拟栈)
意甲冠军 由于矩阵乘法计算链表达的数量,需要的计算 后的电流等于行的矩阵的矩阵的列数 他们乘足够的人才 非法输出error 输入是严格合法的 即使仅仅有两个相乘也会用括号括起来 并且括号中 ...
- 矩阵链乘(UVa 442)
结构体 struct matrix 用来保存矩阵的行和列: map<string,matrix> 用来保存矩阵名和相应的行列数: stack<string> 用来保存表达式中遇 ...
- 【UVa-442】矩阵链乘——简单栈练习
题目描述: 输入n个矩阵的维度和一些矩阵链乘表达式,输出乘法的次数.如果乘法无法进行,输出error. Sample Input 9 A 50 10 B 10 20 C 20 5 D 30 35 E ...
- UVa 442 Matrix Chain Multiplication(栈的应用)
题目链接: https://cn.vjudge.net/problem/UVA-442 /* 问题 输入有括号表示优先级的矩阵链乘式子,计算该式进行的乘法次数之和 解题思路 栈的应用,直接忽视左括号, ...
- UVa 10003 切木棍(区间DP+最优矩阵链乘)
https://vjudge.net/problem/UVA-10003 题意: 有一根长度为L的棍子,还有n个切割点的位置.你的任务是在这些切割点的位置处把棍子切成n+1部分,使得总切割费用最小.每 ...
- UVA - 442 Matrix Chain Multiplication(栈模拟水题+专治自闭)
题目: 给出一串表示矩阵相乘的字符串,问这字符串中的矩阵相乘中所有元素相乘的次数. 思路: 遍历字符串遇到字母将其表示的矩阵压入栈中,遇到‘)’就将栈中的两个矩阵弹出来,然后计算这两个矩阵的元素相乘的 ...
- COJ 0016 20603矩阵链乘
传送门:http://oj.cnuschool.org.cn/oj/home/solution.htm?solutionID=35454 20603矩阵链乘 难度级别:B: 运行时间限制:1000ms ...
- UVA 442 二十 Matrix Chain Multiplication
Matrix Chain Multiplication Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %l ...
- 例题6-3 Matrix Chain Multiplication ,Uva 442
这个题思路没有任何问题,但还是做了近三个小时,其中2个多小时调试 得到的经验有以下几点: 一定学会调试,掌握输出中间量的技巧,加强gdb调试的学习 有时候代码不对,得到的结果却是对的(之后总结以下常见 ...
随机推荐
- UI UIView
课程内容: 一.iOS概述 2007年1月9日Macworld大会上公布iPhone OS系统,2010WWDC大会上改名为iOS 二. UI编程概述 UI的本意是用户界面,是英文User和 ...
- SO从 \u 这样的字符串 构建对象
ShowMessage(SO('\u4F18\u8D28\u670D\u52A112').AsString); 正确 得到 优质服务12 ShowMessage(so( 个数字,后面的中文未能解析出.
- 使用 JDBC 调用函数 & 存储过程
/** * 如何使用 JDBC 调用存储在数据库中的函数或存储过程 */ @Test public void testCallableStatment() { Connection connectio ...
- SQL Server 索引介绍
数据库索引是对数据表中一个或多个列的值进行排序的结构,就像一本书的目录一样,索引提供了在行中快速查询特定行的能力 详细出处参考:http://www.jb51.net/article/30950.ht ...
- android中正确导入第三方jar包
android中正确导入第三方jar包 andriod中如果引入jar包的方式不对就会出现一些奇怪的错误. 工作的时候恰好有一个jar包需要调用,结果用了很长时间才解决出现的bug. 刚开始是这样引用 ...
- 永不消逝的电波(三):低功耗蓝牙(BLE)入门之如何调戏别人的小米手环
0×00 前言 蓝牙(Bluetooth),一种无线技术标准,用来让固定与移动设备,在短距离间交换数据,以形成个人局域网(PAN).其使用短波特高频(UHF)无线电波,经由2.4至2.485 GHz的 ...
- 对NSNumber的理解
1.nsnumber最重要的作用是可以封装任何的值对象,就是说nsnumber对象的类型可以是任何的类型. 如nsnumber *number = @"12" nsnumber * ...
- JQuery源码分析(八)
jQuery的each迭代器 jQuery的each方法从使用上就要分2种情况: $.each()函数 $(selector).each() $.each()函数和$(selector).each() ...
- iOS常用正则表达式验证(手机号、密码格式、身份证号等)
#import @interfaceUtils : NSObject #pragma 正则匹配手机号 + (BOOL)checkTelNumber:(NSString*) telNumber; #pr ...
- iOS开发:icon和启动图尺寸
歪果仁的总结: Asset iPhone 6s Plus and iPhone 6 Plus (@3x) iPhone 6s, iPhone 6, and iPhone 5 (@2x) iPhone ...