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. 在Ubuntu为Android硬件抽象层(HAL)模块编写JNI方法提供Java访问硬件服务接口(老罗学习笔记4)

    在上两篇文章中,我们介绍了如何为Android系统的硬件编写驱动程序,包括如何在Linux内核空间实现内核驱动程序和在用户空间实现硬件抽象层接口.实现这两者的目的是为了向更上一层提供硬件访问接口,即为 ...

  2. Mybatis中配置Mapper的方法

    在这篇文章中我主要想讲一下Mybatis配置文件中mappers元素的配置.关于基础部分的内容可以参考http://haohaoxuexi.iteye.com/blog/1333271. 我们知道在M ...

  3. System,Integer,Calendar,Random和容器

    System 1)arraycopy int[] a = {1.2.3.4}; int[] b = new int[5]; System.arraycopy(a,1,b,3,2); //把数组a中从下 ...

  4. Codeforces Round #242 (Div. 2) C. Magic Formulas (位异或性质 找规律)

    题目 比赛的时候找出规律了,但是找的有点慢了,写代码的时候出了问题,也没交对,还掉分了.... 还是先总结一下位移或的性质吧: 1.  交换律 a ^ b = b ^ a 2. 结合律 (a^b) ^ ...

  5. Qt之QTableView添加复选框(QAbstractItemDelegate)

    简述 上节分享了使用自定义模型QAbstractTableModel来实现复选框.下面我们来介绍另外一种方式: 自定义委托-QAbstractItemDelegate. 简述 效果 QAbstract ...

  6. LA 3295 (计数 容斥原理) Counting Triangles

    如果用容斥原理递推的办法,这道题确实和LA 3720 Highway很像. 看到大神们写的博客,什么乱搞啊,随便统计一下,这真的让小白很为难,于是我决定用比较严格的语言来写这篇题解. 整体思路很简单: ...

  7. Java [Leetcode 144]Binary Tree Preorder Traversal

    题目描述: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given bin ...

  8. 【转】Windows环境下Android Studio v1.0安装教程

    原文网址:http://ask.android-studio.org/?/article/9 http://android-studio.org/index.php/docs/experience/1 ...

  9. 【转】APUE习题4.6---测试lseek作用

    原文网址:http://m.blog.csdn.net/blog/u014488381/42556509 原题:如果使用追加标志打开一个文件以便读.写,能否仍用 lseek 在任一为止开始读?能否用 ...

  10. [Everyday Mathematics]20150108

    设 $f$ 在 $(a,b)$ 上 $n+1$ 次可导, 且 $$\bex \ln\frac{f(b)+f'(b)+\cdots+f^{(n)}(b)}{f(a)+f'(a)+\cdots+f^{(n ...