如果左括号数量和右括号数量不等,输出No

进行一次匹配,看匹配完之后栈中还有多少元素:

如果n=2,并且栈中无元素,说明是()的情况,输出No

如果n=2,并且栈中有元素,说明是)(的情况,输出Yes

如果n>2,并且栈中没有元素,或者有2个,或者有4个,输出Yes

如果n>2,并且栈中元素个数大于4个,输出No

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<algorithm>
  6. #include<vector>
  7. #include<map>
  8. #include<set>
  9. #include<queue>
  10. #include<stack>
  11. #include<iostream>
  12. using namespace std;
  13. typedef long long LL;
  14. const double pi=acos(-1.0),eps=1e-;
  15. void File()
  16. {
  17. freopen("D:\\in.txt","r",stdin);
  18. freopen("D:\\out.txt","w",stdout);
  19. }
  20. inline int read()
  21. {
  22. char c = getchar(); while(!isdigit(c)) c = getchar(); int x = ;
  23. while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
  24. return x;
  25. }
  26.  
  27. const int maxn=;
  28. stack<int >s;
  29. int T,n;
  30. char t[maxn];
  31.  
  32. int main()
  33. {
  34. scanf("%d",&T);
  35. while(T--)
  36. {
  37. scanf("%d",&n); scanf("%s",t);
  38. while(!s.empty()) s.pop();
  39. int num1=,num2=;
  40. if(n%==) printf("No\n");
  41. else
  42. {
  43. for(int i=;t[i];i++)
  44. {
  45. if(t[i]=='(') num1++; else num2++;
  46. if(s.empty())
  47. {
  48. if(t[i]=='(') s.push(-);
  49. else s.push();
  50. }
  51. else
  52. {
  53. int num; if(t[i]=='(') num=-; else num=;
  54. if(num==&&s.top()==-) s.pop();
  55. else s.push(num);
  56. }
  57. }
  58. if(num1!=num2) { printf("No\n"); continue; }
  59. if(n==)
  60. {
  61. if(s.size()==) printf("No\n");
  62. else printf("Yes\n");
  63. }
  64. else
  65. {
  66. if(s.size()==||s.size()==||s.size()==) printf("Yes\n");
  67. else printf("No\n");
  68. }
  69. }
  70. }
  71. return ;
  72. }

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 括号匹配+交换

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

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

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

  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 5424——Rikka with Graph II——————【哈密顿路径】

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

随机推荐

  1. 百度地图API的自动定位和搜索功能(移动端)

    近期有个项目涉及到百度地图API,要求做到自动定位和搜索功能.煞费苦心的研究半天,终于能将两个功能合二为一,现将代码贴出来分享给大家,希望你们的砖搬得又快又好.注释不多,具体请参照:http://lb ...

  2. IMG图片垂直居中的问题

    之前老是碰到图片文字位置调整的问题,图片不按自己的要求变化,后来发现其实很简单. <P><img src="" style="vertical-alig ...

  3. JQuery基础知识(1)

    JQuery基础知识(1) 对JQuery学习中遇到的小细节进行了整理和总结 1.JQuery hide()和show()方法,分别对选中的元素进行隐藏和显示,语法:hide()和show分别有对应的 ...

  4. Iperf[转]

    原文链接:https://openmaniak.com/iperf.php Iperf is a tool to measure the bandwidth and the quality of a ...

  5. SSH通过超链接传递中文参数出现乱码问题

    通过超链接传递中文参数出现乱码问题 tomcat中的server.xml文件中修改如下配置: <Connector port="8080" protocol="HT ...

  6. GIT 代码管理工具 SourceTree

    什么是git? git是一款开源的分布式版本控制工具 在世界上所有的分布式版本控制工具中,git是最快.最简单.最流行的 git的起源 作者是Linux之父:Linus Benedict Torval ...

  7. python3数据结构

    列表 list.append(x) 把一个元素添加到列表的结尾,相当于a[len(a):]=[x] list.extend(L) 将一个给定列表中的所有元素都添加到另一个列表中,相当于a[(len): ...

  8. AsyncHttpClient 中的重定向和 setEnableRedirects 方法异常解决

    今天使用 AsyncHttpClient  开源库,遇到个很崩溃的问题: 方法  setEnableRedirects(false); 从名称上看应该是重定向开关的方法,设置为 false 后则普通请 ...

  9. FZU 2243 Daxia like uber

    枚举,最短路. 求出5个点出发的最短路,然后枚举一下这些点之间走的顺序. #pragma comment(linker, "/STACK:1024000000,1024000000" ...

  10. 4-Bom&Dom总结篇

    其实Bom就是指浏览器的东西,比如弹窗啊.浏览器信息啊等 而Dom则是指文档的东西,就是浏览器里边html的东西,如元素啊.属性啊.事件什么的 但Bom的唯一顶层对象window又包含Dom的顶层对象 ...