一开始理解错了子串的含义,尴尬…
理解了题意.
其实就是个后缀自动机裸题,把第一个串建后缀自动机,然后把所有串放在上面跑即可…
c++代码如下:
#include<bits/stdc++.h>
#define rep(i,x,y) for(register int i = x ; i <= y; ++ i )
#define repd(i,x,y) for(register int i = x ; i >= y; -- i)
using namespace std;
template<typename T>inline bool chkmax(T&x,T y) { return x < y ? x = y,1: 0; }
template<typename T>inline bool chkmin(T&x,T y) { return x > y ? x = y,1: 0; }
template<typename T>inline void read(T&x)
{
char c;int sign = 1;x = 0;
do { c = getchar(); if(c == '-') sign = -1; }while(!isdigit(c));
do { x = x * 10 + c - '0'; c = getchar(); }while(isdigit(c));
x *= sign;
}
const int N = 4e3 + 50;
int p = 1,sz = 1,root = 1;
int f[N],mn[N],ans;
char s[N];
struct SAM { int ch[26],len,pre; }t[N];
inline void insert(int x)
{
int np = ++ sz;
mn[np] = t[np].len = t[p].len + 1;
for(;p && !t[p].ch[x];p = t[p].pre) t[p].ch[x] = np;
if(!p) t[np].pre = root;
else
{
int q = t[p].ch[x];
if(t[q].len == t[p].len + 1) t[np].pre = q;
else
{
int nq = ++ sz;
t[nq] = t[q];
t[nq].len = t[p].len + 1;
t[q].pre = t[np].pre = nq;
for(;p && t[p].ch[x] == q;p = t[p].pre) t[p].ch[x] = nq;
}
}
p = np;
}
int n,l,len,id[N],buc[N];
inline void toposort()
{
rep(i,1,sz) ++ buc[t[i].len];
rep(i,1,len) buc[i] += buc[i - 1];
rep(i,1,sz) id[buc[t[i].len] -- ] = i;
}
int main()
{
read(n);
scanf("%s",s + 1);
len = strlen(s + 1);
rep(i,1,len) insert(s[i] - 'a');
toposort();
rep(ii,2,n)
{
memset(f,0,sizeof f);
scanf("%s",s + 1);
l = strlen(s + 1);
int now = 1,le = 0;
rep(i,1,l)
{
int x = s[i] - 'a';
if(t[now].ch[x]) ++le,now = t[now].ch[x];
else
{
for(;now && !t[now].ch[x];now = t[now].pre) ;
if(!now) now = root,le = 0;
else le = t[now].len + 1,now = t[now].ch[x];
}
chkmax(f[now],le);
}
repd(i,sz,1)
{
now = id[i];
chkmin(mn[now],f[now]);
chkmax(f[t[now].pre],f[now]);
}
}
rep(i,1,sz) ans = max(ans,mn[i]);
cout << ans << endl;
return 0;
}