题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19102
其实这只是一道非常非常非常简单的对c++栈的应用
一个一个地读入字符
如果是“(”或 “[" 就入栈,如果是 ”)" 或 "]" 就与栈顶元素做比较,如果相匹配就继续,否则直接跳出(其实用一个简单的数组就可以了)
但是本题非常坑爹,因为你需要考虑中间是否有空格(为此我换了两种方法WA了6次)
下面是只用了数组的代码:
#include<cstdio>
#include<cstring>
using namespace std;
char s[200],c;
int main()
{
int t;
scanf("%d%*c",&t);
while(t--)
{
int l=0;
bool f=true;
while((c=getchar())!='\n')
if(f)
{
if(c=='('||c=='[')s[++l]=c;
else
if(c==')'&&s[l]=='('||c==']'&&s[l]=='[')l--;else if(c!=' ')f=false;
}
if(f&&l==0)printf("Yes\n");else printf("No\n");
}
return 0;
}