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 ...
随机推荐
- 爬取LeetCode题目——如何发送GraphQL Query获取数据
前言 GraphQL 是一种用于 API 的查询语言,是由 Facebook 开源的一种用于提供数据查询服务的抽象框架.在服务端 API 开发中,很多时候定义一个接口返回的数据相对固定,因此要获得 ...
- [转帖]K8s集群安装--最新版 Kubernetes 1.14.1
K8s集群安装--最新版 Kubernetes 1.14.1 http://www.cnblogs.com/jieky/p/10679998.html 原作者写的比较简单 大略流程和跳转的多一些 改天 ...
- PostgreSQL索引思考
当在看Monetdb列存行只支持IMPRINTS和ORDERED这两种索引,且只支持定长数值类型时,就在思考,对于列存,还有必要建索引吗?在PostgreSQL的索引就要灵活很多,我对常用列建合理的索 ...
- 剑指Offer编程题(Java实现)——删除链表中重复的结点
题目描述 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针. 例如,链表1->2->3->3->4->4->5 处理后 ...
- java基础知识部分知识点
1.Java常见的注释有哪些,语法是怎样的? 1)单行注释用//表示,编译器看到//会忽略该行//后的所文本 2)多行注释/* */表示,编译器看到/*时会搜索接下来的*/,忽略掉/* */之间的文 ...
- C++中的字符串类
1,本文分析 C++ 中的字符串,C 语言中的字符串利用的是 C 语言中的字符数组, 在 C 语言中没有真正意义上的字符串,利用了字符数组表示了字符串,最初设 计 C 语言仅仅是为了开发 Unix ...
- Linux :环境变量设置和本地变量加载
bash: 全局变量: /etc/profile, /etc/profile.d/*, /etc/bashrc 个人变量: ~/.bash_profile, ~/.bashrc bash运行方 ...
- Cypher 语句实战
Cypher 语句实战 下载和安装 Neo4j windows 桌面版- 环境设置 https://www.w3cschool.cn/neo4j/neo4j_exe_environment_setup ...
- 清理Windows图标缓存 | 懒人屋
原文:清理Windows图标缓存 | 懒人屋 文章背景 这是一个抄袭的文章,原文在参考资料中 运行环境 操作系统:Windows 10 x64(1903) 清理脚本 @echo off rem 关闭W ...
- 算法(C#版)动态规划和贪心算法
https://blog.csdn.net/kouzhuanjing1849/article/details/88954811