poj3450之kmp入门
2013-07-08 21:06
465 查看
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: 4087 | Accepted: 1547 |
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
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<queue> #include<algorithm> #include<map> #include<iomanip> #define INF 99999999 using namespace std; const int MAX=4000+10; char s[MAX][210],temp[210],temp2[210]; int next[210]; void Strcpy(char *a,char *b,int S,int T){ int k=0; for(int i=S;i<=T;++i)a[k++]=b[i]; a[k]='\0'; } void get_next(char *a){ int i=-1,j=0,len=strlen(a); next[0]=-1; while(j<len){ if(i == -1 || a[i] == a[j]){ if(a[++i] == a[++j])next[j]=next[i]; else next[j]=i; }else i=next[i]; } return; } bool KMP(char *a,char *b){ int lena=strlen(a),lenb=strlen(b); if(lena>lenb)return false; get_next(a); int i=0,j=0; while(i<lena && j<lenb){ if(i == -1 || a[i] == b[j])++i,++j; else i=next[i]; } if(i>=lena)return true; return false; } int main(){ int n,k; while(cin>>n,n){ for(int i=0;i<n;++i)cin>>s[i]; int len=strlen(s[0]),flag=0; for(int i=len;i>0;--i){ for(int j=0;j+i-1<len;++j){//取长度为i的字符串并且从j开始 Strcpy(temp,s[0],j,j+i-1); for(k=1;k<n;++k){ if(!KMP(temp,s[k]))break; } if(k == n){ if(flag){if(strcmp(temp2,temp)>0)strcpy(temp2,temp);} else strcpy(temp2,temp); flag=true; } } if(flag)break; } if(!flag)cout<<"IDENTITY LOST"<<endl; else cout<<temp2<<endl; } return 0; }
相关文章推荐
- poj 3450 Corporate Identity(二分长度 + 暴力kmp)
- poj--3450 KMP求多个字符串的最长公共子串
- poj 3450 KMP+暴力枚举(类似于3080)
- POJ 3080 Blue Jeans(暴力枚举+KMP 类似POJ 3450)
- poj 3461 Oulipo(简单的kmp入门)
- POJ 3450 Corporate Identity KMP解决问题的方法
- POJ 3450 Corporate Identity (KMP+暴搞)
- POJ 3450 Corporate Identity (KMP+暴搞)
- POJ 3461 Oulipo 【KMP多次匹配,新手入门】
- POJ 2406 入门KMP
- POJ 3450 Corporate Identity (KMP+暴搞)
- POJ 3450--Corporate Identity【KMP && 枚举】
- kmp poj 3450 3461
- poj 3080(3450) KMP(暴力也能过) 多个串的最长公共子串
- [KMP或者暴力]POJ 3450 Corporate Identity
- poj3450 Corporate Identity kmp || 后缀数组
- poj 3450 Corporate Identity (KMP)
- poj3080之KMP入门
- poj kmp入门几题 mark
- poj_3450 Corporate Identity(KMP+枚举)