[suffix automaton] BZOJ5084. hashit

Teacher Chen shenti
Consider how to delete. Add two nodes at most each time. Then delete only two nodes

Two values will be changed when characters are added, one is nxt and the other is fail

When we delete the node uu, if the failure of a point vv is uu, then we need to change the failvfailv to failufailu, which can be realized by using concurrent query set

Another is nxt. When nxt changes, only when nq is added, can linked list be used for maintenance (probably also a concurrent query set)

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

typedef long long ll;

const int N=200010;

int n;
char a[N];

int len[N],fail[N],nxt[N][30],pos[N],S[N],vis[N],size[N],fa[N][20],ad[N],top,cnt=1;

ll ans;

inline int calc(int x){
  int cur=x;
  for(int i=17;~i;i--)
    if(!vis[fa[x][i]]) x=fa[x][i];
  return len[cur]-len[fa[x][0]];
}

int Find(int x){
  if(!vis[fail[x]]) return fail[x]=Find(fail[x]);
  return fail[x];
}

int Link[N];

int Get(int &x){
  if(!vis[x] && x) return x=Get(Link[x]);
  return x;
}

inline void extend(int p,int c,int ps){
  int np=++cnt; len[np]=len[p]+1; pos[ps]=np; vis[np]=1;
  while(p && !Get(nxt[p][c])) nxt[p][c]=np,p=fail[p];
  if(!p) fail[np]=1,size[1]++;
  else{
    int q=nxt[p][c];
    if(len[q]==len[p]+1) fail[np]=q,size[q]++;
    else{
      int nq=++cnt; len[nq]=len[p]+1; ad[ps]=nq; vis[nq]=1;
      memcpy(nxt[nq],nxt[q],sizeof(nxt[nq]));
      ans-=len[q]-len[Find(q)];
      fail[nq]=fail[q]; Link[nq]=q;
      fail[q]=fail[np]=nq;
      ans+=len[q]-len[nq]+len[nq]-len[Find(nq)];
      size[nq]+=2;
      while(p && Get(nxt[p][c])==q) nxt[p][c]=nq,p=fail[p];
    }
  }
  ans+=len[np]-len[Find(np)];
}

void PutAns(ll x){
  if(x>=10) PutAns(x/10); putchar(x%10+'0');
}

inline void del(int x){
  ans-=len[x]-len[Find(x)];
  vis[x]=0; ans+=(len[x]-len[Find(x)])*size[x];
  size[Find(x)]+=size[x]-1;
}

int main(){
  scanf("%s",a+1); n=strlen(a+1);
  pos[0]=1; vis[1]=1;
  for(int i=1;i<=n;i++){
    if(a[i]!='-'){
      S[++top]=i; extend(pos[S[top-1]],a[i]-'a',i);
    }
    else{
      int x=S[top--];
      if(ad[x]) del(ad[x]);
      del(pos[x]); 
    }
    PutAns(ans); putchar('\n');
  }
  return 0;
}

Added by Grofit on Thu, 30 Apr 2020 13:50:19 +0300