Rikka with Parenthesis II
来源:互联网 发布:淘宝帐号俩个手机登陆 编辑:程序博客网 时间:2024/05/14 06:13
Rikka with Parenthesis II
.
.
题意:给定一个由“(”,“)”组成的序列,看交换其中两个位置的符号使得序列合法(一定要交换)。
.
.
解法:弄一个队列如果出现相邻的()就出队,然后记录有多少种不合法的情况,比如队列开头为),那么就把这个替换成(,并记录有一个不合法,如果最终队列不为空且最后为(,那么替换成)并记录。最后如果恰好只有两个或没有不合法那么就可以了。注意特判一下长度为2的“()”这种情况就好了。
.
.
#include <iostream>#include <stdio.h>#include <stdlib.h>using namespace std;const int maxn = 100010;char c[maxn];int n, m, correct, l, r;int main() { //freopen("a.in","r",stdin); int tt; scanf("%d", &tt); while (tt--) { scanf("%d", &n); m = 0; correct = 0; l = 0; r = 0; for (int i = 1; i <= n; i++) { m++; cin >> c[m]; if (c[m] == '(') l++; if (c[m] == ')') r++; if (m == 1 && c[m] == ')') { correct++; c[m] = '('; } while (m >= 2 && c[m] == ')' && c[m-1] == '(' ) { m = m-2; } } if (m > 0) { c[m] = ')'; correct++; } while (m >= 2 && c[m] == ')' && c[m-1] == '(' ) { m = m-2; } if (l != r) { printf("No\n"); continue; } if (l == 1 && correct == 0) { printf("No\n"); continue; } if (correct != 0 && correct != 2) { printf("No\n"); continue; } if (m > 0) { printf("No\n"); continue; } printf("Yes\n"); }}
0 0
- Rikka with Parenthesis II
- Rikka with Parenthesis II
- HDU5831 Rikka with Parenthesis II
- HDU5831 Rikka with Parenthesis II
- HDU5831-Rikka with Parenthesis II
- hdu 5831 Rikka with Parenthesis II【水题】
- HDU 5831 Rikka with Parenthesis II
- hdu Rikka with Parenthesis II 模拟
- hdu5831 Rikka with Parenthesis II(水)
- Problem 1011 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
- java的calendar类常见日期处理
- Unity3d ugui(3) button按钮
- Java中的OOM
- Java学习笔记 Struts常用相关配置
- 【Bootstrap3.0建站笔记一】表单元素排版
- Rikka with Parenthesis II
- kaggle项目实战一
- 一元购宝倒计时之angularJs-$interval
- onActivityResult:拍照不进入原因
- iOS崩溃堆栈信息的符号化解析
- Map 集合详解HashMap_TreeMap_LinkedHashMap
- FZU-2105 Digits Count(线段树)
- RxJava学习(十二)
- 安卓谷歌电子市场学习笔记系列——怎么移除集合中的元素(迭代器的使用)