2016湖南省赛----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').

Input

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).

Output

For each question, output " Yes" if P remains balanced, or " No" otherwise.

Sample Input

4 2
(())
1 3
2 3
2 1
()
1 2

Sample Output

No
Yes
No

題意: 给出一个已经匹配的括号序列,任意的交换两个位置的括号,判断是否匹配,如果匹配输出 Yes 不匹配输出 No。

输入一个n一个m,n表示括号序列的长度,m代表下面给出m种交换方式,对每一种方式进行判断。

思路:

  可将该问题分为三类:

            1. 若交换的两个位置的括号相同 则可直接输出 Yes;

            2.若后面的左括号与前面的右括号交换,则可直接输出Yes. 因为一开始是平衡串,  如果左边的字符')'与右边的'('交换,那么此时交换的两个必能匹配为一对。

            3.若后面的右括号与前面的左括号交换,则可根据括号匹配问题进行判断.

代码实现:

#include <iostream>
#include <string.h>
#include <string>
#include <stdio.h>
#include <algorithm>
#include <stdio.h>
#include <stack>
using namespace std; char s1[]={}; int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
getchar();
scanf("%s",s1);
int a,b;
for(int i=;i<=m;i++){
char c;
scanf("%d%d",&a,&b);
if(a>b)
swap(a,b);
if(s1[a-]==s1[b-]||s1[b-]=='('){ //第一种情况与第二种情况
printf("Yes\n");
continue;
}
swap(s1[a-],s1[b-]);
int sum = ;
for(int i = ;i<n;i++){ // 第三种情况的判断
if(s1[i]=='(')
sum++;
if(s1[i]==')')
sum--;
if(sum<)
break;
}
if(sum==)
printf("Yes\n");
else
printf("No\n");
swap(s1[a-],s1[b-]); //将字符串还原
}
}
return ;
}

2016湖南省赛----G - Parenthesis (括号匹配)的更多相关文章

  1. 第十二届湖南省赛G - Parenthesis (树状数组维护)

    Bobo has a balanced parenthesis sequence P=p 1 p 2…p n of length n and q questions. The i-th questio ...

  2. 2016湖南省赛----A 2016 (同余定理)

    2016湖南省赛----A 2016 (同余定理) Description  给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) 的数量: 1. 1≤a≤n,1≤b≤m; 2. a×b 是 ...

  3. 2016湖南省赛 I Tree Intersection(线段树合并,树链剖分)

    2016湖南省赛 I Tree Intersection(线段树合并,树链剖分) 传送门:https://ac.nowcoder.com/acm/contest/1112/I 题意: 给你一个n个结点 ...

  4. 2016湖南省赛 [Cloned]

    A.2016 给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) 的数量: 1. 1≤a≤n,1≤b≤m; 2. a×b 是 2016 的倍数. Input   输入包含不超过 30 组数 ...

  5. 2016湖南省赛--A题--2016

    2016 [TOC] Description 给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) 的数量: 1. 1≤a≤n,1≤b≤m; 2. a×b 是 2016 的倍数. Input ...

  6. 2016湖南省赛 A 2016 题解(同余)

    题目链接 题目大意 给出正整数 n 和 m,统计满足以下条件的正整数对 (a, b) 的数量: 1<=a<=n 1<=b<=m a*b%2016=0 题目思路 我本来以为是容斥 ...

  7. 2016年省赛G题, Parenthesis

    Problem G: Parenthesis Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 398  Solved: 75[Submit][Status ...

  8. 湖南省第十二届大学生计算机程序设计竞赛 G Parenthesis

    1809: Parenthesis Description Bobo has a balanced parenthesis sequence P=p1 p2…pn of length n and q ...

  9. C. Serval and Parenthesis Sequence 【括号匹配】 Codeforces Round #551 (Div. 2)

    冲鸭,去刷题:http://codeforces.com/contest/1153/problem/C C. Serval and Parenthesis Sequence time limit pe ...

随机推荐

  1. Win10权限问题

    通过组策略打开Administrator用户后,Edge.图片查看器等内置程序不能使用,提示“无法使用内置管理员账户打开” 网上的方法: 1.组策略:本地安全策略编辑器——安全设置——本地策略——安全 ...

  2. 问题驱动的Git学习

    (搬运自我在SegmentFault的博客) 本人是个Git新手,平时用Git最多的就是push,因为别的都不怎么会用.这几天因为在小组中负责代码的整合,顺便将代码提交到Github,接触到了Git更 ...

  3. .net 实现的上传下载,如果是图片就显示上传的图片

    HTML: <div> <input id="fileUpload" type="file" runat="server" ...

  4. Oracle中ROWID详解

    oracle数据库的表中的每一行数据都有一个唯一的标识符,或者称为rowid,在oracle内部通常就是使用它来访问数据的.rowid需要 10个字节的存储空间,并用18个字符来显示.该值表明了该行在 ...

  5. WINDOWS-基础:LPTSTR

    1. LPTSTR解释 与char*等价,表示普通字符/字符串变量,指向字符/字符串的指针. LP:  长指针(long pointer). T:   win32环境中有一个_T宏,用来标识字符是否采 ...

  6. BCB:WebBrowser 控件说明

      控件文件:system32\shdocvw.oca  shdocvw.dll 注册:regsvr32 shdocvw.dll WebBrowser 是 IE 内核做的 VB 控件, WebBrow ...

  7. 安全错误使用CORS在IE10与Node和Express及XMLHttpRequest: 网络错误 0x4c7, 操作已被用户取消

    在IE下:VUE项目,后台替换为https请求之后,vue热更新请求挂起,控制台报错:XMLHttpRequest: 网络错误 0x4c7, 操作已被用户取消.但是chrome与Firefox正常 当 ...

  8. ajax的dataType有哪些类型?

    ajax的dataType有哪些类型? 格式为:dataType:"xxx", •"xml": 返回 XML 文档,可用 jQuery 处理 •"ht ...

  9. mysql绿色版下载及应用

    一.mysql绿色版下载 第一歩:打开下载网址:https://www.oracle.com 点击Menu-->Database and Technologies-->Databases- ...

  10. 禅与 Objective-C 编程艺术(Zen and the Art of the Objective-C Craftsmanship)

    英文版Zen and the Art of the Objective-C Craftsmanshiphttps://github.com/objc-zen/objc-zen-book 中文版禅与 O ...