似乎也是例题?
找两个字符串的最长公共子串。
用间隔符把两个字符串拼起来,然后在height数组里挑相邻两个后缀不在同一个字符串的最大值即可。
(为什么要把cnt开到15w才不RE)因为在倍增过程中字符集会增大,所以大小最好和字符串长度一个数量级...
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 200005, M = 30;
int sa[maxn], rank[maxn], height[maxn];
int wa[maxn], wb[maxn], wv[maxn], cnt[maxn];
void SA(int *r, int n, int m) {
int *x = wa, *y = wb;
for(int i = 0; i < m; i++) cnt[i] = 0;
for(int i = 0; i < n; i++) cnt[x[i] = r[i]]++;
for(int i = 1; i < m; i++) cnt[i] += cnt[i - 1];
for(int i = n - 1; i >= 0; i--) sa[--cnt[x[i]]] = i;
for(int j = 1; j < n; j <<= 1) {
int p = 0;
for(int i = n - j; i < n; i++) y[p++] = i;
for(int i = 0; i < n; i++) if(sa[i] >= j) y[p++] = sa[i] - j;
for(int i = 0; i < n; i++) wv[i] = x[y[i]];
for(int i = 0; i < m; i++) cnt[i] = 0;
for(int i = 0; i < n; i++) cnt[wv[i]]++;
for(int i = 1; i < m; i++) cnt[i] += cnt[i - 1];
for(int i = n - 1; i >= 0; i--) sa[--cnt[wv[i]]] = y[i];
swap(x, y);
p = 1; x[sa[0]] = 0;
for(int i = 1; i < n; i++)
x[sa[i]] = y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + j] == y[sa[i] + j] ? p - 1 : p++;
if(p >= n) break;
m = p;
}
}
void calcHeight(int *r, int n) {
int i, j, k;
for(i = j = k = 0; i < n; height[rank[i++]] = k)
for(k ? k-- : 0, j = sa[rank[i] - 1]; r[i + k] == r[j + k]; k++);
}
int n, s[maxn];
int main() {
ios::sync_with_stdio(false);
string str1, str2; cin >> str1 >> str2;
int len1 = str1.size(), len2 = str2.size(); n = len1 + len2 + 1;
for(int i = 0; i < len1; i++) s[i] = str1[i] - 'a' + 1; s[len1] = 28;
for(int i = 0; i < len2; i++) s[len1 + 1 + i] = str2[i] - 'a' + 1; s[n] = 0;
SA(s, n + 1, M);
for(int i = 0; i <= n; i++) rank[sa[i]] = i;
calcHeight(s, n);
int __max = -1;
for(int i = 1; i < n; i++) if(height[i] > __max) {
if(0 <= sa[i - 1] && sa[i - 1] < len1 && len1 < sa[i]) __max = height[i];
if(0 <= sa[i] && sa[i] < len1 && len1 < sa[i - 1]) __max = height[i];
}
printf("%d\n", __max);
return 0;
}