对顶栈
【题意】模拟编辑器 算法进阶P50
【题解】对顶栈,(1)对于当前的sum求和的时候下标直接用当前前面的一个栈的size代替就可以;(2)还要注意字符的读入空格会有影响要加getchar;(3)还有stack.pop()之前要注意判断stack是不是空的。
# include <bits/stdc++.h>
using namespace std;
stack<int> a,b;
const int MAXN=1e6+100;
const int INF=1e9;
int sum[MAXN],f[MAXN];
void init()
{
memset(sum,0,sizeof(sum));
memset(f,0,sizeof(f));
f[0]=-INF;
while(!a.empty()) a.pop();
while(!b.empty()) b.pop();
return ;
}
int main()
{
int Q;
while(~scanf("%d",&Q)){
init();
int l;
for(int i=1;i<=Q;i++){
char c;
getchar();
scanf("%c",&c);
if(c=='I'){
int x;
scanf("%d",&x);
a.push(x);
l=a.size();
sum[l]=sum[l-1]+x;
f[l]=max(f[l-1],sum[l]);
}else if(c=='D'){
if(!a.empty()){
a.pop();
}
}else