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

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

题意:

给你一个括号序列,其中包含若干个'(',')','?',并且‘?’可以替换为任意左右括号,现在让你构造一个新序列,要求新序列的任一严格前缀字串不能满足括号正确匹配,但新序列可以满足括号正确匹配,能构造输出序列,否则打印:(。

题解:

因为字串不能括号匹配,所有可以直接先让第一个和最后一个字符等于‘(’,‘)’,然后直接总长度÷2算出需要的‘(’个数用cnt表示,遍历字符串,先查找原先存在的‘(’,每有一个cnt-1,为了让字串不能匹配,所以让前面的‘?’直接等于‘(’,直到cnt==0,然后剩余‘?’全部等于‘)’即可。

反思:

本来是能做出来的,第一次值忘记初始化,结果以为方法错了,改了方法走上了不归路。。下次遇到题一定多思考,仔细考虑前后关系再做题,不能有个苗条就上,结果粗心大意丢了分。。。

代码:

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 300005;
char str[MAXN];
int n, ans;
int main() {
	scanf("%d%s", &n, str + 1);
	if ((n % 2) || str[1] == ')' || str[n] == '(') {
		puts(":(");
		return 0;
	}
	str[1] = '(';
	str[n] = ')';
	int cnt = (n - 2) / 2;
	for (int i = 2; i < n; i++) {
		if (str[i] == '(') {
			cnt--;
		}
	}
	if (cnt < 0) {
		puts(":(");
		return 0;
	}
	for (int i = 2; i < n && cnt; i++) {
		if (str[i] == '?') {
			str[i] = '(';
			cnt--;
		}
	}
	if (cnt > 0) {
		puts(":(");
		return 0;
	}
	for (int i = 2; i < n; i++) {
		if (str[i] == '?') {
			str[i] = ')';
		}
	}
	for (int i = 2; i < n; i++) {
		if (str[i] == '(') {
			ans++;
		} else {
			ans--;
		}
		if (ans < 0) {
			puts(":(");
			return 0;
		}
	}
	puts(str + 1);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值