SPOJ - PHRASES
题意:
给n个字符串,求出最长的子串。使得子串在每个字符串中不重叠地至少出现2次。输出子串长度。
题解:
用后缀数组求出height数组,之后二分答案。check时对height数组进行分组,并维护每个字符串的最前和最后位置。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <bits/stdc++.h> using namespace std; const int N = 1e5+20; int t, n, k, len; int vis[N], val[15][2]; char s[N]; int r[N]; int sa[N], t1[N], t2[N], c[N], height[N], rank[N]; void SA(int *r, int n, int m) { int *x = t1, *y = t2; for(int i = 0; i < m; i++) c[i] = 0; for(int i = 0; i < n; i++) c[x[i] = r[i]]++; for(int i = 0; i < m; i++) c[i] += c[i-1]; for(int i = n-1; i >= 0; i--) sa[--c[x[i]]] = i; for(int k = 1; k <= n; k <<= 1) { int p = 0; for(int i = n-k; i < n; i++) y[p++] = i; for(int i = 0; i < n; i++) if(sa[i] >= k) y[p++] = sa[i]-k; for(int i = 0; i < m; i++) c[i] = 0; for(int i = 0; i < n; i++) c[x[y[i]]]++; for(int i = 0; i < m; i++) c[i] += c[i-1]; for(int i = n-1; i >= 0; i--) sa[--c[x[y[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]+k] == y[sa[i]+k] ? p-1 : p++; if(p >= n) break; m = p; } int k = 0, j; for(int i = 0; i < n; i++) rank[sa[i]] = i; for(int i = 0; i < n; height[rank[i++]] = k) for(k ? k--:0, j = sa[rank[i]-1]; r[i+k] == r[j+k]; k++); } int add(int x, int v) { int p = vis[sa[x]], c = 0; if(val[p][0] == -1 && val[p][1] == -1) { val[p][0] = val[p][1] = sa[x]; c++; } else { if(sa[x] < val[p][0]) { if(val[p][1] - val[p][0] < v && val[p][1] - sa[x] >= v) c++; val[p][0] = sa[x]; } else if(sa[x] > val[p][1]) { if(val[p][1] - val[p][0] < v && sa[x] - val[p][0] >= v) c++; val[p][1] = sa[x]; } } return c; } bool check(int x) { int cnt = 0; int l = n; while(l < len) { if(height[l] < x) { memset(val, -1, sizeof(val)); cnt = 0; cnt += add(l, x); } else cnt += add(l, x); l++; if(cnt == 2*n) return true; } return false; } int main() { scanf("%d", &t); while(t--) { len = 0; scanf("%d", &n); memset(vis, 0, sizeof(vis)); for(int i = 1; i <= n; i++) { scanf("%s", s); k = strlen(s); for(int j = 0; j < k; j++) r[len++] = s[j]-'a'+11; vis[len] = 1; r[len++] = i; } for(int i = 1; i < len; i++) vis[i] += vis[i-1]; SA(r, len, 37); int l = 1, r = 1e5; while(l <= r) { int mid = l+r>>1; if(check(mid)) l = mid+1; else r = mid-1; } printf("%d\n", r); } }