参考链接:https://blog.csdn.net/SSLGZ_yyc/article/details/81700623

对顶栈的思想:

建立两个栈,栈A存储从序列开头到当前光标的位置的一段序列,栈B存储从光标到结尾的序列。这两个栈一共存储了整个序列。

java版本代码

  1. import java.util.Scanner;
  2. import java.util.Stack;
  3.  
  4. public class Main {
  5. public static void main(String[] args) {
  6. int f[]=new int[200000];
  7. f[0]=-999999999;
  8. int sum=0;
  9. Scanner in = new Scanner(System.in);
  10. int n=in.nextInt();
  11. while(in.hasNext())
  12. {
  13. Stack<Integer> a=new Stack<Integer>();
  14. Stack<Integer> b=new Stack<Integer>();
  15.  
  16. while((n--)!=0) {
  17. String s=in.next();
  18. if (s.equals("I")) {
  19. int x=in.nextInt();
  20. a.push(x);
  21. sum=sum+x;
  22. int l=a.size();
  23. f[l]=Math.max(sum, f[l-1]);
  24. }else if (s.equals("D")&&a.size()>0) {
  25. int x=a.pop();
  26. sum=sum-x;
  27. }else if (s.equals("L")&&a.size()>0) {
  28. int x=a.pop();
  29. sum=sum-x;
  30. b.push(x);
  31. }else if (s.equals("R")&&b.size()>0) {
  32. int x=b.pop();
  33. a.push(x);
  34. sum+=x;
  35. int l=a.size();
  36. f[l]=Math.max(sum,f[l-1]);
  37. }else if (s.equals("Q")) {
  38. int x=in.nextInt();
  39. System.out.println(f[x]);
  40. }
  41. }
  42. }
  43. }
  44. }

 c++版本代码:

  1. #include<iostream>
  2. #include<stack>
  3. #include<stdio.h>
  4. using namespace std;
  5. int f[2000000];
  6.  
  7. int main()
  8. {
  9. char ch,zfc[200];
  10. int n;
  11. while (scanf("%d",&n)!=EOF)
  12. {
  13. stack <int> a;
  14. stack <int> b;
  15. int sum=0,x;
  16. f[0]=-999999999;
  17. while (n--)
  18. {
  19. scanf("%s",zfc);
  20. ch=zfc[0];
  21. if (ch=='I')
  22. {
  23. scanf("%d",&x);
  24. a.push(x);
  25. sum+=x;
  26. int l=a.size();
  27. f[l]=max(sum,f[l-1]);
  28. } else
  29. if (ch=='D'&&a.size()>=1)
  30. {
  31. x=a.top();
  32. a.pop();
  33. sum-=x;
  34. } else
  35. if (ch=='L'&&a.size()>=1)
  36. {
  37. x=a.top();
  38. a.pop();
  39. sum-=x;
  40. b.push(x);
  41. } else
  42. if (ch=='R'&&b.size()>=1)
  43. {
  44. x=b.top();
  45. b.pop();
  46. a.push(x);
  47. sum+=x;
  48. int l=a.size();
  49. f[l]=max(sum,f[l-1]);
  50. } else
  51. if (ch=='Q')
  52. {
  53. scanf("%d",&x);
  54. printf("%d\n",f[x]);
  55. }
  56. }
  57. }
  58. return 0;
  59. }

  

 

0x11栈之Editor的更多相关文章

  1. 0x11栈之火车进栈

    参考<算法竞赛进阶指南>p.49 题目链接:https://www.acwing.com/problem/content/description/131/ 递推与递归的宏观描述 对于一个待 ...

  2. 0x11 栈

    这个不难吧,算是常识了..毕竟也是刷过USACO的人 对顶栈这东西前几天才遇到过,好像和在线求中位数那东西放一起了吧 单调栈倒是没什么...贴个代码算了.一开始有点蠢的每个位置算,后来发现出栈再算就行 ...

  3. 2019CSP-S初赛知识点汇总

    0x00 基本算法 0x01 位运算 0x02 前缀和与差分 0x03 二分 0x04 倍增 0x05 排序 0x06 离散化 0x07 高精度 0x10 数据结构 0x11 栈和队列 0x12 链表 ...

  4. Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 栈 链表

    E. Correct Bracket Sequence Editor 题目连接: http://www.codeforces.com/contest/670/problem/E Description ...

  5. HDOJ 4699 Editor 栈 模拟

    用两个栈模拟: Editor Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  6. hdu 4699 Editor 模拟栈

    思路:刚开始用STL中的栈,一直RE……,之后改为手动模拟栈操作,在注意点细节就可以了!!! 代码如下: #include<cstdio> #include<cstring> ...

  7. hdu4699 Editor 2013 多校训练第十场 D题 数列维护 splay | 线段树 | 栈!!!!!

    题意:维护一个文本编辑,并且查询最大前缀和. 写了splay,wa了13次 过了之后觉着特傻逼.发现题解两个栈就可以了,光标前后维护两个栈,维护前面的栈的前缀和 和 最大前缀和. 哎,傻逼,太弱了,还 ...

  8. [HDU4669]Editor (栈)

    题意 模拟编辑器,还是给链接吧 https://vjudge.net/problem/HDU-4699 思路 两个栈 代码 //poj1050 //n^4暴力 #include<algorith ...

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

随机推荐

  1. filter 过滤器的基本使用

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  2. [ipsec][crypto] 在IPSec ESP使用AES-GCM加密时的IV

    IV IV是指初始化向量. 在我们当前讨论的场景中: 在IPSec ESP使用AES-GCM加密 IV有两个含义: 1. ESP报文封装时的IV,RFC中称为 AES-GCM IV +-+-+-+-+ ...

  3. NodeJS笔记(四) NPM 指令--- npm start

    在上一节中使用我们使用下面的指令启动了Express的demo APP项目 npm start 这个指令具体执行了哪些内容呢? Node.js新版本改变了启动方式,npm start  会执行  bi ...

  4. vue常考面试题

    组件中 data 什么时候可以使用对象? 这道题其实更多考的是 JS 功底: 组件复用时所有组件实例都会共享 data,如果 data 是对象的话,就会造成一个组件修改 data 以后会影响到其他所有 ...

  5. AndroidStudio中如何使用GsonFormat

    转载:https://www.jianshu.com/p/3b82f42e5937 第一步: 找到AndroidStudio中得Prefrences的plugins的Browse repositori ...

  6. LINQ交集/并集/差集/去重

    using System.Linq; List<string> ListA = new List<string>(); List<string> ListB = n ...

  7. PowerBI/Excel - PowerQuery数据转换系列 - 如何将多行的值串联到一行 - 行列转换

    Power Query 是做数据转换.数据清洗的利器,不管是在Excel还是PowerBI,如何玩好Power Query是成功建模的必不可少的一步. 今天要get到的一个新技巧:行列转换 如何将多行 ...

  8. [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 ...

  9. (转载)Oracle procedure 基本语法

    转自:http://www.cnblogs.com/wolfplan/p/4004624.html 关键字: oracle 存储过程 1.基本结构 CREATE OR REPLACE PROCEDUR ...

  10. 算法题:int 数组中 只有一个是id 只出现一次 其他都出现2次 怎么找出只出现一次的id

    首先讲一个最笨的算法:时间复杂度为N  空间复杂度为N 代码如下:输出结果id=3完全正确: int[] a = new int[] { 1, 1, 2, 2, 3, 4, 4 }; Dictiona ...