E. Correct Bracket Sequence Editor
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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?

Input

The first line contains three positive integers nm 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.

Examples
input
  1. 8 4 5
  2. (())()()
  3. RDLD
output
  1. ()
input
  1. 12 5 3
  2. ((()())(()))
  3. RRDLD
output
  1. (()(()))
input
  1. 8 8 8
  2. (())()()
  3. LLLLLLDD
output
  1. ()()
Note

In the first sample the cursor is initially at position 5. Consider actions of the editor:

  1. command "R" — the cursor moves to the position 6 on the right;
  2. 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;
  3. command "L" — the cursor moves to the position 4 on the left;
  4. 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),就可以进行删除操作了。

代码:

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cstdlib>
  4. #include<sstream>
  5. #include<cstring>
  6. #include<cstdio>
  7. #include<string>
  8. #include<deque>
  9. #include<stack>
  10. #include<cmath>
  11. #include<queue>
  12. #include<list>
  13. #include<set>
  14. #include<map>
  15. using namespace std;
  16. #define INF 0x3f3f3f3f
  17. #define MM(x,y) memset(x,y,sizeof(x))
  18. #define LC(x) (x<<1)
  19. #define RC(x) ((x<<1)+1)
  20. #define MID(x,y) ((x+y)>>1)
  21. typedef pair<int,int> pii;
  22. typedef long long LL;
  23. const double PI=acos(-1.0);
  24. const int N=500010;
  25. char s[N],ops[N];
  26. int cnt[2];
  27. int main(void)
  28. {
  29. list<char>pos;
  30. int n,m,p,i,j,l,r;
  31. while (cin>>n>>m>>p)
  32. {
  33. pos.clear();
  34. cnt[0]=cnt[1]=0;
  35. cin>>s>>ops;
  36.  
  37. pos.push_back('0');//开头放一个0
  38. list<char>::iterator it,st,en,l,r,now;
  39. for (i=0; i<n; ++i)
  40. pos.push_back(s[i]);
  41. pos.push_back('0');//结尾放一个0
  42. now=pos.begin();
  43. advance(now,p);//使用advance函数让其到初始位置p
  44. for (i=0; i<m; ++i)
  45. {
  46. if(ops[i]=='L')
  47. advance(now,-1);
  48. else if(ops[i]=='R')
  49. advance(now,1);
  50. else
  51. {
  52. MM(cnt,0);
  53. if(*now=='(')
  54. {
  55. l=r=now;
  56. for (it=now; it!=pos.end(); ++it,++r)
  57. {
  58. if(*it=='(')
  59. ++cnt[0];
  60. else if(*it==')')
  61. ++cnt[1];
  62. if(cnt[0]==cnt[1])
  63. break;
  64. }
  65. advance(r,1);
  66. for (it=l; it!=r; )
  67. pos.erase(it++);//erase的正确使用姿势
  68. now=it;
  69. if(*it=='0')//判断是否越界
  70. advance(now,-1);
  71. }
  72. else
  73. {
  74. l=r=now;
  75. ++r;
  76. for (it=now; it!=pos.begin(); --it,--l)
  77. {
  78. if(*it=='(')
  79. ++cnt[0];
  80. else if(*it==')')
  81. ++cnt[1];
  82. if(cnt[0]==cnt[1])
  83. break;
  84. }
  85. for (it=l; it!=r; )
  86. pos.erase(it++);
  87. now=it;
  88. if(*it=='0')
  89. advance(now,-1);
  90. }
  91. }
  92. }
  93. st=pos.begin();
  94. advance(st,1);
  95. en=pos.end();
  96. advance(en,-1);
  97. for (it=st; it!=en; ++it)
  98. cout<<*it;
  99. cout<<endl;
  100. }
  101. return 0;
  102. }

CodeForces 670E Correct Bracket Sequence Editor(list和迭代器函数模拟)的更多相关文章

  1. Codeforces 670E - Correct Bracket Sequence Editor - [线段树]

    题目链接:https://codeforces.com/contest/670/problem/E 题意: 给出一个已经匹配的括号串,给出起始的光标位置(光标总是指向某个括号). 有如下操作: 1.往 ...

  2. Codeforces 670E - Correct Bracket Sequence Editor - [链表]

    题目链接:https://codeforces.com/contest/670/problem/E 题意: 给出一个已经匹配的括号串,给出起始的光标位置(光标总是指向某个括号). 有如下操作: 1.往 ...

  3. Codeforces 670E - Correct Bracket Sequence Editor - [对顶栈]

    题目链接:https://codeforces.com/contest/670/problem/E 题意: 给出一个已经匹配的括号串,给出起始的光标位置(光标总是指向某个括号). 有如下操作: 1.往 ...

  4. CodeForces 670E Correct Bracket Sequence Editor

    链表,模拟. 写一个双向链表模拟一下过程. #pragma comment(linker, "/STACK:1024000000,1024000000") #include< ...

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

    E. Correct Bracket Sequence Editor 题目连接: http://www.codeforces.com/contest/670/problem/E Description ...

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

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

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

  8. cf670E Correct Bracket Sequence Editor

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

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

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

随机推荐

  1. 给mysql数据库插入当前时间

    mysql相关的三个函数有: NOW()函数以`'YYYY-MM-DD HH:MM:SS'返回当前的日期时间,可以直接存到DATETIME字段中.CURDATE()以’YYYY-MM-DD’的格式返回 ...

  2. HDU 5651 xiaoxin juju needs help (组合数)

    xiaoxin juju needs helpTime Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64uSu ...

  3. AJAX,JSON搜索智能提示

    效果 开发结构参考AJAX,JSON用户校验 主要有两个核心文件 1,处理输入字符,进行后台搜索的servlet Suggest.java package org.guangsoft.servlet; ...

  4. Windows环境下配置Vim为Python的IDE

    (一)安装Python 2.7 在官网下载Python,并安装,我的安装路径是D:\Program Files\Python.安装完成后编辑环境变量Path,在其后添加;D:\Program File ...

  5. grep -C n "匹配字符串" 匹配字符串上下N行

    [root@xxxxx ~]# grep -C 'ip_whitelist' /etc/gitlab/gitlab.rb # 'PATH' => "/opt/gitlab/bin:/o ...

  6. 使用Java内存映射(Memory-Mapped Files)处理大文件

    >>NIO中的内存映射 (1)什么是内存映射文件内存映射文件,是由一个文件到一块内存的映射,可以理解为将一个文件映射到进程地址,然后可以通过操作内存来访问文件数据.说白了就是使用虚拟内存将 ...

  7. Android中获取蓝牙log

    1.蓝牙的snoop log存放位置 /etc/bluetooth/bt_stack.conf   2.修改方法 #关闭蓝牙 修改bt_stack.conf文件中打印log的等级 adb root a ...

  8. adbWireless 简单教程

    adbWireless:无线使用ADB(siir.es.adbWireless) 下载: http://www.coolapk.com/apk/siir.es.adbWireless >> ...

  9. Windows Phone8 遇见的问题

    1.公司的无线路由可以自动分发ip地址,模拟器可以自动获取ip,进行连接.宿舍的无线路设置了DHCP 不能自动分发ip地址,模拟器连接不到ip,上不去网,我就去hyper-v 管理器里修改了静态mac ...

  10. laravel框架中widget模糊查询

    .配置模糊查询的路由 Route::);     .在widgets.php下注册widget <?php Widget::register() {      $model = Str::stu ...