HDU 5831 Rikka with Parenthesis II(六花与括号II)
HDU 5831 Rikka with Parenthesis II (六花与括号II)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
|
Description |
题目描述 |
|
As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: Correct parentheses sequences can be defined recursively as follows: 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? |
六花是个数学渣,你懂的。勇太表示前途堪忧,因此他决定来个数学特训。 下面是部分简介: 正确的括号序列定义如下: 1.空字符串 "" 为正确序列。 2.如果 "X" 与 "Y" 都是正确序列, 那么 "XY" (将 X, Y相互连接) 也是一个正确的是序列。 现在勇太有个括号序列 S, 让六花选两个不同的位置 i, j 然后交换 Si, Sj。 六花喜欢正确的括号序列。因此她想知道是否能通过这种做法把S变成一个正确的括号序列。 六花表示痛苦无力,急需援助。 |
|
Input |
输入 |
|
The 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 ‘)’. |
第一行有一个数t(1<=t<=1000),表示测试用例的数量。并且不超过10个测试用例是n>100。 对于每个测试用例,第一行有一个整数n(1<=n<=100000),表示S的长度。第二行有一个只包含’(‘与‘)’长度为S的字符串。 |
|
Output |
输出 |
|
For each testcase, print "Yes" or "No" in a line. |
对于每个测试用例,输出一行"Yes"或"No"。 |
|
Sample Input - 输入样例 |
Sample Output - 输出样例 |
|
3 |
Yes |
|
Hint |
提示 |
|
For the second sample input, Rikka can choose (1,3) or (2,4) to swap. But do nothing is not allowed. |
对于第二个样例输出,六花可以选择 (1,3) 或 (2,4) 进行交换。但什么也不做并不好。 |
【题解】
大意就是交换一次后能否得到合法的括号序列,但是必须交换。
尽可能合并配对的括号,最后剩下...)))(((...的形式,只有空、)(、以及))((为交换后合法。
需要注意输入是()时必得)(,加个if即可。
【代码 C++】
#include <cstdio>
int main(){
int t, n, l, e, nTemp;
char c;
scanf("%d", &t);
while (t--){
scanf("%d ", &n); nTemp = n;
l = e = ;
while (n--){
c = getchar();
if (c == '(') ++l;
else{
if (l) --l;
else ++e;
}
}
if (nTemp == && e == ) puts("No");
else{
if (l == e && e <= ) puts("Yes");
else puts("No");
}
}
return ;
}
HDU 5831 Rikka with Parenthesis II(六花与括号II)的更多相关文章
- 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的字符串,字符串只包含'('或')',随后交换其中两个位置,必须交换一次也只能交换一次,问能否构成一个合法的括号匹配,就是()( ...
- 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是可以的,因为:“))((”可以把最左边的和最右边的 ...
- 【HDU5831】Rikka with Parenthesis II(括号)
BUPT2017 wintertraining(16) #4 G HDU - 5831 题意 给定括号序列,问能否交换一对括号使得括号合法. 题解 注意()是No的情况. 任意时刻)不能比(超过2个以 ...
- hdu 5204 Rikka with sequence 智商不够系列
Rikka with sequence Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.p ...
随机推荐
- python通过win32api轻松获取控件的属性值
1.如何利用句柄操作windows窗体 首先,获得窗体的句柄 win32api.FindWindows() 第二,获得窗体中控件的id号,spy++ 第三,根据控件的ID获得控件的句柄(hwnd) ...
- 【python cookbook】【数据结构与算法】5.实现优先级队列
问题:要实现一个队列,它能够以给定的优先级对元素排序,且每次pop操作时都会返回优先级最高的那个元素: 解决方案:采用heapq模块实现一个简单的优先级队列 # example.py # # Exam ...
- javaWeb 使用cookie显示上次访问网站时间
package de.bvb.cookie; import java.io.IOException; import java.io.PrintWriter; import java.util.Date ...
- 转:redis常用命令
一 Redis介绍 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的开发 ...
- 使用crontab不能正常执行的问题
crontab -l: 列出当前用户的crontab列表crontab -e: 以vi打开crontab文件,可以进行编辑.如果需要加新的自启动项目,可以在此进行添加后再输入:wq 保存. & ...
- java面试每日一题9
题目:判断一个数是否是2的方次幂 public class Power { public static void main(String [] args) throws NumberFormatExc ...
- 简单选择排序(Java)
简单选择排序: 每一趟在整个记录中找到最小的那个作为有序序列的第i个记录. class SelectSort{ public void p(int[] a){ for(int i=0;i<a.l ...
- SQLite详解
一.新建SQLite操作类(继承SQLiteOpenHelper) public class SQLiteTest extends SQLiteOpenHelper { final static St ...
- 使用Decision Tree对MNIST数据集进行实验
使用的Decision Tree中,对MNIST中的灰度值进行了0/1处理,方便来进行分类和计算熵. 使用较少的测试数据测试了在对灰度值进行多分类的情况下,分类结果的正确率如何.实验结果如下. #Te ...
- SQL Server 索引和表体系结构(二)
转自:http://www.cnblogs.com/chenmh 非聚集索引 概述 对于非聚集索引,涉及的信息要比聚集索引更多一些,由于整个篇幅比较大涉及接下来的要写的“包含列的索引”,“索引碎片”等 ...