Rikka with Parenthesis II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0

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

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 ‘)’.

Output

For 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.

题目大意:

给你n长度的一个括号串,问你是否能够在必须交换两个括号的情况下,使得最终交换得到的括号串是匹配的

题解:

若左括号和右括号数量不等,输出’No’

若n==2,这个串刚好是’()’,因为必须交换的原因,也是’No’。

其它情况:

①0个需要挪动的括号,而且n>=4,那么随便挪动一对括号就行。

②1个需要挪动的括号,那么一定有一个右括号也需要挪动与之匹配:)(

③2个需要挪动的括号,那么一定有两个右括号也需要挪动与之匹配:))((挪动14就可以达到目的.

若大于等于三队,则是’No’。(采用栈的方法判断括号的匹配情况)

#include <iostream>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<cstring> using namespace std;
int T,n,l,r;
char ch[];
stack<char>s;
int main()
{
scanf("%d",&T);
for(;T>;T--)
{
scanf("%d",&n);
scanf("%s",&ch);
if (n== && ch[]=='(' && ch[]==')')
{
printf("No\n");
continue;
}
while(!s.empty()) s.pop();
l=; r=;
for(int i=;i<n;i++)
{
if (ch[i]=='(') s.push(ch[i]);
else
{
if (!s.empty()) s.pop();
else l++;
}
}
r=s.size();
if (r!=l) {printf("No\n"); continue;}
if (l>) {printf("No\n"); continue;}
printf("Yes\n");
}
return ;
}

HDU 5831 Rikka with Parenthesis II (贪心)的更多相关文章

  1. HDU 5831 Rikka with Parenthesis II (贪心) -2016杭电多校联合第8场

    题目:传送门. 题意:T组数据,每组给定一个长度n,随后给定一个长度为n的字符串,字符串只包含'('或')',随后交换其中两个位置,必须交换一次也只能交换一次,问能否构成一个合法的括号匹配,就是()( ...

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

    31 Rikka with Parenthesis II (六花与括号II) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536 ...

  3. HDU 5831 Rikka with Parenthesis II (栈+模拟)

    Rikka with Parenthesis II 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5831 Description As we kno ...

  4. hdu 5831 Rikka with Parenthesis II 线段树

    Rikka with Parenthesis II 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5831 Description As we kno ...

  5. hdu 5831 Rikka with Parenthesis II 括号匹配+交换

    Rikka with Parenthesis II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  6. HDU 5831 Rikka with Parenthesis II

    如果左括号数量和右括号数量不等,输出No 进行一次匹配,看匹配完之后栈中还有多少元素: 如果n=2,并且栈中无元素,说明是()的情况,输出No 如果n=2,并且栈中有元素,说明是)(的情况,输出Yes ...

  7. HDU 5831 Rikka with Parenthesis II ——(括号匹配问题)

    用一个temp变量,每次出现左括号,+1,右括号,-1:用ans来记录出现的最小的值,很显然最终temp不等于0或者ans比-2小都是不可以的.-2是可以的,因为:“))((”可以把最左边的和最右边的 ...

  8. 【HDU5831】Rikka with Parenthesis II(括号)

    BUPT2017 wintertraining(16) #4 G HDU - 5831 题意 给定括号序列,问能否交换一对括号使得括号合法. 题解 注意()是No的情况. 任意时刻)不能比(超过2个以 ...

  9. HDU 5424——Rikka with Graph II——————【哈密顿路径】

    Rikka with Graph II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

随机推荐

  1. idea中 在接口中如何直接跳转到该接口的是实现类中?

    例如,我想跳转到UserInfoDao 这个接口的实现类中,操作如下: 把鼠标放到这个接口UserInfoDao 上面,右键,选择 GO To ,然后选择 Implementations,就可以直接跳 ...

  2. Shape-in​​side css新属性 几何图形的实现

    https://www.w3cplus.com/css3/css-shapes-101.html

  3. c++第三十二天

    p164~p170: 大致内容 迭代语句 1.while语句. 2.传统for. 3.范围for.两个注意点是:1- 通常使用auto来确保类型相容,2- 如果需要修改元素则需要使用&符(还可 ...

  4. Centos下给PHP7添加Xhprof性能分析

    什么是 Xhprof?XHProf是facebook 开发的一个测试php性能的扩展,本文记录了在PHP应用中使用XHProf对PHP进行性能优化,查找性能瓶颈的方法. 它报告函数级别的请求次数和各种 ...

  5. c/c++调用dll

    1.lib.h 1 #ifndef LIB_H 2 #define LIB_H 3 4 #include <windows.h> 5 #include <iostream> 6 ...

  6. Python3.x:遍历select下拉框获取value值

    Python3.x:遍历select下拉框获取value值 Select提供了三种选择方法: # 通过选项的顺序,第一个为 0 select_by_index(index) # 通过value属性 s ...

  7. Python3.x:selenium获取iframe内嵌页面的源码

    Python3.x:selenium获取iframe内嵌页面的源码 前言 在一些网页中经常会看到ifrmae/frame标签,iframe是嵌入式框架一般用来在已有的页面中嵌入另一个页面,当一个元素在 ...

  8. 20145216史婧瑶《Java程序设计》第8周学习总结

    20145216 <Java程序设计>第8周学习总结 教材学习内容总结 第十五章 通用API 15.1 日志 java.util.logging 包提供了日志功能相关类与接口,不必额外配置 ...

  9. 强大的jQuery选择器 平时用的太少了 下次要先来看看

  10. 防止XSS攻击的方式

    主要有三种请求方式,进行过滤替换非法符号 1.普通的GET请求数据: 2.FORM表单提交数据: 3.Json格式数据提交: 把下面5个文件放入项目中即可 package com.joppay.adm ...