括号匹配 加 括号的对数

#include <string.h>
#include<iostream>
#include <stdio.h>
#include <stack>
using namespace std;
int s[1000005];  //存括号的层数 
stack<char>q;
int main()
{
    int bbs,k=1;
    scanf("%d",&bbs);
    getchar();
    while(bbs--)
    {
        int maxn=0;
        int tt=0;
        s[tt]=0;
        char ch;
        while(!q.empty()) q.pop(); //清栈 
        while(1)
        {
            ch=getchar();
            if(ch=='\n') break;
            if(ch=='('||ch=='['||ch=='{')
            {
                if(ch=='(') //对应到')' ,,为()的 ascaii码 差1, 
                {
                    ch++;
                }
                else ch+=2;  //对应到 ] 或 }  ,【】{}的ascaii码值差2 
                q.push(ch);
                s[++tt]=0;
            }
            else
            {

                if(q.empty())
                {
                   tt=0;s[tt]=0;
                }
                else if(ch==q.top())
                {
                    q.pop();
                    s[tt]+=2;
                    s[tt-1]+=s[tt];
                    tt--;
                    maxn=(s[tt]>maxn)?s[tt]:maxn;
                }
                else
                {
                    while(!q.empty())  //没得匹配 不符 记录为0层 
                     q.pop();
                     tt=0;
                     s[tt]=0;
                }
            }
        }
        printf("Case #%d: ",k++);
        if(maxn!=0)
        printf("%d\n",maxn);
        else printf("I think H is wrong!\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值