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;
	scanf("%d%d%d",&n,&m,&pos);
    scanf("%s",s1+1);
	int len = strlen(s1+1);
	for (int i = 0;i<=n+1;i++)
		nodes[i].l=i-1,nodes[i].r=i+1;
	int now = 0;
	for (int i = 1;i<=len;i++)
	{
		if(s1[i]=='(')
			a[now++]=i;
		else
			d[i]=a[--now],d[a[now]]=i;
	}
    scanf("%s",s2+1);
//	int pos = 0;
	for (int i = 1;i<=m;i++)
	{
		if (s2[i]=='R')
			pos = nodes[pos].r;
		else if (s2[i]=='L')
			pos = nodes[pos].l;
		else
		{
			int l = min(d[pos],pos);
			int r = max(d[pos],pos);
			nodes[nodes[l].l].r=nodes[r].r;
			nodes[nodes[r].r].l=nodes[l].l;
			pos = nodes[r].r;
			if (pos==n+1)
				pos = nodes[n+1].l;
			if(!pos)
				pos = nodes[0].r;
		}
	}
    pos = nodes[0].r;
	while(pos!=n+1)
	{
		printf("%c",s1[pos]);
		pos = nodes[pos].r;
	}
	printf("\n");
}

Description

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 nm 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.

Sample Input

Input

8 4 5
(())()()
RDLD

Output

()

Input

12 5 3
((()())(()))
RRDLD

Output

(()(()))

Input

8 8 8
(())()()
LLLLLLDD

Output

()()
时间: 2024-12-17 07:09:11

CodeForces 670E(模拟双向链表)的相关文章

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

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

CodeForces - 427B (模拟题)

Prison Transfer Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description The prison of your city has n prisoners. As the prison can't accommodate all of them, the city mayor has decided to transfer c of t

CodeForces - 404B(模拟题)

Marathon Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description Valera takes part in the Berland Marathon. The marathon race starts at the stadium that can be represented on the plane as a square whose

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&

【日常学习】【模拟双向链表】【疑问】Uva12657 - Boxes in a Line题解

这道题目我做的不对.事实上,我按书上的标程抄的,几乎一模一样,我认为他没有什么错误,可我就是不知道为什么我在代码仓库下的刘汝佳写的程序就AC,我写的就WA.跳了一下午,两程序样例输出完全一样(奇怪的是和书上答案不一样)一个字一个字的比对,就是找不出哪里不一样.我觉得极少不一样的地方应该没有影响,哪位大神愿意给看看? 这是一道双向链表,同样没有用指针,而是用两个数组模拟,道理和上面的那道非指针单向链表题目一样 刘汝佳的代码 // UVa12657 Boxes in a Line // Rujia

Codeforces 452D [模拟][贪心]

题意: 给你k件衣服处理,告诉你洗衣机烘干机折叠机的数量,和它们处理一件衣服的时间,要求一件衣服在洗完之后必须立刻烘干,烘干之后必须立刻折叠,问所需的最小时间. 思路: 1.按照时间模拟 2.若洗完的衣服或者烘干的衣服较多来不及进行下一个步骤,则从一开始就顺延洗衣服的时间,贪心的思想也是体现在这里. 3.关键在于烘干衣服的顺延如何处理,因为需要调整洗衣服的起始时间,其实我们只要对烘干衣服的时间进行顺延处理就可以了,因为即使没有调整洗衣服的起始时间,那么下次到了烘干衣服的时间的时候因为烘干衣服的数

CodeForces - 404A(模拟题)

Valera and X Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description Valera is a little boy. Yesterday he got a huge Math hometask at school, so Valera didn't have enough time to properly learn the Engli

CodeForces - 200ACinema模拟题

CodeForces - 200A Cinema Time Limit: 1500MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description The capital of Berland has the only movie theater in the country. Besides, it consists of only one room. The room is divid

【题解】Berland.Taxi Codeforces 883L 模拟 线段树 堆

Prelude 题目传送门:ヾ(?ω?`)o Solution 按照题意模拟即可. 维护一个优先队列,里面装的是正在运营中的出租车,关键字是乘客的下车时间. 维护一个线段树,第\(i\)个位置表示第\(i\)个房子前面有没有停放出租车,这样在有人需要打车的时候可以快速找到离她最近的车的位置. 对每个房子维护一个堆,里面装的是停在这个房子前面的出租车,关键字是出租车的编号和上一个乘客下车的时间,上一个乘客下车越早,等待时间越长. 然后模拟时间的流逝就可以了,代码非常好写. Code #includ