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是可以的,因为:“))((”可以把最左边的和最右边的 ...
随机推荐
- navicat 连接 mysql 解决出现client does not support authentication protocol requested by server的问题
MySQL8换了加密插件,数据库管理客户端都来不及更新,连接方式缺乏sha2的加密方式首先第一步, UPDATE mysql.user SET plugin = 'mysql_native_passw ...
- UI BOL 练习 get value set attr
" " " " "**********************change list************************* "2 ...
- 模拟数据库丢失undo表空间
数据库无事务情况下丢失undo表空间数据文件 1. 查看当前undo表空间,并删除物理undo文件 SYS@userdata>show parameter undo_tablespace; NA ...
- [洛谷 P2508] 圆上的整点
题目描述 求一个给定的圆(x^2+y^2=r^2),在圆周上有多少个点的坐标是整数. 输入输出格式 输入格式: r 输出格式: 整点个数 输入输出样例 输入样例#1: 4 输出样例#1: 4 说明 n ...
- php获取当月天数及当月第一天及最后一天
1.获取上个月第一天及最后一天. echo date('Y-m-01', strtotime('-1 month')); echo "<br/>"; ech ...
- Nodejs+mysql+Express: 一个简单的博客
推荐网址: https://github.com/nswbmw/N-blog/blob/backup/book/%E7%AC%AC1%E7%AB%A0%20%E4%B8%80%E4%B8%AA%E7% ...
- Nginx反向代理配置教程(php-fpm)
1.安装nginx http://www.cnblogs.com/lsdb/p/6543441.html 2.安装php-fpm yum install -y php-fpm 3.配置Nginx反向代 ...
- 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 ...
- from…import 语句
- VMware workstation 14 Pro下载、安装及激活码
虚拟机安装 1.百度搜索VMware应用程序 2.功能介绍 3.下载完成 4.开始安装 双击应用程序开始安装 5.点击“下一步” 6.勾选“我接受” 选择“下一步” 7.安装文件的选择:1.默认安装路 ...