Codeforces 670E - Correct Bracket Sequence Editor 链表
2016-05-09 12:17
519 查看
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 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.
Examples
Input
Output
Input
Output
Input
Output
Note
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 ().
题意:
«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).
分析:
显然这个是用链表来模拟操作的。
next[i]表示i的有边的位置,pre[i]表示i的左边的位置,模拟一下左移,右移,和删除操作即可
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 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.
Examples
Input
8 4 5 (())()() RDLD
Output
()
Input
12 5 3 ((()())(())) RRDLD
Output
(()(()))
Input
8 8 8
(())()()LLLLLLDD
Output
()()
Note
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 ().
题意:
«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).
分析:
显然这个是用链表来模拟操作的。
next[i]表示i的有边的位置,pre[i]表示i的左边的位置,模拟一下左移,右移,和删除操作即可
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=5e5+9;
int n,m,k;
int next
,pre
;
char s
,p
;
int main(){
//freopen("f.txt","r",stdin);
scanf("%d%d%d",&n,&m,&k);
scanf("%s%s",s+1,p);
for(int i=1;i<=n;i++){
next[i]=i+1;
pre[i]=i-1;
}
next[0]=1;pre[n+1]=n;
for(int i=0;i<m;i++){
if(p[i]=='L')k=pre[k];
else if(p[i]=='R')k=next[k];
else{
if(s[k]==')'){
int t=1;
int x=next[k];
while(t>0){
k=pre[k];
if(s[k]=='(')t--;
else t++;
}
k=pre[k];
next[k]=x;pre[x]=k;
k=x;
if(k==n+1)k=pre[k];
}
else{
int t=1;
int x=pre[k];
while(t>0){
k=next[k];
if(s[k]=='(')t++;
else t--;
}
k=next[k];
next[x]=k;pre[k]=x;
if(k==n+1)k=pre[k];
}
}
}
for(int i=next[0];i<=n;i=next[i])printf("%c",s[i]);
return 0;
}
相关文章推荐
- 64-Unique Binary Search Trees
- Arcengine 开发,FeatureClass新增feature时“The Geometry has no z-value”或"The Geometry has null z-value"的解决方案
- MUI-页面初始化
- Ultra-QuickSort--POJ2299(归并排序求逆序数对)
- UIButton setBackgroundImage与setImage区别
- UITabBarController使用
- android 你所不知道的类SpannableStringBuilder的总结--实现图文混排,查看更多,下划线等等
- Moqui之时间转换
- words in view Moqui resource code
- 区分Integer.getInteger和Integer.valueOf、Integer.parseInt() 的使用方法
- TestSuite很多skipped,报错Exception org.openqa.selenium.SessionNotCreatedException
- 专访|腾讯、阿里、adidas的服务方UID WORKS
- sdutoj 3252 Lowest Unique Price
- Android UI 优化 使用<include/>和 <merge />标签
- QtGuid4.dll处有未经处理的异常,0xsdddddd5写入位置错误
- om.android.builder.model.SourceProvider.getShadersDirectories() not found
- Moqui学习之代码分析mantle priceServices.xml
- TabBar高度修改以及UITabBarItem坐标移动
- 界面设计,事件处理--GUI编程的核心--
- Implement Stack using Queues