UVA 442(p141)----Matrix Chain Multiplication
2016-02-25 22:57
676 查看
#include<iostream> #include<cstdio> #include<stack> #include<cstring> #include<algorithm> using namespace std; struct point { int a,b; point(int x=0,int y=0):a(x),b(y) {} }; int n; point m[30]; stack<point> s; int main() { cin>>n; for(int i=0; i<n; i++) { char a; int x,y; cin>>a>>x>>y; int tmp=a-'A'; m[tmp].a=x; m[tmp].b=y; } string st; while(cin>>st) { int l=st.length(); int flag=0,ans=0; for(int i=0; i<l; i++) { if(isalpha(st[i])) s.push(m[st[i]-'A']); else if(st[i]==')') { point x=s.top(); s.pop(); point y=s.top(); s.pop(); if(y.b!=x.a) { flag=1; break; } else { ans+=y.a*y.b*x.b; s.push(point(y.a,x.b)); } } } if(flag) cout<<"error"<<endl; else cout<<ans<<endl; } return 0; }题目地址:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=6&page=show_problem&problem=383
相关文章推荐
- UVA 253(p97)----Cube painting
- 研究一下TForm.WMPaint过程(也得研究WM_ERASEBKGND)——TForm虽然继承自TWinControl,但是自行模仿了TCustomControl的全部行为,一共三种自绘的覆盖方法,比TCustomControl还多一种
- TPanel的默认颜色存储在dfm中,读取后在Paint函数中设置刷子的颜色,然后填充整个背景
- LightOJ 1236 Pairs Forming LCM(唯一分解定理)
- IielgnairTs'lacsaP.119
- poll_wait阻塞/唤醒
- elgnairTs'lacsaP.118
- HA(高可用)集群之AIS(corosync),高可用httpd+NFS
- TWinControl.DefaultHandler里的CallWindowProc(FDefWndProc)还挺有深意的,TButton对WM_PAINT消息的处理就是靠它来处理的(以前不明白为什么总是要调用inherited,其实就是没有明白TWinControl.DefaultHandler的真正用处,而且还很有用)
- Rails铁轨(栈)
- hdu 1789 Doing Homework Again!
- MessagingTimeout: Timed out waiting for a reply to message ID
- main()函数
- Failed to download samples index, please check your connection and try again 解决
- how to tell if a library contain debug symbols or not
- Ubuntu下安装ideaIU14并添加桌面快捷方式
- type_traits之has_* 系列
- 安装cuda时 提示toolkit installation failed using unsupported compiler解决方法
- “Unable to open log device '/dev/log/main': No such file or directory”
- C- C&AI