Corporate Identity
2015-12-29 17:07
351 查看
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
3
aabbaabb
abbababb
bbbbbabb
2
xyz
abc
0
Sample Output
abb
IDENTITY LOST
这个问题就是叫你求出最长的公共字符串,如果求出两个字符串长度长度相等,则按照字典序排序,输出字典序较小的字服串
主要做法就是枚举出第一串字符串所有的子字符串,然后kmp匹配后面的每一串字符串,就可以找到串最长的公共字符串,如果遇到两串字符串长度相等
那么就字典序排序,把字典序较小的留下来
代码如下#include <cstdio>
#include <cstdlib>
#include<cstring>
#include <algorithm>
using namespace std;
char str [20000][20000];
int next1[20000];
char temp[20000];
char sum[20000];
void getnext(char *s1){
int j=0,k=-1;
int len=strlen(s1);
next1[0]=-1;
while(j<len){
if(k==-1||s1[j]==s1[k]){
++j;
++k;
if(s1[j]!=s1[k]) next1[j]=k;
else next1[j]=next1 [k];
}
else k=next1[k];
}
return ;
}
bool kmp(char *s1,char *s2){
int len1=strlen(s2);
int len2=strlen(s1);
getnext(s1);
int i=0,j=0;
while(i<len1){
if(j==-1||s1[j]==s2[i]){
++i;
++j;
}
else j=next1[j];
if(j==len2)
return true;
}
return false;
}
int main()
{
int n;
int i,j,k;
while(scanf("%d",&n)==1&&n){
for( i=0;i<n;i++)
scanf("%s",str[i]);
int len=strlen(str[0]);
memset(sum,'\0',sizeof(sum));
for(i=0;i<len;i++){
int ans=0;
for(j=i;j<len;j++){
temp[ans]=str[0][j];
ans++;
temp[ans]='\0';//注意这里一定要加上,否则出现越界等情况
int flag=1;
for(k=1;k<n;k++){
if(!kmp(temp,str[k])){
flag=0;
break;
}
}
if(flag){
if(strlen(temp)>strlen(sum)){
strcpy(sum,temp);
}
else if(strlen(temp)==strlen(sum)&&strcmp(temp,sum)<0)
strcpy(sum,temp);
}
}
}
if(strlen(sum)>0) printf("%s\n",sum);
else printf("IDENTITY LOST\n");
}
return 0;
}
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
这个问题就是叫你求出最长的公共字符串,如果求出两个字符串长度长度相等,则按照字典序排序,输出字典序较小的字服串
主要做法就是枚举出第一串字符串所有的子字符串,然后kmp匹配后面的每一串字符串,就可以找到串最长的公共字符串,如果遇到两串字符串长度相等
那么就字典序排序,把字典序较小的留下来
代码如下#include <cstdio>
#include <cstdlib>
#include<cstring>
#include <algorithm>
using namespace std;
char str [20000][20000];
int next1[20000];
char temp[20000];
char sum[20000];
void getnext(char *s1){
int j=0,k=-1;
int len=strlen(s1);
next1[0]=-1;
while(j<len){
if(k==-1||s1[j]==s1[k]){
++j;
++k;
if(s1[j]!=s1[k]) next1[j]=k;
else next1[j]=next1 [k];
}
else k=next1[k];
}
return ;
}
bool kmp(char *s1,char *s2){
int len1=strlen(s2);
int len2=strlen(s1);
getnext(s1);
int i=0,j=0;
while(i<len1){
if(j==-1||s1[j]==s2[i]){
++i;
++j;
}
else j=next1[j];
if(j==len2)
return true;
}
return false;
}
int main()
{
int n;
int i,j,k;
while(scanf("%d",&n)==1&&n){
for( i=0;i<n;i++)
scanf("%s",str[i]);
int len=strlen(str[0]);
memset(sum,'\0',sizeof(sum));
for(i=0;i<len;i++){
int ans=0;
for(j=i;j<len;j++){
temp[ans]=str[0][j];
ans++;
temp[ans]='\0';//注意这里一定要加上,否则出现越界等情况
int flag=1;
for(k=1;k<n;k++){
if(!kmp(temp,str[k])){
flag=0;
break;
}
}
if(flag){
if(strlen(temp)>strlen(sum)){
strcpy(sum,temp);
}
else if(strlen(temp)==strlen(sum)&&strcmp(temp,sum)<0)
strcpy(sum,temp);
}
}
}
if(strlen(sum)>0) printf("%s\n",sum);
else printf("IDENTITY LOST\n");
}
return 0;
}
相关文章推荐
- Python中字典(dict)和列表(list)的排序方法实例
- Substrings
- Python - 字典按值(value)排序
- nyoj-366-D的小L
- 全排列实现(递归,去重复,字典排序,stl实现)
- 字典序问题
- Java字典排序
- Python3匿名函数字典排序、生成式与生成器、装饰器简介
- Python排序总结:列表、字典、包含字典的列表、包含元组的列表排序
- NYOJ背包问题
- 使用sorted对列表和字典 排序
- IOS·Swift 集合类型之字典类型排序
- 【算法相关】高频词汇统计系统(二)
- 找出某目录下前N个最大文件
- 安卓中引入第三方汉字转拼音架包的写法及注意事项
- Android数组列表按照字母排序
- Python排序字典
- 字母 && 字典排序
- Python字典排序
- Python字典排序