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

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

题解:既然不能提前结束,那第一个字符必须和最后一个匹配,所以我们只要关注从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. ImportError: libcusolver.so.8.0: cannot open shared object file: No such file or directory

    问题描述: ImportError: libcusolver.so.8.0: cannot open shared object file: No such file or directory 首先检 ...

  2. dvwa学习之七:SQL Injection

    1.Low级别 核心代码: <?php if( isset( $_REQUEST[ 'Submit' ] ) ) { // Get input $id = $_REQUEST[ 'id' ]; ...

  3. Java构造器(构造方法/constructor)

    我们先来看一下什么是构造器: 1.构造器也叫构造方法或构造函数,分为有参构造器和无参构造器: 2.构造器也是一种方法,只不过是一种特殊的方法,它会在对象创建的时候被调用: 3.构造器最大的作用就是在创 ...

  4. [noip模拟]B<构造>

    [题目描述] 在两个n*m的网格上染色,每个网格中被染色的格子必须是一个四联通块(没有任何格子被染色也可以),四联通块是指所有染了色的格子可以通过网格的边联通,现在给出哪些格子在两个网格上都被染色了, ...

  5. 实战级Stand-Alone Self-Attention in CV,快加入到你的trick包吧 | NeurIPS 2019

    论文提出stand-alone self-attention layer,并且构建了full attention model,验证了content-based的相互关系能够作为视觉模型特征提取的主要基 ...

  6. .net core系统跨平台部署手册

    前言 .net core跨平台版本基于.net core 3.1 SDK开发,剥离原来的基于MS Office进行文档转换功能的模块,使用基于开源跨平台的LibreOffice进行文档转换的模块.以此 ...

  7. 关于C#三层架构增删改查中的“添加”问题

    关于“添加”功能的实现比较简单: 先来一个简单的界面: 然后是代码: ··采用的是三层架构的思想写的·· 在DAO中的方法为: (使用了动软自动生成代码) 希望对您有所帮助!

  8. mysql清空表后id重1开始

    通过"truncate table 表名"方式重置清空id,让id从1开始自动递增,

  9. Git Bash使用时有个end,无法进行其他操作

    如图: 遇到这种情况,按下q即可回到$命令界面.

  10. Tkinter布局管理器

    Layout management in Tkinter 原英文教程地址:zetcode.com In this part of the Tkinter tutorial, we introduce ...