Serval and Parenthesis Sequence

题目链接(点击)

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

题意:

给一个字符串 只包含 '(' 、 ')' 、和 ' ?'  要求改变 '?' 为 '(' 或 ‘)’ 使最终的字符串满足:从第一位开始到任意一位(非最后一位)的字符串不出现形如 ‘( )’的情况 如果没有情况满足 输出 ' :) '

思路:

要满足上面所说的情况肯定是‘‘(‘ 和’)’’的数目相同 并且不出现()的情况

所以开始的思路是 遍历字符串 统计 l 和 r 括号的数目 必须满足 在最后一位之前必须有 l > r 且不可以相等

正解:

分别统计 l和r的个数 判断l和n/2相差的数目num 并将前num个?全部赋值成(

后面如果有剩余的 就再赋值)

最终用上面的的方法(蓝色字体)判断是否符合条件

AC代码:

#include<stdio.h>
const int MAX=3e5;
int main()
{
int n,count1=0,count2=0;
char a[MAX+5];
scanf("%d%s",&n,a);
if(a[0]==')'||a[n-1]=='('||n%2!=0){
printf(":(\n");
}
else{
a[0]='(',a[n-1]=')';
for(int i=0;i<n;i++){
if(a[i]=='('){
count1++;
}
else if(a[i]==')'){
count2++;
}
}
//printf("*%d\n",count1);
count1=n/2-count1;
//printf("*%d\n",count1);
for(int i=0;i<n;i++){
if(a[i]=='?'&&count1){
a[i]='(';
count1--;
}
else if(a[i]=='?'){
a[i]=')';
}
}
int l=0,r=0;
for(int i=0;i<n;i++){
if(a[i]=='('){
l++;
}
else{
r++;
}
if(l<=r&&i!=n-1){
printf(":(\n");
return 0;
}
}
if(l==r){
puts(a);
}
else{
printf(":(\n");
}
}
return 0;
}

Serval and Parenthesis Sequence【思维】的更多相关文章

  1. CF1153C Serval and Parenthesis Sequence

    题目地址:CF1153C Serval and Parenthesis Sequence 思路:贪心 如果有解,那么 \(s_0 = (\) && \(s_{n-1} = )\) &a ...

  2. C. Serval and Parenthesis Sequence 【括号匹配】 Codeforces Round #551 (Div. 2)

    冲鸭,去刷题:http://codeforces.com/contest/1153/problem/C C. Serval and Parenthesis Sequence time limit pe ...

  3. cf——C. Serval and Parenthesis Sequence

    括号正确匹配问题,应该不难 #include <iostream> #include <cstring> #include <string> #include &l ...

  4. cf-Round551-Div2-C. Serval and Parenthesis Sequence(贪心)

    题目链接:http://codeforces.com/contest/1153/problem/C 题意:给定由'(',')','?'组成的字符串,问是否能将其中的?全部换成'(‘,’)'使得字符串的 ...

  5. Serval and Parenthesis Sequence CodeForces - 1153C

    题目大意:一个字符串只含有? ( ),?可以变成 ) 或者 ( ,将字符串中所有的?变成) 或者 ( 使得字符串合法. 合法就是让括号配对,并且不可以提前结束比如:()()这样是不合法的. 题解:既然 ...

  6. HDU 5805 NanoApe Loves Sequence (思维题) BestCoder Round #86 1002

    题目:传送门. 题意:题目说的是求期望,其实翻译过来意思就是:一个长度为 n 的数列(n>=3),按顺序删除其中每一个数,每次删除都是建立在最原始数列的基础上进行的,算出每次操作后得到的新数列的 ...

  7. ZOJ 4060 - Flippy Sequence - [思维题][2018 ACM-ICPC Asia Qingdao Regional Problem C]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4060 题意: 给出两个 $0,1$ 字符串 $S,T$,现在你有 ...

  8. 2018山东省赛 E Sequence ( 思维 )

    题目链接 题意 : 给出一个排列,让你删除一个数,使得删除后整个序列的 Good 数数量最多.Good 数的定义为 若 Ai 为 Good 则存在 Aj < Ai ( j < i ) 分析 ...

  9. Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维,模拟栈)

    题意:给你一串括号,每次仅可以修改一个位置,问有多少位置仅修改一次后所有括号合法. 题解:我们用栈来将这串括号进行匹配,每成功匹配一对就将它们消去,因为题目要求仅修改一处使得所有括号合法,所以栈中最后 ...

随机推荐

  1. jsp获取当前日期

    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>  <jsp: ...

  2. BitArray虽好,但请不要滥用,又一次线上内存暴增排查

    一:背景 1. 讲故事 前天写了一篇大内存排查在园子里挺火,这是做自媒体最开心的事拉,干脆再来一篇满足大家胃口,上个月我写了一篇博客提到过使用bitmap对原来的List<CustomerID& ...

  3. Hyperledger Fabric开发(一):环境配置

    macOS系统下配置hyperledger fabric环境 cURL mac中自带curl,如果需要下载最新版本,则可按照以下命令,并将路径添加在系统环境变量的最前面即可 brew install ...

  4. 通过Python扫描代码关键字并进行预警

    近期线上出现一个bug,研发的小伙伴把测试环境的地址写死到代码中,在上线前忘记修改,导致线上发布的代码中使用了测试环境地址. 开发过程中虽然有各种规范制度,但是难免有粗心,与其责备不如通过技术手段将问 ...

  5. golang基础教程——字符串篇

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是golang专题的第6篇文章,这篇主要和大家聊聊golang当中的字符串的使用. 字符串定义 golang当中的字符串本质是只读的字符 ...

  6. [CSharp]传一个包含多个属性的对象,只改变其中个别属性值的方法

    需求 假如有这么一个需求,一个对象Person内的属性设置外包给了另外一个类Options, 而要设这个Person对象的属性,就必须传一个Options实例, 但又不能每个属性重新设一遍,只设要修改 ...

  7. JavaScript几种继承方式的总结

    1.原型链继承 直接将子类型的原型指向父类型的实例,即"子类型.prototype = new 父类型();",实现方法如下: //父类构造函数 function father(n ...

  8. Linux题目

    1.删除某目录下所有文件,只保留指定文件. 例:假设/abc文件夹下有a1.a2....a10文件,保留a5和a6文件,其他全部删除 [root@localhost abc]# touch a{1.. ...

  9. ASP.NET Core 依赖注入最佳实践与技巧

    ASP.NET Core 依赖注入最佳实践与技巧 原文地址:https://medium.com/volosoft/asp-net-core-dependency-injection-best-pra ...

  10. Keycloak快速上手指南,只需10分钟即可接入Spring Boot/Vue前后端分离应用实现SSO单点登录

    登录及身份认证是现代web应用最基本的功能之一,对于企业内部的系统,多个系统往往希望有一套SSO服务对企业用户的登录及身份认证进行统一的管理,提升用户同时使用多个系统的体验,Keycloak正是为此种 ...