Rikka with Parenthesis II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 857    Accepted Submission(s): 442

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

 /*
找出几个特殊情况,剩下的就好办了,))((也是可以的,()不可以。从左向右,(,a++,如果是)并且a==0,b++;a!=0,a--;
*/
#include<iostream>
#include<string>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<queue>
#include<stack>
using namespace std;
int t,n;
string s;
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
cin>>s;
int a=,b=;
int k=s.size();
if(n%==)
{
printf("No\n");
continue;
}
for(int i=;i<n;i++)
{
if(s[i]=='(') a++;
else if(s[i]==')')
{
if(a==)
b++;
else a--;
}
}
if(a==&&b==)
printf("Yes\n");
else if(a==&&b==&&n!=)
printf("Yes\n");
else if(a==&&b==)
printf("Yes\n");
else printf("No\n");
}
return ;
}

HDU5831的更多相关文章

  1. 【HDU5831】Rikka with Parenthesis II(括号)

    BUPT2017 wintertraining(16) #4 G HDU - 5831 题意 给定括号序列,问能否交换一对括号使得括号合法. 题解 注意()是No的情况. 任意时刻)不能比(超过2个以 ...

随机推荐

  1. HDU 3341 Lost's revenge(AC自动机+DP)

    Lost's revenge Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)T ...

  2. MySQL级联删除的问题

    一.FOREIGN KEY 的定义分为两种:列级约束和表级约束 .列及约束的话,可以在列定义的同时,定义外键约束.比如 如果有2张表,主表:T1(A1 )) 要在从表T2中定义外键列这可以: Crea ...

  3. javase基础笔记2——数据类型和面向对象

    API:Application program interface  程序调用一个方法去实现一个功能 正则表达式:regex 用来匹配的 javaEE里边有三大框架 SSH struts spring ...

  4. 通信原理实践(一)——音频信号处理

    一.信号的离散化 1.采样定理: –如果信号是带限的,并且采样频率fs超过信号最高频率的两倍,那么,原来的连续信号可以从采样样本中完全重建出来. 因此在仿真过程中,采样率(fs)是一个非常重要的参数. ...

  5. cocos2dx游戏开发——微信打飞机学习笔记(二)——游戏框架

    一.游戏的基本框架: WelcomeScene    ——>    GameScene   ——>   GameOverScene ||                           ...

  6. hdu1863 最小生成树(prim)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1863 思路:最小生成树模板题,直接套模板. 代码: #include<iostrea ...

  7. SU suvelan命令学习

  8. Zepto tap 穿透bug

    当两个层重叠在一起时,使用Zepto的tap事件时,点击上面的一层时会触发下面一层的事件,特别是底层如果是input框时,必“穿透”,“google”说原因是“tap事件实际上是在冒泡到body上时才 ...

  9. js 对象(Object)

    一.对象 除了字符串.数字.true.false.null和undefined之外,javascript中的值都是对象. javascript对象属性包括名字和值,属性名可以是包含空字符串在内的任意字 ...

  10. POJ2976 Dropping tests(01分数规划)

    题目大概说给n个二元组Ai和Bi,要去掉k个,求余下的100*∑Ai/∑Bi的最大值. 假设要的最大的值是ans,令Di=Ai-ans*∑Bi,对Di排序取最大的n-k个,如果∑Ai-ans*∑Bi& ...