Serval and Parenthesis Sequence CodeForces - 1153C
题目大意:一个字符串只含有? ( ),?可以变成 ) 或者 ( ,将字符串中所有的?变成) 或者 ( 使得字符串合法。
合法就是让括号配对,并且不可以提前结束比如:()()这样是不合法的。
题解:既然不能提前结束,那第一个字符必须和最后一个匹配,所以我们只要关注从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的更多相关文章
- 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 ...
- CF1153C Serval and Parenthesis Sequence
题目地址:CF1153C Serval and Parenthesis Sequence 思路:贪心 如果有解,那么 \(s_0 = (\) && \(s_{n-1} = )\) &a ...
- Serval and Parenthesis Sequence【思维】
Serval and Parenthesis Sequence 题目链接(点击) Serval soon said goodbye to Japari kindergarten, and began ...
- cf-Round551-Div2-C. Serval and Parenthesis Sequence(贪心)
题目链接:http://codeforces.com/contest/1153/problem/C 题意:给定由'(',')','?'组成的字符串,问是否能将其中的?全部换成'(‘,’)'使得字符串的 ...
- cf——C. Serval and Parenthesis Sequence
括号正确匹配问题,应该不难 #include <iostream> #include <cstring> #include <string> #include &l ...
- Increasing Sequence CodeForces - 11A
Increasing Sequence CodeForces - 11A 很简单的贪心.由于不能减少元素,只能增加,过程只能是从左到右一个个看过去,看到一个小于等于左边的数的数就把它加到比左边大,并记 ...
- Almost Regular Bracket Sequence CodeForces - 1095E (线段树,单点更新,区间查询维护括号序列)
Almost Regular Bracket Sequence CodeForces - 1095E You are given a bracket sequence ss consisting of ...
- AC日记——The Child and Sequence codeforces 250D
D - The Child and Sequence 思路: 因为有区间取模操作所以没法用标记下传: 我们发现,当一个数小于要取模的值时就可以放弃: 凭借这个来减少更新线段树的次数: 来,上代码: # ...
- 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 ...
随机推荐
- 曹工说Spring Boot源码(26)-- 学习字节码也太难了,实在不能忍受了,写了个小小的字节码执行引擎
曹工说Spring Boot源码(26)-- 学习字节码也太难了,实在不能忍受了,写了个小小的字节码执行引擎 写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean De ...
- Hive面试准备
Hive与HBase的区别Hive架构原理Hive的数据模型及各模块的应用场景Hive支持的文件格式和压缩格式及各自特点Hive内外表的区分方法及内外部差异Hive视图如何创建.特点及应用场景Hive ...
- TensorFlow RNN 教程和代码
分析: 看 TensorFlow 也有一段时间了,准备按照 GitHub 上的教程,敲出来,顺便整理一下思路. RNN部分 定义参数,包括数据相关,训练相关. 定义模型,损失函数,优化函数. 训练,准 ...
- AI领域:如何做优秀研究并写高水平论文?
来源:深度强化学习实验室 每个人从本科到硕士,再到博士.博士后,甚至工作以后,都会遇到做研究.写论文这个差事.论文通常是对现有工作的一个总结和展示,特别对于博士和做研究的人来说,论文则显得更加重要. ...
- 基于 Spring Cloud 的微服务架构实践指南(上)
show me the code and talk to me,做的出来更要说的明白 GitHub 项目learnSpringCloud同步收录 我是布尔bl,你的支持是我分享的动力! 一. 引入 上 ...
- linux 中的页缓存和文件 IO
本文所述是针对 linux 引入了虚拟内存管理机制以后所涉及的知识点.linux 中页缓存的本质就是对于磁盘中的部分数据在内存中保留一定的副本,使得应用程序能够快速的读取到磁盘中相应的数据,并实现不同 ...
- 读者来信-5 | 如果你家HBase集群Region太多请点进来看看,这个问题你可能会遇到
前言:<读者来信>是HBase老店开设的一个问答专栏,旨在能为更多的小伙伴解决工作中常遇到的HBase相关的问题.老店会尽力帮大家解决这些问题或帮你发出求救贴,老店希望这会是一个互帮互助的 ...
- springboot + aop + Lua分布式限流的最佳实践
整理了一些Java方面的架构.面试资料(微服务.集群.分布式.中间件等),有需要的小伙伴可以关注公众号[程序员内点事],无套路自行领取 一.什么是限流?为什么要限流? 不知道大家有没有做过帝都的地铁, ...
- Educational Codeforces Round 83 (Rated for Div. 2)
A. Two Regular Polygons 题意:给你一个 正n边形,问你能否以这个 n 的其中一些顶点组成一个 m边形, 思路 :如果 n % m == 0 ,就可 收获:边均分 B. Bogo ...
- 【Net】ABP框架学习之它并不那么好用
前言 上一篇文章介绍了ABP的Web API,本文在继续介绍ABP的其他内容. 在ABP中,WEBAPI是一个值得用的东西.但其他东西,就不一定是那么好用了. 下面我们看一下ABP的Controlle ...