`

括号配对问题--一道ACM在线测试题(补充)

 
阅读更多

刚才之所以说简直弱爆了。。。是因为那个系统的测试做的实在是不怎么样!大家可以看看下面这个程序,C++的,同样可以通过。但是如果你输入()()(或者[][][的时候,它是不会有输出的。大家可以试试。下面我将代码贴出来:

#include <iostream>
using namespace std;
#include <string.h>
int main()
{
char ch[10000],s[5000];
int n,i,top=0;
cin>>n;
if(n<=0 || n>100)
return -1;
while(n--)
{
cin>>ch;
for(i=0;i<strlen(ch);i++)
{
if(ch[i]=='[' || ch[i]=='(')
s[top++]=ch[i];
else
{
if((ch[i]==')' && s[top-1]=='(') || (ch[i]==']' && s[top-1]=='['))
top--;
else 
{
cout<<"No"<<endl;
break;
}
}
}
if(strlen(ch)!=0)
{
if(i==strlen(ch)&&top==0)
cout<<"Yes"<<endl;
}
else n++;
top=0; //注意此处要将top重新设置成0
}
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics