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
Copy
6 (?????
output
Copy
(()())
input
Copy
10 (???(???(?
output
Copy
:(
Note
It can be proved that there is no solution for the second sample, so print ":(".
题意:要搞出来一个字符串使得他所有的前缀字串不满足括号序列要求,但其本身满足括号序列要求。
题解:保证所有的前缀字串不满足括号序列要求的话,只要尽可能的先把左括号放在前面就好了,最后再检查一遍(是否有不可抗力导致前面的字串满足了括号序列~)
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
string s;
cin>>s;
if(n%2)//奇数个字符一定不能做成合法的括号序列
{
cout<<":("<<endl;
return 0;
}
int cnt=0;
for(int i=0;i<n;i++)//首先记录左括号的个数
{
if(s[i]=='(')
cnt++;
}
for(int i=0;i<n;i++)
{
if(s[i]=='?')
{
if(cnt<n/2)//如果有答案的话,那么左括号的个数等于有括号的个数,这里先把左括号放完
{
s[i]='(';
cnt++;
}
else
s[i]=')';
}
}
cnt=0;
for(int i=0;i<n;i++)
{
if(s[i]=='(')
cnt++;
else
cnt--;
if(cnt<0||(cnt==0&&i!=n-1))//检查前面的字串是否满足序列要求,cnt<0是最前面几个就为右括号导致最后的字符串一定不满足要求
{
cout<<":("<<endl;
return 0;
}
}
if(cnt)//最后的完整序列一定是满足要求的,否则不行
cout<<":("<<endl;
else
cout<<s<<endl;
return 0;
}