Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 5996 | Accepted: 2189 |
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
abb IDENTITY LOST
题意:
给定N个字符串,寻找最长的公共字串,如果长度相同,则输出字典序最小的那个。
找其中一个字符串,枚举它的所有的字串,然后,逐个kmp比较.......相当暴力,可二分优化。
#include <cstdio> #include <cmath> #include <iostream> #include <cstring> #include <string> #include <algorithm> using namespace std; char str[4111][221]; int next[221]; void getnext(char *t) { int i = 0, j = -1; int len = strlen(t); next[0] = -1; while (i < len){ if (t[i] == t[j] || j == -1){ i++; j++; next[i] = j; } else j = next[j]; } } int kmp(char *s, char *t) { int lens = strlen(s); int lena = strlen(t); int i = 0, j = 0; while (i < lens && j < lena){ if (s[i] == t[j] || j == -1){ i++; j++; } else j = next[j]; } if (j < lena) return -1; return i - lena; } int main() { int n, len; while (cin >> n && n){ char tmp[222]; int minn = 111111; for (int i = 0; i < n; i++){ scanf("%s", str[i]); len = strlen(str[i]); if (minn > len){ minn = len; strcpy(tmp, str[i]); } } len = strlen(tmp); char p[222]; char final[222] = {0}; int ans = 0; for (int i = 1; i <= len; i++){ //枚举所有的字串 int cnt; for (int j = 0; j + i <= len; j++){ cnt = 0; strncpy(p, tmp + j, i); p[i] = '\0'; getnext(p); for (int k = 0; k < n; k++){ //逐个比较 if (kmp(str[k],p) != -1){ cnt++; } else break; } if (cnt == n){ ans++; if (strlen(final) < strlen(p)) strcpy(final, p); else if (strcmp(final, p) > 0) strcpy(final, p); } } } if (ans == 0) printf("IDENTITY LOST\n"); else printf("%s\n", final); } return 0; }