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. 如何将Android Studio项目提交(更新)到github

    转载:http://blog.csdn.net/jinrall/article/details/45787477 前言 在写这篇文章之前首先我假设你已经安装了Android Studio 并已经会用A ...

  2. Maven 项目导入错误解决。

    Description Resource Path Location Type Failure to transfer org.apache.maven:maven-core:jar:2.0.6 fr ...

  3. springMVC 的工作原理和机制(转)

    工作原理上面的是springMVC的工作原理图: 1.客户端发出一个http请求给web服务器,web服务器对http请求进行解析,如果匹配DispatcherServlet的请求映射路径(在web. ...

  4. JQuery学习之jQuery尺寸

    1.width()和height()方法: width():设置或返回元素的宽度(不包括内边距,边框或外边距) height():设置或返回元素的高度(不包括内边距,边框或外边距) $("b ...

  5. POJ 1200 字符串HASH

    题目链接:http://poj.org/problem?id=1200 题意:给定一个字符串,字符串只有NC个不同的字符,问这个字符串所有长度为N的子串有多少个不相同. 思路:字符串HASH,因为只有 ...

  6. 《DSP using MATLAB》示例Example4.9

    收敛域在圆外,对应原始时间序列为右边序列. 上代码: b = 1; a = poly([0.9, 0.9, -0.9]); % compute the polynomials coefficients ...

  7. maven 各种用途

    1.maven 管理项目编译 作为项目编译代码管理工具,可以方便的进行编译集成. 2. maven 扩展单元测试 扩展对接junit可以方便进行单元测试 3.maven profiles各种devel ...

  8. 我的c++学习(1)hello world!

    // texthello.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> using na ...

  9. Android自动化测试 - MonkeyRunner(二) 锤子便签测试脚本

    来源于:http://testerhome.com/topics/878 # encoding=utf-8 #导入python中自带的time模块和sys模块,脚本中都要用到它们. import ti ...

  10. Codeforces 567C Geometric Progression(思路)

    题目大概说给一个整数序列,问里面有几个包含三个数字的子序列ai,aj,ak,满足ai*k*k=aj*k=ak. 感觉很多种做法的样子,我想到这么一种: 枚举中间的aj,看它左边有多少个aj/k右边有多 ...