zoukankan      html  css  js  c++  java
  • 湖南省第十二届省赛: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;
    }
    
    
  • 相关阅读:
    bootstap 折叠
    AtCoder AGC019E Shuffle and Swap (DP、FFT、多项式求逆、多项式快速幂)
    Codeforces Gym 101630J Journey from Petersburg to Moscow (最短路)
    BZOJ 4042 Luogu P4757 [CERC2014]Parades (树形DP、状压DP)
    BZOJ 2734 [HNOI2012]集合选数 (状压DP、时间复杂度分析)
    BZOJ 2759 一个动态树好题 (LCT)
    Codeforces 1205C Palindromic Paths (交互题、DP)
    getopt实现传参自动识别
    powershell笔记
    bat语法需要注意的地方
  • 原文地址:https://www.cnblogs.com/teble/p/7420066.html
Copyright © 2011-2022 走看看