hdu 5831 Rikka with Parenthesis II 括号匹配+交换
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
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?
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 ‘)’.
4
())(
4
()()
6
)))(((
Yes
No
For the second sample input, Rikka can choose (1,3) or (2,4) to swap. But do nothing is not allowed.
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <cstdlib>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <stack>
- #include <map>
- #include <algorithm>
- #include <set>
- using namespace std;
- typedef long long ll;
- typedef unsigned long long Ull;
- #define MM(a,b) memset(a,b,sizeof(a));
- const double eps = 1e-10;
- const int inf =0x7f7f7f7f;
- const double pi=acos(-1);
- const int N=100005;
- char s[N];
- int main()
- {
- int cas,n;
- scanf("%d",&cas);
- while(cas--)
- {
- scanf("%d",&n);
- scanf("%s",s);
- int l=0,r=0,len=n;
- if(len%2==1) {printf("No\n");continue;}
- for(int i=0;s[i]!='\0';i++)
- {
- if(s[i]=='(') r++;
- else if(s[i]==')')
- {
- if(r>=1) r--;
- else l++;
- }
- }
- if(len==2)
- {
- if(l==1&&r==1) printf("Yes\n");
- else printf("No\n");
- continue;
- }
- if(l==0&&r==0) printf("Yes\n");
- else if(l+r==2)
- {
- if(l==1&&r==1) printf("Yes\n");
- else printf("No\n");
- }
- else if(l+r==4)
- {
- if(l==2&&r==2) printf("Yes\n");
- else printf("No\n");
- }
- else printf("No\n");
- }
- return 0;
- }
分析:比赛时只看到这道题是以前做过的题目的简化版,,结果还是太大意了,,,
本来分析出来了 ))(( 这种特殊情况也是可以的,但是草稿纸没打好,一不留神以为是右移成了())(,,,,其实是()()。。悲剧
hdu 5831 Rikka with Parenthesis II 括号匹配+交换的更多相关文章
- HDU 5831 Rikka with Parenthesis II(六花与括号II)
31 Rikka with Parenthesis II (六花与括号II) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- HDU 5831 Rikka with Parenthesis II (栈+模拟)
Rikka with Parenthesis II 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5831 Description As we kno ...
- hdu 5831 Rikka with Parenthesis II 线段树
Rikka with Parenthesis II 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5831 Description As we kno ...
- HDU 5831 Rikka with Parenthesis II (贪心)
Rikka with Parenthesis II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- HDU 5831 Rikka with Parenthesis II (贪心) -2016杭电多校联合第8场
题目:传送门. 题意:T组数据,每组给定一个长度n,随后给定一个长度为n的字符串,字符串只包含'('或')',随后交换其中两个位置,必须交换一次也只能交换一次,问能否构成一个合法的括号匹配,就是()( ...
- HDU 5831 Rikka with Parenthesis II ——(括号匹配问题)
用一个temp变量,每次出现左括号,+1,右括号,-1:用ans来记录出现的最小的值,很显然最终temp不等于0或者ans比-2小都是不可以的.-2是可以的,因为:“))((”可以把最左边的和最右边的 ...
- HDU 5831 Rikka with Parenthesis II
如果左括号数量和右括号数量不等,输出No 进行一次匹配,看匹配完之后栈中还有多少元素: 如果n=2,并且栈中无元素,说明是()的情况,输出No 如果n=2,并且栈中有元素,说明是)(的情况,输出Yes ...
- Rikka with Parenthesis II---hdu5831(括号匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5831 给你一个只包含‘(’‘)’的字符串,然后让我们交换两个字符一次,问是否能得到一个合法的匹配:必须 ...
- 2016湖南省赛----G - Parenthesis (括号匹配)
2016湖南省赛----G - Parenthesis (括号匹配) Bobo has a balanced parenthesis sequence P=p 1 p 2…p n of lengt ...
随机推荐
- django channels
django channels django channels 是django支持websocket的一个模块. 1. 安装 `pip3 install channels` 2. 快速上手 2.1 在 ...
- WebMvcConfigurationSupport跨域和fastjson全局替换
@Configuration public class WarnWebMvcConfigurationSupport extends WebMvcConfigurationSupport { /** ...
- 数据库数据导入/导出报错:无法在只读列“Id”中插入数据。
本文仅供小白参考,大佬请随意...... 本例是:从vs 2017自带的localDB数据库的数据---导出到---->Sql Server 2008中的相应数据库中 1. 导出数据库: 2. ...
- 解决sql "Compatibility_199_804_30003" 和 "SQL_Latin1_General_CP1_CI_AS" 之间的排序规则冲突。
关联条件加 COLLATE Compatibility_199_804_30003
- MFC之CImageList(1)
CImageList BOOL Create( int cx, int cy, UINT nFlags, int nInitial, int nGrow ); 其中各项参数的含义为:cx定义图像的宽度 ...
- linux系统编程相关
基本的概念:程序,进程,并发,单道程序设计,多道程序设计,时钟中断. 存储介质:寄存器(操作系统的位数是针对寄存器而言的,32位识字节,64位就是8字节).缓存cache.内存,硬盘,网络. cpu的 ...
- JS中的SRC
当应用SRC属性时,首先需要创建一个JS文件.为什么不在此文件中使用<script>标记?您可以直接使用输出语句吗?我会分享我的报告一个答案 JS文件不是HTM文件,因此内部不能有HTML ...
- XSS防御和绕过2
上一篇已经总结过,这里转载一篇,备忘 0x01 常规插入及其绕过 转自https://blog.csdn.net/qq_29277155/article/details/51320064 1 Scri ...
- KVM命令记录
创建qcow2镜像qemu-img create -f qcow2 /vm/kvm/img/vm41.img 500G 创建虚拟机virt-install --name=vm41 --disk pat ...
- Java实现文本中的关键字高亮,匹配所有长度
这个方法还不完整,后面想起来再看,直接放代码 public static String getHeightlightWord(String textWord, String key){ StringB ...