您的位置:首页 > 其它

括号匹配 加 括号的对数

2015-04-20 12:54 127 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: