[Codeforces]1263E Editor
The development of a text editor is a hard problem. You need to implement an extra module for brackets coloring in text.
Your editor consists of a line with infinite length and cursor, which points to the current character. Please note that it points to only one of the characters (and not between a pair of characters). Thus, it points to an index character. The user can move the cursor left or right one position. If the cursor is already at the first (leftmost) position, then it does not move left.
Initially, the cursor is in the first (leftmost) character.
Also, the user can write a letter or brackets (either (, or )) to the position that the cursor is currently pointing at. A new character always overwrites the old value at that position.
Your editor must check, whether the current line is the correct text. Text is correct if the brackets in them form the correct bracket sequence.
Formally, correct text (CT) must satisfy the following rules:
- any line without brackets is CT (the line can contain whitespaces);
- If the first character of the string — is (, the last — is ), and all
the rest form a CT, then the whole line is a CT; - two consecutively written CT is also CT.
Examples of correct texts: hello(codeforces), round, ((i)(write))edi(tor)s, ( me). Examples of incorrect texts: hello)oops(, round), ((me).
The user uses special commands to work with your editor. Each command has its symbol, which must be written to execute this command.
The correspondence of commands and characters is as follows:
- L — move the cursor one character to the left (remains in place if it
already points to the first character); - R — move the cursor one character to the right;
- any lowercase Latin letter or bracket (( or )) — write the entered
character to the position where the cursor is now.
For a complete understanding, take a look at the first example and its illustrations in the note below.
You are given a string containing the characters that the user entered. For the brackets coloring module’s work, after each command you need to:
- check if the current text in the editor is a correct text;
- if it is, print the least number of colors that required, to color
all brackets.
If two pairs of brackets are nested (the first in the second or vice versa), then these pairs of brackets should be painted in different colors. If two pairs of brackets are not nested, then they can be painted in different or the same colors. For example, for the bracket sequence ()(())()() the least number of colors is 222, and for the bracket sequence (()(()())())(()) — is 333.
Write a program that prints the minimal number of colors after processing each command.
输入
The first line contains an integer n(1≤n≤106)n (1≤n≤10^6)n(1≤n≤106) — the number of commands.
The second line contains sss— a sequence of commands. The string s consists of n characters. It is guaranteed that all characters in a string are valid commands.
输出
In a single line print nnn integers, where the iii-th number is:
- −1−1−1 if the line received after processing the first iii commands is
not valid text, - the minimal number of colors in the case of the correct text.
题目大意
给333种指令,左移光标,右移光标,将当前光标的位置的字符设置为ccc。
每次移动后对现在的文本进行检测,如果是合法的括号序列,则输出序列的最大层数,否则输出−1-1−1.
思路
关键在于如何快速地检验合法并且得到最大层数。
检验合法可以用栈,若栈非空或插右括号而无左括号则不合法。
但问题是有左移操作,因此我们可以开一个数组,令(((权值为111,)))权值为−1-1−1,那么总权值和为0则保证了左右括号数量匹配。
但还要求每个位置的左括号数量都要大于等于右括号数量。那么再维护一个区间前缀和最小值,如果这个最小值小于0则肯定不合法。
而输出最大层数呢?其实就是区间前缀和最大值。
如果朴素维护,复杂度O(n)O(n)O(n),那么总复杂度O(n2)O(n^2)O(n2)明显超时。考虑使用数据结构优化。
区间操作,上线段树!使用线段树维护区间前缀和最大最小值,维护区间和,即可解决问题。
复杂度O(nlogn)O(n\log n)O(nlogn)
其中维护区间前缀和最大最小值代码如下:
smax[p] = max(smax[p << 1], sum[p << 1] + smax[p << 1 | 1]);
smin[p] = min(smin[p << 1], sum[p << 1] + smin[p << 1 | 1]);
一个区间前缀和最大最小值,要么取左半边区间的值,要么就取右半边的值再加上左半边整个区间权值和。
其他就是单点修改的板子了。
代码
#include <cstdio>
using namespace std;
int n, pos = 1;
const int maxn = 1000010;
int sum[maxn << 2], smax[maxn << 2], smin[maxn << 2];
inline int max(const int &a, const int &b) { return a > b ? a : b; }
inline int min(const int &a, const int &b) { return a < b ? a : b; }
void push_up(const int &p)
{
sum[p] = sum[p << 1] + sum[p << 1 | 1];
smax[p] = max(smax[p << 1], sum[p << 1] + smax[p << 1 | 1]);
smin[p] = min(smin[p << 1], sum[p << 1] + smin[p << 1 | 1]);
}
int k;
void update(const int &l, const int &r, const int &p)
{
if (l == r)
{
//单点修改
sum[p] = smax[p] = smin[p] = k;
return;
}
int mid = (l + r) >> 1;
if (pos <= mid)
update(l, mid, p << 1);
else
update(mid + 1, r, p << 1 | 1);
push_up(p);
}
int main()
{
scanf("%d", &n);
getchar();
char c;
for (int i = 1; i <= n; ++i)
{
c = getchar();
switch (c)
{
case 'L':
pos = (pos == 1 ? pos : pos - 1);
break;
case 'R':
++pos;
break;
case '(':
k = 1;
update(1, n, 1);
break;
case ')':
k = -1;
update(1, n, 1);
break;
default:
k = 0;
update(1, n, 1); //修改成非括号字母也要修改权值
}
//当前序列合法条件: 权值和为0,最小权值和>=0
if (sum[1] != 0 || smin[1] < 0)
{
printf("-1 ");
continue;
}
printf("%d ", smax[1]);
}
return 0;
}
[Codeforces]1263E Editor的更多相关文章
- CodeForces - 1263E(线段树维护前缀和最值)
题意 https://vjudge.net/problem/CodeForces-1263E 您要设计一个只有一行的打字机,这一行的长度是无限大,一开始可以认为每个字符都是空.您的打字机有一个光标只指 ...
- [Codeforces] #603 (Div. 2) A-E题解
[Codeforces]1263A Sweet Problem [Codeforces]1263B PIN Code [Codeforces]1263C Everyone is a Winner! [ ...
- 模板 - 数据结构 - 栈/Stack
普通的栈大家都会写,STL的栈据说默认实现方式是deque,没关系反正deque跑得飞快. 这里收录的是一些奇怪的栈,当然双栈实现的队列收录在队列里面. 对顶栈 众所周知,栈可以维护一系列前缀和,包括 ...
- CodeForces 670E Correct Bracket Sequence Editor(list和迭代器函数模拟)
E. Correct Bracket Sequence Editor time limit per test 2 seconds memory limit per test 256 megabytes ...
- Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 栈 链表
E. Correct Bracket Sequence Editor 题目连接: http://www.codeforces.com/contest/670/problem/E Description ...
- 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
E. Editor 题目链接: https://codeforces.com/contest/1263/problem/E 题目大意: 输入一个字符串S1含有‘(’ , ‘)’ , ‘R’ , ‘L’ ...
- Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 模拟
题目链接: http://codeforces.com/contest/670/problem/E 题解: 用STL的list和stack模拟的,没想到跑的还挺快. 代码: #include<i ...
随机推荐
- PTA的Python练习题(二)
继续在PTA上练习Python (从 第2章-5 求奇数分之一序列前N项和 开始) 1. x=int(input()) a=i=1 s=0 while(i<=x): s=s+1/a a=a+2 ...
- Django--redis 保存session
pipenv install django-redis settings.py: # 作为 cache backend 使用配置 使用redis保存session CACHES = { "d ...
- idea没有import project解决办法
参考:https://blog.csdn.net/zengxiaosen/article/details/52807540
- 我的学习经历——Linux系统入门教程
我想把最近学习Linux的经验和过程分析出来,当时是在上大三,是学生一枚,以前对开源也没有什么特殊的认识,只觉得很高深,不明觉厉的东西,在当时因为学校要参加职业技能大赛,其中有一团体性质的比赛,几个同 ...
- PCSearch需要管理员权限,开机自启
1.添加Windows服务,并设为自动启动: 2.通过服务启动AutoStartSevice.exe,通过AutoStartSevice.exe运行AutoStart.bat,通过AutoStart. ...
- Linux--常用的linux基本命令学习大全01(适合所有人群)
常用 Linux 命令的基本使用 序号 命令 对应英文 作用 01 ls list 查看当前文件夹下的内容 02 pwd print wrok directory 查看当前所在文件夹 03 cd [目 ...
- 4.使用Redis+Flask维护动态代理池
1.为什么使用代理池 许多⽹网站有专⻔门的反爬⾍虫措施,可能遇到封IP等问题. 互联⽹网上公开了了⼤大量量免费代理理,利利⽤用好资源. 通过定时的检测维护同样可以得到多个可⽤用代理理. 2.代理池的要 ...
- 设计模式课程 设计模式精讲 2-4 UML类图讲解 对比讲解 demo
1 主要内容 1.1 关联和依赖的对比 1.2 组合和聚合的对比 1.3 继承和实现的对比 1.4 各种关系代码实现demo 1 主要内容 1.1 关联和依赖的对比 关联是a类中存在b类对象,企鹅类中 ...
- ASP.NET MVC Web项目中使用Log4Net记录日志,并按照日志类型分文件存储
1.创建MvcLog4Net项目 2.创建 空的MVC项目 3.项目创建完成的效果 4.选择项目,点击鼠标右键,在弹出菜单中选择“管理解决方案的 NuGet 程序包” 5. 在NuGet浏览界面: 点 ...
- 十五 链表与递归,leetCode203题
两种方式: package com.lt.datastructure.LinkedList; /** * leetCode 203题 * /** * Definition for singly-lin ...