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. SqlServer 填充因子的说明

    CREATE NONCLUSTERED INDEX IX_d_name ON department(d_name) with fillfactor=30 使用 fill factor 选项可以指定 M ...

  2. MyBatis归纳

      SqlSessionTemplate详解 SqlSessionTemplate类是MyBatis-Spring的核心.这个类负责管理MyBatis的SqlSession,调用MyBatis的SQL ...

  3. GitHub和码云的简单使用

    年轻,又经历了初高大学的英语的纠缠,导致连最简单的语句都看不懂,我在慢慢寻找语言的快乐 GitHub 的简单使用 : https://www.cnblogs.com/zhcncn/p/3731707. ...

  4. vs 2012打开vs2013的sln

    Project -> Properties -> General -> Platform Toolset (as IInspectable correctly commented)

  5. 【转】MFC右键显示菜单之LoadMenu()

    如何在界面内单击右键弹出自己设置的菜单选项? 步骤如下: 1.在资源MENU里添加一个菜单资源,命名为IDR_POP_MENU. 2.在自己添加的菜单中添加事件,如事件1,事件2,事件3,分别添加响应 ...

  6. Mycat高可用解决方案二(主从复制)

    Mycat高可用解决方案二(主从复制) 系统部署规划 名称 IP 主机名称 用户名/密码 配置 mysql主节点 192.168.199.110 mysql-01 root/hadoop 2核/2G ...

  7. Vim编辑器基础

    Vim编辑器基础 vi:Visual Interface vim:VI iMproved Vim模式 1.编辑模式(命令模式) 只能下达命令,不能键入字符 2.输入模式 键入字符 3.末行模式 左下角 ...

  8. token验证机制

    最近在vue-cli项目实现登录的过程中用到了token验证,在此总结如下 1. 登录时,客户端通过用户名与密码请求登录 2. 服务端收到请求去验证用户名与密码 3. 验证通过,服务端会签发一个Tok ...

  9. python 面向对象基础和高级复习

    面向对象基础 面向对象编程 面向过程编程:类似于工厂的流水线 优点:逻辑清晰 缺点:扩展性差 面向对象编程:核心是对象二字,对象属性和方法的集合体,面向对象编程就是一堆对象交互 优点:扩展性强 缺点: ...

  10. python-函数的对象、函数嵌套、名称空间和作用域

    目录 函数的对象 函数对象的四大功能 引用 当做参数传给一个函数 可以当做函数的返回值 可以当做容器类型的元素 函数的嵌套 函数的嵌套定义 函数的嵌套调用 名称空间与作用域 名称空间 内置名称空间 全 ...