算是挺简单的。有一些坑点。
我们通过分隔符将字符串们连接起来。
注意分隔符不可以相等,否则在匹配时分隔符可能被算上。
然后我们二分答案长度,进行height分组,判断是否可行。
得到正确的长度后我们再去找子串,只要找到分组就好了,然后随便记录下分组中的一个坐标。
之后将这些坐标按照ranks[i]序排序就好了。
之后输出,每一个输出len长
在二分判断时,我们要注意判断时n的奇偶。最方便的是乘2看是否大于n
代码如下:
#include<iostream> #include<algorithm> #include<set> #include<vector> using namespace std; const int max_n = 1e5 + 200; int a[max_n]; int ranks[max_n], SA[max_n], height[max_n]; int wa[max_n], wb[max_n], wvarr[max_n], wsarr[max_n]; inline int cmp(int* r, int a, int b, int l) { return r[a] == r[b] && r[a + l] == r[b + l]; } inline void get_sa(int* r, int* sa, int n, int m) { //r:原数组 //sa:SA //n:原数组长度 //m:原数组种类数,用于基数排序 ++n; int i, j, p, * x = wa, * y = wb, * t; for (i = 0; i < m; ++i) wsarr[i] = 0; for (i = 0; i < n; ++i) wsarr[x[i] = r[i]]++; for (i = 1; i < m; ++i) wsarr[i] += wsarr[i - 1]; for (i = n - 1; i >= 0; --i) sa[--wsarr[x[i]]] = i; for (j = 1, p = 1; p < n; j <<= 1, m = p) { for (p = 0, i = n - j; i < n; ++i) y[p++] = i; for (i = 0; i < n; ++i) if (sa[i] >= j) y[p++] = sa[i] - j; for (i = 0; i < n; ++i) wvarr[i] = x[y[i]]; for (i = 0; i < m; ++i) wsarr[i] = 0; for (i = 0; i < n; ++i) wsarr[wvarr[i]]++; for (i = 1; i < m; ++i) wsarr[i] += wsarr[i - 1]; for (i = n - 1; i >= 0; --i) sa[--wsarr[wvarr[i]]] = y[i]; for (t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; ++i) x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++; } }//求解高度数组,height[i]指排名 void get_height(int* r, int* sa, int n) { int i, j, k = 0; for (i = 0; i <= n; ++i) ranks[sa[i]] = i; for (i = 0; i < n; height[ranks[i++]] = k) for (k ? k-- : 0, j = sa[ranks[i] - 1]; r[i + k] == r[j + k]; k++); return; } int n; int col[max_n]; bool check(int mid,int tot) { set<int>myset; for (int i = 2;i <= tot;++i) { if (myset.size() * 2 > n)return true; if (height[i] < mid)myset.clear(); else { myset.insert(col[SA[i]]); myset.insert(col[SA[i - 1]]); } }return false; } bool ccmp(int aa, int bb) { return ranks[aa] < ranks[bb]; } int main(){ ios::sync_with_stdio(0); while (cin >> n) { if (n == 0)break; if (n == 1) { string s;cin >> s; cout << s << "\n\n"; continue; } fill(col, col + max_n, -1); int tot = 0;int co = 0; for (int i = 1;i <= n;++i) { string s;cin >> s;++co; for (int j = 0;j < s.size();++j) { a[tot++] = s[j] - 'a' + 1; col[tot - 1] = co; }a[tot++] = 30 + i; }--tot;a[tot] = 0; get_sa(a, SA, tot, 200); get_height(a, SA, tot); int lft = 1;int rght = 1000; while (lft <= rght) { int mid = (lft + rght) >> 1; if (check(mid, tot))lft = mid + 1; else rght = mid - 1; } int len = lft - 1; if (len == 0) { cout << "?\n\n"; continue; }vector<int> ans; set<int> myset;int id = -1; for (int i = 2;i <= tot;++i) { if (height[i] >= len) { id = SA[i]; myset.insert(col[SA[i]]); myset.insert(col[SA[i - 1]]); } else { if (myset.size() * 2 > n) ans.push_back(id); myset.clear(); } }if (myset.size() * 2 > n) ans.push_back(id); sort(ans.begin(), ans.end(), ccmp); for (int i = 0;i < ans.size();++i) { int id = ans[i]; for (int j = 0;j < len;++j) cout << (char)(a[id + j] + 'a' - 1); cout << endl; }cout << endl; } }