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.

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
0

Sample Output

abb
IDENTITY LOST
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include <sstream>
#include<string>
#include<cstring>
#include<list>
using namespace std;
#define MAXN 202
#define INF 0x3f3f3f3f
#define N 4002
typedef long long LL;
/*
枚举第一个串的所有子串 找到就停止
*/
char s[N][MAXN];
int Next[MAXN];
void kmp_pre(char t[])
{
int m = strlen(t);
int j,k;
j = ;k = Next[] = -;
while(j<m)
{
if(k==-||t[j]==t[k])
Next[++j] = ++k;
else
k = Next[k];
}
}
bool KMP(char t[],char s[])
{
int n = strlen(s),m=strlen(t);
int i,j;
j = ;
for(i=;i<n;i++)
{
while(j>&&s[i]!=t[j])
j = Next[j];
if(s[i]==t[j])
j++;
if(j>=m)
return true;
}
return false;
}
int main()
{
int n;
while(scanf("%d",&n),n)
{
int len = INF,k=-;
for(int i=;i<n;i++)
{
scanf("%s",s[i]);
if(len>strlen(s[i]))
{
len = strlen(s[i]);
k = i;
}
}
bool f = false;
char ans[MAXN];
for(int l=len;l>;l--)
{
for(int i=;i+l<=len;i++)
{
char t[MAXN] = {};
strncpy(t,s[k]+i,l);
kmp_pre(t);
int j;
//cout<<t<<endl;
for(j=;j<n;j++)
{
if(j==k) continue;
if(KMP(t,s[j]))
continue;
else
break;
}
if(j==n)
{
if(!f)
{
strcpy(ans,t);
f = true;
}
else
{
if(strcmp(ans,t)>)
strcpy(ans,t);
}
}
if(f&&(i+l==len))
{
printf("%s\n",ans);
i = INF;
l = -;
break;
}
}
}
if(!f)
printf("IDENTITY LOST\n");
}
}

只需要枚举所有后缀,然后在其他串中找最长相同前缀即可

#include <stdio.h>
#include <string.h> const int MAX_N = ;
const int MAX_CHS = ;
const int ARR_SIZE = ;
int N;
char res[MAX_CHS], dict[MAX_N][MAX_CHS];
short next[MAX_CHS]; inline int min(int a, int b) { return a < b ? a : b; }
inline int max(int a, int b) { return a > b ? a : b; } void getNext(char *chs, int len)
{
memset(next, , sizeof(short)*len);
for (int i = , j = ; i < len; )
{
if (chs[i] == chs[j]) next[i++] = ++j;
else if (j > ) j = next[j-];
else i++;
}
} int getLogestPre(char *chs, int len)
{
getNext(chs, len);
for (int i = ; i < N; i++)
{
char *p = dict[i];
int j = , tmp = ;
for (; *p && j < len; )
{
if (*p == chs[j])
{
p++, j++;
tmp = max(tmp, j);
}
else if (j > ) j = next[j-];
else p++;
}
len = tmp;
}
return len;
} int main()
{
while (scanf("%d", &N) && N)
{
getchar(); // don't forget to get rid of '\'
for (int i = ; i < N; i++)
{
gets(dict[i]);
}
int len = strlen(dict[]), ans = , pos = ;
for (int i = ; i < len; i++)
{
int tmp = getLogestPre(dict[]+i, len-i);
if (tmp >= ans)
{
if (tmp > ans)
{
ans = tmp;
pos = i;
}
else
{
bool smaller = true;
for (int t = ; t < ans; t++)
{
if (dict[][pos+t] > dict[][i+t]) break;
else if (dict[][pos+t] < dict[][i+t])
{
smaller = false;
break;
}
}
if (smaller) pos = i;
}
}
}
if (ans)
{
for (int i = ; i < ans; i++)
{
putchar(dict[][pos+i]);
}
putchar('\n');
}
else puts("IDENTITY LOST");
}
return ;
}

最新文章

  1. BigPipe学习研究
  2. 深入了解javascript事件流
  3. jQuery Ajax无刷新操作
  4. linux强大IDE——Geany配置说明
  5. Eclipse formater(google Java 编码规范)
  6. t持久化与集群部署开发详解
  7. HDU 1728 逃离迷宫(DFS经典题,比赛手残写废题)
  8. Python面向对象进阶示例--自定义数据类型
  9. Kafka分区与消费者的关系
  10. 创建nodejs服务
  11. 函数QFileSystemModelPrivate::_q_fileSystemChanged
  12. Thymeleaf相关补充
  13. IDEA复制某个类的包名路径
  14. Spring mvc Json 的正确返回姿势
  15. 用myeclipse自动发布web程序
  16. linux 关于数据库的部分命令
  17. Intersecting Lines(叉积,方程)
  18. 编写简单登陆和注册功能的demo时遇到的问题
  19. LambdaMART简介——基于Ranklib源码(二 Regression Tree训练)
  20. u-boot中的Makefile

热门文章

  1. mysql大数据的分表
  2. Gym - 101972B Arabella Collegiate Programming Contest (2018) B. Updating the Tree 树DFS
  3. 【Kafka】《Kafka权威指南》——从Kafka读取数据
  4. akka设计模式系列(Actor模型)
  5. [转]linux下logrotate 配置和理解
  6. kubernetes installing and using 单机版
  7. MySQL关于存储过程
  8. Laravel5.1学习笔记16 数据库2 查询构造器(这个不用看,不如用EloquentORM)
  9. (转)50道JavaScript基础面试题(附答案)
  10. python编辑csv