Matrix Chain Multiplication (堆栈)
题目链接:https://vjudge.net/problem/UVA-442

题目大意:输入n个矩阵的维度和一些矩阵链乘表达式,输出乘法的次数。如果乘法无法进行,输出error。 假定A是m*n的矩阵,B是n*p的矩阵,乘法次数为m*n*p。如果A的列数不等于B的行数,则乘法
无法进行。
例如A是50*10的,B是10*20的,C是20*5的,则(A(BC))的乘法次数为10*20*5(BC的乘法次数)+50*10*5((A(BC)的乘法次数)=3500
分析:本题的关键是解析表达式,本题的表达式比较简单,可以用一个栈来完成,遇到字母时入栈,遇到右括号时出栈并计算,然后结果入栈。 因为保证输入合法,括号无需入栈
#include<iostream>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=+;
struct Matrix
{
int a,b;
//Matrix (int a=0,int b=0):a(a),b(b){}
Matrix (int c=,int d=)
{
a=c;
b=d;
}
}m[maxn];
stack<Matrix> s;
int main()
{
int n;
cin>>n;
for(int i=;i<n;i++)
{
string name;
cin>>name;
int k=name[]-'A';//存下标
cin>>m[k].a>>m[k].b;
}
string expr;
while(cin>>expr)
{
int len=expr.length();
bool error=false;
int ans=;
for(int i=;i<len;i++)
{
if(isalpha(expr[i])) s.push(m[expr[i]-'A']);//是否是字母 也就是矩阵 是的话入栈
else if(expr[i]==')')
{
Matrix m2=s.top(); s.pop();//取两个字符
Matrix m1=s.top(); s.pop();
if(m1.b!=m2.a)
{
error=true;
break;
}
ans+=m1.a*m1.b*m2.b;
s.push(Matrix(m1.a,m2.b));
}
}
if(error) cout<<"error"<<endl;
else cout<<ans<<endl;
}
return ;
}
Matrix Chain Multiplication (堆栈)的更多相关文章
- Matrix Chain Multiplication[HDU1082]
Matrix Chain Multiplication Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- 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调试的学习 有时候代码不对,得到的结果却是对的(之后总结以下常见 ...
- UVa442 Matrix Chain Multiplication
// UVa442 Matrix Chain Multiplication // 题意:输入n个矩阵的维度和一些矩阵链乘表达式,输出乘法的次数.假定A和m*n的,B是n*p的,那么AB是m*p的,乘法 ...
- UVa 442 Matrix Chain Multiplication(矩阵链,模拟栈)
意甲冠军 由于矩阵乘法计算链表达的数量,需要的计算 后的电流等于行的矩阵的矩阵的列数 他们乘足够的人才 非法输出error 输入是严格合法的 即使仅仅有两个相乘也会用括号括起来 并且括号中 ...
- Matrix Chain Multiplication(表达式求值用栈操作)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1082 Matrix Chain Multiplication Time Limit: 2000/100 ...
- 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 ...
- ACM学习历程——UVA442 Matrix Chain Multiplication(栈)
Description Matrix Chain Multiplication Matrix Chain Multiplication Suppose you have to evaluate ...
- uva-442 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 ma ...
随机推荐
- Mat 与 IplImage 和 CvMat 的转换
在 OpenCV 2 中虽然引入了方便的 Mat 类,出于兼容性的考虑,OpenCV 依然是支持 C 语言接口的 IplImage 和 CvMat 结构.如果你要与以前的代码兼容,将会涉及 Mat 与 ...
- Servlet入门第二天
1. GenericServlet: 1). 是一个 Serlvet. 是 Servlet 接口和 ServletConfig 接口的实现类. 但是一个抽象类. 其中的 service 方法为抽象方法 ...
- hive和sequoiadb对接的问题
使用hive和spark对接的时候,当两个表做JOIN的时候,如果表的数据量很大一定要做 set hive.auto.convert.join=false
- vimrc配置-新建文件时自动生成文件头
vimrc配置-新建文件时自动生成文件头 auto add file header autocmd BufNewFile *.py 0r /home/zxkletters/.vim/vim_te ...
- 关于CRTP(Curiously Recurring Template Prattern)的使用
在阅读frameworks/rs/cpp/util/RefBase.h之LightRefBase时,我记得<C++设计新思维>里对这种用法是有过介绍的,可是今天翻箱倒柜,怎么都找不到那本奇 ...
- IMP-00003: 遇到 ORACLE 错误 959 ORA-00959: 表空间 '' 不存在
描述 在使用imp命令将dmp文件导入oracle中时,遇到如下错误: IMP: 遇到 ORACLE 错误 ORA: 表空间 'TBS_CDUSER' 不存在 IMP命令如下: IMP cduser/ ...
- P1472 奶牛家谱 Cow Pedigrees
题意:问你指定二叉树有几种 1.高度为k 2.节点数为n 3.每个点的度为0或2 爆搜------->30分QAQ 首先,因为每个节点度为0或2, 所以如果n是偶数直接输出0就行了吧(嘿嘿) 如 ...
- kuangbin专题十六 KMP&&扩展KMP HDU2594 Simpsons’ Hidden Talents
Homer: Marge, I just figured out a way to discover some of the talents we weren’t aware we had. Marg ...
- kuangbin专题十六 KMP&&扩展KMP HDU2087 剪花布条
一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? Input输入中含有一些数据,分别是成对出现的花布条和小 ...
- 使用instsrv.exe和srvany.exe将应用程序安装成windows后台服务
好的参考链接: http://www.jb51.net/softjc/374631.html 利用这两个工具,将 exe程序或者 bat文件,做成Windows后台服务.