hdu 5831 Rikka with Parenthesis II 括号匹配+交换

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

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.

 
Author
学军中学
 
Source
 
Recommend
wange2014   |   We have carefully selected several similar problems for you:       
 题意:给你一个由'('和')'组成的字符串,首先你必须交换其中两个不同位置的字符,然后判断是否有一种方案使得最后形成的字符串 合法;
#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;
}

  分析:比赛时只看到这道题是以前做过的题目的简化版,,结果还是太大意了,,,

本来分析出来了  ))((  这种特殊情况也是可以的,但是草稿纸没打好,一不留神以为是右移成了())(,,,,其实是()()。。悲剧

上一篇:linux的学习系列 10---vi


下一篇:linux的yum命令