后缀自动机
/** @xigua */ #include <cstdio> #include <cmath> #include <iostream> #include <algorithm> #include <vector> #include <stack> #include <cstring> #include <queue> #include <set> #include <string> #include <map> #include <climits> #define PI acos(-1) using namespace std; typedef long long ll; typedef double db; const int maxn = 250005 + 5; const int mod = 1e9 + 7; const int INF = 1e8 + 5; const ll inf = 1e15 + 5; const db eps = 1e-6; char s[maxn]; int Min[maxn<<1], Max[maxn<<1]; int ch[maxn<<1][26]; int fa[maxn<<1]; int len[maxn<<1]; int cnt, last; void init() { memset(ch, 0, sizeof(ch)); memset(fa, 0, sizeof(fa)); last=cnt=1; } void add(int c) { int p = last, np = last = ++cnt; len[np] = len[p] + 1; while(!ch[p][c] && p) { ch[p][c] = np; p = fa[p]; } if(p == 0) fa[np] = 1; else { int q = ch[p][c]; if(len[p] + 1 == len[q]) { fa[np] = q; } else { int nq = ++cnt; len[nq] = len[p] + 1; memcpy(ch[nq], ch[q], sizeof ch[q]); fa[nq] = fa[q]; fa[q] = fa[np] = nq; while(ch[p][c] == q && p) { ch[p][c] = nq; p = fa[p]; } } } } void solve() { scanf("%s", s); int lenn = strlen(s); init(); for(int i = 0; i < lenn; i++) { add(s[i] - ‘a‘); } scanf("%s", s); lenn = strlen(s); int p = 1; int ans = 0; int c = 0; for(int i = 0; i < lenn; i++) { if(ch[p][s[i] - ‘a‘]) { p = ch[p][s[i] - ‘a‘]; c++; } else { while(p&&!ch[p][s[i]-‘a‘]) p=fa[p]; if (!p) c=0, p=1; else c=len[p]+1, p=ch[p][s[i]-‘a‘]; } ans = max(ans, c); } printf("%d\n", ans); } int main() { int t = 1; //freopen("in.txt", "r", stdin); // scanf("%d", &t); while(t--) solve(); return 0; }
时间: 2024-11-10 06:58:53