E. Correct Bracket Sequence Editor

题目连接:

http://www.codeforces.com/contest/670/problem/E

Description

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?

Input

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.

Output

Print the correct bracket sequence, obtained as a result of applying all operations to the initial sequence.

Sample Input

8 4 5
(())()()
RDLD

Sample Output

()

题意

你有长度为n的括号序列,有m个操作,光标现在在pos位置

操作有3个

d,l,r

删除,左移动,右移动。

删除就是,你每次可以删除光标所在的位置以及他所对应的括号,和这括号内包括的东西

然后这个光标就跑到这个地方的右边去了

问你操作完之后是什么样子了?

题解:

对应的括号,这个用栈去找就好了

然后操作,我们就模拟一下链表就好了

然后就完了……

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 5e5+7;
struct node{int l,r;}p[maxn];
int n,m,pos,a[maxn],d[maxn],len,now=0;
char s1[maxn],s2[maxn];
int main()
{scanf("%d%d%d",&n,&m,&pos);scanf("%s",s1+1);len=strlen(s1+1);for(int i=0;i<=n+1;i++)p[i].l=i-1,p[i].r=i+1;for(int i=1;i<=len;i++){if(s1[i]=='(')a[now++]=i;else d[i]=a[--now],d[a[now]]=i;}scanf("%s",s2+1);for(int i=1;i<=m;i++){if(s2[i]=='R')pos=p[pos].r;else if(s2[i]=='L')pos=p[pos].l;else{int l=min(d[pos],pos);int r=max(d[pos],pos);p[p[l].l].r=p[r].r;p[p[r].r].l=p[l].l;pos=p[r].r;if(pos==n+1)pos=p[n+1].l;if(pos==0)pos=p[0].r;}}int pos=p[0].r;while(pos!=n+1){cout<<s1[pos];pos=p[pos].r;}cout<<endl;return 0;
}

转载于:https://www.cnblogs.com/qscqesze/p/5465954.html

Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 栈 链表相关推荐

  1. Correct Bracket Sequence Editor

    E. Correct Bracket Sequence Editor time limit per test           2 seconds memory limit per test    ...

  2. Codeforces Round #709 (Div. 1) C. Skyline Photo dp + 单调栈优化

    传送门 文章目录 题意: 思路: 题意: 思路: 首先一个非常明显的dpdpdp式子就是f[i]=max(f[j]+val(j+1,i))f[i]=max(f[j]+val(j+1,i))f[i]=m ...

  3. Codeforces Round #350 (Div. 2) B. Game of Robots 水题

    B. Game of Robots 题目连接: http://www.codeforces.com/contest/670/problem/B Description In late autumn e ...

  4. Codeforces Round #693 (Div. 3) E. Correct Placement 思维

    传送门 题意: 思路: 对于每个人都有个二元组(x,y)(x,y)(x,y),从题意中提取有效信息就是:当(x1,y1)(x_1,y_1)(x1​,y1​)的最大值大于(x2,y2)(x_2,y_2) ...

  5. Codeforces Round #FF (Div. 2)C.DYZ Loves Sequence

    一道类似求严格递增子序列的题目.这道题可以改变一个数成任意数,使得严格递增子序列增加. *思路:两个标记数组from[i],to[i];from[i]的含义是从i出发的最长的严格递增序列长度,to[i ...

  6. Codeforces Round #604 (Div.2)

    Codeforces Round #604 (Div.2) 2019/12/5 22:35--2019/12/6 00:35 Codeforces Round #604 (Div.2) A. Beau ...

  7. Codeforces Round #693 (Div. 3)A~G解题报告

    Codeforces Round #693 (Div. 3)A~G解题报告 A Cards for Friends 原题信息 http://codeforces.com/contest/1472/pr ...

  8. Codeforces Round #693 (Div. 3)部分题解

    Codeforces Round #693 (Div. 3) 部分题解 D. Even-Odd Game 思路: 贪心:田忌赛马 (1)先将数组从大到小排序,取数时从大到小取,用一个ans变量记录取数 ...

  9. Codeforces Round #597 (Div. 2) - BenFromHRBUST

    Codeforces Round #597 (Div. 2) -----比赛传送门----- A - Good ol' Numbers Coloring Problem Description Con ...

最新文章

  1. 使用AutoCAD 2021创建真实世界的土木设计项目
  2. 自定义SeekBarPreference控件(老外出品,直接在preferences文件中使用,无需其他代码)...
  3. 协作与协同有什么区别_OA软件, BPM系统, 移动办公系统, 协同办公系统到底有什么区别...
  4. jQuery常用的层次选择器
  5. Visual Studio 2017 通过SSH 调试Linux 上.NET Core
  6. 今天在群里面讨论了驱动机制的学习
  7. 固定尺寸内存块的缓冲队列类及C++实现源代码
  8. js document 触发按键事件
  9. 短视频解析易语言代码
  10. MYSQL initialize、install 、启动服务报错解决方法
  11. IE 浏览器旧版本下载
  12. C语言扑克牌洗牌发牌代码
  13. asp.net配置文件connectionStrings加密和解密
  14. 多自由度机械臂运动学正-逆解|空间轨迹规划控制|MATLAB仿真+实际机器调试
  15. 【20210823】学习本不应该痛苦人生其实是一种享受
  16. 一种将pkl转成excel的弯道[可以实现有点奇怪,以防excel显示不全]
  17. 安装java8和tomcat_jdk1.8.0+tomcat8.0安装
  18. 戒指眼镜蹦迪灯,亚马逊的智能硬件帝国上线了
  19. NAS映射网络驱动器如何操作?
  20. 关于前端进行ios配置微信config出现验签失败的问题解决

热门文章

  1. 全部python编程语言-编程语言高质量代码的优秀Python工具
  2. python贴吧爬虫-Python 爬虫练习: 爬取百度贴吧中的图片
  3. python基础单词-学Python必背的初级单词,快来看看学吧
  4. python装饰器函数-Python精进-装饰器与函数对象
  5. python有证书考吗-学python需要考证吗?
  6. python基本语法-Python语法基础50题
  7. python爬虫案例-Python3爬虫三大案例实战分享
  8. 以下哪一个不属于python语言的特点-Python语言的特点有哪些
  9. 脚本编程语言python语言-python算的上脚本语言吗
  10. 怎么自学python自动化测试-Python实现性能自动化测试竟然如此简单