一个串建后缀自动机, 其他串在上面跑, 然后用当前串跑的去更新全部
---------------------------------------------------------------------------
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 10009;
const int n = 26;
char s[maxn];
struct Node {
Node *fa, *ch[n];
int len, mx, ans;
} pool[maxn], *pt, *Root, *Last;
Node* newNode(int l) {
pt->fa = NULL;
pt->ans = pt->len = l;
memset(pt->ch, 0, sizeof pt->ch);
return pt++;
}
void InitSAM() {
pt = pool;
Last = Root = newNode(0);
}
void Extend(int c) {
Node *p = Last, *np = newNode(p->len + 1);
for(; p && !p->ch[c]; p = p->fa)
p->ch[c] = np;
if(!p)
np->fa = Root;
else {
Node *q = p->ch[c];
if(q->len == p->len + 1)
np->fa = q;
else {
Node *nq = newNode(p->len + 1);
memcpy(nq->ch, q->ch, sizeof q->ch);
nq->fa = q->fa;
q->fa = np->fa = nq;
for(; p && p->ch[c] == q; p = p->fa)
p->ch[c] = nq;
}
}
Last = np;
}
inline void upd(int &x, int t) {
if(t > x)
x = t;
}
int main() {
InitSAM();
int T; scanf("%d", &T);
scanf("%s", s);
for(int i = 0, n = strlen(s); i < n; i++) Extend(s[i] - 'a');
if(T == 1) {
printf("%d
", strlen(s));
return 0;
}
while(--T) {
for(Node *o = pt; o-- != pool; ) o->mx = 0;
scanf("%s", s);
Node *t = Root;
for(int i = 0, n = strlen(s), lth = 0; i < n; i++) {
int c = s[i] - 'a';
if(t->ch[c])
upd((t = t->ch[c])->mx,++lth);
else {
for(; t && !t->ch[c]; t = t->fa);
if(!t) {
lth = 0; t = Root;
} else {
lth = t->len + 1;
upd((t = t->ch[c])->mx, lth);
}
}
}
for(Node *o = pt; o-- != pool; )
if(o->fa) upd(o->fa->mx, o->mx);
for(Node *o = pt; o-- != pool; )
o->ans = min(o->ans, o->mx);
}
int ans = 0;
for(Node *o = pt; o-- != pool; upd(ans, o->ans));
printf("%d
", ans);
return 0;
}
---------------------------------------------------------------------------
2946: [Poi2000]公共串
Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 327 Solved: 143
[Submit][Status][Discuss]
Description
给出几个由小写字母构成的单词,求它们最长的公共子串的长度。
任务:
l 读入单词
l 计算最长公共子串的长度
l 输出结果
Input
文件的第一行是整数 n,1<=n<=5,表示单词的数量。接下来n行每行一个单词,只由小写字母组成,单词的长度至少为1,最大为2000。
Output
仅一行,一个整数,最长公共子串的长度。
Sample Input
3
abcb
bca
acbc
abcb
bca
acbc
Sample Output
2