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个以 ...
随机推荐
- (转载)RTorrent 命令行使用说明
转自:http://blog.chinaunix.net/uid-22457844-id-2973262.html 参考:http://forum.ubuntu.org.cn/viewtopic.ph ...
- currentStyle
用js的style属性可以获得html标签的样式,但是不能获取非行间样式. 解决方法: 在IE下可以用currentStyle; 在FF下用getComputedStyle; 然而,为了让其兼容,解决 ...
- POJ 2774 后缀数组
题目链接:http://poj.org/problem?id=2774 题意:给定两个只含小写字母的字符串,求字符串的最长公共子串长度. 思路:根据<<后缀数组——处理字符串的有力工具&g ...
- DSP using Matlab 书内练习Example 2.1
先上代码,后出结果图. a. n = [-5:5]; x=2*impseq(-2,-5,5) - impseq(4,-5,5); set(gcf,'Color',[1,1,1]) % 改变坐标外围背景 ...
- express-14 发送邮件
简介 Node和Express都没有内置的邮件发送功能,所以必须使用第三方模块.推荐Andris Reinman的Nodemailer SMTP.MSA和MTA 发送邮件的通用语言是简单邮件传输协议( ...
- linux read简单用法
#!/bin/bash #read chars into var read -n var echo $var #read chars without showing it read -s var ec ...
- 点击 Run 之后发生了什么?
这是我以前去链家网面试的一个题目,当时回答不够全面,现在看了2016 WWDC以及Sunnyxx iDev大会的分享之后,感觉对这个问题有了一些简单的认识,就来总结下.如果大家有补充,麻烦评论下哦! ...
- jetty 长时间运行之后出现 PWC6117 file not found
严重: PWC6117: File "%2Ftmp%2Fjetty-0.0.0.0-9090-admin.war-_admin-any-%2Fwebapp%2Ferror%2F404.jsp ...
- three.js入门3
为什么要用three.js Three.js为我们封装了底层的WebGl接口,使我们在无需掌握繁冗的图形学知识的基础下可以轻松的创建三维场景.相比较使用底层的WebGL我们可以使用更少的代码,大大的降 ...
- SQL Prompt
SQL Prompt介绍编辑 SQL Prompt[1] 是一款拥有SQL智能提示功能的SQL Server和VS插件.SQL Prompt能根据数据库的对象名称,语法和用户编写的代码片段自动进行检索 ...