0x11栈之Editor
参考链接:https://blog.csdn.net/SSLGZ_yyc/article/details/81700623
对顶栈的思想:
建立两个栈,栈A存储从序列开头到当前光标的位置的一段序列,栈B存储从光标到结尾的序列。这两个栈一共存储了整个序列。
java版本代码
import java.util.Scanner;
import java.util.Stack; public class Main {
public static void main(String[] args) {
int f[]=new int[200000];
f[0]=-999999999;
int sum=0;
Scanner in = new Scanner(System.in);
int n=in.nextInt();
while(in.hasNext())
{
Stack<Integer> a=new Stack<Integer>();
Stack<Integer> b=new Stack<Integer>(); while((n--)!=0) {
String s=in.next();
if (s.equals("I")) {
int x=in.nextInt();
a.push(x);
sum=sum+x;
int l=a.size();
f[l]=Math.max(sum, f[l-1]);
}else if (s.equals("D")&&a.size()>0) {
int x=a.pop();
sum=sum-x;
}else if (s.equals("L")&&a.size()>0) {
int x=a.pop();
sum=sum-x;
b.push(x);
}else if (s.equals("R")&&b.size()>0) {
int x=b.pop();
a.push(x);
sum+=x;
int l=a.size();
f[l]=Math.max(sum,f[l-1]);
}else if (s.equals("Q")) {
int x=in.nextInt();
System.out.println(f[x]);
}
}
}
}
}
c++版本代码:
#include<iostream>
#include<stack>
#include<stdio.h>
using namespace std;
int f[2000000]; int main()
{
char ch,zfc[200];
int n;
while (scanf("%d",&n)!=EOF)
{
stack <int> a;
stack <int> b;
int sum=0,x;
f[0]=-999999999;
while (n--)
{
scanf("%s",zfc);
ch=zfc[0];
if (ch=='I')
{
scanf("%d",&x);
a.push(x);
sum+=x;
int l=a.size();
f[l]=max(sum,f[l-1]);
} else
if (ch=='D'&&a.size()>=1)
{
x=a.top();
a.pop();
sum-=x;
} else
if (ch=='L'&&a.size()>=1)
{
x=a.top();
a.pop();
sum-=x;
b.push(x);
} else
if (ch=='R'&&b.size()>=1)
{
x=b.top();
b.pop();
a.push(x);
sum+=x;
int l=a.size();
f[l]=max(sum,f[l-1]);
} else
if (ch=='Q')
{
scanf("%d",&x);
printf("%d\n",f[x]);
}
}
}
return 0;
}
0x11栈之Editor的更多相关文章
- 0x11栈之火车进栈
参考<算法竞赛进阶指南>p.49 题目链接:https://www.acwing.com/problem/content/description/131/ 递推与递归的宏观描述 对于一个待 ...
- 0x11 栈
这个不难吧,算是常识了..毕竟也是刷过USACO的人 对顶栈这东西前几天才遇到过,好像和在线求中位数那东西放一起了吧 单调栈倒是没什么...贴个代码算了.一开始有点蠢的每个位置算,后来发现出栈再算就行 ...
- 2019CSP-S初赛知识点汇总
0x00 基本算法 0x01 位运算 0x02 前缀和与差分 0x03 二分 0x04 倍增 0x05 排序 0x06 离散化 0x07 高精度 0x10 数据结构 0x11 栈和队列 0x12 链表 ...
- Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 栈 链表
E. Correct Bracket Sequence Editor 题目连接: http://www.codeforces.com/contest/670/problem/E Description ...
- HDOJ 4699 Editor 栈 模拟
用两个栈模拟: Editor Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- hdu 4699 Editor 模拟栈
思路:刚开始用STL中的栈,一直RE……,之后改为手动模拟栈操作,在注意点细节就可以了!!! 代码如下: #include<cstdio> #include<cstring> ...
- hdu4699 Editor 2013 多校训练第十场 D题 数列维护 splay | 线段树 | 栈!!!!!
题意:维护一个文本编辑,并且查询最大前缀和. 写了splay,wa了13次 过了之后觉着特傻逼.发现题解两个栈就可以了,光标前后维护两个栈,维护前面的栈的前缀和 和 最大前缀和. 哎,傻逼,太弱了,还 ...
- [HDU4669]Editor (栈)
题意 模拟编辑器,还是给链接吧 https://vjudge.net/problem/HDU-4699 思路 两个栈 代码 //poj1050 //n^4暴力 #include<algorith ...
- Editor HDU - 4699 (栈)
Problem Description Sample Input 8 I 2 I -1 I 1 Q 3 L D R Q 2 Sample Output 2 3 Hint The followi ...
随机推荐
- JDK安装路径下的JRE与独立安装的JRE区别
在JDK安装目录下的子文件下,已经默认安装了一个jre.且与独立安装的JRE6所包含的文件几乎完全一样. JDK里面内置的JRE和独立的JRE是有一点差别的: 在JDK安装文件中包含了一个完整的独立版 ...
- yii 1.x 添加 rules 验证url数组
public function rules() { return CMap::mergeArray( parent::rules(),array( array('third_link', 'urlAr ...
- gitIgnore说明
有些内容不需要提交到git服务器上,这时我们可以配置.gitIgnore文件.可参考:https://www.cnblogs.com/kevingrace/p/5690241.html 可能有时候你会 ...
- SNMP 优秀帖子
-- 比较系统的描述http://blog.sina.com.cn/s/blog_54837cf301011607.html 几个SNMP官方网站(搜索关键字:snmplibrary C#):http ...
- jmeter常用插件介绍
一.下载安装及使用 下载地址:jmeter-plugins.org 安装:下载后文件为plugins-manager.jar格式,将其放入jmeter安装目录下的lib/ext目录,然后重启jmete ...
- python做数据驱动
python代码如下: import unittestfrom openpyxl import load_workbookfrom openpyxl.styles import Fontfrom op ...
- what's the 灰盒测试
what's the 灰盒测试 灰盒测试的概念:是一种综合测试的方法,他将白盒测试和黑盒测试结合在一起,构成一种无缝测试技术. 灰盒测试的思想:是基于程序运行时的外部表现又结合程序内部逻辑结构来设计测 ...
- NSRunLoop 在mac command line tool上的部分运用
首先RunLoop相关博客参考这篇https://blog.csdn.net/lengshengren/article/details/12905627. 最近开发了一个mac上的命令行工具,我在主线 ...
- AngularJS简单例子
双大括号标记{{}}绑定的表达式 <html ng-app> <script src="http://code.angularjs.org/angular-1.0.1.mi ...
- (转)手工释放linux内存——/proc/sys/vm/drop_cache
linux的内存查看: [root@localhost 0.1.0]# free -m total used free shared ...