E. Correct Bracket Sequence Editor
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 ().
----------------------------------------------------------EDITORIAL--------------------------------------------------------------------
THIS PROBLEM CAN BE EASILY SOLVED IF SOME HAVE SOLVED THIS PROBLEM
http://gautamimp.blogspot.in/2015/11/goodvesselsdiv2-d.html
this problem is a part of the above problem ,
approach
Let's solve this problem in the following way. At first with help of stack let's calculate the array pos, where pos[i] equals to the position of the bracket which paired for the bracket in the position i. Then we need to use two arrays left and right. Then left[i] will equals to the position of the closest to the left bracket which did not delete and right[i] will equals to the position of the closest to the right bracket which did not delete. If there are no such brackets we will store -1 in the appropriate position of the array.
Let's the current position of the cursor equals to p. Then if the current operation equals to \texttt{L} let's make p = left[p] and if the current operation equals to \texttt{R} let's make p = right[p]. We need now only to think how process the operation \texttt{D}.
Let lf equals to p and rg equals to pos[p]. If lf > rg let's swap them. Now we know the ends of the substring which we need to delete now. If right[rg] = = - 1 we need to move p to the left (p = left[lf]), else we need to move p to the right (p = right[rg]). Now we need to recalculate the links for the ends of the deleted substring. Here we need to check is there any brackets which we did not deleted to the left and to the right from the ends of the deleted substring.
To print the answer we need to find the position of the first bracket which we did not delete and go through all brackets which we did not delete (with help of the array right) and print all such brackets. To find the position of the first bracket which we did not delete we can store in the array all pairs of the ends of substrings which we deleted, then sort this array and find the needed position. Bonus: how we can find this position in the linear time?
-----------------------------------------------------CODE-------------------------------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
set<int> se;
int nextt[1000000];
int prevv[1000000];
int matching[1000000];
int used[10000000];
int main()
{
int n,o,ini;
cin>>n>>o>>ini;
for(int i=1;i<=n;i++)
{
nextt[i]=i+1;
prevv[i]=i-1;
}
string br,op;
cin>>br>>op;
stack<int> s;
for(int i=1;i<=n;i++)
{
if(br[i-1]=='(') s.push(i);
else
{
int last=s.top();
s.pop();
matching[last]=i;
matching[i]=last;
}
}
for(int i=0;i<o;i++)
{
char ty=op[i];
if(ty=='D')
{
int l=ini;
int r=matching[ini];
if(l>r) swap(l,r);
for(int i=l;i<=r;)
{
used[i]=1;
nextt[prevv[i]]=nextt[i];
prevv[nextt[i]]=prevv[i];
i=nextt[i];
}
ini=nextt[r];
if(ini==n+1)
{
ini=prevv[r];
}
}
else if(ty=='R')
{
ini=nextt[ini];
if (ini==n+1) ini=prevv[i];
}
else if(ty=='L')
{
ini=prevv[ini];
if(ini==0) ini=nextt[i];
}
}
for(int i=1;i<=n;i++)
if(!used[i])cout<<br[i-1];
}