UVA442解题报告

很简单的一个模拟应用,用到了map、stack,因为括号里永远都只是两个数据,所以我们可以在遇到右括号时弹出两个元素合并成一个元素后再入栈。关于矩阵的信息我们可以存储在一个自定义的结构体里,通过string在map中搜出我们想要的类。

附上AC代码Time 0ms

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


const int maxn=1000+10;
char s[maxn];


struct Node{
    int row,col;
    Node(int row=0,int col=0):row(row),col(col){}
}M[26];


int main(){
    int N;
    scanf("%d",&N); getchar();
    while(N--){
        char c;
        scanf("%c",&c);
        scanf("%d %d",&M[c-'A'].row,&M[c-'A'].col); getchar();
    }
    while(scanf("%s",s)==1){
        stack<Node> S;
        int error=0;
        long long sum=0;
        for(int i=0;i<strlen(s);i++){
            if(isalpha(s[i])) S.push(M[s[i]-'A']);
            else if(s[i]==')'){
                Node p2=S.top(); S.pop();
                Node p1=S.top(); S.pop();
                if(p1.col!=p2.row) { error=1; break; }
                sum+=p1.row*p1.col*p2.col;
                S.push(Node(p1.row,p2.col));
            }
        }
        if(error) printf("error\n");
        else printf("%lld\n",sum);
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值