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 题目大意:给多个字符串,找出最长的并且字典序最小的公共子串.
两种做法:
  (1).使用KMP.每一个子串都是某个后缀的前缀.枚举最短字符串的每一个后缀suff(i),让其他所有的字符串去跟suff(i)做kmp匹配,会得到一个suff(i)的公共前缀pre_suff(i),选一个最长的pre_suff
即为答案.枚举后缀时按照字典序枚举,可以省略掉比较长度相同的pre_suff这一过程.
  (2).使用后缀数组.首先将所有的字符串连接成一条长串,求出height数组之后,二分枚举最长公共子串的长度mid,然后根据height值是否不小于mid将数组分成若干个连续的区间.查看每一个区
间,只要某个区间中的前缀来自所有的字符串,那么存在长度为mid的公共子串.但是我一直TLE. 代码如下(第一种做法):
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int N=4005; int SA[205];
int tSA[205];
int rk[205];
int cnt[205]; bool isSame(int *y,int i,int j,int k,int n)
{
if(y[i]!=y[j]) return false;
if(i+k<n&&j+k>=n) return false;
if(i+k>=n&&j+k<n) return false;
return y[i+k]==y[j+k];
} void buildSA(char* str,int n)
{
int *x=rk;
int *y=tSA;
int m=26;
for(int i=0;i<m;++i) cnt[i]=0;
for(int i=0;i<n;++i) ++cnt[x[i]=(str[i]-'a')];
for(int i=1;i<m;++i) cnt[i]+=cnt[i-1];
for(int i=n-1;i>=0;--i) SA[--cnt[x[i]]]=i; for(int k=1;k<=n;k<<=1){
int p=0;
for(int i=n-k;i<n;++i) y[p++]=i;
for(int i=0;i<n;++i) if(SA[i]>=k) y[p++]=SA[i]-k; for(int i=0;i<m;++i) cnt[i]=0;
for(int i=0;i<n;++i) ++cnt[x[y[i]]];
for(int i=1;i<m;++i) cnt[i]+=cnt[i-1];
for(int i=n-1;i>=0;--i) SA[--cnt[x[y[i]]]]=y[i]; p=1;
swap(x,y);
x[SA[0]]=0;
for(int i=1;i<n;++i)
x[SA[i]]=isSame(y,SA[i],SA[i-1],k,n)?p-1:p++; if(p>=n) break;
m=p;
}
} char tdmks[N][205];
int nxt[205]; void getNext(char* str,int str_len)
{
nxt[0]=nxt[1]=0;
for(int i=1;i<str_len;++i){
int j=nxt[i];
while(j&&str[i]!=str[j]) j=nxt[j];
nxt[i+1]=(str[i]==str[j])?j+1:0;
}
} int match(char* str,int str_len,char* ptr)
{
int ptr_len=strlen(ptr);
int tempLen=0,k=0;
for(int i=0;i<ptr_len;++i){
while(k&&ptr[i]!=str[k]) k=nxt[k];
if(str[k]==ptr[i]){
++k;
tempLen=max(tempLen,k);
}
}
return tempLen;
} int getLongestPre(char* str,int str_len,int cnt_tdmks)
{
getNext(str,str_len);
int long_pre=N;
for(int i=0;i<cnt_tdmks;++i){
long_pre=min(match(str,str_len,tdmks[i]),long_pre);
}
return long_pre;
} int input(int cnt_tdmks)
{
int minLen=N,id_minLen;
for(int i=0;i<cnt_tdmks;++i){
scanf("%s",tdmks[i]);
if(strlen(tdmks[i])<minLen){
minLen=strlen(tdmks[i]);
id_minLen=i;
}
}
return id_minLen;
} void solve(int p,int cnt_tdmks)
{
int m=strlen(tdmks[p]);
buildSA(tdmks[p],m);
int ans_len=0,ans_p;
for(int i=0;i<m;++i){
int len=getLongestPre(tdmks[p]+SA[i],m-SA[i],cnt_tdmks);
if(len>ans_len){
ans_len=len;
ans_p=SA[i];
}
}
if(ans_len){
for(int i=0;i<ans_len;++i)
printf("%c",tdmks[p][ans_p+i]);
printf("\n");
}else{
printf("IDENTITY LOST\n");
}
} int main()
{
//freopen("in.txt","r",stdin);
int n;
while(scanf("%d",&n)&&n)
{
solve(input(n),n);
}
return 0;
}

POJ-3450 Corporate Identity (KMP+后缀数组)的更多相关文章

  1. POJ 3450 Corporate Identity kmp+最长公共子串

    枚举长度最短的字符串的所有子串,再与其他串匹配. #include<cstdio> #include<cstring> #include<algorithm> #i ...

  2. POJ 3450 Corporate Identity KMP解决问题的方法

    这个问题,需要一组字符串求最长公共子,其实灵活运用KMP高速寻求最长前缀. 请注意,意大利愿父亲:按照输出词典的顺序的规定. 另外要提醒的是:它也被用来KMP为了解决这个问题,但是很多人认为KMP使用 ...

  3. poj 3450 Corporate Identity

    题目链接:http://poj.org/problem?id=3450 题目分类:后缀数组 题意:求n个串的最长公共字串(输出字串) //#include<bits/stdc++.h> # ...

  4. POJ3450 Corporate Identity 【后缀数组】

    Corporate Identity Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 7662   Accepted: 264 ...

  5. [HDU2328]Corporate Identity(后缀数组)

    传送门 求 n 个串的字典序最小的最长公共子串. 和 2 个串的处理方法差不多. 把 n 个串拼接在一起,中间连上一个没有出现过的字符防止匹配过界. 求出 height 数组后二分公共子串长度给后缀数 ...

  6. POJ 3450 Corporate Identity(KMP)

    [题目链接] http://poj.org/problem?id=3450 [题目大意] 求k个字符串的最长公共子串,如果有多个答案,则输出字典序最小的. [题解] 我们对第一个串的每一个后缀和其余所 ...

  7. POJ 3450 Corporate Identity (KMP,求公共子串,方法很妙)

    http://blog.sina.com.cn/s/blog_74e20d8901010pwp.html我采用的是方法三. 注意:当长度相同时,取字典序最小的. #include <iostre ...

  8. POJ 3450 Corporate Identity (KMP+暴搞)

    题意: 给定N个字符串,寻找最长的公共字串,如果长度相同,则输出字典序最小的那个. 找其中一个字符串,枚举它的所有的字串,然后,逐个kmp比较.......相当暴力,可二分优化. #include & ...

  9. poj 3518 Corporate Identity 后缀数组->多字符串最长相同连续子串

    题目链接 题意:输入N(2 <= N <= 4000)个长度不超过200的字符串,输出字典序最小的最长公共连续子串; 思路:将所有的字符串中间加上分隔符,注:分隔符只需要和输入的字符不同, ...

随机推荐

  1. 搭建本地yum仓库(centos 7.5)

    1. 保留下载的软件包 [root@zabbix-server01 ~]# vim /etc/yum.conf keepcache=1 #开启后下载的软件都会保存到本地 2.将软件进行下载,并将所有r ...

  2. linux系统安装MongoDB

    1.在官网https://www.mongodb.org/dl/linux/x86_64下载对应的安装压缩包,我用的是3.2.5,建议使用此版本,版本越高,可能会出现其它未知问题,比如需要glibc高 ...

  3. nginx ../logs/nginx.pid" failed (2: No such file or directory)

    [1]nginx.pid相关 (1)可能出现两种场景: 1.1 nginx.pid文件不存在 发生现象:nginx: [error] open() "/usr/local/lib/ubcsr ...

  4. maven中target不能访问

    原因是maven  clean的时候已经把target文件夹删除  但是文件夹还存在页面中  所以我们看得到但是不能打开.正常操作是获得管理员权限删除后再重新clean,但是我电脑有360,直接360 ...

  5. MQ知识点汇总

    1. MQ是什么 2. MQ能做什么 3. 消息模式 4. 使用MQ的时候需要注意什么 5. 常用MQ 6. MQ的不足 7. 什么时候不适用MQ 8. MQ的组成 9. MQ的关注点 1. MQ是什 ...

  6. webServices接口开发过程中项目启动遇到的错误org.xml.sax.SAXParseException; lineNumber: 20; columnNumber: 422; schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/beans/spring-bean

    org.xml.sax.SAXParseException; lineNumber: 20; columnNumber: 422; schema_reference.4: Failed to read ...

  7. 网易cetus数据库中间件安装-读写分离版本

    安装前提:1.数据库主从关系要做好:2.授权用户登录要做好:3.mysql最大连接数设置好,不然会报连接错误:4.版本最好是5.6以上. 1.安装依赖 yum install cmake gcc gl ...

  8. 使用kubesql进行kubernetes资源查询

    kubesql kubesql(https://github.com/xuxinkun/kubesql)是我最近开发的一个使用sql查询kubernetes资源的工具.诸如node,pod等kuber ...

  9. Mapped Statements collection already contains value for ***.***的问题

    情景,在我们配置项目或者开发的过程中,可能由于项目工程量大或误操作等原因,造成Map映射文件的ID重复,造成项目启动报以下错误, org.springframework.beans.factory.B ...

  10. Bigger-Mai 养成计划,前端基础学习之HTML

    HTML 超文本标记语言(Hyper Text Markup Language) 1.一套规则,浏览器认识的规则. 2.开发者: 学习Html规则 开发后台程序: - 写Html文件(充当模板的作用) ...