首页 技术 正文
技术 2022年11月17日
0 收藏 439 点赞 4,017 浏览 1090 个字

题目链接:https://codeforces.com/contest/670/problem/E

题意:

给出一个已经匹配的括号串,给出起始的光标位置(光标总是指向某个括号)。

有如下操作:

  1、往左移动一下光标;

  2、往左移动一下光标;

  3、删除当前光标指向的括号,以及和它匹配的那个括号,以及这两个括号之间的所有括号。

要求你给出在做完所有操作后的括号串。

题解:

用对顶栈进行模拟。由于删除是不可逆的,因此删除的总时间复杂度为 $O(n)$,因此所有 $m$ 次操作的总时间复杂度为 $O(n+m)$。

另外可以参看:本题的链表做法本题的线段树做法

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=5e5+;
int n,m,p;
char str[maxn],op[maxn];
int bro[maxn];
vector<int> s,t;
void print()
{
for(int i=;i<s.size();i++) printf("%c",str[s[i]]);
for(int i=t.size()-;i>=;i--) printf("%c",str[t[i]]);
printf("\n");
}
int main()
{
cin>>n>>m>>p;
scanf("%s",str+);
for(int i=;i<=p;i++) s.push_back(i);
for(int i=n;i>p;i--) t.push_back(i); stack<int> S;
for(int i=;i<=n;i++)
{
if(str[i]=='(') S.push(i);
if(str[i]==')') bro[S.top()]=i, bro[i]=S.top(), S.pop();
} scanf("%s",op+);
for(int i=;i<=m;i++)
{
if(op[i]=='L') t.push_back(s.back()), s.pop_back();
if(op[i]=='R') s.push_back(t.back()), t.pop_back();
if(op[i]=='D')
{
p=s.back();
if(p<bro[p])
{
s.pop_back();
while(t.size() && t.back()<=bro[p]) t.pop_back();
}
if(bro[p]<p)
{
while(s.size() && s.back()>=bro[p]) s.pop_back();
}
if(t.size()) s.push_back(t.back()), t.pop_back();
}
} print();
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,400
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,813
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,894