POJ 3450 Corporate Identity 求所有字符的最长公共子串
来源:互联网 发布:淘宝客服面试基本问题 编辑:程序博客网 时间:2024/06/07 07:26
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
3aabbaabbabbababbbbbbbabb2xyzabc0
Sample Output
abbIDENTITY 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 求所有字符的最长公共子串
- poj 3450 Corporate Identity (KMP+最长公共子串)
- poj 3450 Corporate Identity 【KMP】【求公共子串】
- POJ 3450 Corporate Identity(kmp求多个字符串的最长公共子串)
- POJ 3450 Corporate Identity(KMP:最长连续公共子序列)
- POJ3450 Corporate Identity 最长公共子串
- POJ 3450--->Corporate Identity(后缀数组求多个字符串的公共子串)
- Poj 3080 Blue Jeans + Hdu 2328 Corporate Identity (后缀数组 字典序最小的最长公共子串)
- POJ3450 Corporate Identity(kmp,最长公共子串)
- poj 3450 Corporate Identity(后缀数组 多个字符串的最长公共字串)
- HDU 2328 Corporate Identity(Trie·最长公共子串)
- [poj 3450]Corporate Identity[暴力枚举子串]
- poj 3450 Corporate Identity
- POJ-3450-Corporate Identity
- POJ 3450 Corporate Identity
- POJ 3450 Corporate Identity
- poj 3450 Corporate Identity
- POJ 3450-Corporate Identity
- 在Windows下C++获得程序路径
- 黑马程序员——Java语言基础——06.集合框架(2)泛型和Map集合
- 内存管理1
- easings缓动函数速查 贝塞尔曲线实例
- 在eclipse中开发hadoop MapReduce
- POJ 3450 Corporate Identity 求所有字符的最长公共子串
- Protocol 协议相关定义和操作方法
- 博客都变味了!
- Java读取文件
- 自己编的MFC应用程序可以在没有安装编译器的电脑上运行了
- 用jquery获取复选框属性值
- Category 类别相关定义和操作方法
- Spark1.1.1官网文档翻译2快速开始
- Hibernate, Spring, Struts