【牛客】数据结构-栈-Editor(自我学习用)
思路:虽说是放在栈的题库下面,但是也可以用别的做,这里用栈结合数组
光标可以看作下标,然后左右可以看作两个数组,并且用到了前缀和的知识
下面是AC代码
#include <bits/stdc++.h>
using namespace std;
const int max_n=1000006;
int a[max_n],top_a;//top_a是光标的位置,top_b是光标后的东西
//a数组是光标及之前的数组,b数组是光标后的数组
long long sum[max_n],len[max_n];//前缀和
stack<int> sta;
void I(int x){
a[++top_a]=x;
sum[top_a]=sum[top_a-1]+x;//前缀和
len[top_a]=max(sum[top_a],len[top_a-1]);//这里直接把Q操作需要的给做好了
}
void D(){
if(top_a) top_a--;
}
void L(){
if(top_a!=0) sta.push(a[top_a--]);
}
void R(){//由于光标右边数组不重要,故放栈里
if(sta.empty()) return;//注意如果栈顶空还访问top是非法的
int now=sta.top();
sta.pop();
a[++top_a]=now;
sum[top_a]=sum[top_a-1]+now;
len[top_a]=max(len[top_a-1],sum[top_a]);
}
void Q(int k){
cout<<len[k]<<endl;
}
int main(){
int n;
len[0]=-1e9;
while(cin>>n){
while(n--){
char c; cin>>c;
if(c=='I'){
int x; cin>>x;
I(x);
}else if(c=='D')D();
else if(c=='L')L();
else if(c=='R')R();
else if(c=='Q'){
int k; cin>>k;
Q(k);
}
}
}
return 0;
}