[codeforces]#350E. Correct Bracket Sequence Editor

Posted sysu_zjl

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[codeforces]#350E. Correct Bracket Sequence Editor相关的知识,希望对你有一定的参考价值。

E. Correct Bracket Sequence Editor

          time limit per test2 seconds
        memory limit per test256 megabytes
            inputstandard input
          outputstandard 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 ().
题目大意:给定一组正确的括号序列,加上一个游标p
对序列可以3种操作:
第一种‘L’:游标p向左移动一格
第二种‘R’: 游标p向右移动一格
第三种‘D’:删除当前游标所在的左括号与其对应的右括号(或当前游标所指的右括号与其对应的左括号)之间的括号序列
求出最后剩下的括号序列
解题思路:
看数据量可以大概知道该时间复杂度需要O(n)或O(nlogn) 内
首先用stack实现左括号下标i与右括号下标j相对应
b[j] = i;b[i] = j;
接着用数组leftt[i] 记录与在brakets[i]左边最接近的值
数组rightt[i]记录与在brakets[i]右边最接近的值
每次删除[l….r]区间时 :
closetl = leftt[l];
closetr = right[r];
并记录该序列最左边的有效位置。
if (minl == l)
minl = closetr;
修改right[closetl] = closer;
与left[closetr]= closetl;
若closetr存在 :
游标p = closer;
不存在
游标p = closel;

#include <cstdio>
#include <set>
#include <vector>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
int n, p, m;
int b[500010];
int leftt[500010], rightt[500010];
int ll = 0;
stack<int> q;
string barkets, op;
int main() 
    scanf("%d%d%d", &n, &m, &p);
    p--;
    cin>>barkets;
    cin>>op;
    for (int i = 0; i < n; i++) 
        leftt[i] = i-1;
        rightt[i] = i+1;
        if (barkets[i] == '(')
            q.push(i);
        else 
            b[q.top()] = i;
            b[i] = q.top();
            q.pop();
        
    
    rightt[n-1] = -1;
    for (int i = 0; i < m; i++) 
        if (op[i] == 'L') 
            if (leftt[p] != -1)
                p = leftt[p];
        
        else if (op[i] == 'R') 
            if (rightt[p] != -1)
                p = rightt[p];

        
        else 
            int lf = p;
            int rt = b[p];
            if (lf > rt)
                swap(lf, rt);
            if (rightt[rt] != -1)
                p = rightt[rt];
            else
                p = leftt[lf];
            leftt[rightt[rt]] = leftt[lf];
            rightt[leftt[lf]] = rightt[rt];
            if (lf == ll) //若最左边的有效位置被删除,修改ll
                ll =rightt[rt];
        
    
    string c;
    for (int i = ll; i != -1; i = rightt[i]) 
        c += barkets[i];
    
    cout << c << endl;
    return 0;

以上是关于[codeforces]#350E. Correct Bracket Sequence Editor的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #350 (Div. 2) C. Cinema

Codeforces 350BResort

codeforces 350CBombs

Unknown custom element did you register the component correc

CodeForces 797E 部分dp

Failed to acquire connection "SAP_PRD_NEW.SAPSR3". Connection may not be configured correc