HDU 5831 Rikka with Parenthesis II 六花与括号II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

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?

六花是个数学渣,你懂的。勇太表示前途堪忧,因此他决定来个数学特训。

下面是部分简介:

正确的括号序列定义如下:

1.空字符串 "" 为正确序列。

2.如果 "X" 与 "Y" 都是正确序列, 那么 "XY" (将 X, Y相互连接) 也是一个正确的是序列。
3.如果 "X" 是一个正确的序列, 那么 "(X)" 也是一个正确的序列。
每个正确序列都适用上诉规则。
正确括号序列例子如下 "", "()", "()()()", "(()())", 和 "(((())))".

现在勇太有个括号序列 S, 让六花选两个不同的位置 i, j 然后交换 Si, Sj。

六花喜欢正确的括号序列。因此她想知道是否能通过这种做法把S变成一个正确的括号序列。

六花表示痛苦无力,急需援助。

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 ‘)’.

第一行有一个数t(1<=t<=1000),表示测试用例的数量。并且不超过10个测试用例是n>100。

对于每个测试用例,第一行有一个整数n(1<=n<=100000),表示S的长度。第二行有一个只包含’(‘与‘)’长度为S的字符串。

Output

输出

For each testcase, print "Yes" or "No" in a line.

对于每个测试用例,输出一行"Yes"或"No"。

Sample Input - 输入样例

Sample Output - 输出样例

3
4
())(
4
()()
6
)))(((

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,3) 或 (2,4) 进行交换。但什么也不做并不好。

【题解】

大意就是交换一次后能否得到合法的括号序列,但是必须交换。

尽可能合并配对的括号,最后剩下...)))(((...的形式,只有空、)(、以及))((为交换后合法。

需要注意输入是()时必得)(,加个if即可。

【代码 C++】

  1. #include <cstdio>
  2. int main(){
  3. int t, n, l, e, nTemp;
  4. char c;
  5. scanf("%d", &t);
  6. while (t--){
  7. scanf("%d ", &n); nTemp = n;
  8. l = e = ;
  9. while (n--){
  10. c = getchar();
  11. if (c == '(') ++l;
  12. else{
  13. if (l) --l;
  14. else ++e;
  15. }
  16. }
  17. if (nTemp == && e == ) puts("No");
  18. else{
  19. if (l == e && e <= ) puts("Yes");
  20. else puts("No");
  21. }
  22. }
  23. return ;
  24. }

HDU 5831 Rikka with Parenthesis II(六花与括号II)的更多相关文章

  1. HDU 5831 Rikka with Parenthesis II (栈+模拟)

    Rikka with Parenthesis II 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5831 Description As we kno ...

  2. hdu 5831 Rikka with Parenthesis II 线段树

    Rikka with Parenthesis II 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5831 Description As we kno ...

  3. HDU 5831 Rikka with Parenthesis II (贪心)

    Rikka with Parenthesis II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

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

    Rikka with Parenthesis II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  5. HDU 5831 Rikka with Parenthesis II (贪心) -2016杭电多校联合第8场

    题目:传送门. 题意:T组数据,每组给定一个长度n,随后给定一个长度为n的字符串,字符串只包含'('或')',随后交换其中两个位置,必须交换一次也只能交换一次,问能否构成一个合法的括号匹配,就是()( ...

  6. HDU 5831 Rikka with Parenthesis II

    如果左括号数量和右括号数量不等,输出No 进行一次匹配,看匹配完之后栈中还有多少元素: 如果n=2,并且栈中无元素,说明是()的情况,输出No 如果n=2,并且栈中有元素,说明是)(的情况,输出Yes ...

  7. HDU 5831 Rikka with Parenthesis II ——(括号匹配问题)

    用一个temp变量,每次出现左括号,+1,右括号,-1:用ans来记录出现的最小的值,很显然最终temp不等于0或者ans比-2小都是不可以的.-2是可以的,因为:“))((”可以把最左边的和最右边的 ...

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

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

  9. hdu 5204 Rikka with sequence 智商不够系列

    Rikka with sequence Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.p ...

随机推荐

  1. ID3DXMesh接口 创建自己的立方体网格

    D3DXCreateMeshFVF 首先创建一个ID3DXMesh接口. ID3DXMesh接口的说明可以参数龙书. 这里主要是用代码来讲解: #define VERTEX_FVF (D3DFVF_X ...

  2. win7 dos命令窗口内容显示不全解决办法--将命令执行结果输出到一个文件中

    执行命令:命令 >>某某路径\文件全名

  3. blade模版之页面的嵌套

    blade模版 相关关键词:@section @yield @extends @extends @show @parent(追加内容而不是覆盖) 父页面view\layout\f.blade.php ...

  4. 常用Git命令

    Git教程:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 一般来说,日常使用只要 ...

  5. float浮动与清除浮动

    浮动: 浮动的框可以左右移动,直到它的边缘碰到包含框或另一个浮动框的边框为止.文档的普通流中的块框会当浮动框不存在一样.但会影响内联框(通常是文本)的排列. 属性值有:left,right,none; ...

  6. tomcat部署到根路径

    在Tomcat中部署war包很简单: 首先,直接把相应的war包放到$TOMCAT_HOME/webapps下,不用建目录: 然后,修改$TOMCAT_HOME/conf/server.xml,在Ho ...

  7. css2---必须学的经典---定位问题

    定位 position body<html<文档  定位是相对文档的 不是相对 body 或 html 1.position:relative 相对定位 a.不能使内嵌元素支持宽高  b. ...

  8. c# 之抽象工厂模式

    Email整体项目 Email类 using System; using System.Collections.Generic; using System.Linq; using System.Tex ...

  9. ThinkPhp之分页

    function page($count,$pagesize=3){ $Page=new \Think\Page($count,$pagesize); //每页显示记录数 $Page->setC ...

  10. hdu1074 Doing Homework

    这题比较有意思,暴力搜索必然tle,可以用状态压缩dp解决. 我们先不考虑完成所有作业的扣分,而考虑其一个子集的情况. 假设我们得到了完成某子集S对应的作业最少扣分,我们试着向该子集中增加一个元素a, ...