Rikka with Parenthesis II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 136 Accepted Submission(s): 97
Correct parentheses sequences can be defined recursively as follows:
1.The empty string "" is a correct sequence.
2.If "X" and "Y" are correct sequences, then "XY" (the concatenation of X and Y) is a correct sequence.
3.If "X" is a correct sequence, then "(X)" is a correct sequence.
Each correct parentheses sequence can be derived using the above rules.
Examples of correct parentheses sequences include "", "()", "()()()", "(()())", and "(((())))".
Now Yuta has a parentheses sequence S, and he wants Rikka to choose two different position i,j and swap Si,Sj.
Rikka likes correct parentheses sequence. So she wants to know if she can change S to a correct parentheses sequence after this operation.
It is too difficult for Rikka. Can you help her?
For each testcase, the first line contains an integers n(1<=n<=100000), the length of S. And the second line contains a string of length S which only contains ‘(’ and ‘)’.
4
())(
4
()()
6
)))(((
Yes
No
For the second sample input, Rikka can choose (1,3) or (2,4) to swap. But do nothing is not allowed.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-10;
const int inf =0x7f7f7f7f;
const double pi=acos(-1);
const int N=100005; char s[N];
int main()
{
int cas,n;
scanf("%d",&cas);
while(cas--)
{
scanf("%d",&n);
scanf("%s",s);
int l=0,r=0,len=n;
if(len%2==1) {printf("No\n");continue;}
for(int i=0;s[i]!='\0';i++)
{
if(s[i]=='(') r++;
else if(s[i]==')')
{
if(r>=1) r--;
else l++;
}
}
if(len==2)
{
if(l==1&&r==1) printf("Yes\n");
else printf("No\n");
continue;
} if(l==0&&r==0) printf("Yes\n");
else if(l+r==2)
{
if(l==1&&r==1) printf("Yes\n");
else printf("No\n");
}
else if(l+r==4)
{
if(l==2&&r==2) printf("Yes\n");
else printf("No\n");
}
else printf("No\n");
}
return 0;
}
分析:比赛时只看到这道题是以前做过的题目的简化版,,结果还是太大意了,,,
本来分析出来了 ))(( 这种特殊情况也是可以的,但是草稿纸没打好,一不留神以为是右移成了())(,,,,其实是()()。。悲剧