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
error
3500
15000
40500
47500
15125
题目意思:给出字符代表矩阵的定义,计算表达示中矩阵相乘的总次数;
解题思路:定义一个stack<node>st,用node(-1,-1)代表左括号,从左到右扫描,如果是左括号则st.push(node(-1,01));如果是字母则push字母对应的矩阵;如果是右括号则把矩阵pop出来计算,一直到左括号;
#include <iostream>
#include<deque>
#include<algorithm>
#include<cstdio>
#include<stack>
#include<string>
#include<vector>
#include<map>
using namespace std; //bool check (vector<string>&v,map<string,int>&m)
//{
// for(unsigned int i=1;i<v.size();i++)
// if(m[v[i]]-m[v[i-1]]!=1)
// return false;
// return true;
//}
//int main()
//{
// int cas;
// cin>>cas;
// getchar();
// while(cas--)
// {
// int n;
// cin>>n;
// getchar();
// vector<string>v;
// string s;
// map<string ,int>m;
// for(int i=0; i<n; i++)
// {
// getline(cin,s);
// v.push_back(s);
// }
// for(int i=0; i<n; i++)
// {
// getline(cin,s);
// m[s]=i;
// }
// for(int q=0; q<n-1;)
// {
// //if(check(v,m))break;
// bool flag=1;
// for(int i=q+1; i<n; i++)
// {
// if(m[v[q]]-m[v[i]]==1)
// {
// cout<<v[i]<<endl;
// string t=v[i];
// for(int j=i; j>0; j--)
// v[j]=v[j-1];
// v[0]=t;
// flag=0;
// q=0;
// break;
// }
// }
// if(flag)q++;
// }
// cout<<endl;
// }
// return 0;
//} struct node
{
int x,y;
node(int a=0,int b=0):x(a),y(b) {}
};
int main()
{
int n;
cin>>n;
char c;
int x,y;
node arr[200];
for(int i=0; i<n; i++)
cin>>c>>x>>y,arr[c]=node(x,y);
string exp;
while(cin>>exp)
{
stack<node>st;
int sum=0;
bool flag=0;
for(int i=0; i<exp.size(); i++)
{
if(exp[i]!=')')
{
if(exp[i]=='(')
st.push(node(-1,-1));
else
st.push(arr[exp[i]]);
}
else if(!st.empty())
{
node matrix1=st.top();
st.pop();
while(st.top().x!=-1)
{
node matrix2=st.top();
st.pop();
if(matrix1.x!=matrix2.y)
{
flag=1;
cout<<"error"<<endl;
i=exp.size();
break;
}
sum+=matrix2.x*matrix2.y*matrix1.y;
matrix1=node(matrix2.x,matrix1.y);
}
st.pop();
st.push(matrix1);
}
else
{
cout<<"error"<<endl;
flag=1;
}
}
while(st.size()!=1&&!flag)
{
node matrix2=st.top();
st.pop();
node matrix1=st.top();
st.pop();
if(matrix1.y!=matrix2.x)
{
cout<<"error"<<endl;
break;
}
st.push(node(matrix1.x,matrix2.y));
sum+=matrix1.x*matrix1.y*matrix2.y;
}
if(!flag)
cout<<sum<<endl;
}
return 0;
}
												

uva-442 Matrix Chain Multiplication的更多相关文章

  1. 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 ...

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

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

  3. stack UVA 442 Matrix Chain Multiplication

    题目传送门 题意:给出每个矩阵的行列,计算矩阵的表达式,如果错误输出error,否则输出答案 分析:表达式求值,stack 容器的应用:矩阵的表达式求值A 矩阵是a * b,B 矩阵是b * c,则A ...

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

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

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

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

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

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

  7. UVA 442 二十 Matrix Chain Multiplication

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

  8. Matrix Chain Multiplication[HDU1082]

    Matrix Chain Multiplication Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  9. UVa442 Matrix Chain Multiplication

    // UVa442 Matrix Chain Multiplication // 题意:输入n个矩阵的维度和一些矩阵链乘表达式,输出乘法的次数.假定A和m*n的,B是n*p的,那么AB是m*p的,乘法 ...

  10. Matrix Chain Multiplication(表达式求值用栈操作)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1082 Matrix Chain Multiplication Time Limit: 2000/100 ...

随机推荐

  1. linux运维常用命令集

    1.删除0字节文件 find -type f -size 0 -exec rm -rf {} \;   2.查看进程 按内存从大到小排列 PS -e   -o "%C   : %p : %z ...

  2. 查看进程所用的内存(使用GetWindowThreadProcessId取得进程ID,OpenProcess打开进程和GetProcessMemoryInfo取得内存信息)

    // function GetProcessMemorySize(_sProcessName: string; var _nMemSize: Cardinal): Boolean; var l_nWn ...

  3. oracle rac 在完成安装错误。

    今天是2014.05.26,离别N继续使用长今博客.成交一直忙于最近.该条目加上家庭网络还没有缴纳会费,我开始变得不太安全.学习是一个需要冷静情绪.心脏的声音是. 由于改变笔记本,特别需要重新建立Ra ...

  4. NET Core 中的依赖注入

    NET Core 中的依赖注入 [共7篇] 一.控制反转(IoC) ASP.NET Core在启动以及后续针对每个请求的处理过程中的各个环节都需要相应的组件提供相应的服务,为了方便对这些组件进行定制, ...

  5. Oracle定时执行存储过程(转)

    定时执行存储过程在平时开发中经常会用到,年前的时候自己也做了一个,由于时间关系一直没能记录,现记录下来.       首先用一个完整的例子来实现定时执行存储过程. 任务目标:每小时向test表中插入一 ...

  6. MySQL多表查询之外键、表连接、子查询、索引

    MySQL多表查询之外键.表连接.子查询.索引 一.外键: 1.什么是外键 2.外键语法 3.外键的条件 4.添加外键 5.删除外键 1.什么是外键: 主键:是唯一标识一条记录,不能有重复的,不允许为 ...

  7. C++不支持Unicode,即使utf8

    今天,字符串unicode我们已经不需要常理的理由,但是,一些有编程语言的悠久历史.这仍然是一个头疼. 尽管第三方库支持的假设,C++事实上没有真正有效地支持unicode.即使utf8.(注意:本文 ...

  8. 10 个迅速提升你 Git 水平的提示(转)

    最近我们推出了两个教程:熟悉Git的基本功能和 让你在开发团队中熟练的使用Git . 我们所讨论的命令足够一个开发者在Git使用方面游刃有余.在这篇文章中,我们试图探索怎样有效的管理你的时间和充分的使 ...

  9. 诺贝尔物理学奖公布:LED灯将点亮了整个21世纪

    很多其它精彩.破晓博客:点击打开链接 7日.在瑞典首都斯德哥尔摩,瑞典皇家科学院常任秘书诺尔马克(左二)宣布2014年诺贝尔物理学奖得主.新华社发 ■人物 中村修二 勇于追讨酬劳的科学家 被誉为&qu ...

  10. HighChart学习-更新数据data Series与重绘

    一:HighChart介绍 基于JQuery的纯JavaScript的图标库,支持各种图表显示,同时还支持Mootools 与Prototype详细版本支持在这里: JQuery 1.3.2 - 1. ...