UVA

来源:互联网 发布:美甲软件 编辑:程序博客网 时间:2024/05/16 15:55

题目 : 计算一个矩阵乘法的次数

讲道理,应该用struct的,但是最近几天写stl写傻了。。。直接上了pair,也不知道是为什么。。。

#include <algorithm>#include <cstring>#include <iostream>#include <cstdio>#include <queue>#include <stack>#include <map>using namespace std;#define Pair pair<int,int>int n;map<int ,pair<int,int> > mat;int getdata(){    char s[1005];    stack<pair<Pair,int> > S;    while(cin.getline(s,1005))    {        int slen = strlen(s);        int flag = 1;        for(int i = 0; i < slen; i++)        {            if(s[i] == ')')            {                pair<Pair,int> b = S.top(); S.pop();                pair<Pair,int> a = S.top(); S.pop();                if(a.first.second != b.first.first)                {                    flag = 0;                    printf("error\n"); break;                }                else                {                    int jieguo = (a.first.first)*(a.first.second)*(b.first.second);                    S.push(pair<Pair,int>(Pair(a.first.first,b.first.second),jieguo+a.second+b.second));                }            }            if(isupper(s[i]))            {                int a = s[i] - 'a';                S.push(pair<Pair,int>(Pair(mat[a].first,mat[a].second),0));            }        }        if(flag)            printf("%d\n",S.top().second);    }}int main(){    scanf("%d",&n);    for(int i = 0; i < n ; i++)    {        char s[2];        int a,b;        scanf("%s%d%d",s,&a,&b);        mat[s[0] -'a'] = Pair(a,b);    }    getchar();    getdata();    return 0;}
0 0