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

题意:

求n个串的最长公共字串,如果有多个,输出最小字典序的一个。

思路:

KMP||后缀数组||广义后缀自动机,不说了,上高数课了。代码比较暴力。下课了再试一试优化。

对比:

后缀数组SA已经排序了,最小字典序好找。而后缀自动机则需要像字典树一样搜索。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<memory>
#include<cmath>
using namespace std;
int n,len,ans,Max,now;
const int maxn=;
char s[],cap[];
struct SAM
{
int ch[maxn][],fa[maxn],maxlen[maxn],Last,sz;
int root,nxt[maxn],size[maxn];bool Flag;
void init()
{
sz=;Flag=false;
root=++sz;
memset(size,,sizeof(size));
memset(ch[],,sizeof(ch[]));
memset(nxt,,sizeof(nxt));
}
void add(int x)
{
int np=++sz,p=Last;Last=np;
memset(ch[np],,sizeof(ch[np]));
maxlen[np]=maxlen[p]+;
while(p&&!ch[p][x]) ch[p][x]=np,p=fa[p];
if(!p) fa[np]=;
else {
int q=ch[p][x];
if(maxlen[p]+==maxlen[q]) fa[np]=q;
else {
int nq=++sz;
memcpy(ch[nq],ch[q],sizeof(ch[q]));size[nq]=size[q]; nxt[nq]=nxt[q];
maxlen[nq]=maxlen[p]+;
fa[nq]=fa[q];
fa[q]=fa[np]=nq;
while(p&&ch[p][x]==q) ch[p][x]=nq,p=fa[p];
}
}
for(;np;np=fa[np])
if(nxt[np]!=now) {
size[np]++;
nxt[np]=now;
}else break;
}
/* void dfs(int x,int d){//输出
if(Flag||d>n) return;
if(d==n){ puts(cap); Flag=true; return; }
for(int i=0;i<26;i++)
if(ch[x][i]&&size[ch[x][i]]==n){ cap[d]=i+'a'; dfs(ch[x][i],d+1); cap[d]=0; }
}*/
void dfs(int x,int d){//输出
if(d!=maxlen[x]||d>ans||Flag) return;
if(maxlen[x]==ans&&size[x]>=n) { puts(cap); Flag=true; return; }
for(int i=;i<;++i)
if(ch[x][i]){ cap[d]=i+'a'; dfs(ch[x][i],d+); cap[d]=; }
}
};
SAM Sam;
int main()
{
while(~scanf("%d",&n)&&n){
Sam.init();
for(int i=;i<=n;i++) {
scanf("%s",s+);
Sam.Last=Sam.root;
len=strlen(s+);
now=i;
for(int j=;j<=len;j++) Sam.add(s[j]-'a');
}
ans=;
for(int i=;i<=Sam.sz;i++)
if(Sam.size[i]==n&&Sam.maxlen[i]>ans) ans=Sam.maxlen[i];
if(ans) Sam.dfs(,);
else printf("IDENTITY LOST\n");
}
return ;
}

POJ3080 POJ3450Corporate Identity(广义后缀自动机||后缀数组||KMP)的更多相关文章

  1. poj 1743 Musical Theme 后缀自动机/后缀数组/后缀树

    题目大意 直接用了hzwer的题意 题意:有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的主题."主题&qu ...

  2. [模板] 后缀自动机&&后缀树

    后缀自动机 后缀自动机是一种确定性有限状态自动机, 它可以接收字符串\(s\)的所有后缀. 构造, 性质 翻译自毛子俄罗斯神仙的博客, 讲的很好 后缀自动机详解 - DZYO的博客 - CSDN博客 ...

  3. UVA - 11107 Life Forms (广义后缀自动机+后缀树/后缀数组+尺取)

    题意:给你n个字符串,求出在超过一半的字符串中出现的所有子串中最长的子串,按字典序输出. 这道题算是我的一个黑历史了吧,以前我的做法是对这n个字符串建广义后缀自动机,然后在自动机上dfs,交上去AC了 ...

  4. 回文树&后缀自动机&后缀数组

    KMP,扩展KMP和Manacher就不写了,感觉没多大意思.   之前感觉后缀自动机简直可以解决一切,所以不怎么写后缀数组.   马拉车主要是通过对称中心解决问题,有的时候要通过回文串的边界解决问题 ...

  5. bzoj 3277: 串 & bzoj 3473: 字符串【后缀自动机||后缀数组】

    建一个广义后缀自动机(每加完一个串都返回root),在parent树上dpsum记录合法长度,打着时间戳往上跳,最后每个串在自动机上跑一变统计答案即可. 后缀数组理解起来可能方便一点,但是难写,就只说 ...

  6. BZOJ 3926: [Zjoi2015]诸神眷顾的幻想乡 广义后缀自动机 后缀自动机 字符串

    https://www.lydsy.com/JudgeOnline/problem.php?id=3926 广义后缀自动机是一种可以处理好多字符串的一种数据结构(不像后缀自动机只有处理一到两种的时候比 ...

  7. SPOJ705 Distinct Substrings (后缀自动机&后缀数组)

    Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...

  8. SPOJ SUBLEX - Lexicographical Substring Search 后缀自动机 / 后缀数组

    SUBLEX - Lexicographical Substring Search Little Daniel loves to play with strings! He always finds ...

  9. 康复计划#1 再探后缀自动机&后缀树

    本篇口胡写给我自己这样的东西都忘光的残废选手 以及那些刚学SAM,看了其他的一些东西并且没有完全懵逼的人 (初学者还是先去看有图的教程吧,虽然我的口胡没那么好懂,但是我觉得一些细节还是讲清楚了的) 大 ...

随机推荐

  1. centos7.0 安装php

    1:去php官网下载对应版本的php包 2:解压php包 3:进入解压后的php包 ./configure --with-apxs2=/usr/local/apache2/bin/apxs --wit ...

  2. u-boot下载模式LCD显示图片修改方法(基于TQ2440)

    1.明确液晶型号,这点非常重要,我手头的液晶是天嵌4.3寸屏,让人很郁闷的是液晶背面竟然写着LCD 3.5,这一点让我在上面浪费了好几个小时: 2.根据液晶型号,修改u-boot1.1.6--> ...

  3. map 玩家上线

    map 玩家上线 else if(gs2ms_add_player == pkt.cmd) { PlayerChannel* pPC = new PlayerChannel(this); //加到地图 ...

  4. hdu1573(线性同余方程组)

    套模板,因为要是正整数,所以处理一下x=0的情况. X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  5. codevs 必做:2776、1222

    2776 寻找代表元  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 广州二中苏元实验学校一共有n个社团,分 ...

  6. 【BZOJ1226】[SDOI2009]学校食堂Dining 状压DP

    [BZOJ1226][SDOI2009]学校食堂Dining Description 小F 的学校在城市的一个偏僻角落,所有学生都只好在学校吃饭.学校有一个食堂,虽然简陋,但食堂大厨总能做出让同学们满 ...

  7. Numerical Differentiation 数值微分

    zh.wikipedia.org/wiki/數值微分 数值微分是数值方法中的名词,是用函数的值及其他已知资讯来估计一函数导数的算法. http://mathworld.wolfram.com/Nume ...

  8. C# unicode GBK UTF-8和汉字互转

    界面: 源码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Da ...

  9. Python基础(4)_字典、集合、bool值

    三.字典 定义:{key1:value1,key2:value2},key-value结构,key必须是不可变类型,或者可hash 基本形式:key:value特性: 1.可存放多个值 2.可修改指定 ...

  10. Kattis - horrorfilmnight 【贪心】

    题意 有两个人想去一起看电影,然后分别给出两个人 分别喜欢看的电影都在哪些天 然后 同一个人 不能连续看两天他不喜欢的电影 求他们最多可以看多少次电影 思路 先将两人喜欢看的电影进行排序, ① 选择两 ...