刚从NanoApe学来的SAM..
SPOJ LCS 求两串的最长公共子串..
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int Maxn = 250010;
int F[Maxn*2], ch[Maxn*2][26], now, tot, d[Maxn*2];
char s[Maxn]; int len;
int _max ( int x, int y ){ return x > y ? x : y; }
int copy ( int p, int c ){
int x = ++tot, y = ch[p][c];
d[x] = d[p]+1;
for ( int i = 0; i < 26; i ++ ) ch[x][i] = ch[y][i];
F[x] = F[y]; F[y] = x;
while ( ~p && ch[p][c] == y ){ ch[p][c] = x; p = F[p]; }
return x;
}
void add ( int c ){
int p, o;
if ( p = ch[now][c] ){
if ( d[p] != d[now]+1 ) copy ( now, c );
now = ch[now][c];
}
else {
d[o=++tot] = d[now]+1; p = now; now = o;
while ( ~p && !ch[p][c] ) ch[p][c] = o, p = F[p];
F[o] = ~p ? ( d[ch[p][c]] == d[p]+1 ? ch[p][c] : copy ( p, c ) ) : 0;
}
}
int main (){
int i, j, k;
F[0] = -1; now = 0;
scanf ( "%s", s+1 ); len = strlen (s+1);
for ( i = 1; i <= len; i ++ ) add (s[i]-'a');
scanf ( "%s", s+1 ); len = strlen (s+1);
int ans = 0;
int o = 0; now = 0;
for ( i = 1; i <= len; i ++ ){
int c = s[i]-'a';
while ( ~o && !ch[o][c] ){ o = F[o]; now = o ? d[o] : 0; }
if ( o < 0 ) o = now = 0; else o = ch[o][c], now ++;
ans = _max ( ans, now );
}
printf ( "%d\n", ans );
return 0;
}