题目链接:https://vjudge.net/problem/POJ-3450

Corporate Identity
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 8046   Accepted: 2710

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

Source

CTU Open 2007

题意:

给出n个字符串,求这n个字符串的最长公共子序列,输出字典序最小的一个。

题解:

1.将n个字符串拼接在一起,并且相邻两个之间用分隔符隔开,并且分隔符应各异。因此得到新串。

2.求出新串的后缀数组,然后二分公共子串的长度mid:可知当前的mid可将新串的后缀按排名的顺序将其分成若干组,且每一组的最长公共前缀都大于等于mid,于是就在每一组内统计出现了多少个字符串,如果等于n,即表明当前mid合法,否则不合法,因此可以根据此规则最终求得长度。

3.由于题目还要求输出字典序最小的。所以,如果当前mid合法,那么就记录下公共子串的起始点和结束点。因为枚举是按sa[i]从小到大的顺序,因此在同一个mid下,第一组符合条件的公共子串即为字典序最小的。

代码如下:

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 #include <vector>
  6 #include <cmath>
  7 #include <queue>
  8 #include <stack>
  9 #include <map>
 10 #include <string>
 11 #include <set>
 12 using namespace std;
 13 typedef long long LL;
 14 const int INF = 2e9;
 15 const LL LNF = 9e18;
 16 const int MOD = 1e9+7;
 17 const int MAXN = 1e6+100;
 18
 19 int id[MAXN];   //记录属于哪个字符串
 20 int r[MAXN], sa[MAXN], Rank[MAXN], height[MAXN];
 21 int t1[MAXN], t2[MAXN], c[MAXN];
 22
 23 bool cmp(int *r, int a, int b, int l)
 24 {
 25     return r[a]==r[b] && r[a+l]==r[b+l];
 26 }
 27
 28 void DA(int str[], int sa[], int Rank[], int height[], int n, int m)
 29 {
 30     n++;
 31     int i, j, p, *x = t1, *y = t2;
 32     for(i = 0; i<m; i++) c[i] = 0;
 33     for(i = 0; i<n; i++) c[x[i] = str[i]]++;
 34     for(i = 1; i<m; i++) c[i] += c[i-1];
 35     for(i = n-1; i>=0; i--) sa[--c[x[i]]] = i;
 36     for(j = 1; j<=n; j <<= 1)
 37     {
 38         p = 0;
 39         for(i = n-j; i<n; i++) y[p++] = i;
 40         for(i = 0; i<n; i++) if(sa[i]>=j) y[p++] = sa[i]-j;
 41
 42         for(i = 0; i<m; i++) c[i] = 0;
 43         for(i = 0; i<n; i++) c[x[y[i]]]++;
 44         for(i = 1; i<m; i++) c[i] += c[i-1];
 45         for(i = n-1; i>=0; i--) sa[--c[x[y[i]]]] = y[i];
 46
 47         swap(x, y);
 48         p = 1; x[sa[0]] = 0;
 49         for(i = 1; i<n; i++)
 50             x[sa[i]] = cmp(y, sa[i-1], sa[i], j)?p-1:p++;
 51
 52         if(p>=n) break;
 53         m = p;
 54     }
 55
 56     int k = 0;
 57     n--;
 58     for(i = 0; i<=n; i++) Rank[sa[i]] = i;
 59     for(i = 0; i<n; i++)
 60     {
 61         if(k) k--;
 62         j = sa[Rank[i]-1];
 63         while(str[i+k]==str[j+k]) k++;
 64         height[Rank[i]] = k;
 65     }
 66 }
 67
 68 bool vis[4040];
 69 int Le, Ri;
 70 bool test(int n, int len, int k)
 71 {
 72     int cnt = 0;
 73     memset(vis, false, sizeof(vis));
 74     for(int i = 2; i<=len; i++)
 75     {
 76         if(height[i]<k)
 77         {
 78             cnt = 0;
 79             memset(vis, false, sizeof(vis));
 80         }
 81         else
 82         {
 83             if(!vis[id[sa[i-1]]]) vis[id[sa[i-1]]] = true, cnt++;
 84             if(!vis[id[sa[i]]]) vis[id[sa[i]]] = true, cnt++;
 85             if(cnt==n)
 86             {
 87                 Le = sa[i]; Ri = sa[i]+k-1;
 88                 return true;
 89             }
 90         }
 91     }
 92     return false;
 93 }
 94
 95 char str[MAXN];
 96 int main()
 97 {
 98     int n;
 99     while(scanf("%d", &n)&&n)
100     {
101         int len = 0;
102         for(int i = 0; i<n; i++)
103         {
104             scanf("%s", str);
105             int LEN = strlen(str);
106             for(int j = 0; j<LEN; j++)
107             {
108                 r[len] = str[j]-'a'+1;
109                 id[len++] = i;
110             }
111             r[len] = 30+i;  //分隔符要各异
112             id[len++] = i;
113         }
114         r[len] = 0;
115         DA(r,sa,Rank,height,len,30+n);
116
117         int L = 0, R = strlen(str);
118         while(L<=R)
119         {
120             int mid = (L+R)>>1;
121             if(test(n,len,mid))
122                 L = mid + 1;
123             else
124                 R = mid - 1;
125         }
126
127         if(R==0) puts("IDENTITY LOST");
128         else
129         {
130             for(int i = Le; i<=Ri; i++)
131                 printf("%c", r[i]+'a'-1);
132             putchar('\n');
133         }
134     }
135 }

View Code

转载于:https://www.cnblogs.com/DOLFAMINGO/p/8480366.html

POJ3450 Corporate Identity —— 后缀数组 最长公共子序列相关推荐

  1. 后缀数组--(最长公共前缀)

    问题描述:给一个字符串,询问某两个后缀的最长公共前缀. 解析:当然用后缀数组最方便,在后缀数组中有很多重要的定义和性质,现在我们来认识一些: 定义:LCP(i,j)=suffix(SA[i])与suf ...

  2. 最长重复子数组最长公共子序列不相交的线

    引言 这同样是两种类型的题目,有很多相似的地方和不同的地方,区别依然是连续和不连续之分. 最长重复子数组 给两个整数数组 A 和 B ,返回两个数组中公共的.长度最长的子数组的长度. 示例: 输入: ...

  3. CSU1632Repeated Substrings(后缀数组/最长公共前缀)

    题意就是求一个字符串的重复出现(出现次数>=2)的不同子串的个数. 标准解法是后缀数组.最长公共前缀的应用,对于样例aabaab,先将所有后缀排序: aab 3    aabaab 1    a ...

  4. HDU1403(后缀数组--最长公共子串)

    题目:Longest Common Substring 看代码注释请戳这里 题意:判断给定的两个串中,最长的公共串. 思路:将它们合并为一个串,然后利用后缀数组求解. 首先是二倍增算法:时间复杂度为O ...

  5. 最长公共子序列|最长公共子串|最长重复子串|最长不重复子串|最长回文子串|最长递增子序列|最大子数组和...

    最长公共子序列|最长公共子串|最长重复子串|最长不重复子串|最长回文子串|最长递增子序列|最大子数组和 文章作者:Yx.Ac   文章来源:勇幸|Thinking (http://www.ahathi ...

  6. Ural 1297 Palindrome(后缀数组+最长回文子串)

    https://vjudge.net/problem/URAL-1297 题意: 求最长回文子串. 思路: 先将整个字符串反过来写在原字符串后面,中间需要用特殊字符隔开,那么只需要某两个后缀的最长公共 ...

  7. 试题 算法训练 后缀数组——最长重复子串

    资源限制 时间限制:100ms 内存限制:256.0MB 问题描述 给定一个长度为n的数串,求至少出现k 次的最长重复子串的长度,这k 个子串可以重叠.保证有子串出现至少k次. 输入格式 第一行:两个 ...

  8. Algorithm:C++/python语言实现之求旋转数组最小值、求零子数组、求最长公共子序列和最长公共子串、求LCS与字符串编辑距离

    Algorithm:C++/python语言实现之求旋转数组最小值.求零子数组.求最长公共子序列和最长公共子串.求LCS与字符串编辑距离 目录 一.求旋转数组最小值 1.分析问题 2.解决思路 二.求 ...

  9. python查找最长公共前缀_Python实现查找字符串数组最长公共前缀示例

    本文实例讲述了Python实现查找字符串数组最长公共前缀.分享给大家供大家参考,具体如下: 编写一个函数来查找字符串数组中的最长公共前缀. class Solution: def longestCom ...

最新文章

  1. CTFshow 命令执行 web71
  2. 关于python学习路线
  3. P5502 [JSOI2015]最大公约数(gcd性质/min性质/分治)
  4. gnuplot读取mysql数据库_Tpcc-MySQL测试并使用gnuplot生成图表
  5. 哈工大-操作系统的引导
  6. 基于Jquery的图片自动分组且自适应页面的缩略图展示特效
  7. springboot分页展示功能_SpringBoot实战项目(三)用户列表以及分页功能实现
  8. redhat linux iso下载
  9. HTML第五章课后作业,第五章 组合逻辑电路 课后习题答案详解 资料资料资料.pdf...
  10. 2020年微信怎么推刷脸支付
  11. 如何读博士-2021.06.12
  12. Python中将科学计数法(或以e为底的自然对数)字符串转换为float浮点数
  13. 手把手教你使用Django如何连接Mysql
  14. app推广“惊天地”的一件大事件
  15. 电商版3Q大战 815电商大战一周年祭
  16. 地级市高新技术企业统计情况(2000-2019)
  17. Android Studio : 导入源码包/源码库/公共库/开源库
  18. Starfish OS:以现实为纽带,打造元宇宙新范式
  19. Silverlight.XNA(C#)跨平台3D游戏研发手记:(一)差集运算在SLG战斗范围设定中的应用
  20. 第三方授权的应用苹果审核被驳回解决方案和app版本更新

热门文章

  1. PAT (Basic Level) Practice (中文)C++ python 语言实现 —— 题解目录
  2. SQL开发技巧 join从句
  3. 【嵌入式】嵌入式天地博客汇总
  4. exfat linux 驱动_(实例)Linux 内核添加exfat驱动
  5. 每天一道LeetCode-----最长回文子串/序列,从头开始的最长回文子串长度
  6. 最短无序连续子数组—leetcode581
  7. linux系统上传代码到gitlab服务器
  8. CF-85E.Guard Towers(二分+染色)
  9. Redis的设计与实现之跳表
  10. C++ Primer这本书怎么样?