2016湖南省省赛G题
来源:互联网 发布:php门户网站源码 编辑:程序博客网 时间:2024/06/10 00:58
G - Parenthesis
Bobo has a balanced parenthesis sequence P=p 1 p 2…p n of length n and q questions.
The i-th question is whether P remains balanced after p ai and p bi swapped. Note that questions are individual so that they have no affect on others.
Parenthesis sequence S is balanced if and only if:
1. S is empty;
2. or there exists balanced parenthesis sequence A,B such that S=AB;
3. or there exists balanced parenthesis sequence S' such that S=(S').
The input contains at most 30 sets. For each set:
The first line contains two integers n,q (2≤n≤10 5,1≤q≤10 5).
The second line contains n characters p 1 p 2…p n.
The i-th of the last q lines contains 2 integers a i,b i (1≤a i,b i≤n,a i≠b i).
4 2(())1 32 32 1()1 2
NoYesNo
题解:就是一个括号配对问题。
下面附上代码:
#include<stdio.h>#include<algorithm>#include<stack>using namespace std;char str[100005];int main(){ int n, m; while(~scanf("%d%d", &n, &m)) { scanf("%s", str+1); int a, b; for(int i = 1; i <= m; i++) { scanf("%d%d", &a, &b); if(a>b) swap(a,b); if(str[b]=='(' || str[a] == str[b])//这个地方不加前面的str[b]=='(',就会超时。 { printf("Yes\n"); continue; } swap(str[a], str[b]); int ans = 0; for(int i = 1; i <= n; i++) { if(str[i] == '(') ans++; else ans--; if(ans < 0) { printf("No\n"); break;} } if(ans == 0) printf("Yes\n"); swap(str[a], str[b]); } } return 0;}
0 0
- 2016湖南省省赛G题
- 2016湖南省赛G题。
- 2016湖南省省赛G-Parenthesis(CSU1809)
- csu 1809 Parenthesis 2016湖南省赛 G
- 2016 湖南省省赛
- 2016年湖南省省赛A题
- 2011湖南省ACM大赛G题
- ACM2016湖南省赛2016
- 2016ACM湖南省赛
- 湖南省赛A题
- 第九届湖南省省赛
- 第十二届湖南省赛--2016
- 2016湖南省省赛A-2016(CSU1803)
- 2016湖南省省赛B-有向无环图(CSU1804)
- 2016湖南省省赛J-三角形和矩形(CSU1812)
- 2016湖南省省赛F-地铁(CSU1808)
- 湖南省省赛 e题 二分 内部收益率
- 2011ACM湖南省省赛K题 RMQ with Shifts
- 指针
- 集群上打印rdd的元素的坑
- Oracle PL/SQL开发基础(第十七弹:集合类型)
- 异步提交加表单验证
- Jvm的配置参数
- 2016湖南省省赛G题
- 二叉树的后序遍历(leetcode 145)
- 怎么完整的写单例
- 原码、反码、补码和移码其实很简单
- 分析ondraw()过程
- 嵌入式车牌识别,DSP平台
- 2017-5-9 C++笔记
- [CSU 1923 Mysterious Block D]哈希
- 腾讯云正式加入 CNCF 和 Linux 基金会