首页 > 代码库 > HDOJ 4699 Editor 栈 模拟
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 3HintThe following diagram shows the status of sequence after each instruction:
Source
2013 Multi-University Training Contest 10
#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 栈 模拟
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。