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;
}
你需要写一个括号序列编辑器。你有一个长度为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;
}
相关文章推荐
- Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor【模拟+链表】
- Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor (链表)
- Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 模拟
- Codeforces 350 Div2 E Correct Bracket Sequence Editor(list模拟)
- Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 栈 链表
- Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor
- Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor (括号匹配和删除,输出最后的括号序列)
- Codeforces 670E - Correct Bracket Sequence Editor 链表
- CodeForces 670 E.Correct Bracket Sequence Editor(链表)
- Codeforces Round #350 (Div. 2) E - Correct Bracket Sequence Editor(并查集)
- Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 线段树模拟
- 【31.93%】【codeforces 670E】Correct Bracket Sequence Editor
- cf670E Correct Bracket Sequence Editor
- java模拟链表和栈以及简单实现
- Codeforces Round #175 (Div. 2) B. Find Marble(简单模拟)
- Codeforces Beta Round #92 (Div. 2) / 124A The number of positions 【简单模拟】【水题】
- 帮同学写的ATM机模拟(简单链表操作)
- Codeforces Round #107 (Div. 2) A(简单模拟)
- Codeforces Round #109 (Div. 2) A (简单模拟)
- 【Codeforces Round 333 (Div 2)A 】【水题 简单进制模拟】Two Bases 不同进制数比大小