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 (思维,模拟栈)
题意:给你一串括号,每次仅可以修改一个位置,问有多少位置仅修改一次后所有括号合法. 题解:我们用栈来将这串括号进行匹配,每成功匹配一对就将它们消去,因为题目要求仅修改一处使得所有括号合法,所以栈中最后 ...
随机推荐
- D:Sequence Swapping
BaoBao has just found a strange sequence {<, >, <, >, , <, >} of length in his poc ...
- poj2914无向图的最小割
http://blog.csdn.net/vsooda/article/details/7397449 //算法理论 http://www.cnblogs.com/ylfdrib/archive/20 ...
- HDU6040 Hints of sd0061
题目链接:https://vjudge.net/problem/HDU-6040 题目大意: 给出 \(n\) 个数,有 \(m\) 次询问,每次询问这 \(n\) 个数中第 \(k+1\) 大的数是 ...
- SSH_ProductCRUD的项目结构与配置文件
项目结构 各类配置文件 <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hib ...
- for循环结构的使用
/* for循环格式: for(①初始化条件; ②循环条件 :③迭代部分){ //④循环体 } 执行顺序:①-②-④-③---②-④-③-----直至循环条件不满足 退出当前循环 * */ publi ...
- 拨号vps,拨号vps是什么意思干什么用的,如何使用拨号vps
首先,拨号vps是动态IP的VPS.vps虚拟服务器.拨号服务器.有些业务,如刷单.投票等操作对ip地址有限制,不能过多的使用.而拨号VPS通过拨号上网,每拨号一次号,就变一次IP,完成ip地址的动态 ...
- Rocket - tilelink - Xbar
https://mp.weixin.qq.com/s/UXFHYEQaYotWNEhshro68Q 简单介绍Xbar的实现. 1. 基本介绍 用于为Xbar的输入和输出连接生成内 ...
- Rocket - tilelink - toBools
https://mp.weixin.qq.com/s/UGMH8EoaVcFkkQW-l4HLWg 分析toBools在Intellij中显示为红色的问题. 1. 问题 在TLA ...
- Rocket - diplomacy - TransferSizes
https://mp.weixin.qq.com/s/Sf0owQxWzxacVvykJZ5oTQ 介绍TransferSizes的实现. 1. 基本定义 从min到max的闭合 ...
- 【Hadoop】配置全分布式模式
分布式原理 配置 详细过程 假设有三台虚拟机,1台master主机namenode,2台slave奴隶机datanode 所有机器都要配好jdk.Java环境变量.hadoop_env.sh里java ...