Description
Beside other services, ACM helps companies to clearly state their “corporate identity”, which includes company logo but also other signs, like trademarks. One of such companies is Internet Building Masters (IBM), which has recently asked ACM for a help with their new identity. IBM do not want to change their existing logos and trademarks completely, because their customers are used to the old ones. Therefore, ACM will only change existing trademarks instead of creating new ones.
After several other proposals, it was decided to take all existing trademarks and find the longest common sequence of letters that is contained in all of them. This sequence will be graphically emphasized to form a new logo. Then, the old trademarks may still be used while showing the new identity.
Your task is to find such a sequence.
Input
The input contains several tasks. Each task begins with a line containing a positive integer N, the number of trademarks (2 ≤ N ≤ 4000). The number is followed by N lines, each containing one trademark. Trademarks will be composed only from lowercase letters, the length of each trademark will be at least 1 and at most 200 characters.
After the last trademark, the next task begins. The last task is followed by a line containing zero.
Output
For each task, output a single line containing the longest string contained as a substring in all trademarks. If there are several strings of the same length, print the one that is lexicographically smallest. If there is no such non-empty string, output the words “IDENTITY LOST” instead.
Sample Input
3 aabbaabb abbababb bbbbbabb 2 xyz abc 0
Sample Output
abbIDENTITY LOST
问最长公共子串,后缀数组+二分
#include<set> #include<map> #include<ctime> #include<cmath> #include<stack> #include<queue> #include<bitset> #include<cstdio> #include<string> #include<cstring> #include<iostream> #include<algorithm> #include<functional> #define rep(i,j,k) for (int i = j; i <= k; i++) #define per(i,j,k) for (int i = j; i >= k; i--) #define lson x << 1, l, mid #define rson x << 1 | 1, mid + 1, r #define fi first #define se second #define mp(i,j) make_pair(i,j) #define pii pair<int,int> using namespace std; typedef long long LL; const int low(int x) { return x&-x; } const double eps = 1e-8; const int INF = 0x7FFFFFFF; const int mod = 1e9 + 7; const int N = 2e5 + 10; const int read() { char ch = getchar(); while (ch<'0' || ch>'9') ch = getchar(); int x = ch - '0'; while ((ch = getchar()) >= '0'&&ch <= '9') x = x * 10 + ch - '0'; return x; } int T, m; struct Sa { char c[N]; int s[N]; int rk[2][N], sa[N], h[N], w[N], now, n; int rmq[N][20], lg[N], bel[N]; int f[N], cnt; bool GetS() { m = read(); if (!m) return false; int q = 300; n = 0; rep(i, 0, m - 1) { scanf("%s", c); for (int j = 0; c[j]; j++) s[++n] = c[j], bel[n] = i; s[++n] = q++; bel[n] = m; } --n; return true; } void getsa(int z, int &m) { int x = now, y = now ^= 1; rep(i, 1, z) rk[y][i] = n - i + 1; for (int i = 1, j = z; i <= n; i++) if (sa[i] > z) rk[y][++j] = sa[i] - z; rep(i, 1, m) w[i] = 0; rep(i, 1, n) w[rk[x][rk[y][i]]]++; rep(i, 1, m) w[i] += w[i - 1]; per(i, n, 1) sa[w[rk[x][rk[y][i]]]--] = rk[y][i]; for (int i = m = 1; i <= n; i++) { int *a = rk[x] + sa[i], *b = rk[x] + sa[i - 1]; rk[y][sa[i]] = *a == *b&&*(a + z) == *(b + z) ? m - 1 : m++; } } void getsa(int m) { //n = strlen(s + 1); rk[1][0] = now = sa[0] = s[0] = 0; rep(i, 1, m) w[i] = 0; rep(i, 1, n) w[s[i]]++; rep(i, 1, m) rk[1][i] = rk[1][i - 1] + (bool)w[i]; rep(i, 1, m) w[i] += w[i - 1]; rep(i, 1, n) rk[0][i] = rk[1][s[i]]; rep(i, 1, n) sa[w[s[i]]--] = i; rk[1][n + 1] = rk[0][n + 1] = 0; //多组的时候容易出bug for (int x = 1, y = rk[1][m]; x <= n && y <= n; x <<= 1) getsa(x, y); for (int i = 1, j = 0; i <= n; h[rk[now][i++]] = j ? j-- : j) { if (rk[now][i] == 1) continue; int k = n - max(sa[rk[now][i] - 1], i); while (j <= k && s[sa[rk[now][i] - 1] + j] == s[i + j]) ++j; } } void getrmq() { h[n + 1] = h[1] = lg[1] = 0; rep(i, 2, n) rmq[i][0] = h[i], lg[i] = lg[i >> 1] + 1; for (int i = 1; (1 << i) <= n; i++) { rep(j, 2, n) { if (j + (1 << i) > n + 1) break; rmq[j][i] = min(rmq[j][i - 1], rmq[j + (1 << i - 1)][i - 1]); } } } int lcp(int x, int y) { int l = min(rk[now][x], rk[now][y]) + 1, r = max(rk[now][x], rk[now][y]); return min(rmq[l][lg[r - l + 1]], rmq[r - (1 << lg[r - l + 1]) + 1][lg[r - l + 1]]); } bool check(int x, int &t) { int sum = 0; rep(i, 2, n) { if (h[i] >= x) { if (f[bel[sa[i]]] < cnt) sum++, f[bel[sa[i]]] = cnt; if (f[bel[sa[i - 1]]] < cnt) sum++, f[bel[sa[i - 1]]] = cnt; } else { if (sum == m) { t = sa[i - 1]; return true; } else ++cnt, sum = 0; } } return sum == m; } void work() { getsa(5000); if (m == 1) { puts(c); return; } int l = 1, r = n, t = cnt = 1; rep(i, 0, m) f[i] = 0; while (l <= r) { if (check(l + r >> 1, t)) l = (l + r >> 1) + 1; else r = (l + r >> 1) - 1; } if (!r) { printf("IDENTITY LOST\n"); return; } check(r, t); rep(i, 1, r) printf("%c", (char)s[t + i - 1]); putchar(10); } }sa; int main() { while (sa.GetS()) sa.work(); return 0; }