标签:
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
Source
题意很长,其实就是求一组字符串的最长公共子字符串。
利用KMP可以在O(N)时间内查找一个字符串在另外一个字符串的最长前缀子字符串的特点加速程序。
#include <stdio.h> #include <string.h> const int MAX_N = 4001; const int MAX_L = 201; char sameStr[MAX_L], dict[MAX_N][MAX_L]; int nxTbl[MAX_L]; int len[MAX_N]; template<typename T1, typename T2> inline bool equ(T1 t1, T2 t2) { return (T1)t1 == (T1)t2; } void getNextTble(int *t, char *s, int len) { t[0] = 0; for (int i = 1, j = 0; i < len; )//j 记录最后一个好前缀的下标 { if (s[i] == s[j]) t[i++] = ++j;//++j等于有多少个好前缀 else if (j > 0) j = t[j-1];//取得下一个对比字符的下标 else t[i++] = 0; } } int getLongestPre(char *chs, char *s, int len1, int len2) { int len3 = 0; int i = 0, j = 0; for (; i < len2 && j < len1; ) { if (equ(s[i], chs[j])) { i++, j++; if (j > len3) len3 = j; } else if (j > 0) j = nxTbl[j-1]; else i++; } return len3; } bool smaller(char *s1, char *s2, int L)//lexicographically smaller { for (int i = 0; i < L; i++) { if (s1[i] < s2[i]) return true; else if (s1[i] > s2[i]) return false; } return false; } int main() { int N; while (~scanf("%d\n", &N) && N) { for (int i = 0; i < N; i++) { gets(dict[i]); len[i] = strlen(dict[i]); } char *p = dict[0]; char *pRes = NULL; int L = 0; for (; len[0]; len[0]--, p++) { getNextTble(nxTbl, p, len[0]); int tmp = len[0]; for (int i = 1; i < N && tmp; i++) { tmp = getLongestPre(p, dict[i], tmp, len[i]); } if (tmp > L || equ(tmp, L) && smaller(p, pRes, L)) { L = tmp; pRes = p; } } if (L) { for (int i = 0; i < L; i++) putchar(pRes[i]); putchar('\n'); } else puts("IDENTITY LOST"); } return 0; }
POJ 3450 Corporate Identity 求所有字符的最长公共子串
标签:
原文地址:http://blog.csdn.net/kenden23/article/details/42027373