HDU 5831 Rikka with Parenthesis II (栈&思维)
来源:互联网 发布:制作身份证软件下载 编辑:程序博客网 时间:2024/05/22 02:07
Problem 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 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?
Input
The 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 ‘)’.
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.
数据结构练习括号匹配的升级版,对整个串进行括号匹配,最后剩下的只有
1.空栈2.栈内串为")(" 或"))(("
时才能只交换一次,加上对n==0的特判,其余情况都是”No”;
注意必须要有操作,所以初始“()”结果是No;
ac代码:
#include <bits/stdc++.h>using namespace std;#define rep(i,a,n) for(int i = (a); i < (n); i++)#define per(i,a,n) for(int i = (n)-1; i >= (a); i--)#define clr(arr,val) memset(arr, val, sizeof(arr))#define mp make_pair#define pb push_back#define fi first#define se second#define pi acos(-1)typedef pair<int, int> pii;typedef long long LL;const int maxn = 1006;const double eps = 1e-8;const int mod = 1000000007;int main(int argc, char const *argv[]) { int t; int n; string a; cin >> t; while (t--) { stack<char>s; char ans[1005]; cin >> n; if(n == 0) {puts("Yes");continue;} cin >> a; int idx = 0; rep(i, 0, n){ if(a[i] == '(') s.push(a[i]); else { if(!s.empty() && s.top() == '(') s.pop(); else s.push(a[i]); } } bool f = false; if(s.empty() && n!=2) f = true; else { while (!s.empty()) { ans[idx++] = s.top()=='(' ? ')' :'('; s.pop(); } ans[idx] = '\0'; if(strlen(ans) == 2 && ans[0] ==')' && ans[1] == '(') f = true; if(strlen(ans) == 4 && ans[0] ==')' && ans[1] == ')' && ans[2] == '(' && ans[3] == '(') f = true; } puts(f ? "Yes" : "No"); } return 0;}
- HDU 5831 Rikka with Parenthesis II (栈&思维)
- hdu 5831 Rikka with Parenthesis II【水题】
- HDU 5831 Rikka with Parenthesis II
- hdu 5831 (Rikka with Parenthesis II)
- HDU 5831 Rikka with Parenthesis II
- HDU 5831 Rikka with Parenthesis II
- HDU 5831 Rikka with Parenthesis II
- HDU 5831Rikka with Parenthesis II
- hdu 5831 Rikka with Parenthesis II
- HDU 5831 Rikka with Parenthesis II 【贪心】
- hdu 5831 Rikka with Parenthesis II
- HDU 5831 Rikka with Parenthesis II(栈的使用)
- [HDU 5831] Rikka with Parenthesis II (实现)
- HDU 5831 Rikka with Parenthesis II (模拟)
- hdu 5831 Rikka with Parenthesis II 2016 Multi-University 8
- HDU-5831-Rikka with Parenthesis II-括号匹配
- HDU.5831 Rikka with Parenthesis II【模拟】【8月13】
- HDU 5831 Rikka with Parenthesis II(机智)
- Garbage Collectors
- 静态库与动态库链接、执行时的搜索路径顺序
- 可变字典(NSDictionary)与不可变字典(NSMutableDictionary)
- 返回自增Id,循环插入关联数据
- TIM_OutputState,TIM_OutputNState的意思
- HDU 5831 Rikka with Parenthesis II (栈&思维)
- js拼接html字符串注意事项
- leetcode:字符串之Length of Last Word
- BMFont制作字体
- AccessbilityService实现第三方软件的点击
- Android内存泄露——全解析和处理办法
- linux apache https配置
- regexp_like字符串中的指定字符串或者字符,区分逗号
- [BZOJ4422][Cerc2015]Cow Confinement(差分+扫描线+线段树)