Codeforces Round #350 (Div. 2) E:Correct Bracket Sequence Editor (简单链表模拟)

*题目描述:
你需要写一个括号序列编辑器。你有一个长度为n的括号序列,以及m个操作,还有初始时光标的位置。每次的操作有:
1.将光标左移一位
2.将光标右移一位
3.删除光标所在的括号以及和它匹配的括号中间的部分。
*题解:

先用一个栈来匹配左右括号序列,然后再用链表维护整个括号序列。每次删除操作就在链表上进行区间删除。(我真的是数据结构写傻了,第一反应居然是Treap,后来马上发现用链表维护就可以了,最后因为边界写搓了没有在规定的时间内ac,后来调过了总算是a了)

#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define L(i) i<<1
#define R(i) i<<1|1
#define INF  0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-9
#define maxn 1000010
#define MOD 1000000007

struct node
{
    char c;
    node *pre,*next;
    node()
    {
        pre = NULL;
        next = NULL;
    }
};
int n,m,p;

int main()
{
    int t,C = 1;
    //scanf("%d",&t);
    while(scanf("%d%d%d",&n,&m,&p) != EOF)
    {
        char c;
        node *root = new node();
        node *pre = root;
        getchar();
        while(scanf("%c",&c) && c != '\n')
        {
            node *st = new node();
            pre->next = st;
            st->c = c;
            st->pre = pre;
            pre = st;
        }
        pre->next = new node();
        node *pos = root;
        for(int i = 0; i < p; i++)
            pos = pos->next;
        while(scanf("%c",&c) && c != '\n')
        {
            if(c == 'L')
                pos = pos->pre;
            else if(c == 'R')
                pos = pos->next;
            else
            {
                int k = 1,flag;
                if(pos->c == ')')
                {
                    flag = 0;
                    pre = pos->next;
                }
                else
                {
                    flag = 1;
                    pre = pos->pre;
                }
                while(k)
                {
                    if(flag)
                    {
                        pos = pos->next;
                        if(pos->c == '(')
                            k++;
                        else
                            k--;
                    }
                    else
                    {
                        pos = pos->pre;
                        if(pos->c == '(')
                            k--;
                        else
                            k++;
                    }
                }
                if(flag)
                {
                    pos = pos->next;
                    pre->next = pos;
                    pos->pre = pre;
                }
                else
                {
                    pos = pos->pre;
                    pre->pre = pos;
                    pos->next = pre;
                }
                if(!flag)
                    pos = pre;
                if(pos->next == NULL)
                    pos = pos->pre;
            }
//            node *ppos = root->next;
//            while(ppos->next)
//            {
//                printf("%c",ppos->c);
//                ppos = ppos->next;
//            }
//            printf("\n");
        }
        pos = root->next;
        while(pos->next)
        {
            printf("%c",pos->c);
            pos = pos->next;
        }
        printf("\n");
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值