题目大意:一个字符串只含有? ( ),?可以变成 ) 或者 ( ,将字符串中所有的?变成) 或者 ( 使得字符串合法。

合法就是让括号配对,并且不可以提前结束比如:()()这样是不合法的。

题解:既然不能提前结束,那第一个字符必须和最后一个匹配,所以我们只要关注从2~n-1就可以了。

我们可以正着扫一遍,在任何一个位置")"的数目必须小于等于"("+"?"。

然后再倒着扫一遍,同样地,在任何位置的"("的数目必须小于等于")"+"?"

在新的串中,"("=")"=(n-2)/2,然后让前边的问好补""(",剩下的问好补")"。

code:

#include<bits/stdc++.h>
using namespace std;
const int N=3E5+;
char s[N];
void solve(){
int n;
cin>>n;
scanf("%s",s+);
if((n&)||s[]==')'||s[n]=='('){
puts(":(");
return ;
}
int t1=,t2=,t3=;
for(int i=;i<=n-;i++){
if(s[i]=='(') t1++;
else if(s[i]==')') t2++;
else t3++;
if(t2>t1+t3){
puts(":(");
return ;
}
}
t1=,t2=,t3=;
for(int i=n-;i>=;i--){
if(s[i]=='(') t1++;
else if(s[i]==')') t2++;
else t3++;
if(t1>t2+t3){
puts(":(");
return ;
}
}
int c=(n-)/;
if(t1>c||t2>c) {
puts(":(");
return ;
}
int tmp1=;
cout<<"(";
for(int i=;i<=n-;i++){
if(s[i]==')'||s[i]=='(') cout<<s[i];
else {
if(tmp1<c-t1) {
cout<<"(";
tmp1++;
}
else cout<<")";
}
}
cout<<")";
puts("");
}
int main(){
solve();
return ;
}

Serval and Parenthesis Sequence CodeForces - 1153C的更多相关文章

  1. 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 ...

  2. CF1153C Serval and Parenthesis Sequence

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

  3. Serval and Parenthesis Sequence【思维】

    Serval and Parenthesis Sequence 题目链接(点击) Serval soon said goodbye to Japari kindergarten, and began ...

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

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

  5. cf——C. Serval and Parenthesis Sequence

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

  6. Increasing Sequence CodeForces - 11A

    Increasing Sequence CodeForces - 11A 很简单的贪心.由于不能减少元素,只能增加,过程只能是从左到右一个个看过去,看到一个小于等于左边的数的数就把它加到比左边大,并记 ...

  7. Almost Regular Bracket Sequence CodeForces - 1095E (线段树,单点更新,区间查询维护括号序列)

    Almost Regular Bracket Sequence CodeForces - 1095E You are given a bracket sequence ss consisting of ...

  8. AC日记——The Child and Sequence codeforces 250D

    D - The Child and Sequence 思路: 因为有区间取模操作所以没法用标记下传: 我们发现,当一个数小于要取模的值时就可以放弃: 凭借这个来减少更新线段树的次数: 来,上代码: # ...

  9. C. Vasily the Bear and Sequence Codeforces 336C(枚举,思维)

    C. Vasily the Bear and Sequence time limit per test 1 second memory limit per test 256 megabytes inp ...

随机推荐

  1. hdu1072 逃离迷宫系列 bfs

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1072/ 题意:逃离迷宫,路中可能有炸弹,总时间是6个单位,在有炸弹的位置,如果到达的时刻时间大于0,则恢复到6时 ...

  2. Android之注册界面练习

    今天要分享的是一个安卓注册小练习,记录一下自己的学习. 做一个注册页面. 要求填入用户如下信息: 用户名.密码.确认密码.性别(单选).爱好(多选,包括至少六个选项,如音乐.美术.阅读.篮球等).em ...

  3. 3.Scikit-Learn实现完整的机器学习项目

    1       完整的机器学习项目 完成项目的步骤: (1)    项目概述 (2)    获取数据 (3)    发现并可视化数据,发现规律. (4)    为机器学习算法准备数据. (5)    ...

  4. Excel知识点与技巧1

    1.工作区:方便两个工作表之间进行对比 2.工作表标签颜色 3.交换两列的次序 4.快速到达边界:即快速到达第一行或最后一行 5.冻结窗格:可以固定某几行或某几列一直存在于窗口,不会随着往下拉或往右拉 ...

  5. TCP、UDP服务器模型 在网络程序里面,通常都是一

    TCP.UDP服务器模型 在网络程序里面,通常都是一个服务器处理多个客户机,为了出个多个客户机的请求,服务器端的程序有不同的处理方式. 目前最常用的服务器模型: 循环服务器:循环服务器在同一时刻只能响 ...

  6. Redis数据结构——quicklist

    之前的文章我们曾总结到了Redis数据结构--链表和Redis数据结构--压缩列表这两种数据结构,他们是Redis List(列表)对象的底层实现方式.但是考虑到链表的附加空间相对太高,prev 和 ...

  7. java面试基础篇-List

    一.ArrayList: 底层为数组实现,线程不安全,查询,修改快,增加删除慢, 数据结构:数组以0为下标依次连续进行存储 数组查询元素:根据下标查询就行 数组增加元素:如果需要给index为10的位 ...

  8. Codeforces题解集 1.0

    记录 Codeforces 2019年12月19日到 2020年2月12日 的部分比赛题 Educational Codeforces Round 82 (Rated for Div. 2) D Fi ...

  9. CSS3实现3D转换

    实现效果: 当鼠标悬停在图片上面时,图片发生翻转,文案出来,鼠标取消之后就又恢复 实现代码: <!DOCTYPE html> <html lang="en"> ...

  10. lly的瞬移方块(并查集)

    lly的瞬移方块 Description llyllylly最近发明了一个叫瞬移方块的游戏,为啥llyllylly这么闲呢,这得从一只蝙蝠说起..... llyllylly决定给大家也分享一下这个游戏 ...