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;
}

Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 栈 链表的更多相关文章

  1. 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 ...

  2. Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 模拟

    题目链接: http://codeforces.com/contest/670/problem/E 题解: 用STL的list和stack模拟的,没想到跑的还挺快. 代码: #include<i ...

  3. Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor (链表)

    题目链接:http://codeforces.com/contest/670/problem/E 给你n长度的括号字符,m个操作,光标初始位置是p,'D'操作表示删除当前光标所在的字符对应的括号字符以 ...

  4. CodeForces 670E Correct Bracket Sequence Editor(list和迭代器函数模拟)

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

  5. 【31.93%】【codeforces 670E】Correct Bracket Sequence Editor

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. cf670E Correct Bracket Sequence Editor

    Recently Polycarp started to develop a text editor that works only with correct bracket sequences (a ...

  7. Codeforces Round #350 (Div. 2) F. Restore a Number 模拟构造题

    F. Restore a Number   Vasya decided to pass a very large integer n to Kate. First, he wrote that num ...

  8. Codeforces Round #350 (Div. 2) D2. Magic Powder - 2

    题目链接: http://codeforces.com/contest/670/problem/D2 题解: 二分答案. #include<iostream> #include<cs ...

  9. Codeforces Round #350 (Div. 2)解题报告

    codeforces 670A. Holidays 题目链接: http://codeforces.com/contest/670/problem/A 题意: A. Holidays On the p ...

随机推荐

  1. MySQL分布式集群之MyCAT(二)【转】

    在第一部分,有简单的介绍MyCAT的搭建和配置文件的基本情况,这一篇详细介绍schema的一些具体参数,以及实际作用        首先贴上自己测试用的schema文件,双引号之前的反斜杠不会消除,姑 ...

  2. python 根据输入的内容输出类型

    类型判断 from functools import singledispatch import numbers from collections import abc from collection ...

  3. linux终端操作快捷键

    终端操作快捷键: 新建家目录下终端窗口:Ctrl+Alt+t在当期当前路径下新建终端窗口:Ctrl+Shift+n退出终端窗口:Ctrl+Shift+q 多个终端窗口之间相互切换:Tab+Alt 终端 ...

  4. android studio实现Intent通信-------牛刀小试

    概述: 本博文实现一种小程序,两个Activity单向通信,主从关系,MainActivty 页面布局一个EditText+Button,实现逻辑是单击按钮将信息发送给另外一个DisplayMessa ...

  5. 程序设计分层思想和DAO设计模式的开发

    无论是一个应用程序项目还是一个Web项目,我们都可以按照分层思想进行程序设计.对于分层,当下最流行划分方式是:表现层+控制层+业务层+数据层.其中,业务层和数据层被统称为后台业务层,而表现层和控制层属 ...

  6. SQL SERVER2008 存储过程、表、视图、函数的权限

    EXEC sp_addrolemember N'db_owner', N'db'----将db 设置为 db_owner 角色中的一员 EXEC sp_droprolemember N'db_owne ...

  7. 20165333 学习基础和C语言学习基础

    说实话,我并没有什么技能比90%以上的人更好,非要拿一个出来的话,篮球勉强好一点吧.最初接触篮球是小学的时候跟着哥哥看他打球,哥哥的球技在同龄人中算是好的,每次看他各种突破过人,我都觉得特别潇洒帅气, ...

  8. Hadoop案例(十)WordCount

    WordCount案例 需求1:统计一堆文件中单词出现的个数(WordCount案例) 0)需求:在一堆给定的文本文件中统计输出每一个单词出现的总次数 1)数据准备:Hello.txt hello w ...

  9. Web前端开发最佳实践(7):使用合理的技术方案来构建小图标

    大家都对网站上使用的小图标肯定都不陌生,这些小图标作为网站内容的点缀,增加了网站的美观度,提高了用户体验,可是你有没有看过在这些网站中使用的图标都是用什么技术实现的?虽然大部分网站还是使用普通的图片实 ...

  10. 易普优APS-3C行业解决方案助力国家智能制造示范车间实现高效计划排程

    一.      项目背景 广东劲胜智能集团国家智能制造专项——移动终端金属加工智能制造新模式项目是2015年国家94家智能制1.造专项之一.本项目实施车间为金属CNC加工车间(下称“智能制造示范车间” ...