Codeforces 670E - Correct Bracket Sequence Editor - [对顶栈]
题目链接: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();
}
Codeforces 670E - Correct Bracket Sequence Editor - [对顶栈]的更多相关文章
- CodeForces 670E Correct Bracket Sequence Editor(list和迭代器函数模拟)
E. Correct Bracket Sequence Editor time limit per test 2 seconds memory limit per test 256 megabytes ...
- Codeforces 670E - Correct Bracket Sequence Editor - [线段树]
题目链接:https://codeforces.com/contest/670/problem/E 题意: 给出一个已经匹配的括号串,给出起始的光标位置(光标总是指向某个括号). 有如下操作: 1.往 ...
- Codeforces 670E - Correct Bracket Sequence Editor - [链表]
题目链接:https://codeforces.com/contest/670/problem/E 题意: 给出一个已经匹配的括号串,给出起始的光标位置(光标总是指向某个括号). 有如下操作: 1.往 ...
- CodeForces 670E Correct Bracket Sequence Editor
链表,模拟. 写一个双向链表模拟一下过程. #pragma comment(linker, "/STACK:1024000000,1024000000") #include< ...
- Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 栈 链表
E. Correct Bracket Sequence Editor 题目连接: http://www.codeforces.com/contest/670/problem/E Description ...
- Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 线段树模拟
E. Correct Bracket Sequence Editor Recently Polycarp started to develop a text editor that works o ...
- 【31.93%】【codeforces 670E】Correct Bracket Sequence Editor
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- cf670E Correct Bracket Sequence Editor
Recently Polycarp started to develop a text editor that works only with correct bracket sequences (a ...
- Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 模拟
题目链接: http://codeforces.com/contest/670/problem/E 题解: 用STL的list和stack模拟的,没想到跑的还挺快. 代码: #include<i ...
随机推荐
- redis优化配置和redis.conf说明(转)
1. redis.conf 配置参数: #是否作为守护进程运行 daemonize yes #如以后台进程运行,则需指定一个pid,默认为/var/run/redis.pid pidfile redi ...
- mark 阿里支付
开源软件企业版特惠高校版博客 我的码云 ·· 8月18日(周六)成都源创会火热报名中,四位一线行业大牛与你面对面,探讨区块链技术热潮下的冷思考. 开源项目 > WEB应用开发 > Web开 ...
- lua -- 系统提示框
-- -- Author: chentong -- Date: 2014-3-24 -- 系统提示: -- 先做个简单的,在中上位置出现提示,逐渐消失,如果有新提示,则直接删除旧提示. -- 规则: ...
- numpy数组(4)-二维数组
python创建二维 list 的方法是在 list 里存放 list : l = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]] numpy可以直接 ...
- vue使用node的入门
1.安装cnpm npm install -g cnpm --registry=https://registry.npm.taobao.org 验证是否安装 cnpm -v 2.安装vue cnpm ...
- Unity的Attribute(特性)还算多吧
属性 (Attribute) 使用 Unity 的C#语言 ,利用属性(Attribute)来类定义和变量定义或区分其他的变量,您可以设置一种特殊行为.* 1 例如,您添加[SerializeFiel ...
- FROM USE CASES TO TEST CASES
FROM USE CASES TO TEST CASES -Test note of “Essential Software Test Design” 2015-08-31 Content: 12.1 ...
- 启动TDS LDAP 服务器遇到的问题总结
在启动TDS LDAP服务器时遇到一些问题,由于习惯使用Oracle数据库,而对DB2数据库比较陌生,在遇到这些问题时也是摸不到头脑,好在现在解决了,并把所遇到的问题罗列如下: 使用命令启动TDS L ...
- cp显示进度条
cp显示进度条 alias cp='rsync -av --progress'
- cvCreateImage
CvCreateImage函数说明 cvCreateImage是openCV中的一个函数.OpenCV是Intel公司支持的开源计算机视觉库. cvCreateImage: 创建头并分配数据 ...