Codeforces Round #551 (Div. 2) C. Serval and Parenthesis Sequence

探讨了如何在给定包含括号和问号的字符串中,替换问号以确保所有严格前缀不构成正确括号序列,而整个序列则需正确。介绍了判断与替换策略,包括优先填充左括号。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. Serval and Parenthesis Sequence

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 a;
	cin>>a;
	if(n==0||n==1||n%2!=0)cout<<"";
	int l=0,r=0;
	for(int i=0;i<n;i++)
	{
		if(a[i]=='(')l++;
		if(a[i]==')')r++;
		if(l>n/2||r>n/2)
		{
			cout<<":("<<endl;
			return 0;
		}
	}
	l=n/2-l; 
	for(int i=0;i<n;i++)
	{
		if(a[i]=='?'&&l)
		{
			a[i]='(';
			l--;
		}
		else if(a[i]=='?')
		{
			a[i]=')';
			r--;
		}
		
	}
	l=0;
	for(int i=0;i<n;i++)
	{
		if(a[i]=='(')l++;
		if(a[i]==')')l--;
		if(l<0||(l==0&&i!=n-1))
		{
			cout<<":("<<endl;
			return 0;
		}
	}
	if(l!=0)
		{
			cout<<":("<<endl;
			return 0;
		}
	cout<<a;
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值