后缀自动机

题解:

例题啥的看https://www.cnblogs.com/Sakits/p/8251363.html

我基本是照着这个学的

代码:

#include <bits/stdc++.h>
#define ll long long
#define rint register int
#define rep(i,h,t) for (rint i=h;i<=t;i++)
#define dep(i,t,h) for (rint i=t;i>=h;i--)
using namespace std;
const int N=3e6;
char s[N];
int size[N],len[N],ch[N][26];
int lst=1,node=1,t[N],a[N],fa[N];
void extend(int c)
{
  int f=lst,p=++node; lst=p;
  len[p]=len[f]+1; size[p]=1;
  while (f&&!ch[f][c]) ch[f][c]=p,f=fa[f];
  if (!f) { fa[p]=1; return;};
  int x=ch[f][c],y=++node;
  if (len[f]+1==len[x]) {fa[p]=x; node--;return;};
  len[y]=len[f]+1; fa[y]=fa[x]; fa[x]=fa[p]=y;
  memcpy(ch[y],ch[x],sizeof(ch[x]));
  while (f&&ch[f][c]==x) ch[f][c]=y,f=fa[f];
}
int main()
{
  freopen("1.in","r",stdin);
  freopen("1.out","w",stdout);
  ios::sync_with_stdio(false);
  cin>>s;
  int l=strlen(s);
  rep(i,1,l) extend(s[i-1]-'a');
  rep(i,1,node) t[len[i]]++;
  rep(i,1,node) t[i]+=t[i-1];
  rep(i,1,node) a[t[len[i]]--]=i;
  ll ans=0;
  dep(i,node,1)
  {
    int now=a[i]; size[fa[now]]+=size[now];
    if (size[now]>1) ans=max(ans,1ll*size[now]*len[now]);
  }
  cout<<ans<<endl;
  return 0;
}

 

posted @ 2018-07-21 10:53  尹吴潇  阅读(184)  评论(0编辑  收藏  举报