湖南省第十二届省赛:Parenthesis

Description

Bobo has a balanced parenthesis sequence P=p1 p2…pn of length n and q questions.
The i-th question is whether P remains balanced after pai and pbi  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≤105,1≤q≤105).
The second line contains n characters p1 p2…pn.
The i-th of the last q lines contains 2 integers ai,bi (1≤ai,bi≤n,ai≠bi).

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

Hint

Source

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

题目大意:给你个平衡的括号串,然后询问a,b两个数字交换后括号串是否依旧平衡
题目思路:如果a,b两个位置的括号是一样的,显然交换了是没有影响的;如果位置在前面的‘)’与位置在后面的‘(’交换后依旧是平衡的括号串;当位置在前面的‘(’与后面的‘)’交换后,用1与-1来遍历一遍字符串,如果中途出现了-1则代表失去平衡无法匹配。

# include <bits/stdc++.h>
using namespace std;
# define IOS ios::sync_with_stdio(false);
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
///coding...................................

const int MAXM=2e5+5;
char a[MAXM];

int main()
{
    IOS
#ifdef FLAG
    freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
#endif /// FLAG
    int n,q,q1,q2;
    while(cin>>n>>q) {
        cin>>a+1;
        FOR(i,1,q) {
            cin>>q1>>q2;
            if(q1>q2)swap(q1,q2);
            if(a[q1]==a[q2])puts("Yes");
            else if(a[q1]==')')puts("Yes");
            else {
                int sum=0;
                swap(a[q1],a[q2]);
                FOR(j,1,n) {
                    if(a[j]=='(')++sum;
                    else--sum;
                    if(sum<0)break;
                }
                if(sum==0)puts("Yes");
                else puts("No");
                swap(a[q1],a[q2]);
            }
        }
    }
    return 0;
}

posted @ 2017-08-23 20:37  韵祈  阅读(228)  评论(0编辑  收藏  举报