zoukankan      html  css  js  c++  java
  • Serval and Parenthesis Sequence【思维】

    Serval and Parenthesis Sequence

    题目链接(点击)

    Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School.

    In his favorite math class, the teacher taught him the following interesting definitions.

    A parenthesis sequence is a string, containing only characters "(" and ")".

    A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition.

    We define that |s||s| as the length of string ss. A strict prefix s[1…l]s[1…l] (1≤l<|s|)(1≤l<|s|) of a string s=s1s2…s|s|s=s1s2…s|s| is string s1s2…sls1s2…sl. Note that the empty string and the whole string are not strict prefixes of any string by the definition.

    Having learned these definitions, he comes up with a new problem. He writes down a string ss containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in ss independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence.

    After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable.

    Input

    The first line contains a single integer |s||s| (1≤|s|≤3⋅1051≤|s|≤3⋅105), the length of the string.

    The second line contains a string ss, containing only "(", ")" and "?".

    Output

    A single line contains a string representing the answer.

    If there are many solutions, any of them is acceptable.

    If there is no answer, print a single line containing ":(" (without the quotes).

    Examples

    Input

    6
    (?????
    

    Output

    (()())

    Input

    10
    (???(???(?
    

    Output

    :(
    

    Note

    It can be proved that there is no solution for the second sample, so print ":(".

    题意:

           给一个字符串 只包含 '(' 、 ')' 、和 ' ?'  要求改变 '?' 为 '(' 或 ‘)’ 使最终的字符串满足:从第一位开始到任意一位(非最后一位)的字符串不出现形如 ‘( )’的情况 如果没有情况满足 输出 ' :) '

    思路:

    要满足上面所说的情况肯定是‘‘(‘ 和’)’’的数目相同 并且不出现()的情况

    所以开始的思路是 遍历字符串 统计 l 和 r 括号的数目 必须满足 在最后一位之前必须有 l > r 且不可以相等 

    正解:

    分别统计 l和r的个数 判断l和n/2相差的数目num 并将前num个?全部赋值成(

    后面如果有剩余的 就再赋值)

    最终用上面的的方法(蓝色字体)判断是否符合条件

    AC代码:

    #include<stdio.h>
    const int MAX=3e5;
    int main()
    {
        int n,count1=0,count2=0;
        char a[MAX+5];
        scanf("%d%s",&n,a);
        if(a[0]==')'||a[n-1]=='('||n%2!=0){
            printf(":(
    ");
        }
        else{
            a[0]='(',a[n-1]=')';
            for(int i=0;i<n;i++){
                if(a[i]=='('){
                    count1++;
                }
                else if(a[i]==')'){
                    count2++;
                }
            }
            //printf("*%d
    ",count1);
            count1=n/2-count1;
            //printf("*%d
    ",count1);
            for(int i=0;i<n;i++){
                if(a[i]=='?'&&count1){
                    a[i]='(';
                    count1--;
                }
                else if(a[i]=='?'){
                    a[i]=')';
                }
            }
            int l=0,r=0;
            for(int i=0;i<n;i++){
                if(a[i]=='('){
                    l++;
                }
                else{
                    r++;
                }
                if(l<=r&&i!=n-1){
                    printf(":(
    ");
                    return 0;
                }
            }
            if(l==r){
                puts(a);
            }
            else{
               printf(":(
    ");
            }
        }
        return 0;
    }
  • 相关阅读:
    Python 中的Lock与RLock
    Python 自定义三方库
    Python 通过RSA实现license验证设备指纹与有效期
    Python 通过wmi获取Window服务器硬件信息
    Java List对象集合按对象属性分组、分组汇总、过滤等操作示例
    BrokenPipeError: [Errno 32] Broken pipe
    Python 通过dmidecode获取Linux服务器硬件信息
    Linux 解决E: Sub-process /usr/bin/dpkg returned an error code (1)错误
    Ubuntu 增加新用户并赋予root权限及免密的方法
    关于smali插桩
  • 原文地址:https://www.cnblogs.com/ldu-xingjiahui/p/12407444.html
Copyright © 2011-2022 走看看