您的位置:首页 > 大数据 > 人工智能

UVA442 - Matrix Chain Multiplication

2015-02-27 18:38 405 查看
判断字符)计算

#include <iostream>
#include <stack>
#include <string>
#include <cstdio>
using namespace std;

int main(){
ios::sync_with_stdio(false);
//freopen("in.txt","r",stdin);
int T;
cin>>T;
int a[26][2],b[26][2];
while(T--){
char c;
int x,y;
cin>>c>>x>>y;
b[c-65][0]=x;
b[c-65][1]=y;
}
string line;
while(cin>>line){
for(int i =0;i<26;i++){ a[i][0]=b[i][0]; a[i][1]=b[i][1];}
stack<char> s;
int wrong=0;
int time_mul=0,num=0;
for(string::iterator t=line.begin(); t!=line.end(); t++){
if(*t=='(') { continue; }
else if(*t==')'){
cal:
if(s.empty()) continue;
char c2=s.top(); s.pop();
if(s.empty()){ s.push(c2);continue; }
char c1=s.top(); s.pop();
if(a[c1-65][1]!=a[c2-65][0]) {wrong=1; break;}
time_mul+=a[c1-65][0]*a[c1-65][1]*a[c2-65][1];
a[c1-65][1]=a[c2-65][1];
s.push(c1);
}
else { s.push(*t);}
if(t+1==line.end()) goto cal;//鲁 棒 性
}
wrong==1&&(cout<<"error" <<endl);
wrong==0&&(cout<<time_mul<<endl);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: