码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5831 Rikka with Parenthesis II(六花与括号II)

时间:2016-08-12 06:35:06      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

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:
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?

六花是个数学渣,你懂的。勇太表示前途堪忧,因此他决定来个数学特训。

下面是部分简介:

 

正确的括号序列定义如下:

1.空字符串 "" 为正确序列。

2.如果 "X"  "Y" 都是正确序列那么 "XY" ( X, Y相互连接也是一个正确的是序列。
3.如果 "X" 是一个正确的序列那么 "(X)" 也是一个正确的序列。
每个正确序列都适用上诉规则。
正确括号序列例子如下 "", "()", "()()()", "(()())",  "(((())))".

现在勇太有个括号序列 S, 让六花选两个不同的位置 i, 然后交换 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
4
())(
4
()()
6
)))(((

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,3)  (2,4) 进行交换。但什么也不做并不好。

 

【题解】

大意就是交换一次后能否得到合法的括号序列,但是必须交换。

尽可能合并配对的括号,最后剩下...)))(((...的形式,只有空、)(、以及))((为交换后合法。

需要注意输入是()时必得)(,加个if即可。

【代码 C++

 1 #include <cstdio>
 2 int main(){
 3     int t, n, l, e, nTemp;
 4     char c;
 5     scanf("%d", &t);
 6     while (t--){
 7         scanf("%d ", &n); nTemp = n;
 8         l = e = 0;
 9         while (n--){
10             c = getchar();
11             if (c == () ++l;
12             else{
13                 if (l) --l;
14                 else ++e;
15             }
16         }
17         if (nTemp == 2 && e == 0) puts("No");
18         else{
19             if (l == e && e <= 2) puts("Yes");
20             else puts("No");
21         }
22     }
23     return 0;
24 }

 

 

 

HDU 5831 Rikka with Parenthesis II(六花与括号II)

标签:

原文地址:http://www.cnblogs.com/Simon-X/p/5763398.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!