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 矩阵链乘(栈)的更多相关文章

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

    意甲冠军  由于矩阵乘法计算链表达的数量,需要的计算  后的电流等于行的矩阵的矩阵的列数  他们乘足够的人才  非法输出error 输入是严格合法的  即使仅仅有两个相乘也会用括号括起来  并且括号中 ...

  2. 矩阵链乘(UVa 442)

    结构体 struct matrix 用来保存矩阵的行和列: map<string,matrix> 用来保存矩阵名和相应的行列数: stack<string> 用来保存表达式中遇 ...

  3. 【UVa-442】矩阵链乘——简单栈练习

    题目描述: 输入n个矩阵的维度和一些矩阵链乘表达式,输出乘法的次数.如果乘法无法进行,输出error. Sample Input 9 A 50 10 B 10 20 C 20 5 D 30 35 E ...

  4. UVa 442 Matrix Chain Multiplication(栈的应用)

    题目链接: https://cn.vjudge.net/problem/UVA-442 /* 问题 输入有括号表示优先级的矩阵链乘式子,计算该式进行的乘法次数之和 解题思路 栈的应用,直接忽视左括号, ...

  5. UVa 10003 切木棍(区间DP+最优矩阵链乘)

    https://vjudge.net/problem/UVA-10003 题意: 有一根长度为L的棍子,还有n个切割点的位置.你的任务是在这些切割点的位置处把棍子切成n+1部分,使得总切割费用最小.每 ...

  6. UVA - 442 Matrix Chain Multiplication(栈模拟水题+专治自闭)

    题目: 给出一串表示矩阵相乘的字符串,问这字符串中的矩阵相乘中所有元素相乘的次数. 思路: 遍历字符串遇到字母将其表示的矩阵压入栈中,遇到‘)’就将栈中的两个矩阵弹出来,然后计算这两个矩阵的元素相乘的 ...

  7. COJ 0016 20603矩阵链乘

    传送门:http://oj.cnuschool.org.cn/oj/home/solution.htm?solutionID=35454 20603矩阵链乘 难度级别:B: 运行时间限制:1000ms ...

  8. UVA 442 二十 Matrix Chain Multiplication

    Matrix Chain Multiplication Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %l ...

  9. 例题6-3 Matrix Chain Multiplication ,Uva 442

    这个题思路没有任何问题,但还是做了近三个小时,其中2个多小时调试 得到的经验有以下几点: 一定学会调试,掌握输出中间量的技巧,加强gdb调试的学习 有时候代码不对,得到的结果却是对的(之后总结以下常见 ...

随机推荐

  1. [开发笔记]-sqlite数据库在使用时遇到的奇葩问题记录

    有时候做些简单的项目一般都会选择sqlite数据库,优点有很多,这里就不详细说了. 在此主要记录一些平时在使用时遇到的问题及解决方法.希望能对大家有所帮助. --------------------- ...

  2. Android 获取网络状态

    1.检测网络是否可用 public boolean isNetWorkConnected() { ConnectivityManager cm = (ConnectivityManager)getSy ...

  3. NSURLSession使用实战教程

    我的前面两篇文章介绍了NSURLSession套件的使用和NSURLSession套件的主要类.今天我们使用NSURLSession来完成一个小的应用程序.在实战之前,我先补充一点,为什么苹果会主推N ...

  4. Android Context

    http://www.cnblogs.com/android100/p/Android-Context.html

  5. 王家卫i

    〈旺角卡门〉 因为我很了解我自己,我不能对你承诺什么. 你不要说两次,说两次我就相信了. 我宁愿做一日英雄,都不想成世做条虫! 厨房里有煮好的饭,另外我还买了几个杯子,我知道,用不了多久就都会被打破, ...

  6. ABBYY

    ABBYY FineReader Engine泰比OCR文字识别控件移动版 产品功能:OMR识别控件 平台: 开发商:ABBYY泰比科技 版本:产品介绍:手机识别的高品质和精度 泰比( ...

  7. 【Oracle XE系列之三】使用OMF方式手工创建Oracle XE数据库

    环境:win10_X64_Pro 1.创建数据库实例,实例名为PF C:\oraclexe\app\oracle\product\11.2.0\server\bin>oradim -new -s ...

  8. 基于HTTP Live Streaming(HLS) 搭建在线点播系统

    1. 为何要使用HTTP Live Streaming 可以参考wikipedia HTTP Live Streaming(缩写是 HLS)是一个由苹果公司提出的基于HTTP的流媒体 网络传输协议.是 ...

  9. Configuring Squid as an accelerator/SSL offload for Outlook Web Access

    reference:http://wiki.squid-cache.org/SquidFaq/ReverseProxy/ Configuring Squid as an accelerator/SSL ...

  10. postfix 邮件备份方法

    postfix 邮件备份方法: postfix的bcc(密送)功能可以根据条件,将所有经过postfix队列的邮件根据规则密送到指定的邮箱. postfix带有三个bcc参数: ①.always_bc ...