Rikka with Parenthesis II

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

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.

 
Author
学军中学
 
Source
 
Recommend
wange2014   |   We have carefully selected several similar problems for you:       
 题意:给你一个由'('和')'组成的字符串,首先你必须交换其中两个不同位置的字符,然后判断是否有一种方案使得最后形成的字符串 合法;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-10;
const int inf =0x7f7f7f7f;
const double pi=acos(-1);
const int N=100005; char s[N];
int main()
{
int cas,n;
scanf("%d",&cas);
while(cas--)
{
scanf("%d",&n);
scanf("%s",s);
int l=0,r=0,len=n;
if(len%2==1) {printf("No\n");continue;}
for(int i=0;s[i]!='\0';i++)
{
if(s[i]=='(') r++;
else if(s[i]==')')
{
if(r>=1) r--;
else l++;
}
}
if(len==2)
{
if(l==1&&r==1) printf("Yes\n");
else printf("No\n");
continue;
} if(l==0&&r==0) printf("Yes\n");
else if(l+r==2)
{
if(l==1&&r==1) printf("Yes\n");
else printf("No\n");
}
else if(l+r==4)
{
if(l==2&&r==2) printf("Yes\n");
else printf("No\n");
}
else printf("No\n");
}
return 0;
}

  分析:比赛时只看到这道题是以前做过的题目的简化版,,结果还是太大意了,,,

本来分析出来了  ))((  这种特殊情况也是可以的,但是草稿纸没打好,一不留神以为是右移成了())(,,,,其实是()()。。悲剧

hdu 5831 Rikka with Parenthesis II 括号匹配+交换的更多相关文章

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

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

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

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

  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 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

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

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

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

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

  7. HDU 5831 Rikka with Parenthesis II

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

  8. Rikka with Parenthesis II---hdu5831(括号匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5831 给你一个只包含‘(’‘)’的字符串,然后让我们交换两个字符一次,问是否能得到一个合法的匹配:必须 ...

  9. 2016湖南省赛----G - Parenthesis (括号匹配)

    2016湖南省赛----G - Parenthesis (括号匹配)   Bobo has a balanced parenthesis sequence P=p 1 p 2…p n of lengt ...

随机推荐

  1. iview Message(全局提示)与Notice(通知提醒)全局配置方法

    在使用iview 的Message与Notice组件时,可以对提示框的显示位置与显示时长进行配置. iview提供了两个配置属性.分别是: top 提示组件距离顶端的距离,单位像素. duration ...

  2. python基础知识0-4

    collection 他是对字典 元组 集合 进行加工的  是计数器 无论 深 ,浅 ,赋值 拷贝 内存地址都不变 赋值也是拷贝的一种 拷贝分两类数字 字符串 另一类: 列表 字典 元组 这一类还分两 ...

  3. Jenkins常用插件介绍

    摘要: 对于中小型运维团队,jenkins作为运维利器,可以解决很多工作中的痛点.基于UI的特性从而让使用者的入门成本很低,基于插件可以具备认证,记录,条件触发以及联动,让运维工程师可以将精力放在业务 ...

  4. mpstat

    mpstat--multiprocessor statistics,统计多处理器的信息 1.安装mpstat工具 [root@localhost ~]# yum install sysstat 2:展 ...

  5. C#面向对象16 访问修饰符

    C# 访问修饰符 public:公开的公共的 private:私有的,只能在内部类中访问 protected:受保护的,子类可以访问和类的内部 internal:只能在当前项目/程序集中访问.在同一项 ...

  6. jvm自带的监控机制

    Jdk为我们提供了查看java服务运行时的监控情况 1.如下图所示,打开指定目录下的jconsole.exe应用程序文件. 2.双击后跳出如下界面,可以看到,我们可以监视本地的,也可以监视远程服务.本 ...

  7. day12 css样式

    目录 1.标签分类   2.浮动布局   3.margin塌陷   4.定位postion   5.背景图 一. 标签分类 默认在标准文档流 行内标签 span,a,em,i,strong,b,inp ...

  8. Delphi CreateFile函数

  9. Delphi 卡通控件

    樊伟胜

  10. Intellij IDEA显示调用时序图插件SequenceDiagram

    1 推荐一个插件SequenceDiagram可以自动生成项目调用的时序图 安装好之后重启idea即可 2 使用,找到你要查询的方法