HDU5831
Rikka with Parenthesis II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 857 Accepted Submission(s): 442
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?
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 ‘)’.
4
())(
4
()()
6
)))(((
Yes
No
For the second sample input, Rikka can choose (1,3) or (2,4) to swap. But do nothing is not allowed.
/*
找出几个特殊情况,剩下的就好办了,))((也是可以的,()不可以。从左向右,(,a++,如果是)并且a==0,b++;a!=0,a--;
*/
#include<iostream>
#include<string>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<queue>
#include<stack>
using namespace std;
int t,n;
string s;
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
cin>>s;
int a=,b=;
int k=s.size();
if(n%==)
{
printf("No\n");
continue;
}
for(int i=;i<n;i++)
{
if(s[i]=='(') a++;
else if(s[i]==')')
{
if(a==)
b++;
else a--;
}
}
if(a==&&b==)
printf("Yes\n");
else if(a==&&b==&&n!=)
printf("Yes\n");
else if(a==&&b==)
printf("Yes\n");
else printf("No\n");
}
return ;
}
HDU5831的更多相关文章
- 【HDU5831】Rikka with Parenthesis II(括号)
BUPT2017 wintertraining(16) #4 G HDU - 5831 题意 给定括号序列,问能否交换一对括号使得括号合法. 题解 注意()是No的情况. 任意时刻)不能比(超过2个以 ...
随机推荐
- 汇编学习(六)——代码转换程序
(一)逻辑运算指令 一.双操作数逻辑运算指令 1.指令格式: AND dst,src ; "与"运算, OR dst,src ; "或"运算 XOR dst,s ...
- Socket编程基础知识
端口号常识: 端口号被从1 开始分配. 通常端口号超出255 的部分被本地主机保留为私有用途. 1到255 之间的号码被用于远程应用程序所请求的进程和网络服务. 每个网络通信循环地 ...
- zookeeper源码分析三LEADER与FOLLOWER同步数据流程
根据二)中的分析,如果一台zookeeper服务器成为集群中的leader,那么一定是当前所有服务器中保存数据最多的服务器,所以在这台服务器成为leader之后,首先要做的事情就是与集群中的其它服务器 ...
- MySql数据库安装&修改密码&开启远程连接图解
相关工具下载地址: mysql5.6 链接:http://pan.baidu.com/s/1o8ybn4I密码:aim1 SQLyog-12.0.8 链接:http://pan.baidu.com/s ...
- MVC3实现多个按钮提交
有时我们需要在同一个Form表单中进行多个按钮的提交,来完成不同的功能.因为MVC中一个表单默认只能提交到一个Action处理,相对比较麻烦. 1.使用客户端脚本 我们可以view中加上如下代码: & ...
- Fzu月赛11 老S的旅行计划 dij
Description 老S在某城市生活的非常不自在,想趁着ICPC举办期间在省内转转.已知老S所在的省有N个城市,M条无向边(对于某一对结点可能出现重边).由于省内的交通相当糟糕,通过某条边所需要花 ...
- stack
stack介绍:先进后出 实现C++STL,栈有两个参数:template<class T,class Container = deque<T>> class stack: 参 ...
- web api :Routing in ASP.NET Web API
引 Web API 和SignalR都是在服务层. If you are familiar with ASP.NET MVC, Web API routing is very similar to M ...
- 由addOneMember引发的思考
addOneMember是一个方法,这个方法在两处地方重复了. 所以在修改页面的时候,发现修改了一处,如果是新手,肯定不会注意到另外一处有问题,他如果没有看清楚这个类到底整体怎样,那么他会犯的错误是就 ...
- ural 1069. Prufer Code
1069. Prufer Code Time limit: 0.25 secondMemory limit: 8 MB A tree (i.e. a connected graph without c ...