zoukankan      html  css  js  c++  java
  • Regular Bracket Sequence

    A. Regular Bracket Sequence

    A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters + and 1 into this sequence. For example, sequences (())(), () and (()(())) are regular, while )(, (() and (()))( are not. Let's call a regular bracket sequence "RBS".

    You are given a sequence s of n characters "(", ")", and/or "?". There is exactly one character ( and exactly one character ) in this sequence.

    You have to replace every character ? with either ) or ( (different characters ? can be replaced with different brackets). You cannot reorder the characters, remove them, insert other characters, and each ? must be replaced.

    Determine if it is possible to obtain an "RBS" after these replacements.

    Input

    The first line contains one integer t ((1leq tleq 1000)) — the number of test cases.

    Each test case consists of one line containing s ((2leq |s|leq 100)) a sequence of characters (, ), and/or ?. There is exactly one character ( and exactly one character ) in this sequence.

    Output

    For each test case, print YES if it is possible to obtain a regular bracket sequence, or NO otherwise.

    You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).

    Example

    input

    Copy

    5
    ()
    (?)
    (??)
    ??()
    )?(?
    

    output

    Copy

    YES
    NO
    YES
    YES
    NO
    

    Note

    In the first test case, the sequence is already an RBS.

    In the third test case, you can obtain an RBS as follows: ()() or (()).

    In the fourth test case, you can obtain an RBS as follows: ()().

    没有引号,看不懂加粗字体,原来是只有一个左括号和一个右括号

    考虑前缀和,左括号+1,右括号-1,则除了?,一个合法的序列是前缀和均大于零且sum[n]=0

    把?替换成1或-1,因为1和-1的个数是确定的,通过sum[n]可求出来

    贪心,先让所有1放左边,-1接着放-1,这样就能让前缀和尽可能多的大于等于0

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const ll MAXN=108,mod=1e9+7,inf=0x3f3f3f3f;
    char s[1000];
    int main(){
        int t;
        scanf("%d",&t);
        while(t--){
            scanf("%s",s);
            int n=strlen(s),ans=1;
            int a=0,b=0,c=0;
            for(int i=0;i<n;++i){
                if(s[i]=='(')a++;
                if(s[i]=='?')b++;
                if(s[i]==')')c++;
            }
            if((a-c+b)&1)ans=0;
            int x=(a-c+b)/2,y=b-x;
            if(y<0)ans=0;
            if(x<0)ans=0;
            for(int i=0;i<n;++i){
                if(s[i]=='?'){
                    if(x)s[i]='(',x--;
                    else s[i]=')',y--;
                }
            }
            for(int i=0,tot=0;i<n;++i){
                if(s[i]=='(')tot++;
                if(s[i]==')')tot--;
                if(tot<0)ans=0;
            }
            if(ans)puts("YES");
            else puts("NO");
        }
        return 0;
    }
    
  • 相关阅读:
    [置顶] android ListView包含Checkbox滑动时状态改变
    Xamarin Android Gestures详解
    尝试在条件“$(_DeviceSdkVersion) >= 21”中对计算结果为“”而不是数字的“$(_DeviceSdkVersion)
    Xamarin Android自定义文本框
    C#四种深拷贝方法(转载)
    设置pictureBox的边框颜色(转载)
    C# 在运行中拖拽,改变控件大小位置类(转载)
    Ocelot + Consul的demo(二)集群部署
    Objective-C 简介
    计算机网络—概述
  • 原文地址:https://www.cnblogs.com/foursmonth/p/14213976.html
Copyright © 2011-2022 走看看