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是可以的,因为:“))((”可以把最左边的和最右边的 ...
随机推荐
- linux的越墙方法
.首先要安装openSSH, Ubuntu缺省没有安装SSH Server,使用以下命令安装: sudo apt-get install openssh-server 但是系统有时候会出现E类错误,无 ...
- [luogu P3275] [SCOI2011]糖果
[luogu P3275] [SCOI2011]糖果 题目描述 幼儿园里有N个小朋友,lxhgww老师现在想要给这些小朋友们分配糖果,要求每个小朋友都要分到糖果.但是小朋友们也有嫉妒心,总是会提出一些 ...
- [NOIP 2015TG D1T3] 斗地主
题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的A到K加上大小王的共54张牌来进行的扑克牌游戏.在斗地主中,牌的大小关系根据牌的数码表示如下:3<4< ...
- Windows系统目录解释
目录 说明 C:\Program Files 64位程序安装目录 C:\Program Files (x86) 32位程序安装目录 C:\Windows 操作系统主要目录 C:\Windows\Sys ...
- android studio maven 仓库的使用
转自:http://www.cnblogs.com/sihaixuan/p/4852974.html 原文:How to distribute your own Android library thr ...
- js 实现class作为选择器
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...
- MyBatis 为什么需要通用 Mapper ?
一.通用 Mapper 的用途 ? 我个人最早用 MyBatis 时,先是完全手写,然后用上了 MyBatis 代码生成器(简称为 MBG),在使用 MBG 过程中,发现一个很麻烦的问题,如果数据库字 ...
- Object value iterator:值迭代器
// ES5 // 对于数组,使用标准的 for 循环,其实这种方式并不是在遍历值,而是遍历下标来指向值!ES5中增加了一些数组迭代器:forEach.every.some // forEach:遍历 ...
- OO Summary Ⅳ
测试与正确性论证的效果差异 测试,或者说用断言进行黑箱测试,用大量的数据进行“覆盖性测试”,目的是当分支覆盖率达到100%也就是理论上来说所有可能的输入都已经测试过了,而输出结果均是正确的,那么我们理 ...
- TOleControl(WebBrowser1).Visible := False 这样就可以隐藏浏览器控件
TOleControl(WebBrowser1).Visible := False 这样就可以隐藏浏览器控件了. ------------------------------------------- ...