• 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;
    }
    
  • 相关阅读:
    计算两个时间相差几个月
    批量生成二维码
    PHP实现二维数组(或多维数组)转换成一维数组
    任意二维数组转换成一维数组
    企业微信的使用与公众号以及其它几个应用的使用方式都是一致的
    判断字符类型
    insert一个表的数据到另外一个表
    同步a表的数据到 b表
    PHP Curl Accept-Encoding: gzip乱码问题解决
    spring boot入门程序
  • 原文地址:https://www.cnblogs.com/foursmonth/p/14213976.html
Copyright © 2020-2023  润新知