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

  1. 3
  2. aabbaabb
  3. abbababb
  4. bbbbbabb
  5. 2
  6. xyz
  7. abc
  8. 0

Sample Output

  1. abb
  2. IDENTITY LOST
  3.  
  4. 题目大意:给多个字符串,找出最长的并且字典序最小的公共子串.
    两种做法:
      (1).使用KMP.每一个子串都是某个后缀的前缀.枚举最短字符串的每一个后缀suff(i),让其他所有的字符串去跟suff(i)做kmp匹配,会得到一个suff(i)的公共前缀pre_suff(i),选一个最长的pre_suff
    即为答案.枚举后缀时按照字典序枚举,可以省略掉比较长度相同的pre_suff这一过程.
      (2).使用后缀数组.首先将所有的字符串连接成一条长串,求出height数组之后,二分枚举最长公共子串的长度mid,然后根据height值是否不小于mid将数组分成若干个连续的区间.查看每一个区
    间,只要某个区间中的前缀来自所有的字符串,那么存在长度为mid的公共子串.但是我一直TLE.
  5.  
  6. 代码如下(第一种做法):
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. const int N=4005;
  8.  
  9. int SA[205];
  10. int tSA[205];
  11. int rk[205];
  12. int cnt[205];
  13.  
  14. bool isSame(int *y,int i,int j,int k,int n)
  15. {
  16. if(y[i]!=y[j]) return false;
  17. if(i+k<n&&j+k>=n) return false;
  18. if(i+k>=n&&j+k<n) return false;
  19. return y[i+k]==y[j+k];
  20. }
  21.  
  22. void buildSA(char* str,int n)
  23. {
  24. int *x=rk;
  25. int *y=tSA;
  26. int m=26;
  27. for(int i=0;i<m;++i) cnt[i]=0;
  28. for(int i=0;i<n;++i) ++cnt[x[i]=(str[i]-'a')];
  29. for(int i=1;i<m;++i) cnt[i]+=cnt[i-1];
  30. for(int i=n-1;i>=0;--i) SA[--cnt[x[i]]]=i;
  31.  
  32. for(int k=1;k<=n;k<<=1){
  33. int p=0;
  34. for(int i=n-k;i<n;++i) y[p++]=i;
  35. for(int i=0;i<n;++i) if(SA[i]>=k) y[p++]=SA[i]-k;
  36.  
  37. for(int i=0;i<m;++i) cnt[i]=0;
  38. for(int i=0;i<n;++i) ++cnt[x[y[i]]];
  39. for(int i=1;i<m;++i) cnt[i]+=cnt[i-1];
  40. for(int i=n-1;i>=0;--i) SA[--cnt[x[y[i]]]]=y[i];
  41.  
  42. p=1;
  43. swap(x,y);
  44. x[SA[0]]=0;
  45. for(int i=1;i<n;++i)
  46. x[SA[i]]=isSame(y,SA[i],SA[i-1],k,n)?p-1:p++;
  47.  
  48. if(p>=n) break;
  49. m=p;
  50. }
  51. }
  52.  
  53. char tdmks[N][205];
  54. int nxt[205];
  55.  
  56. void getNext(char* str,int str_len)
  57. {
  58. nxt[0]=nxt[1]=0;
  59. for(int i=1;i<str_len;++i){
  60. int j=nxt[i];
  61. while(j&&str[i]!=str[j]) j=nxt[j];
  62. nxt[i+1]=(str[i]==str[j])?j+1:0;
  63. }
  64. }
  65.  
  66. int match(char* str,int str_len,char* ptr)
  67. {
  68. int ptr_len=strlen(ptr);
  69. int tempLen=0,k=0;
  70. for(int i=0;i<ptr_len;++i){
  71. while(k&&ptr[i]!=str[k]) k=nxt[k];
  72. if(str[k]==ptr[i]){
  73. ++k;
  74. tempLen=max(tempLen,k);
  75. }
  76. }
  77. return tempLen;
  78. }
  79.  
  80. int getLongestPre(char* str,int str_len,int cnt_tdmks)
  81. {
  82. getNext(str,str_len);
  83. int long_pre=N;
  84. for(int i=0;i<cnt_tdmks;++i){
  85. long_pre=min(match(str,str_len,tdmks[i]),long_pre);
  86. }
  87. return long_pre;
  88. }
  89.  
  90. int input(int cnt_tdmks)
  91. {
  92. int minLen=N,id_minLen;
  93. for(int i=0;i<cnt_tdmks;++i){
  94. scanf("%s",tdmks[i]);
  95. if(strlen(tdmks[i])<minLen){
  96. minLen=strlen(tdmks[i]);
  97. id_minLen=i;
  98. }
  99. }
  100. return id_minLen;
  101. }
  102.  
  103. void solve(int p,int cnt_tdmks)
  104. {
  105. int m=strlen(tdmks[p]);
  106. buildSA(tdmks[p],m);
  107. int ans_len=0,ans_p;
  108. for(int i=0;i<m;++i){
  109. int len=getLongestPre(tdmks[p]+SA[i],m-SA[i],cnt_tdmks);
  110. if(len>ans_len){
  111. ans_len=len;
  112. ans_p=SA[i];
  113. }
  114. }
  115. if(ans_len){
  116. for(int i=0;i<ans_len;++i)
  117. printf("%c",tdmks[p][ans_p+i]);
  118. printf("\n");
  119. }else{
  120. printf("IDENTITY LOST\n");
  121. }
  122. }
  123.  
  124. int main()
  125. {
  126. //freopen("in.txt","r",stdin);
  127. int n;
  128. while(scanf("%d",&n)&&n)
  129. {
  130. solve(input(n),n);
  131. }
  132. return 0;
  133. }

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. laravel 核心类Kernel

    vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php.是laravel处理网络请求的最核心类,在app容器准备好了之后, ...

  2. [NOIP赛前冲刺第一期]初赛基础知识归纳

    关于计算机 1.CPU 中央处理器(CPU,Central Processing Unit)是一块超大规模的集成电路,是一台计算机的运算核心(Core)和控制核心(Control Unit).它的功能 ...

  3. 2018-2019-20175205实验二面向对象程序设计《Java开发环境的熟悉》实验报告

    2018-2019-20175205实验二面向对象程序设计<Java开发环境的熟悉>实验报告 实验要求 没有Linux基础的同学建议先学习<Linux基础入门(新版)>< ...

  4. OO第一单元优化博客

    OO第一单元优化博客 第一次作业: 合并同类项+提正系数项+优化系数指数0/1=满分 第二次作业: 初始想法 一开始是想以\(sin(x)​\)和\(cos(x)​\)的指数作为坐标,在图上画出来就可 ...

  5. Easy methods to select MB Star, Extremely MB Star, MB SD C4, Mercedes BENZ C5 SD

    MB Star, Extremely MB SD Connect C4, MB SD C4, Mercedes BENZ C5 SD are usually analysis tools to get ...

  6. 破圈法求解最小生成树c语言实现(已验证)

    破圈法求解最小生成树c语言实现(已验证) 下面是算法伪代码,每一个算法都取一个图作为输入,并返回一个边集T. 对该算法,证明T是一棵最小生成树,或者证明T不是一棵最小生成树.此外,对于每个算法,无论它 ...

  7. SHA256withRSA证书签名,私钥签名/公钥验签

    证书签名 package test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundE ...

  8. linux查看与修改交换内存配置(解决zabbix-agent启动报错)

    问题 zabbix-agent在一台centos6.5上启动报错: cannot allocate shared memory of size 949056: [28] No space left o ...

  9. 猴子吃桃问题(Java递归实现)

    猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个,第二天早上又将剩下的桃子吃掉一半,又多吃了一个.以后每天早上都吃了前一天剩下的一半零一个.到第10天早上想再吃时,见只剩下 ...

  10. 【双目备课】《学习OpenCV第18章》相机模型与标定整编

    一.相机模型 针孔模型.在这个简单模型中,想象光线是从场景或一个很远的物体发射过来的,但只有一条光线从该场景中的任意特定点进入针孔. 我们将这个图像进行抽象,就能够得到这样的结果: 其中,f为像到针孔 ...