码迷,mamicode.com
首页 > 其他好文 > 详细

HDOJ 4699 Editor 栈 模拟

时间:2014-10-24 16:29:33      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   ar   java   


用两个栈模拟:

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
bubuko.com,布布扣
 

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: bubuko.com,布布扣
 

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 栈 模拟

标签:des   style   blog   http   color   io   os   ar   java   

原文地址:http://blog.csdn.net/ck_boss/article/details/40428965

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!