后缀自动机SAM
249. [POI2000] 最长公共子串
时间限制:1 s 内存限制:64 MB
给出几个由小写字母构成的单词,求它们最长的公共子串的长度。
任务
从文件中读入单词
计算最长公共子串的长度
输出结果到文件
输入
文件的第一行是整数 n,1<=n<=5,表示单词的数量。接下来n行每行一个单词,只由小写字母组成,单词的长度至少为1,最大为2000。
输出:
仅一行,一个整数,最长公共子串的长度。
样例输入:
3
abcb
bca
acbc
样例输出:
2
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 2100
using namespace std;
int n;
char s[5][maxn];
struct Node{
int len, nxt[26], link, mx[5];
}st[maxn << 1];
int root, size, last;
void init(){
last = root = size = 0;
st[root].link = -1;
st[root].len = 0;
}
void Extend(char ch){
int cur = ++ size, c = ch - 'a', p;
st[cur].len = st[last].len + 1;
for(p = last; ~p && !st[p].nxt[c]; p = st[p].link)
st[p].nxt[c] = cur;
if(p == -1)
st[cur].link = root;
else{
int q = st[p].nxt[c];
if(st[p].len + 1 == st[q].len)
st[cur].link = q;
else{
int clone = ++ size;
st[clone] = st[q];
st[clone].len = st[p].len + 1;
for(; p != -1 && st[p].nxt[c] == q; p = st[p].link)
st[p].nxt[c] = clone;
st[cur].link = st[q].link = clone;
}
}
last = cur;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("pow.in","r",stdin);
freopen("pow.out","w",stdout);
#endif
scanf("%d", &n);
for(int i = 0; i < n; i ++)
scanf("%s", s[i] + 1);
int m = strlen(s[0] + 1);
init();
for(int i = 1; i <= m; i ++)
Extend(s[0][i]);
for(int i = 1; i < n; i ++){
int step = 0;
int N = strlen(s[i] + 1), now = root;
for(int j = 1; j <= N; j ++){
int c = s[i][j] - 'a';
if(st[now].nxt[c])now = st[now].nxt[c], step ++;
else{
for(; ~now && st[now].nxt[c] == 0; now = st[now].link);
if(now == -1)now = root, step = 0;
else step = st[now].len + 1, now = st[now].nxt[c];//here!
}
st[now].mx[i] = max(st[now].mx[i], step);
}
}
static int w[maxn << 1], t[maxn << 1];
memset(w, 0, sizeof w);
for(int i = 1; i <= size; i ++)
w[st[i].len] ++;
for(int i = 2; i <= size; i ++)
w[i] += w[i - 1];
for(int i = size; i >= 1; i --)
t[w[st[i].len] --] = i;
for(int i = size; i >= 1; i --){
for(int j = 1; j < n; j ++){
int &ans = st[st[t[i]].link].mx[j];
ans = max(ans, st[t[i]].mx[j]);
}
}
int ans = 0;
for(int i = 1; i <= size; i ++){
int mn = st[i].len;
for(int j = 1; j < n; j ++)
mn = min(mn, st[i].mx[j]);
ans = max(ans, mn);
}
printf("%d\n", ans);
return 0;
}