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;
}

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

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

最新文章

  1. 有用的git建议
  2. 检测IP地址的正则表达式
  3. [转]mysql binlog in realtime
  4. Java每日一则-001
  5. 430flash的操作
  6. tomcat 容器中的UML架构图
  7. yii中上传图片及文件
  8. 常用js代码学习
  9. cf公式专场-续
  10. No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=x86_64, VALID_ARCHS=armv7 armv7s)
  11. macaca 测试web(3)
  12. HTML入门知识汇总
  13. mysql中general_log查询日志
  14. 【Java并发.5】基础构建模块
  15. 2019swpuj2ee作业3
  16. linux 3.10中完成量的使用
  17. UVA 10733 - The Colored Cubes(Ploya)
  18. BIOS设置找不到设置U盘启动
  19. Ubuntu安装ss
  20. 关于django过滤器的使用

热门文章

  1. T100——作业单身网格消失,查询时单身无法输入
  2. maraidb忘记数据密码
  3. sql lesson21homework
  4. C#面向对象10 继承
  5. O060、Restore Volume 操作
  6. Spring中常用的设计模式之:代理模式
  7. Web框架理解
  8. vue事件处理机制
  9. JSP中的普通路径写法
  10. Golang Gateway API 搭建教程