Rikka with Parenthesis II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 106 Accepted Submission(s): 81
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?
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 ‘)’.
Output
For each testcase, print “Yes” or “No” in a line.
Sample Input
3
4
())(
4
()()
6
)))(((
Sample Output
Yes
Yes
No
Hint
For the second sample input, Rikka can choose (1,3) or (2,4) to swap. But do nothing is not allowed.
题意:给你一个由
(
和
思路:我们考虑普通的括号匹配,因为必须需要交换,所以说我们可以分情况讨论,前提是,这个字符串两种字符的数量相同:
1.当前字符串已经括号匹配,那么检查匹配数,如果
≤1
,必定不会成功,当
>1
的时候,可以成功。
2.当前字符串没有括号匹配,那么还是检查匹配数,如果
≥
括号匹配应有的匹配数-2,那么是可以的,否则不行。
ac代码:
/* ***********************************************
Author : AnICoo1
Created Time : 2016-08-11-12.18 Thursday
File Name : D:\MyCode\2016-8月\2016-8-11.cpp
LANGUAGE : C++
Copyright 2016 clh All Rights Reserved
************************************************ */
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 1010000
#define LL long long
#define ll __int64
#define INF 0xfffffff
#define mem(x,y) memset(x,(y),sizeof(x))
#define PI acos(-1)
#define eps 1e-8
using namespace std;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}
//head
char str[MAXN];
int main()
{
int t;scanf("%d",&t);
while(t--)
{
int len;scanf("%d",&len);
scanf("%s",str);
int numl=0,numr=0;
for(int i=0;i<len;i++)
{
if(str[i]=='(')
numr++;
else
numl++;
}
if(numl!=numr)
{
printf("No\n");
continue;
}
stack<char>s;int cnt=0;
for(int i=0;i<len;i++)
{
if(str[i]=='(')
s.push(str[i]);
else
{
if(s.empty())
continue;
char b=s.top();
if(b=='('&&str[i]==')')
{
s.pop();
cnt++;
}
}
}
if(cnt==len/2)
{
if(cnt>1) printf("Yes\n");
else printf("No\n");
}
else
{
if(cnt>=len/2-2)
printf("Yes\n");
else
printf("No\n");
}
}
return 0;
}