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 ...
随机推荐
- filter 过滤器的基本使用
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- [ipsec][crypto] 在IPSec ESP使用AES-GCM加密时的IV
IV IV是指初始化向量. 在我们当前讨论的场景中: 在IPSec ESP使用AES-GCM加密 IV有两个含义: 1. ESP报文封装时的IV,RFC中称为 AES-GCM IV +-+-+-+-+ ...
- NodeJS笔记(四) NPM 指令--- npm start
在上一节中使用我们使用下面的指令启动了Express的demo APP项目 npm start 这个指令具体执行了哪些内容呢? Node.js新版本改变了启动方式,npm start 会执行 bi ...
- vue常考面试题
组件中 data 什么时候可以使用对象? 这道题其实更多考的是 JS 功底: 组件复用时所有组件实例都会共享 data,如果 data 是对象的话,就会造成一个组件修改 data 以后会影响到其他所有 ...
- AndroidStudio中如何使用GsonFormat
转载:https://www.jianshu.com/p/3b82f42e5937 第一步: 找到AndroidStudio中得Prefrences的plugins的Browse repositori ...
- LINQ交集/并集/差集/去重
using System.Linq; List<string> ListA = new List<string>(); List<string> ListB = n ...
- PowerBI/Excel - PowerQuery数据转换系列 - 如何将多行的值串联到一行 - 行列转换
Power Query 是做数据转换.数据清洗的利器,不管是在Excel还是PowerBI,如何玩好Power Query是成功建模的必不可少的一步. 今天要get到的一个新技巧:行列转换 如何将多行 ...
- [LeetCode] 63. Unique Paths II_ Medium tag: Dynamic Programming
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- (转载)Oracle procedure 基本语法
转自:http://www.cnblogs.com/wolfplan/p/4004624.html 关键字: oracle 存储过程 1.基本结构 CREATE OR REPLACE PROCEDUR ...
- 算法题:int 数组中 只有一个是id 只出现一次 其他都出现2次 怎么找出只出现一次的id
首先讲一个最笨的算法:时间复杂度为N 空间复杂度为N 代码如下:输出结果id=3完全正确: int[] a = new int[] { 1, 1, 2, 2, 3, 4, 4 }; Dictiona ...