CodeForces 670E Correct Bracket Sequence Editor(list和迭代器函数模拟)
2 seconds
256 megabytes
standard input
standard output
Recently Polycarp started to develop a text editor that works only with correct bracket sequences (abbreviated as CBS).
Note that a bracket sequence is correct if it is possible to get a correct mathematical expression by adding "+"-s and "1"-s to it. For example, sequences "(())()", "()" and "(()(()))" are correct, while ")(", "(()" and "(()))(" are not. Each bracket in CBS has a pair. For example, in "(()(()))":
- 1st bracket is paired with 8th,
- 2d bracket is paired with 3d,
- 3d bracket is paired with 2d,
- 4th bracket is paired with 7th,
- 5th bracket is paired with 6th,
- 6th bracket is paired with 5th,
- 7th bracket is paired with 4th,
- 8th bracket is paired with 1st.
Polycarp's editor currently supports only three operations during the use of CBS. The cursor in the editor takes the whole position of one of the brackets (not the position between the brackets!). There are three operations being supported:
- «L» — move the cursor one position to the left,
- «R» — move the cursor one position to the right,
- «D» — delete the bracket in which the cursor is located, delete the bracket it's paired to and all brackets between them (that is, delete a substring between the bracket in which the cursor is located and the one it's paired to).
After the operation "D" the cursor moves to the nearest bracket to the right (of course, among the non-deleted). If there is no such bracket (that is, the suffix of the CBS was deleted), then the cursor moves to the nearest bracket to the left (of course, among the non-deleted).
There are pictures illustrated several usages of operation "D" below.
All incorrect operations (shift cursor over the end of CBS, delete the whole CBS, etc.) are not supported by Polycarp's editor.
Polycarp is very proud of his development, can you implement the functionality of his editor?
The first line contains three positive integers n, m and p (2 ≤ n ≤ 500 000, 1 ≤ m ≤ 500 000, 1 ≤ p ≤ n) — the number of brackets in the correct bracket sequence, the number of operations and the initial position of cursor. Positions in the sequence are numbered from left to right, starting from one. It is guaranteed that n is even.
It is followed by the string of n characters "(" and ")" forming the correct bracket sequence.
Then follow a string of m characters "L", "R" and "D" — a sequence of the operations. Operations are carried out one by one from the first to the last. It is guaranteed that the given operations never move the cursor outside the bracket sequence, as well as the fact that after all operations a bracket sequence will be non-empty.
Print the correct bracket sequence, obtained as a result of applying all operations to the initial sequence.
8 4 5
(())()()
RDLD
()
12 5 3
((()())(()))
RRDLD
(()(()))
8 8 8
(())()()
LLLLLLDD
()()
In the first sample the cursor is initially at position 5. Consider actions of the editor:
- command "R" — the cursor moves to the position 6 on the right;
- command "D" — the deletion of brackets from the position 5 to the position 6. After that CBS takes the form (())(), the cursor is at the position 5;
- command "L" — the cursor moves to the position 4 on the left;
- command "D" — the deletion of brackets from the position 1 to the position 4. After that CBS takes the form (), the cursor is at the position 1.
Thus, the answer is equal to ().
题目链接:CodeForces 670E
我用了一点小技巧就是advance这个函数,可以让迭代器前进或者后退,以前只是了解过这个函数没想到现在能用到,另外还在list的结尾和开头都加了0防止越界的情况也方便反向遍历(否则要用返向迭代器想想都麻烦)。然后就可以进行模拟操作了,注意一下list的erase到底如何进行擦除,一般的for里it++会出问题的。
然后是括号如何判断,当然你首先要设一个当前位置迭代器list<char>::iterator now,然后看*now是'('还是')',若是前者,那肯定要向后遍历寻找,后者则相反,为什么?画图就明白了,不可能会反着来的(以前没意识到这点就以为这题十分麻烦)然后就是如何判断刚好是截取区间呢,对包括开始位置的括号种类cnt进行统计,若相等则可以break此时当前的迭代器位置就是闭区间[l,r],然后r++就可以使得区间变成[l,r),就可以进行删除操作了。
代码:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<list>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define MM(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=500010;
char s[N],ops[N];
int cnt[2];
int main(void)
{
list<char>pos;
int n,m,p,i,j,l,r;
while (cin>>n>>m>>p)
{
pos.clear();
cnt[0]=cnt[1]=0;
cin>>s>>ops; pos.push_back('0');//开头放一个0
list<char>::iterator it,st,en,l,r,now;
for (i=0; i<n; ++i)
pos.push_back(s[i]);
pos.push_back('0');//结尾放一个0
now=pos.begin();
advance(now,p);//使用advance函数让其到初始位置p
for (i=0; i<m; ++i)
{
if(ops[i]=='L')
advance(now,-1);
else if(ops[i]=='R')
advance(now,1);
else
{
MM(cnt,0);
if(*now=='(')
{
l=r=now;
for (it=now; it!=pos.end(); ++it,++r)
{
if(*it=='(')
++cnt[0];
else if(*it==')')
++cnt[1];
if(cnt[0]==cnt[1])
break;
}
advance(r,1);
for (it=l; it!=r; )
pos.erase(it++);//erase的正确使用姿势
now=it;
if(*it=='0')//判断是否越界
advance(now,-1);
}
else
{
l=r=now;
++r;
for (it=now; it!=pos.begin(); --it,--l)
{
if(*it=='(')
++cnt[0];
else if(*it==')')
++cnt[1];
if(cnt[0]==cnt[1])
break;
}
for (it=l; it!=r; )
pos.erase(it++);
now=it;
if(*it=='0')
advance(now,-1);
}
}
}
st=pos.begin();
advance(st,1);
en=pos.end();
advance(en,-1);
for (it=st; it!=en; ++it)
cout<<*it;
cout<<endl;
}
return 0;
}
CodeForces 670E Correct Bracket Sequence Editor(list和迭代器函数模拟)的更多相关文章
- Codeforces 670E - Correct Bracket Sequence Editor - [线段树]
题目链接:https://codeforces.com/contest/670/problem/E 题意: 给出一个已经匹配的括号串,给出起始的光标位置(光标总是指向某个括号). 有如下操作: 1.往 ...
- Codeforces 670E - Correct Bracket Sequence Editor - [链表]
题目链接:https://codeforces.com/contest/670/problem/E 题意: 给出一个已经匹配的括号串,给出起始的光标位置(光标总是指向某个括号). 有如下操作: 1.往 ...
- Codeforces 670E - Correct Bracket Sequence Editor - [对顶栈]
题目链接:https://codeforces.com/contest/670/problem/E 题意: 给出一个已经匹配的括号串,给出起始的光标位置(光标总是指向某个括号). 有如下操作: 1.往 ...
- CodeForces 670E Correct Bracket Sequence Editor
链表,模拟. 写一个双向链表模拟一下过程. #pragma comment(linker, "/STACK:1024000000,1024000000") #include< ...
- 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 #350 (Div. 2) E. Correct Bracket Sequence Editor 线段树模拟
E. Correct Bracket Sequence Editor Recently Polycarp started to develop a text editor that works o ...
- 【31.93%】【codeforces 670E】Correct Bracket Sequence Editor
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- cf670E Correct Bracket Sequence Editor
Recently Polycarp started to develop a text editor that works only with correct bracket sequences (a ...
- Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 模拟
题目链接: http://codeforces.com/contest/670/problem/E 题解: 用STL的list和stack模拟的,没想到跑的还挺快. 代码: #include<i ...
随机推荐
- IE的浏览器模式和文档模式
只有IE浏览器中才会有“浏览器模式”和“文档模式”,兼容性视图涉及两个重要的功能 便是“浏览器模式[browser mode]”和“文档模式[document mode]”,在IE8/IE9中按F12 ...
- [Android Pro] proguard.cfg 配置文件
转载自:http://my.oschina.net/zhangzhihao/blog/72393 # ------------------------------------- # android 原 ...
- Stanford大学机器学习公开课(六):朴素贝叶斯多项式模型、神经网络、SVM初步
(一)朴素贝叶斯多项式事件模型 在上篇笔记中,那个最基本的NB模型被称为多元伯努利事件模型(Multivariate Bernoulli Event Model,以下简称 NB-MBEM).该模型有多 ...
- shell定时任务
1.认识Croncron是一个linux下的定时执行工具,可以在无需人工干预的情况下运行作业.由于Cron 是Linux的内置服务,但它不自动起来,可以用以下的方法启动.关闭这个服务:/sbin/se ...
- ***mysql中经度纬度字段用什么存储(关于mysql的float和decimal区别)
float,decimal精确度比较 float,double容易产生误差,对精确度要求比较高时,建议使用decimal来存,decimal在mysql内存是以字符串存储的, 用于定义货币要求精确 ...
- linux下验证码无法显示:Could not initialize class sun.awt.X1 解决方案
转自:http://my.oschina.net/xiangtao/blog/28441 网站验证码突然无法显示,并报如下错误. Caused by: java.lang.NoClassDefFoun ...
- js实现iframe自适应高度
转自:http://www.jb51.net/article/15780.htm 对于自适应高度的代码有很多,可效率什么的考虑进来好代码就不多见了,不过思路倒是差不多的! 不带边框的iframe因为能 ...
- java程序员必须会的技能
1.语法:必须比较熟悉,在写代码的时候IDE的编辑器对某一行报错应该能够根据报错信息知道是什么样的语法错误并且知道任何修正. 2.命令:必须熟悉JDK带的一些常用命令及其常用选项,命令至少需要熟悉:a ...
- P/Invoke .NET调用win32API
项目:无线无源测温软件系统 项目中,用到使用P/Invoke在.NET调用win32API,实现对ini配置文件的读写功能!因为有一些配置信息需要保存下来,以便在下一次启动程序完成初始化,这实际上是一 ...
- php数据库操作封装类
<?php /** * Desc: php操作mysql的封装类 * Author zhifeng * Date: 2015/04/15 * 连接模式:PDO */ class MMysql { ...