紫书例题6-3 UVa442 (栈

来源:互联网 发布:淘宝怎么好友代付 编辑:程序博客网 时间:2024/05/01 08:38

这个应该也是个水题的 可惜题意不是很好理解
紫书上也没说怎么构造新的矩形2333
题目给的样例很给力1A

#include <cstdio>#include <cstring>#include <cmath>#include <queue>#include <stack>#include <map>#include <set>#include <iostream>#include <vector>#include <algorithm>using namespace std;#define ll long long#define N 1000const int mod = 1e9+7;struct node {    int x;int y;}p[100];stack<node> s;void init(){    while(!s.empty()) s.pop();}char str[N];int main(){    int n;    scanf("%d",&n);    char ch;    int a,b;    for(int i = 0;i < n; i++) {        getchar();        scanf("%c%d%d",&ch,&a,&b);        p[ch-'A'].x = a;        p[ch-'A'].y = b;    }    while(~scanf("%s",str)) {        bool flag = true;        int sum = 0;        init();        int len = strlen(str);        for(int i = 0;i < len; i++) {            if(str[i] == '(') continue;            if(str[i]>='A' && str[i]<='Z') {                s.push(p[str[i]-'A']);            }            if(str[i] == ')') {                node m = s.top();                s.pop();                node n = s.top();                s.pop();                if(m.x != n.y) {                    flag = false;                    break;              //不符合要求,直接GG                }                    node zz;                //构造个新的的矩阵,扔进栈中                 zz.x = n.x, zz.y = m.y;                 s.push(zz);                sum += n.x * m.y * n.y;            }        }        if(flag)            printf("%d\n",sum);        else puts("error");        memset(str,0,sizeof*str);    }return 0;}
0 0
原创粉丝点击