您的位置:首页 > 产品设计 > UI/UE

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

2016-10-01 14:21 411 查看
*题目描述:

你需要写一个括号序列编辑器。你有一个长度为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;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: