HDOJ 4699 Editor 栈 模拟

Posted mthoutai

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDOJ 4699 Editor 栈 模拟相关的知识,希望对你有一定的参考价值。


用两个栈模拟:

Editor

Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1913    Accepted Submission(s): 591


Problem Description
技术分享
 

Sample Input
8 I 2 I -1 I 1 Q 3 L D R Q 2
 

Sample Output
2 3
Hint
The following diagram shows the status of sequence after each instruction: 技术分享
 

Source
 



#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <vector>

using namespace std;

const int INF=0x3f3f3f3f;
const int maxn=1001000;

int Left[maxn],Right[maxn];
int sum[maxn],maxsum[maxn];
int nl,nr;

char op[10];
int x,T_T,sz;

void init()
{
  nl=0; nr=0;
  maxsum[0]=-INF;
  sum[0]=0;
  sz=1;
}

int nextInt()
{
  bool ok=false;
  int ret=0; char ch;
  int xi=0;
  while(ch=getchar())
    {
      if(ch==‘-‘||(ch>=‘0‘&&ch<=‘9‘))
        {
          ok=true;
          if(ch==‘-‘) xi=1;
          else ret=ret*10+ch-‘0‘;
        }
      else if(ok==true) break;
    }
  if(xi) ret*=-1;
  return ret;
}

char nextChar()
{
  char ch=0;
  while(ch=getchar())
    {
      if(ch==‘D‘||ch==‘R‘||ch==‘L‘||ch==‘Q‘||ch==‘I‘)
        {
          return ch;
        }
    }
}

int main()
{
  while(scanf("%d",&T_T)!=EOF)
    {
      init();
      while(T_T--)
        {
          op[0]=nextChar();
          if(op[0]==‘I‘)
            {
              x=nextInt();
              Left[nl++]=x;
              sum[sz]=sum[sz-1]+x;
              maxsum[sz]=max(maxsum[sz-1],sum[sz]);
              sz++;
            }
          else if(op[0]==‘D‘)
            {
              if(nl==0) continue;
              nl--;
              sz--;
            }
          else if(op[0]==‘L‘)
            {
              if(nl==0) continue;
              int t=Left[nl-1];
              nl--;
              Right[nr++]=t;
              sz--;
            }
          else if(op[0]==‘R‘)
            {
              if(nr==0) continue;
              int t=Right[nr-1];
              nr--;
              Left[nl++]=t;
              sum[sz]=sum[sz-1]+t;
              maxsum[sz]=max(maxsum[sz-1],sum[sz]);
              sz++;
            }
          else if(op[0]==‘Q‘)
            {
              int x;
              x=nextInt();
              printf("%d\n",maxsum[x]);
            }
        }
    }
  return 0;
}





















以上是关于HDOJ 4699 Editor 栈 模拟的主要内容,如果未能解决你的问题,请参考以下文章

HDU 4699 Editor

[HDU4669]Editor (栈)

HDOJ4699(对顶栈)

HDU 4699 - Editor - [对顶栈]

栈专题练习

HDOJ-ACM1022(JAVA)