Serval and Parenthesis Sequence【思维】

时间:2022-08-29 18:09:15

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&period; Serval and Parenthesis Sequence 【括号匹配】 Codeforces Round &num;551 &lpar;Div&period; 2&rpar;

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

  3. cf——C&period; Serval and Parenthesis Sequence

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

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

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

  5. Serval and Parenthesis Sequence CodeForces - 1153C

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

  6. HDU 5805 NanoApe Loves Sequence &lpar;思维题&rpar; BestCoder Round &num;86 1002

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

  7. ZOJ 4060 - Flippy Sequence - &lbrack;思维题&rsqb;&lbrack;2018 ACM-ICPC Asia Qingdao Regional Problem C&rsqb;

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

  8. 2018山东省赛 E Sequence &lpar; 思维 &rpar;

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

  9. Codeforces Round &num;529 &lpar;Div&period; 3&rpar; E&period; Almost Regular Bracket Sequence &lpar;思维&comma;模拟栈&rpar;

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

随机推荐

  1. IO字 节流&sol;字符流 读取&sol;写入文件

    流是指一连串流动的数据信号,以先进,先出的方式发送和接收的通道 流的分类根据方向分为输入流所有接收,获得,读取的操作都是属于输入流所有的输入流名字都带有input或Reader 输出流所有发送,写的操 ...

  2. &lbrack;LeetCode&rsqb;题解(python):105-Construct Binary Tree from Preorder and Inorder Traversal

    题目来源: https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 题意分析: ...

  3. 【Linux相识相知】bash的基础特性

    命令历史 shell进程会记录用户提交执行过的命令 可以是用history查看: [root@localhost dev]# history ss -tnl ifconfig vi /etc/sysc ...

  4. 网络配置及shell基础

    一:集群已做完 二:临时配置网络(ip,网关,dns)+永久配置 临时配置网络: ip:    [root@localhost ~]# ifconfig [root@localhost ~]# ifc ...

  5. day5&period;am--拷贝构造与拷贝赋值

    Array& operator = Array(Array const& that){ //避免自赋值 if(&that != this){ //释放旧资源 if(m_arra ...

  6. win32进程概念之句柄表&comma;以及内核对象&period;

    句柄表跟内核对象 一丶什么是句柄表什么是内核对象. 1.句柄表的生成 我们知道.我们使用CreateProcess 的时候会返回一个进程句柄.以及线程句柄. 其实在调用CreateProcess的时候 ...

  7. JFrame背景色设置

    JFrame对象本身是包含Pane的,一般我们在往JFrame中添加组件时,都加在了ContentPane中,这个可以通过JFrame的成员方法getContentPane()取出来.所以如果设置JF ...

  8. TZOJ 2648 小希的迷宫&lpar;并查集&rpar;

    描述 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一个通道 ...

  9. 搭建基于crtmpserver的点播解决方案

    1. linux环境下build并启动crtmpserver 这部分可以参见我写的专项详解文章 <crtmpserver流媒体服务器的介绍与搭建> 和 <crtmpserver配置文 ...

  10. vuex2&period;0&plus;两个小例子

    首先vuex概念比较多,一定要搞懂里面的概念,可以参考官网Vuex2.0概念,我写此文的目的是希望能对前端爱好者提供个参考,加深对vuex2.0各核心概念的理解. 废话少说,直接上干货.这是官网上的一 ...