描述
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.
输入
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.输出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.
样例输入
3
aabbaabb
abbababb
bbbbbabb
2
xyz
abc
0
样例输出
abb
IDENTITY LOST
来源
CTU Open 2007
1 #include <iostream> 2 #include <string.h> 3 #include <algorithm> 4 #include <stack> 5 #include <string> 6 #include <math.h> 7 #include <queue> 8 #include <stdio.h> 9 #include <string.h> 10 #include <vector> 11 #include <fstream> 12 #define maxn 805000 13 #define inf 999999 14 #define cha 127 15 using namespace std; 16 17 int n,allsize; 18 int all[maxn][2], height[maxn], Rank[maxn],wa[maxn],wb[maxn],wv[maxn], Ws[maxn], sa[maxn]; 19 bool covered[4005]; 20 int start; 21 22 void buildHeight() { 23 int i, j, k; 24 for (int i = 0; i < allsize; i++) 25 Rank[sa[i]] = i; 26 for (i = k = 0; i < allsize; height[Rank[i++]] = k) 27 for (k ? k-- : 0, j = sa[Rank[i] - 1]; 28 all[i + k][0] == all[j + k][0]; 29 k++ 30 ); 31 } 32 33 void buildSa() { 34 int i, j, p, *pm = wa, *k2sa = wb, *t, m = cha + n; 35 for (i = 0; i < m; i++)Ws[i] = 0; 36 for (i = 0; i < allsize; i++)Ws[pm[i] = all[i][0]]++; 37 for (i = 1; i < m; i++)Ws[i] += Ws[i - 1]; 38 for (i = allsize - 1; i >= 0; i--)sa[--Ws[pm[i]]] = i; 39 for (j = p = 1; p < allsize; j <<= 1, m = p) { 40 for (p = 0, i = allsize - j; i < allsize; i++)k2sa[p++] = i; 41 for (i = 0; i < allsize; i++) 42 if (sa[i] >= j)k2sa[p++] = sa[i] - j; 43 for (i = 0; i < m; i++)Ws[i] = 0; 44 for (i = 0; i < allsize; i++)Ws[wv[i] = pm[k2sa[i]]]++; 45 for (i = 1; i < m; i++)Ws[i] += Ws[i - 1]; 46 for (i = allsize - 1; i >= 0; i--)sa[--Ws[wv[i]]] = k2sa[i]; 47 for (t = pm, pm = k2sa, k2sa = t, pm[sa[0]] = 0, p = i = 1; i < allsize; i++) { 48 int a = sa[i - 1], b = sa[i]; 49 if (k2sa[a] == k2sa[b] && k2sa[a + j] == k2sa[b + j]) 50 pm[sa[i]] = p - 1; 51 else 52 pm[sa[i]] = p++; 53 } 54 } 55 return; 56 } 57 58 bool coveredisfull() { 59 for (int i = 1; i <= n; i++) 60 if (covered[i] == false) 61 return false; 62 return true; 63 } 64 65 bool find(int l) { 66 bool flag = false; 67 for (int i = 1; i <= allsize - 1; i++) { 68 if (height[i] >= l && flag == false) { 69 flag = true; 70 covered[all[sa[i]][1]] = true; 71 covered[all[sa[i - 1]][1]] = true; 72 } 73 else if (height[i] >= l && flag == true) 74 { 75 covered[all[sa[i]][1]] = true; 76 covered[all[sa[i - 1]][1]] = true; 77 } 78 else if (height[i] < l&&flag == true) { 79 if (coveredisfull()) 80 { 81 start = sa[i-1]; 82 return true; 83 } 84 else 85 memset(covered, 0, sizeof(covered)); 86 flag = false; 87 } 88 } 89 return false; 90 } 91 92 void bisolve() { 93 int s = 0, e = 200; 94 while (s < e) { 95 int mid = (s + e) / 2; 96 if (find(mid)) 97 s = mid; 98 else 99 e = mid-1; 100 memset(covered, 0, sizeof(covered)); 101 if (s == e - 1) 102 { 103 if(find(e)) 104 s = e; 105 break; 106 } 107 memset(covered, 0, sizeof(covered)); 108 } 109 if (s == 0) 110 { 111 printf("IDENTITY LOST "); 112 return; 113 } 114 for (int i = start; i < start + s; i++) 115 printf("%c", (char)all[i][0]); 116 printf(" "); 117 } 118 119 void init() { 120 memset(covered, 0, sizeof(covered)); 121 allsize = 0; 122 char line[205]; 123 for (int i = 1; i <= n; i++) { 124 scanf("%s", line); 125 for (int j = 0; line[j]; j++) { 126 all[allsize][1] = i; 127 all[allsize++][0] = line[j]; 128 } 129 all[allsize++][0] = cha + i; 130 } 131 all[allsize-1][0] = 0; 132 buildSa(); 133 buildHeight(); 134 bisolve(); 135 } 136 137 int main() 138 { 139 while(scanf("%d",&n)&&n) 140 init(); 141 return 0; 142 }
太恐怖了……后缀数组……
变量真的看到眼瞎……
真的能背出来吗ssfd
思路
把所有字符串看做整数数组用不同的不可能在字符串中出现的值相隔开
然后利用最大公共前缀数组,用二分法求出最长的公共前缀长度使得有名次连续的后缀之间的公共前缀不比它短(好拗口……)并且这些名次连续的后缀在每个字符串中都有分布
记得顺便记一下这个公共前缀的下标
思路不难,最难的果然是算法本身……死亡循环