UESTC 1823 In Galgame We Trust 模拟

今天比赛时一直没写出来的一道水题,题目很简单,比赛的时候心态不对,一直没写对

其实就是栈进行括号匹配再加一个数组记录层次即可

/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;
stack<char> st;
int ans[1000005],now;
int Max;
int main()
{
    int T,C=0;
    scanf("%d",&T);
    getchar();
    while(T--)
    {
        Max=now=0;
        char s,aim;
        ans[now]=0;
        while(!st.empty())st.pop();
        while(1)
        {
            s=getchar();
            if(s=='\n') break;
            if(s=='{'||s=='['||s=='(')
            {
                if(s=='(')s++;
                else s+=2;
                st.push(s);
                ans[++now]=0;
            }
            else
            {
                if(st.empty())
                {
                    now=0;
                    ans[now]=0;
                }
                else if(s==st.top())
                {
                    st.pop();
                    ans[now]+=2;
                    ans[now-1]+=ans[now];
                    now--;
                    Max=max(Max,ans[now]);
                }
                else
                {
                    while(!st.empty())st.pop();
                    now=0;
                    ans[now]=0;
                }
            }
        }
        printf("Case #%d: ",++C);
        if(Max)printf("%d\n",Max);
        else puts("I think H is wrong!");
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值