??
Test instructions: Given two strings A and B. To find the longest common substring.
Idea: Connect two strings together and cut them with a symbol that doesn't appear in the middle.
So the answer is the height value that satisfies the suffix in a different string and the highest height value
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <iostream > #include <algorithm> #include <vector> #include <map> #include <queue> #include <stack& Gt #include <string> #include <map> #include <set> #define EPS 1e-6 #define LL Long long using namespace std; const int MAXN = 200000 + 200;//const int INF = 0x3f3f3f3f;char str1[100010], str2[100010];struct suffixarray {int S[MAXN]; Original character Array (the last character should be 0, and the preceding character must be not 0) int SA[MAXN]; Suffix array, sa[0] must be n-1, that is, the last character int RANK[MAXN]; Rank array int HEIGHT[MAXN]; Height array int t[maxn], T2[MAXN], C[MAXN]; auxiliary array int n; The number of characters is void clear () {n = 0; memset (SA, 0, sizeof (SA));} M is the maximum character value plus 1.!!! The call needs to be set well before the S and n void Build_sa (int m) {int I, *x = t, *y = t2; for (i = 0; i < m; i++) c[i] = 0; for (i = 0; i < n; i++) c[x[i] = s[i]]++; for (i = 1; i < m; i++) c[i] + = c[i-1]; for (i = n-1; I >= 0; i--) sa[--c[x[i]] = i; for (int k = 1; k <= N; k <<= 1) {int p = 0; for (i = n-k; i < n; i++) y[p++] = i; for (i = 0; i < n; i++) if (Sa[i] >= k) y[p++] = sa[i]-k; for (i = 0; i < m; i++) c[i] = 0; for (i = 0; i < n; i++) c[x[y[i]]]++; for (i = 0; i < m; i++) c[i] + = c[i-1]; for (i = n-1; I >= 0; i--) sa[--c[x[y[i]]] [= Y[i]; Swap (x, y); p = 1; X[sa[0]] = 0; for (i = 1; i < n; i++) x[sa[i] [y[sa[i-1]]==y[sa[i]] && y[sa[i-1]+k]==y[sa[i]+k]? P-1: p++; if (P >= N) break; m = p; }} void Build_height () {int I, j, k = 0; for (i = 0; i < n; i++) rank[sa[i]] = i; for (i = 0; i < n; i++) {if (k) k--; j = Sa[rank[i]-1]; while (s[i+k] = = S[j+k]) k++; Height[rank[i]] = k; }}} sa;void init () {sa.clear (); int len1 = strlen (str1), len2 = strlen (str2); for (int i= 0; i < len1; i++) Sa.s[i] = str1[i]-' a ' + 1;for (int i = 0; i < len2; i++) sa.s[len1+i+1] = str2[i]-' a ' + 1;sa.s[len1] = 27; Sa.s[len1+len2+1] = 0;SA.N = len1 + len2 + 2;sa.build_sa (+); Sa.build_height ();} void Solve () {int ans = 0;int len1 = strlen (str1); for (int i = 1; i < SA.N; i++) {if (Sa.height[i] > Ans) if (sa.sa[i-1] <len1&&sa.sa[i]>len1 | | SA.SA[I]<LEN1&&SA.SA[I-1]>LEN1) ans = sa.height[i]; }cout << ans << endl;} int main () {//freopen ("Input.txt", "R", stdin), while (scanf ("%s%s", str1, str2) = = 2) {init (); Solve ();} return 0;}
POJ 2774 Long long Message (suffix array)