uva 442 Matrix Chain Multiplication
2016-05-23 20:11
501 查看
#include<iostream> #include<cstdio> #include<cstring> #include<stack> using namespace std; struct Matrix{ int a,b; Matrix(int a=0,int b=0):a(a),b(b) {}; }m[26]; stack<Matrix> s; int main() { int n; cin>>n; for(int i=0;i<n;i++) { string name; cin>>name; int k=name[0]-'A'; cin>>m[k].a>>m[k].b; } string expr; while(cin>>expr) { int len=(int)expr.length(); bool error=false; int ans=0; for(int i=0;i<len;i++) { if(isalpha(expr[i])) s.push(m[expr[i]-'A']); else if(expr[i]==')'){ Matrix m1=s.top();s.pop(); Matrix m2=s.top();s.pop(); if(m2.b!=m1.a) {error=true;break;} ans+=m2.a*m2.b*m1.b; s.push(Matrix(m2.a,m1.b)); } } if(error) cout<<"error"<<endl; else cout<<ans<<endl; } return 0; }
相关文章推荐
- 2016百度之星-初赛(Astar Round2A)AII X
- fzu 2109 Mountain Number 数位DP
- 斗地主AI算法实现 一(拆牌)
- uva 514 Rails
- 进程间双向通信--sockpair
- rails bug
- 机器学习, 人工智能
- int main(int argc,char *argv[])参数的应用
- org.apache.hadoop.hive.metastore.HiveMetaException: Schema initialization FAILED! Metastore state wo
- tail命令
- 文件夹目录下文件太多,list直接卡住的解决方式
- Error:Execution failed for task compileReleaseRenderscript llvm-rs-cc is missing 解决方法
- python的邮件模块smtplib&email
- views/admin/main.hbs: Cannot read property 'hash' of undefined
- http://www.itnose.net/detail/6508930.html
- CLOSE_WAIT 问题解决之道
- CLOSE_WAIT状态的原因与解决方法(2)
- jfinal jboss 多个数据源报错 Config already exists: main
- 六十六 aiohttp
- 六十五 async/await