CodeForces 670E Correct Bracket Sequence Editor

链表,模拟。

写一个双向链表模拟一下过程。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-6;
void File()
{
    freopen("D:\\in.txt","r",stdin);
    freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
    char c=getchar(); x=0;
    while(!isdigit(c)) c=getchar();
    while(isdigit(c)) {x=x*10+c-‘0‘; c=getchar();}
}

const int maxn=500010;
struct X
{
    bool f;
    int nx,pr;
}s[maxn];

char t[maxn],op[maxn];
int n,m,p;
stack<int>S;
int flag[maxn];
int f1[maxn],f2[maxn];

int main()
{
    scanf("%d%d%d",&n,&m,&p); p--;
    scanf("%s",t); int len=strlen(t);

    for(int i=0;t[i];i++)
    {
        if(S.empty()) S.push(i);
        else
        {
            if(t[i]==‘(‘) S.push(i);
            else
            {
                if(t[S.top()]==‘(‘)
                {
                    f1[S.top()]=i, f2[i]=S.top();
                    S.pop();
                }
                else S.push(i);
            }
        }
    }

    for(int i=0;i<len;i++)
    {
        if(t[i]==‘(‘) s[i].f=0; else s[i].f=1;
        s[i].pr=s[i].nx=-1;
        if(i!=0) s[i].pr=i-1; if(i!=len-1) s[i].nx=i+1;
    }

    scanf("%s",op);
    for(int i=0;op[i];i++)
    {
        if(op[i]==‘L‘) p=s[p].pr;
        else if(op[i]==‘R‘) p=s[p].nx;
        else
        {
            int pos1=p,pos2;
            if(s[p].f==0) pos2=f1[p]; else pos2=f2[p];
            if(pos1>pos2) swap(pos1,pos2);

           // for(int j=pos1;j<=pos2;j++) flag[j]=1;
            flag[pos1]++;
            flag[pos2+1]--;

            if(s[pos1].pr!=-1) s[s[pos1].pr].nx=s[pos2].nx;
            if(s[pos2].nx!=-1) s[s[pos2].nx].pr=s[pos1].pr;

            if(s[pos2].nx!=-1) p=s[pos2].nx;
            else p=s[pos1].pr;

        }
    }

    LL sum=0;

  //  for(int i=0;i<len;i++) printf("%d ",flag[i]);
   // printf("\n");
    for(int i=0;i<len;i++)
    {
        sum=sum+flag[i];
        if(sum>0) continue;
        printf("%c",t[i]);
    }
    printf("\n");
    return 0;
}
时间: 2024-10-11 16:34:11

CodeForces 670E Correct Bracket Sequence Editor的相关文章

Codeforces 670E Correct Bracket Sequence Editor (list模拟)

题意 给出一个合法的小括号序列,然后有三种操作: L 光标左移 R 光标右移 D 删除当前位置括号并他的对应括号,两括号中间的也一起删除. 输出操作后的序列. 思路 list直接模拟就好了,这题主要考察的也就是list的操作吧. list的删除也确实是个坑,注意光标移动出界之后别忘了向左移动一下. 代码 #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm>

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 only with correct bracket sequences (abbreviated as CBS). Note that a bracket sequence is correct if it is possible to get a correct mathematical express

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 only with correct bracket sequences (abbreviated as CBS). Note that a bracket sequence is correct if it is possible to get a correct mathematical express

cf670E 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 "

Gym 240084E - Correct Bracket Sequence Editor - [线段树]

题目链接:https://codeforces.com/gym/240084/problem/E 题意: 给出一个已经匹配的括号串,给出起始的光标位置(光标总是指向某个括号). 有如下操作: 1.往左移动一下光标: 2.往左移动一下光标: 3.删除当前光标指向的括号,以及和它匹配的那个括号,以及这两个括号之间的所有括号. 要求你给出在做完所有操作后的括号串. 题解: 用线段树维护,每个括号是否存在,存在记为 $1$,被删掉了记为 $0$. 然后我们只需要实现:①区间求和.②区间赋值.③根据 $k

Codeforces 1132A. Regular Bracket Sequence

原题链接:Codeforces 1132A. Regular Bracket Sequence 题目大意:你有\({cnt}_1,{cnt}_2,{cnt}_3,{cnt}_4\)个"((","()",")(","))",问能否将这些字符串组成一个合法的括号序列. 题解:这一道题,很明显的\({cnt}_2\)是不需要管的,对于第三种情况,它并不改变左右括号的数量差,只有第一.四情况改变,那么,很明显\({cnt}_1={cn

CodeForces 670E(模拟双向链表)

题意:给你一串合法的括号和当前光标的位置和一些操作,问操作完之后的串是怎么样的 思路:模拟一个双向链表的操作,首先先预处理出配对的括号组,然后模拟即可 #include<bits\stdc++.h> using namespace std; const int maxn = 1e6; struct Node { int l,r; }nodes[maxn]; char s1[maxn],s2[maxn]; int a[maxn],d[maxn]; int main() { int n,m,pos

Codeforces Beta Round #5 C. Longest Regular Bracket Sequence 栈/dp

C. Longest Regular Bracket Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/5/C Description This is yet another problem dealing with regular bracket sequences. We should remind you that a bracket sequence

贪心+stack Codeforces Beta Round #5 C. Longest Regular Bracket Sequence

题目传送门 1 /* 2 题意:求最长括号匹配的长度和它的个数 3 贪心+stack:用栈存放最近的左括号的位置,若是有右括号匹配,则记录它们的长度,更新最大值,可以在O (n)解决 4 详细解释:http://blog.csdn.net/taoxin52/article/details/26012167 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cstring> 9 #include <