UVA442 - Matrix Chain Multiplication

来源:互联网 发布:python 画热力图 编辑:程序博客网 时间:2024/06/08 07:50

判断字符)计算

#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;}


0 0
原创粉丝点击