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:       
 题意:给你一个由'('和')'组成的字符串,首先你必须交换其中两个不同位置的字符,然后判断是否有一种方案使得最后形成的字符串 合法;
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <vector>
  7. #include <queue>
  8. #include <stack>
  9. #include <map>
  10. #include <algorithm>
  11. #include <set>
  12. using namespace std;
  13. typedef long long ll;
  14. typedef unsigned long long Ull;
  15. #define MM(a,b) memset(a,b,sizeof(a));
  16. const double eps = 1e-10;
  17. const int inf =0x7f7f7f7f;
  18. const double pi=acos(-1);
  19. const int N=100005;
  20.  
  21. char s[N];
  22. int main()
  23. {
  24. int cas,n;
  25. scanf("%d",&cas);
  26. while(cas--)
  27. {
  28. scanf("%d",&n);
  29. scanf("%s",s);
  30. int l=0,r=0,len=n;
  31. if(len%2==1) {printf("No\n");continue;}
  32. for(int i=0;s[i]!='\0';i++)
  33. {
  34. if(s[i]=='(') r++;
  35. else if(s[i]==')')
  36. {
  37. if(r>=1) r--;
  38. else l++;
  39. }
  40. }
  41. if(len==2)
  42. {
  43. if(l==1&&r==1) printf("Yes\n");
  44. else printf("No\n");
  45. continue;
  46. }
  47.  
  48. if(l==0&&r==0) printf("Yes\n");
  49. else if(l+r==2)
  50. {
  51. if(l==1&&r==1) printf("Yes\n");
  52. else printf("No\n");
  53. }
  54. else if(l+r==4)
  55. {
  56. if(l==2&&r==2) printf("Yes\n");
  57. else printf("No\n");
  58. }
  59. else printf("No\n");
  60. }
  61. return 0;
  62. }

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

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

hdu 5831 Rikka with Parenthesis II 括号匹配+交换的更多相关文章

  1. HDU 5831 Rikka with Parenthesis II(六花与括号II)

    31 Rikka with Parenthesis II (六花与括号II) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536 ...

  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 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5831 Description As we kno ...

  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 ——(括号匹配问题)

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

  7. HDU 5831 Rikka with Parenthesis II

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

  8. Rikka with Parenthesis II---hdu5831(括号匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5831 给你一个只包含‘(’‘)’的字符串,然后让我们交换两个字符一次,问是否能得到一个合法的匹配:必须 ...

  9. 2016湖南省赛----G - Parenthesis (括号匹配)

    2016湖南省赛----G - Parenthesis (括号匹配)   Bobo has a balanced parenthesis sequence P=p 1 p 2…p n of lengt ...

随机推荐

  1. django channels

    django channels django channels 是django支持websocket的一个模块. 1. 安装 `pip3 install channels` 2. 快速上手 2.1 在 ...

  2. WebMvcConfigurationSupport跨域和fastjson全局替换

    @Configuration public class WarnWebMvcConfigurationSupport extends WebMvcConfigurationSupport { /** ...

  3. 数据库数据导入/导出报错:无法在只读列“Id”中插入数据。

    本文仅供小白参考,大佬请随意...... 本例是:从vs 2017自带的localDB数据库的数据---导出到---->Sql Server 2008中的相应数据库中 1. 导出数据库: 2. ...

  4. 解决sql "Compatibility_199_804_30003" 和 "SQL_Latin1_General_CP1_CI_AS" 之间的排序规则冲突。

    关联条件加  COLLATE Compatibility_199_804_30003

  5. MFC之CImageList(1)

    CImageList BOOL Create( int cx, int cy, UINT nFlags, int nInitial, int nGrow ); 其中各项参数的含义为:cx定义图像的宽度 ...

  6. linux系统编程相关

    基本的概念:程序,进程,并发,单道程序设计,多道程序设计,时钟中断. 存储介质:寄存器(操作系统的位数是针对寄存器而言的,32位识字节,64位就是8字节).缓存cache.内存,硬盘,网络. cpu的 ...

  7. JS中的SRC

    当应用SRC属性时,首先需要创建一个JS文件.为什么不在此文件中使用<script>标记?您可以直接使用输出语句吗?我会分享我的报告一个答案 JS文件不是HTM文件,因此内部不能有HTML ...

  8. XSS防御和绕过2

    上一篇已经总结过,这里转载一篇,备忘 0x01 常规插入及其绕过 转自https://blog.csdn.net/qq_29277155/article/details/51320064 1 Scri ...

  9. KVM命令记录

    创建qcow2镜像qemu-img create -f qcow2 /vm/kvm/img/vm41.img 500G 创建虚拟机virt-install --name=vm41 --disk pat ...

  10. Java实现文本中的关键字高亮,匹配所有长度

    这个方法还不完整,后面想起来再看,直接放代码 public static String getHeightlightWord(String textWord, String key){ StringB ...