Submit
Status
Description
You are given a string consisting of parentheses () and []. A string of this type is said to be correct:
(1) if it is the empty string
(2) if A and B are correct, AB is correct,
(3) if A is correct, (A) and [A] is correct.
Input
The file contains a positive integer n and a sequence of n strings of parentheses () and [], one string a line.
Output
A sequence of Yes or No on the output file.
Sample Input
3
([])
(([()])))
([()])()
Sample Output
Yes
No
Yes
#include <cstdio>
#include <stack>
#include <cstring>
#include <iostream>
using namespace std;
char s[10000090];
int main()
{
int t;
scanf("%d",&t);
getchar();
while(t--)
{
gets(s);//要注意会有空串!
stack<char> st;
int len=strlen(s);
for(int i=0; i<len; i++)
{
if(!st.size())
st.push(s[i]);
else
{
char tmp=st.top();
if(tmp=='('&&s[i]==')'||tmp=='['&&s[i]==']')
st.pop();
else st.push(s[i]);
}
}
if(st.size()==0)
printf("Yes\n");
else printf("No\n");
}
return 0;
}