Codeforces 1153 C Serval and Parenthesis Sequence

http://codeforces.com/problemset/problem/1153/C

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 ":(".

题目大意:给出一个字符串s只含有(,),?三种字符,?可以抓换成(或),该字符串的前缀定义为:从s头部开始的连续的满足长度<strlen(s)的字符串,若存在一种情况使得该字符串的所有前缀的括号匹配都不成功,但该字符串的括号匹配成功,则输出这种情况下的字符串,否则输出:":("。(不带括号)

思路:贪心,设len=strlen(s),当len为奇数或s的首字符为)或s的末尾字符为(时,必定不满足题意;也就是说s的首个字符必定是(且末尾字符必定是),那么对于1<=i<len的所有i,必须满足[1,i]内的(的个数大于)的个数。考虑用一个前缀和数组sum[MAX][2],其中sum[i][0]记录[1,i]中(的个数,sum[i][1]记录[1,i]中)的个数,因为括号匹配的情况下左括号的数量一定等于右括号的数量,因此我们检测1<=i<len的情况,若存在i使得sum[i][1]>=(i+1)/2或sum[len][0]-sum[i][0]+i/2+1>num,那么该字符串一定不满足题意;否则该字符串是满足题意的,我们优先把(排到前面即可。

#include<iostream>
#include<cstdio>
#include<queue>
#include<cmath>
#include<algorithm>
#include<cstring>
#define INF 0x3f3f3f3f
using namespace std ;

char s[300005];
int sum[300005][2];
int len;

int main()
{
    scanf("%d",&len);
    scanf("%s",s+1);
    if(len&1||s[1]==')'||s[len]=='(')
        printf(":(\n");
    else
    {
        int num=len/2;
        int flag=0;
        for(int i=1;i<=len;i++)
        {
            sum[i][0]=sum[i-1][0];
            sum[i][1]=sum[i-1][1];
            if(s[i]=='(')
                ++sum[i][0];
            else if(s[i]==')')
                ++sum[i][1];
        }
        for(int i=1;i<len;i++)
        {
            if(sum[i][1]>=(i+1)/2||sum[len][0]-sum[i][0]+i/2+1>num)
            {
                flag=1;
                break;
            }
        }
        if(flag)
            printf(":(\n");
        else
        {
            int cnt=num-sum[len][0];
            for(int i=1;i<=len;i++)
            {
                if(s[i]!='?')
                    putchar(s[i]);
                else if(cnt>0)
                {
                    --cnt;
                    putchar('(');
                }
                else
                    putchar(')');
            }
            putchar('\n');
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xiji333/article/details/89355614