trie 树裸题。但确实,若是用 set ,那么字符可以不限, 复杂度也只是带个 \(\log\)。
#include<bits/stdc++.h>
using namespace std;
#define N 500005
inline int read(){
int x=0,f=1;char c=getchar();
while(c<'0'||c>'9'){
if(c=='-')f=-1;c=getchar();
}
while(c>='0'&&c<='9'){
x=x*10+c-'0';c=getchar();
}
return x*f;
}
int tot,now,num[N],fa[N];
map<int, int> son[N];
map<int, int> li;
int cnt,to[N];
char op[7];
int main(){
int Q;scanf("%d,",&Q);
num[0]=-1;
while(Q--){
scanf("%s",op);
int x,y,z;
if(op[0]=='A'){
scanf("%d",&x);
if(son[now][x]==0){
num[++tot]=x;son[now][x]=tot;fa[tot]=now;
now=tot;
}else{
now=son[now][x];
}
}
if(op[0]=='D'){
if(now!=0)now=fa[now];
}
if(op[0]=='S'){
scanf("%d",&y);
if(li[y]==0)li[y]=++cnt;
to[li[y]]=now;
}
if(op[0]=='L'){
scanf("%d",&z);
if(li[z]==0)li[z]=++cnt;
now=to[li[z]];
}
printf("%d ",num[now]);
}
puts("");
return 0;
}
标签:son,int,scanf,li,Notebook,now,op
From: https://www.cnblogs.com/Huster-ZHY/p/16808023.html