Codeforces Round #603 (Div. 2) E. Editor
E. Editor
题目链接:
https://codeforces.com/contest/1263/problem/E
题目大意:
输入一个字符串S1含有‘(’ , ‘)’ , ‘R’ , ‘L’ 以及其他字符。根据这个字符串,得到相应的字符串S2。起始idx=1即S2的初始坐标,然后从左到右读取S1,当遇到'L',idx减小1(当无法左移的情况下idx不减小),当遇到'R',idx增加1,当读取到其他字符时,将idx这个位置上的字符更新为读取到的新的字符。然后输出每一步得到的字符串S2最大的括号级数(例:(()())为2,((()))为3),如果不是一个左右括号完全匹配的字符串输出-1。
解题思路:
另(为1,)为-1,其他字符为0,构造一个线段树,维护区间和与前缀最小值、前缀最大值。可以知道,当前缀最小值小于0时代表不是一个括号完全匹配的字符串,当区间和不为0也表示这不是一个括号完全匹配的字符串。
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 4e6;
string s;
int ans[N];
int mi[N],ma[N]; void update(int node,int start,int end,int p,int v){
if(start==end){
ans[node]=v;
ma[node]=v;
mi[node]=v;
return ;
}
int mid=(start+end)>>;
int left_node=*node+;
int right_node=*node+;
if(p<=mid){
update(left_node,start,mid,p,v);
}
else{
update(right_node,mid+,end,p,v);
}
ans[node]=ans[left_node]+ans[right_node];
ma[node]=max(ma[left_node],ans[left_node]+ma[right_node]);// 一个区间前缀最大值是左边区间中的最大值,与右边区间最大值加上左边区间和中的最大值
mi[node]=min(mi[left_node],ans[left_node]+mi[right_node]);// 一个区间前缀最小值是左边区间中的最小值,与右边区间最小值加上左边区间和中的最小值
} int main(){
int n,idx=;
cin>>n>>s;
for(int i=;i<n;i++){
if(s[i]=='L'){
if(idx>) idx--;
}
else if(s[i]=='R'){
idx++;
}
else if(s[i]=='('){
update(,,n,idx,);//更新此位置改为1
}
else if(s[i]==')'){
update(,,n,idx,-);//更新此位置改为-1
}
else{
update(,,n,idx,);//更新此位置改为0
}
if(mi[]>=&&ans[]==){//判断输出
cout<<ma[]<<" ";
}
else{
printf("-1 ");
}
}
cout<<endl; return ;
}
Codeforces Round #603 (Div. 2) E. Editor的更多相关文章
- Codeforces Round #603 (Div. 2) E. Editor 线段树
E. Editor The development of a text editor is a hard problem. You need to implement an extra module ...
- Codeforces Round #603 (Div. 2) E. Editor(线段树)
链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...
- Codeforces Round #603 (Div. 2) E - Editor(线段树,括号序列)
- Codeforces Round #603 (Div. 2) A. Sweet Problem(水.......没做出来)+C题
Codeforces Round #603 (Div. 2) A. Sweet Problem A. Sweet Problem time limit per test 1 second memory ...
- Codeforces Round #603 (Div. 2)
传送门 感觉脑子还是转得太慢了QAQ,一些问题老是想得很慢... A. Sweet Problem 签到. Code /* * Author: heyuhhh * Created Time: 2019 ...
- Codeforces Round #603 (Div. 2) D. Secret Passwords 并查集
D. Secret Passwords One unknown hacker wants to get the admin's password of AtForces testing system, ...
- Codeforces Round #603 (Div. 2) D. Secret Passwords(并查集)
链接: https://codeforces.com/contest/1263/problem/D 题意: One unknown hacker wants to get the admin's pa ...
- Codeforces Round #603 (Div. 2) C. Everyone is a Winner! (数学)
链接: https://codeforces.com/contest/1263/problem/C 题意: On the well-known testing system MathForces, a ...
- Codeforces Round #603 (Div. 2) B. PIN Codes
链接: https://codeforces.com/contest/1263/problem/B 题意: A PIN code is a string that consists of exactl ...
随机推荐
- linux 正则表达式 目录
linux 通配符与正则表达式 linux 通配符 linux 正则表达式 使用grep命令 linux 扩展正则表达式 egrep linux 正则表达式 元字符
- linux 文件属性文件权限
权限 -rw-------. root root Mar : anaconda-ks.cfg drwxr-xr-x root root May : dir1 drwxr-xr-x root root ...
- “AIIA”杯-国家电网-电力专业领域词汇挖掘
十一之前一直在做“电力领域的词典构建”任务,今天也去聆听了前五支队伍的报告,现结合这段时间来的项目经历,写一下自己的若干心得. 电力领域的词典构建——方法1(非监督学习) 在电力领域词典构建心得1.0 ...
- SQL Server之索引解析(一)
SQL Server之索引解析(一) 1.写在前面 微软专门给出SQL Server设计思路及实现路线,从7大体系结构阐述是如何实现,通过了解这些,我们就可以总结出数据库设计原则.编程中sql写法 ...
- JS基础篇--JS获取元素的宽高以及offsetTop,offsetLeft等的属性值
$(obj).width()与$(obj).height() $(obj).width()与$(obj).height() :jquery方式获取元素的宽高,不包括滚动条与工具条 $(obj).wid ...
- 极*Java速成教程 - (7)
Java高级特性 数组 在Java中,数组是一串连续的,不可改变长度的,对象被固定的,类型固定的连续空间.数组中的随机访问非常迅速,但为了速度放弃了灵活性.而效率也是数组最大的优点. 在使用泛型的容器 ...
- Windows下spark1.6.0本地环境搭建
由于spark是用scala编写的,且需要jdk的环境支撑,所以本地spark环境的搭建需要四个步骤:JDK的安装,scala的安装,hadoop的配置,spark的配置. 一.jdk的安装与环境变量 ...
- 洛谷 - P1522 - 牛的旅行 - Cow Tours - Floyd
https://www.luogu.org/problem/P1522 好坑啊,居然还有直径不通过新边的数据,还好不是很多. 注意一定要等Floyd跑完之后再去找连通块的直径,不然一定是INF. #i ...
- php strpos() 函数介绍与使用方法详解
本文主要和大家介绍PHP中mb_strpos的使用技巧,通过使用语法以及实例给大家详细分析了用法,需要的朋友参考学习下.希望能帮助到大家.mb_strpos(PHP 4 >= 4.0.6, PH ...
- RNN, LSTM, GRU cells
项目需要,先简记cell,有时间再写具体改进原因 RNN cell LSTM cell: GRU cell: reference: 1.https://towardsdatascience.com/a ...