Rikka with Parenthesis II

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5831

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 $S_i,S_j$.
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(1100
For each testcase, the first line contains an integers n(1

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.

Source


2016 Multi-University Training Contest 8


##题意:

括号匹配问题:空、XY、(X) 为合法字串.
给出一个字串,是否可以经过且只经过一次交换操作,使得结果串合法.
(不能不交换,不能与自己位置交换)


##题解:

考虑交换操作:(必须换且只能换一次).
①. 如果原串本身就合法,长度为2时:"()"->"No", ")("->"Yes".
长度大于2时一定为"Yes", 因为可以直接交换两个相同的括号.
②. 如果原串非法,那么交换时一定交换的不同的符号(否则没用).
那么符合条件的串一定是把一个 '(' -> ')' 且一个 ')' -> '(' . 那么只考虑变换即可.

考虑如何判断一个串是否合法的过程:
依次处理字符,若是'('则入栈,若是')'则从栈中弹出一个'('. 若没有'('则不合法.
那么此题就是上述过程的变种,在处理过程中允许两次变换. 由于'('->')'的时机不方便考虑, 这里就只考虑')'->'('.
①. 如果当前是'(',直接入栈.
②. 如果当前是')',如果栈非空,则弹出一个'('; 如果栈空就把当前的')'变成'('入栈. (标记最多只能变化一次).

用flag标记是否有将')'变为'('的操作. 结果栈要么为空,要么全是'('.
1. 如果整个字串没有被处理完,那么肯定是"No".
2. 如果flag=0, 那么要求没有'('剩下.
3. 如果flag=1, 那么结果栈中的'('只能是两个. "((" -> "()".

官方题解:
最优情况下一定交换第一个右括号和最后一个左括号,交换后判断一下即可。 时间复杂度 O(n).


##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 101000
#define mod 100000007
#define inf 0x3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std;

char str[maxn];

stack s;

int main(int argc, char const *argv[])

{

//IN;

int t; cin >> t;
while(t--)
{
int n; scanf("%d", &n);
while(!s.empty()) s.pop();
scanf("%s", str); if(n == 2) {
if(str[0]=='(' && str[1]==')') {
puts("No");
continue;
}
} int i;
int flag1 = 0;
for(i=0; i<n; i++) {
if(str[i] == '(') {
s.push('(');
} else {
if(!s.empty()) s.pop();
else {
if(flag1) break;
flag1 = 1;
s.push('(');
}
}
} if(i == n) {
if(!flag1) {
if(s.empty()) puts("Yes");
else puts("No");
}
else {
if(s.size() != 2) puts("No");
else puts("Yes");
}
}
else puts("No");
} 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 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  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

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

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

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

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

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

  8. HDU 1022 Train Problem I(栈模拟)

    传送门 Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...

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

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

随机推荐

  1. BZOJ 2301 Problem b(莫比乌斯函数)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2301 题意:每次给出a,b,c,d,K.求有多少数对(x,y)满足a<=x< ...

  2. bzoj1266: [AHOI2006]上学路线route

    最短路+最小割 首先如何使最短路变长?就是要每一条最短路都割一条边. 我们求出每个点到点1和点n的距离,就可以知道哪些边在最短路上(一开始没有想到求到0和n的距离,想用floyd,但是n=500,怕超 ...

  3. 前端JS对后台传递的timestamp的转换

    后台传递的timestamp类型的数据的JSON: Date.prototype.format = function(format) { var o = { "M+" : this ...

  4. MyEclipse的快捷使用(含关联源码和Doc的方式)

    删除行代码 :在Eclipse中将光标移至待删除的行上,然后按Ctrl+d 组合键 快速导入包 :在Eclipse中将光标移至相应的类上面,按Ctrl+Shift+M 组合键 批量行注释 :Ctrl+ ...

  5. svn - 常用命令

    基本流程: 获取新的代码,svn up(date),获取最新代码 锁住文件,防止你提交的时候,别人修改,造成冲突,svn lock filename 修改之后,svn add filename,将文件 ...

  6. VirtualBox的工作原理&参考网上文章

    事先申明,我这里有好多东西都是看网上的,文末给出参考博客链接. 1.在设置里面为什么要选择桥接网络?baidu之后,了解到是虚拟机工作原理的不同,也就是说有好几种工作模式. bridged(桥接模式) ...

  7. PNG文件结构分析 ---Png解析

    PNG文件结构分析 ---Png解析   为了实现更高级的应用,我们必须充分挖掘PNG的潜力. PNG的文件结构 根据PNG文件的定义来说,其文件头位置总是由位固定的字节来描述的:   十进制数 13 ...

  8. http请求返回响应码的意思

    HTTP 状态响应码 意思详解/大全 HTTP状态码(HTTP Status Code)是用以表示网页服务器HTTP响应状态的3位数字代码.它由 RFC 2616 规范定义的,并得到RFC 2518. ...

  9. 【转】如何在IOS中使用3D UI - CALayer的透视投影

    原文网址:http://www.tairan.com/archives/2041/ 例子代码可以在 http://www.tairan.com/thread-3607-1-1.html 下载 iOS的 ...

  10. 【转】IOS 计时器 NSTimer

    原文网址:http://blog.csdn.net/tangshoulin/article/details/7644124 1.初始化 + (NSTimer *)timerWithTimeInterv ...