poj Corporate Identity 3450 (KMP&&枚举) 好题
2015-10-25 10:51
211 查看
Corporate Identity
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
Sample Output
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 5804 | Accepted: 2108 |
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个字符串让你找出他们中的最长公共子串。如果最长公共子串有多个,则输出字典序小的那个子串。
//看了大神的代码一早上还是有点迷糊,先贴上代码。。
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; char res[210]; char dict[4010][210]; int p[210]; int n; void getp(char s[],int l) { memset(p,0,sizeof(p)); for(int i=1,j=0;i<l;) { if(s[i]==s[j]) { j++; p[i]=j; i++; } else if(j>0) j=p[j-1]; else i++; } } int kmp(char s[],int l) { int i,j; getp(s,l); for(i=1;i<n;i++) { char *q=dict[i]; int j=0; int tmp=0; for(;*q&&j<l;) { if(*q==s[j]) { q++; j++; tmp=max(tmp,j); } else if(j>0) j=p[j-1]; else q++; } l=tmp; } return l; } int main() { while(scanf("%d",&n),n) { getchar(); for(int i=0;i<n;i++) gets(dict[i]); int l=strlen(dict[0]); int ans=0; int pos=0; for(int i=0;i<l;i++) { int tmp=kmp(dict[0]+i,l-i); if(tmp>=ans) { if(tmp>ans) { ans=tmp; pos=i; } else { bool sma=true; for(int t=0;t<ans;t++) { if(dict[0][pos+t]>dict[0][i+t]) break; else if(dict[0][pos+t]<dict[0][i+t]) { sma=false; break; } } if(sma) pos=i; } } } if(ans) { for(int i=0;i<ans;i++) printf("%c",dict[0][pos+i]); printf("\n"); } else printf("IDENTITY LOST\n"); } return 0; }
相关文章推荐
- 第一次机房收费系统之导出到Excel
- 《javascript语言精粹》读书笔记 Item2 对象
- View动画另一些使用场景--LayoutAnimation和Activity之间的切换效果
- 【UER #4】被删除的黑白树
- swift学习——点点滴滴——5~打印遍历显示optional
- 《javascript语言精粹》读书笔记 Item2 对象
- 金融英语词汇整理
- 腾讯的一个移动端测试小工具GT
- 数据结构实践项目——树和二叉树(1)
- bzoj3391: [Usaco2004 Dec]Tree Cutting网络破坏
- 1、web应用安全
- 欢迎使用CSDN-markdown编辑器
- UVa 10655 - Contemplation! Algebra
- 二、计算机程序
- 深刻理解 js对象间的赋值
- LEETCODE-Rectangle Area
- 机房收费系统之用例图
- MATLAB新的统计数据类型Table
- Xcode7.1 App上线提交不了问题汇总
- vagrant virtualbox VM inaccessible解决办法