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

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| s as the length of string s s s. A strict prefix s [ 1 … l ] s[1\dots l] s[1l] ( 1 ≤ l < ∣ s ∣ ) (1\leq l < |s|) (1l<s) of a string s = s 1 s 2 … s ∣ s ∣ s = s_1s_2\dots s_{|s|} s=s1s2ss is string s 1 s 2 … s l s_1s_2\dots s_l s1s2sl. 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 s s s containing only characters “(”, “)” and “?”. And what he is going to do, is to replace each of the “?” in s s s 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| s ( 1 ≤ ∣ s ∣ ≤ 3 ⋅ 1 0 5 1\leq |s|\leq 3 \cdot 10^5 1s3105), the length of the string.

The second line contains a string s s s, 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).

Example

i n p u t \tt input input
6
(???
o u t p u t \tt output output
(()())
i n p u t \tt input input
10
(???(???(?
o u t p u t \tt output output
:(

Note

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

Tutorial

本题就是字符串匹配问题,但任意前缀不能是合法的括号匹配串,需要在最后一步完成所有的括号匹配才能满足要求,所以需要贪心地将 ( 尽可能向前放,先放 (,再放 ),最后判断字符串是否合法即可

此解法时间复杂度为 O ( n ) \mathcal O(n) O(n)

Solution

n = int(input())
s = list(input())
cnt, cur = n // 2 - s.count('('), 0
for i in range(n):
    if s[i] == '(':
        cur += 1
    elif s[i] == ')':
        cur -= 1
    elif cnt:
        s[i] = '('
        cur += 1
        cnt -= 1
    else:
        s[i] = ')'
        cur -= 1
    if cur <= 0 and i != n - 1:
        exit(print(':('))
else:
    print(''.join(s) if cur == 0 else ':(')
  • 21
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值