admin管理员组文章数量:1616033
Corporate Identity
HDU - 2328
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 the last trademark, the next task begins. The last task is followed by a line containing zero.
3 aabbaabb abbababb bbbbbabb 2 xyz abc 0
abb IDENTITY LOST
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
int Next[205];
void getNext(string w,int len){
int i = -1,j = 0;
memset(Next,0,sizeof(Next));
Next[0] = -1;
while(j < len){
if(i == -1 || w[i] == w[j]){
i++,j++;
Next[j] = i;
}
else
i = Next[i];
}
}
bool kmp(string w,int m,string s,int n){
int i = 0,j = 0;
getNext(w,m);
while(j < n){
if(i == -1 || w[i] == s[j])
i++,j++;
else
i = Next[i];
if(i >= m){
return true;
}
}
return false;
}
int main(){
int n;
while(~scanf("%d",&n)&&n){
string str[4009];
int i,j,k;
for(i = 0; i < n; i++){
cin >> str[i];
}
bool flag = false;
string ans;
for(i = 0; i < str[0].length(); i++){
for(j = i; j < str[0].length(); j++){
string w = str[0].substr(i,j-i+1);
for(k = 1; k < n; k++){
if(!kmp(w,w.length(),str[k],str[k].length()))
break;
}
if(k >= n){
if(!flag || w.length() > ans.length()){
ans = w;
if(!flag)
flag = true;
}
else{
if(w.length() == ans.length() && wpare(ans)<0){
ans = w;
}
}
}
}
}
if(!flag)
cout << "IDENTITY LOST" << endl;
else
cout << ans << endl;
}
return 0;
}
版权声明:本文标题:Corporate Identity HDU - 2328 (kmp+枚举) 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1728725048a1170625.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论