HDU 5831 Rikka with Parenthesis II
如果左括号数量和右括号数量不等,输出No
进行一次匹配,看匹配完之后栈中还有多少元素:
如果n=2,并且栈中无元素,说明是()的情况,输出No
如果n=2,并且栈中有元素,说明是)(的情况,输出Yes
如果n>2,并且栈中没有元素,或者有2个,或者有4个,输出Yes
如果n>2,并且栈中元素个数大于4个,输出No
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<iostream> using namespace std; typedef long long LL; const double pi=acos(-1.0),eps=1e-8; void File() { freopen("D:\\in.txt","r",stdin); freopen("D:\\out.txt","w",stdout); } inline int read() { char c = getchar(); while(!isdigit(c)) c = getchar(); int x = 0; while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); } return x; } const int maxn=200010; stack<int >s; int T,n; char t[maxn]; int main() { scanf("%d",&T); while(T--) { scanf("%d",&n); scanf("%s",t); while(!s.empty()) s.pop(); int num1=0,num2=0; if(n%2==1) printf("No\n"); else { for(int i=0;t[i];i++) { if(t[i]=='(') num1++; else num2++; if(s.empty()) { if(t[i]=='(') s.push(-1); else s.push(1); } else { int num; if(t[i]=='(') num=-1; else num=1; if(num==1&&s.top()==-1) s.pop(); else s.push(num); } } if(num1!=num2) { printf("No\n"); continue; } if(n==2) { if(s.size()==0) printf("No\n"); else printf("Yes\n"); } else { if(s.size()==0||s.size()==2||s.size()==4) printf("Yes\n"); else printf("No\n"); } } } return 0; }