Serval and Parenthesis Sequence【思维】
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【思维】的更多相关文章
- CF1153C Serval and Parenthesis Sequence
题目地址:CF1153C Serval and Parenthesis Sequence 思路:贪心 如果有解,那么 \(s_0 = (\) && \(s_{n-1} = )\) &a ...
- 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 ...
- cf——C. Serval and Parenthesis Sequence
括号正确匹配问题,应该不难 #include <iostream> #include <cstring> #include <string> #include &l ...
- cf-Round551-Div2-C. Serval and Parenthesis Sequence(贪心)
题目链接:http://codeforces.com/contest/1153/problem/C 题意:给定由'(',')','?'组成的字符串,问是否能将其中的?全部换成'(‘,’)'使得字符串的 ...
- Serval and Parenthesis Sequence CodeForces - 1153C
题目大意:一个字符串只含有? ( ),?可以变成 ) 或者 ( ,将字符串中所有的?变成) 或者 ( 使得字符串合法. 合法就是让括号配对,并且不可以提前结束比如:()()这样是不合法的. 题解:既然 ...
- HDU 5805 NanoApe Loves Sequence (思维题) BestCoder Round #86 1002
题目:传送门. 题意:题目说的是求期望,其实翻译过来意思就是:一个长度为 n 的数列(n>=3),按顺序删除其中每一个数,每次删除都是建立在最原始数列的基础上进行的,算出每次操作后得到的新数列的 ...
- 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$,现在你有 ...
- 2018山东省赛 E Sequence ( 思维 )
题目链接 题意 : 给出一个排列,让你删除一个数,使得删除后整个序列的 Good 数数量最多.Good 数的定义为 若 Ai 为 Good 则存在 Aj < Ai ( j < i ) 分析 ...
- Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维,模拟栈)
题意:给你一串括号,每次仅可以修改一个位置,问有多少位置仅修改一次后所有括号合法. 题解:我们用栈来将这串括号进行匹配,每成功匹配一对就将它们消去,因为题目要求仅修改一处使得所有括号合法,所以栈中最后 ...
随机推荐
- http://blog.itpub.net/28602568/viewspace-759789/
varchar .varchar2.nvarchar.nvarchar2 -->存储可变的字符串 varchar .varchar2:varchar:汉字全角等字符占2字节,数字.字母均1个字 ...
- 六、表达式:前缀&&后缀
count为运算后的值.
- 8.8SQL Server数据类型介绍1
image类型:存储二进制字节数组. (相当于C#中的byte[]字节类型) sql server常用的数据类型 1.image类型,用来存储byte[](字节). 2.字符串类型 char ncha ...
- vue 下拉列表动画
点击可以收起,这里注意先给需要收起展开的的容器设置高度,通过样式v-enter和v-leave-to设置结束和开始前的就可以了
- 11.Java连接Redis_Jedis_测试联通
使用Java开发项目的时候使用Redis的话,目前有一些开源API可以使用. 最常用的就是jedis,它提供了许多基于Java的对象和方法来调用Redis的指令. jedis的jar包下载地址http ...
- 上古神器vim系列之移动三板斧
[导读] 前文总结了vim如何进入,如何保存退出,如何进入编辑模式.本文来总结一些稍微进阶的内容,在normal模式下如何高效的浏览代码. 模式回顾 在normal模式下主要用于浏览代码,那么有哪些方 ...
- 几种常见的app推广形式
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- data类型的url
所谓"data"类型的Url格式,是在RFC2397中 提出的,目的对于一些"小"的数据,可以在网页中直接嵌入,而不是从外部文件载入.例如对于img这个Ta ...
- Rocket - util - GenericParameterizedBundle
https://mp.weixin.qq.com/s/vf0PfjbxQ3Ywjk6tk85SfA 介绍GenericParameterizedBundle的实现. 1. 基本介绍 ...
- EntityFramework数据持久化 Linq语法应用
Linq基础语法 LINQ概述 LINQ(Language Integrated Query,语言集成查询)提供了一种跨数据源和数据格式查询的统一模型. LINQ的组成: LINQ To Object ...