kuangbin专题十六 KMP&&扩展KMP HDU2328 Corporate Identity
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.
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.
InputThe 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.OutputFor 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 0Sample Output
abb IDENTITY LOST
跟前面几道题都是类似的。枚举第一个串的所有后缀,然后和其余字符串匹配。得到的公共最小取最大。不断更新答案。
1 #include<stdio.h> 2 #include<iostream> 3 #include<string> 4 #include<vector> 5 #include<algorithm> 6 using namespace std; 7 int n,Next[210]; 8 vector<string> t; 9 string s,ans; 10 11 void prekmp(string s) { 12 int len=s.size(); 13 int i,j; 14 j=Next[0]=-1; 15 i=0; 16 while(i<len) { 17 while(j!=-1&&s[i]!=s[j]) j=Next[j]; 18 Next[++i]=++j; 19 } 20 } 21 22 int kmp(string t,string p) { 23 int len=t.size(); 24 int i,j,res=-1; 25 i=j=0; 26 while(i<len) { 27 while(j!=-1&&t[i]!=p[j]) j=Next[j]; 28 ++i;++j; 29 res=max(res,j); 30 } 31 return res; 32 } 33 34 int main() { 35 //freopen("in","r",stdin); 36 while(~scanf("%d",&n)&&n) { 37 for(int i=0;i<n;i++) { 38 cin>>s; 39 t.push_back(s); 40 } 41 string str=t[0],tempstr; 42 int maxx=-1,res; 43 int len=str.size(); 44 for(int i=0;i<len;i++) { 45 tempstr=str.substr(i,len-i); 46 prekmp(tempstr); 47 res=210; 48 for(int j=1;j<t.size();j++) { 49 res=min(kmp(t[j],tempstr),res); 50 } 51 if(res>=maxx&&res) { 52 if(res==maxx) { 53 string tempstr1=tempstr.substr(0,maxx); 54 if(tempstr1<ans) 55 ans=tempstr1; 56 } else { 57 maxx=res; 58 ans=tempstr.substr(0,maxx); 59 } 60 } 61 } 62 if(ans.size()) cout<<ans<<endl; 63 else cout<<"IDENTITY LOST"<<endl; 64 ans.clear(); 65 t.clear(); 66 } 67 }
埋骨何须桑梓地,人生无处不青山