Rikka with Parenthesis II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 60 Accepted Submission(s): 48
Problem Description
As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:
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?
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?
Input
The first line contains a number t(1<=t<=1000), the number of the testcases. And there are no more then 10 testcases with n>100
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 ‘)’.
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 ‘)’.
Output
For each testcase, print "Yes" or "No" in a line.
Sample Input
3 4 ())( 4 ()() 6 )))(((
Sample Output
Yes Yes NoHintFor the second sample input, Rikka can choose (1,3) or (2,4) to swap. But do nothing is not allowed.
题解:如果是奇数,当然不可能匹配,如果长度只有2,那么()是不行的)(是可以的。剩下的情况下,如果本来就已经匹配了,那么我们任意交换2个相同的就可以啦~如果本来不匹配,那么肯定是一个右括号和一个左括号交换,而且左边的肯定是右括号,右边的是左括号。然后我们从左往右找到第一个不匹配的右括号,从右往左找到第一个不匹配的左括号,我们交换一下然扫一下这个串匹不匹配就好了。
#include <cstdio>
#include <algorithm>
using namespace std;
#define fur(i,a,b) for(int i=(a);i<=(b);i++)
#define furr(i,a,b) for(int i=(a);i>=(b);i--)
const int maxn = 100000+100;
char s[maxn];
int n;
int findl(){
int cnt = 0;
fur(i,1,n){
if(s[i] == '(') cnt++;
else cnt--;
if(cnt<0) return i;
}
return n+1;
}
int findr(){
int cnt = 0;
furr(i,n,1){
if(s[i] == ')') cnt++;
else cnt--;
if(cnt<0) return i;
}
return 0;
}
bool check(){
int cnt = 0;
fur(i,1,n){
if(s[i] == '(') cnt++;
else cnt--;
if(cnt<0) return false;
}
return cnt == 0;
}
bool solve(){
if(n&1) return false;
if(n == 2){
if(s[1] == ')' && s[2] == '(') return true;
else return false;
}
int l = findl();
if(l == n+1) return true;
int r = findr();
if(r == 0) return false;
swap(s[l],s[r]);
return check();
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
scanf("%s",s+1);
if(solve()) puts("Yes");
else puts("No");
}
return 0;
}