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. Linux-VLAN

    Why Vlan? VLAN是为解决以太网的广播问题和安全性而提出的一种协议,它在以太网帧的基础上增加了VLAN头,用VLAN ID把用户划分为更小的工作组,限制不同工作组间的用户二层互访,每个工作组 ...

  2. 如何在滚动报表时保持标题可见 (Reporting Services)

    From: https://msdn.microsoft.com/zh-cn/library/bb934257.aspx 对于跨多页的表或矩阵数据区域,可以控制滚动报表时是否始终显示包含列标题的初始行 ...

  3. Spring学习笔记之BeanFactory

    Spring bean container 的根接口,也是一个bean容器的基本功能,更深一步的接口像ListableBeanFactory 和 ConfigurableBeanFactory 都是 ...

  4. C++指针(部分有误需修改)

    一.取地址运算符&(内存地址) C++编译的程序占用的内存分为以下几个部分: 1.栈区:由编译器自动分配释放 ,存放函数的参数值,局部变量的值等.其操作方式类似于数据结构中的栈.与其它分区不同 ...

  5. Matlab与C/C++联合编程之Matlab以MEX方式调用C/C++代码(四)

    利用Matlab与VC++联合编程,既可在C语言程序中打开Matlab引擎,调用Matlab的ToolBox函数和作图函数,也可在Matlab中调用C代码生成的动态链接库文件,用以加快执行速度.缩短开 ...

  6. C# 启动关闭.exe进程(转)

    后台代码: 1 using System.Diagnostics;  2 3 protected void Button1_Click(object sender, EventArgs e)4     ...

  7. Java知识结构思维导图

  8. EF学习笔记(一)

    EF(EntityFramwork)实体框架:主要是将实体类(EntityClass)和数据表(Table)进行映射(Map). EF核心对象: DbContext   (数据访问核心对象)      ...

  9. IOS 封装类的时候注释格式,使用的时候可以想官方库一样快捷显示

    /** @brief 详情 @param 参数 @note 注意 @return 返回值类型 @code 这里写例题代码 @endcode @see 相似的方法参考 */

  10. iis6.0+.net 4.0 +mvc 404错误

    ps: 在iis中重新注册.net framework命令cd C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i 1 ...