Moderator: Board moderators
4
([])
(([()])))
([()[]()])()
[(])
Yes
No
Yes
NoThe file contains a positive integer n and a sequence of n strings of parentheses () and [], one string a line.
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <stack>
using namespace std;
stack<char> x;
bool judge(char *in)
{
static int i,len;
while(!x.empty()) x.pop();
len=strlen(in);
if(in[0] == ']' || in[0] == ')') return 0;
if(len % 2 != 0) return 0;
for(i=0;i<len;++i){
switch(in[i]){
case '[':
case '(': x.push(in[i]); break;
case ']': if(x.top() == '[') x.pop();
else return 0;
break;
case ')': if(x.top() == '(') x.pop();
else return 0;
break;
default : break;
}
}
if(x.empty()) return 1;
else return 0;
}
int main()
{
int j,times;
char in[130];
times=atol(gets(in));
for(j=0;j<times;++j){
gets(in);
if(judge(in)) cout << "Yes\n";
else cout << "No\n";
}
system("pause");
return 0;
}
Users browsing this forum: No registered users and 0 guests