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. RecyclerView(5)官方教程带简单示例

    Create Lists The RecyclerView widget is a more advanced and flexible version of ListView. This widge ...

  2. BZOJ 1000: A+B Problem

    问题:A + B问题 描述:http://acm.wust.edu.cn/problem.php?id=1000&soj=0 代码示例: import java.util.Scanner; p ...

  3. objcopy

    objcopy objcopy [options] infile [outfile] Copy the contents of the input object file to another fil ...

  4. 函数buf_LRU_add_block

    /******************************************************************//** Adds a block to the LRU list ...

  5. UVa 11168 (凸包+点到直线距离) Airport

    题意: 平面上有n个点,求一条直线使得所有点都在直线的同一侧.并求这些点到直线的距离之和的最小值. 分析: 只要直线不穿过凸包,就满足第一个条件.要使距离和最小,那直线一定在凸包的边上.所以求出凸包以 ...

  6. sql server压缩数据库和日志文件

    DBCC SHRINKDATABASE 功能:压缩数据库 用法:DBCC SHRINKDATABASE tb_115sou_com 注意:只有产生许多未使用空间的操作(如截断表或删除表操作)后,执行收 ...

  7. 转:MVC2表单验证失败后,直接返回View,已填写的内容就会清空,可以这样做;MVC2输出文本;MVC2输出PDF文件

    ViewData.ModelState.AddModelError("FormValidator", message); foreach (string field in Requ ...

  8. uva 11624 Fire!(搜索)

    开始刷题啦= = 痛并快乐着,学到新东西的感觉其实比看那些无脑的小说.电视剧有意思多了 bfs裸体,关键是先把所有的着火点放入队列,分开一个一个做bfs会超时的 发现vis[][]是多余的,完全可以用 ...

  9. macro names must be identifiers

    1.错把 #include 写成了 #define 会报这个错 2.定义一个不存在的宏业会报这个错,如加了-DANDRO 而ANDRO不存在

  10. OLAP、OLTP的介绍和比较 via csdn

    OLAP.OLTP的介绍和比较 数据处理大致可以分成两大类: OLTP(On-Line Transaction Processing)联机事务处理 也称为面向交易的处理系统,其基本特征是顾客的原始数据 ...