16 多校8 Rikka with Parenthesis II
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 SS, and he wants Rikka to choose two different position i,ji,j and swap Si,SjSi,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?
InputThe 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 ‘)’.OutputFor 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.
水题,注意两点1一定要交换所以"()"不行;2记录 l和r 遇到第一次r>l的情况和最后面的'('交换位置,之后就不能交换位置了
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<cmath>
#include<math.h>
using namespace std; int main()
{
int T,n;
int l,r,sum;
scanf("%d",&T);
char a[];
bool flag1,flag2;
while(T--)
{
scanf("%d",&n);
if(n==)
{
printf("Yes\n");
continue;
}
cin>>a;
l=;r=;sum=;
if(n==&&a[]=='('&&a[]==')')
printf("No\n");
else if(n%!=)
printf("No\n");
else
{
flag1=false;flag2=true;
for(int i=;i<n;i++)
{
if(a[i]=='(')
l++;
else if(a[i]==')')
r++;
if(r>l&&!flag1)
{
for(int j=n-;j>i;j--)
{
if(a[j]=='(')
{
a[i]='(';
a[j]=')';
l++;
r--;
flag1=true;
break;
}
}
}
else if(r>l&&flag1==true)
{
flag2=false;
break;
}
}
if(flag2&&l==r)
printf("Yes\n");
else
printf("No\n");
} }
return ;
}
16 多校8 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 括号匹配+交换
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的字符串,字符串只包含'('或')',随后交换其中两个位置,必须交换一次也只能交换一次,问能否构成一个合法的括号匹配,就是()( ...
- 【HDU5831】Rikka with Parenthesis II(括号)
BUPT2017 wintertraining(16) #4 G HDU - 5831 题意 给定括号序列,问能否交换一对括号使得括号合法. 题解 注意()是No的情况. 任意时刻)不能比(超过2个以 ...
- HDU 5831 Rikka with Parenthesis II
如果左括号数量和右括号数量不等,输出No 进行一次匹配,看匹配完之后栈中还有多少元素: 如果n=2,并且栈中无元素,说明是()的情况,输出No 如果n=2,并且栈中有元素,说明是)(的情况,输出Yes ...
- HDU 5831 Rikka with Parenthesis II ——(括号匹配问题)
用一个temp变量,每次出现左括号,+1,右括号,-1:用ans来记录出现的最小的值,很显然最终temp不等于0或者ans比-2小都是不可以的.-2是可以的,因为:“))((”可以把最左边的和最右边的 ...
随机推荐
- 【Java】【6】JDK8 Stream操作整理
摘要: 1,List<EntityOld>转换为List<EntityNew> List<EntityOld> list = oldList; List<En ...
- 【其他】【Restful】【1】简单了解Restful概念
内容: REST是一种设计风格,不是一种标准,是一种思想.符合REST原则的架构方式即可称为RESTful. 在Restful之前的操作:http://127.0.0.1/user/query/1 G ...
- hdu-6397-容斥
Character Encoding Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Oth ...
- System.Web.Optimization 找不到引用,教你如何解决?
在vs 2017 创建 BundleConfig 时添加引用 using System.Web.Optimization 是报错 提示未找到 解决方法: 在最下端窗口中写入:Install-Packa ...
- URL to load resources from the classpath in Java
In Java, you can load all kinds of resources using the same API but with different URL protocols: fi ...
- 【转】Netty之解决TCP粘包拆包(自定义协议)
1.什么是粘包/拆包 一般所谓的TCP粘包是在一次接收数据不能完全地体现一个完整的消息数据.TCP通讯为何存在粘包呢?主要原因是TCP是以流的方式来处理数据,再加上网络上MTU的往往小于在应用处理的消 ...
- qt窗口最小化之后无法打开
转自: https://blog.csdn.net/qiangzi4646/article/details/79743604 http://www.cnblogs.com/lingdhox/p/331 ...
- 使用python将excel数据导入数据库
使用python将excel数据导入数据库 因为需要对数据处理,将excel数据导入到数据库,记录一下过程. 使用到的库:xlrd 和 pymysql (如果需要写到excel可以使用xlwt) 直接 ...
- suffix word ard ar arian arium atic ation atory ator out ~3
1★ ard 不好的人 2★ ar ~的:~人物 1● arian ~人.物 2● arium 地点,场地 3●aster 不怎么样的人 1● ast ~人 ...
- django自定义标签,int转化为str类型
1.在app中创建templatetags目录,目录名必须为templatetags 2.在目录templatetags中创建一个.py文件,例如 strFilter.py strFilter.py ...