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 ...
随机推荐
- 普通项目——>maven项目——>SSM(一)
首先应该明白,SSM是什么? SSM指的是Spring+SpringMVC+MyBatis Spring Spring就像是整个项目中装配bean的大工厂,在配置文件中可以指定使用特定的参数去调用实体 ...
- [LeetCode] 211. 添加与搜索单词 - 数据结构设计
题目链接:https://leetcode-cn.com/problems/add-and-search-word-data-structure-design/ 题目描述: 设计一个支持以下两种操作的 ...
- IO同步阻塞与同步非阻塞
BIO.NIO.AIO IO(BIO)和NIO区别:其本质就是阻塞和非阻塞的区别 阻塞概念:应用程序在获取网络数据的时候,如果网络传输数据很慢,就会一直等待,直到传输完毕为止. 非阻塞概念:应用程序直 ...
- 搭建jumperserver堡垒机管理万台服务器-2
搭建jumperserver堡垒机管理万台服务器-2 1 Jumpserver堡垒机概述-部署Jumpserver运行环境 2 安装Coco组件 3 安装Web-Terminal前端-Luna组 ...
- Windows Neovim
配置文件路径C:\Users\UserName\AppData\Local\nvim\init.vim Vim-plug配置文件路径:C:\Users\UserName\AppData\Local ...
- 特殊权限位:suid、sgid、sticky
linux系统特殊权限位 suid.sgid.stickysuid 使任意用户获得用文件属主相同的权限,sgid使用户获得与文件属组相同的权限(通过sgid获得的权限等同于同一用户组的权限) 表示方法 ...
- PAT Advanced 1042 Shuffling Machine (20 分)(知识点:利用sstream进行转换int和string)
Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techn ...
- 前端每日实战:14# 视频演示如何用纯 CSS 创作一种侧立图书的特效
效果预览 按下右侧的"点击预览"按钮在当前页面预览,点击链接全屏预览. https://codepen.io/zhang-ou/pen/deVgRM 可交互视频教程 此视频是可以交 ...
- git push -u origin master和git push <远程主机名> <本地分支名>:<远程分支名>作用
git push git push命令用于将本地分支的更新,推送到远程主机.它的格式与git pull命令相仿. $ git push <远程主机名> <本地分支名>:< ...
- git-bash.exe参数
baidu搜了很多, 没有结果.估计大家都没有这个场景.google了一下, 几篇非常不错的结果: https://superuser.com/questions/1104567/how-can-i- ...