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

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

1A后缀数组真是快意【模板越敲越来劲】

这道题就是求多字符串的公共最长子串

之前已经写过一个类似的了

把所有字符串分别用一个其它的标识符作为分隔连接起来【分隔符互不相同以保证不参与匹配】

注意这里的字符串比较多,分隔符就有很多,所以不能用char存,要用int存

之后就是二分长度,看看height的每个分组中是否N个串都在里面

找到最大长度再跑一次找到第一个满足要求的分组输出

#include<iostream>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define REP0(i,n) for (int i = 0; i <= (n); i++)
#define PER(i,n) for (int i = (n); i >= 1; i--)
#define Redge(u) for (int k = head[u]; k != -1; k = edge[k].next)
using namespace std;
const int maxn = 1000005,maxm = 4005,INF = 1000000000;
int N,Min,r[maxn];
char A[maxn];
int sa[maxn],rank[maxn],height[maxn],t1[maxn],t2[maxn],c[maxn],id[maxn],vis[maxm],n,m;
void SA(){
int *x = t1,*y = t2;
REP0(i,m) c[i] = 0;
REP(i,n) c[x[i] = r[i]]++;
REP(i,m) c[i] += c[i - 1];
PER(i,n) sa[c[x[i]]--] = i;
for (int k = 1; k <= n; k <<= 1){
int p = 0;
for (int i = n - k + 1; i <= n; i++) y[++p] = i;
REP(i,n) if (sa[i] - k > 0) y[++p] = sa[i] - k;
REP0(i,m) c[i] = 0;
REP(i,n) c[x[y[i]]]++;
REP(i,m) c[i] += c[i - 1];
PER(i,n) sa[c[x[y[i]]]--] = y[i];
swap(x,y);
x[sa[1]] = p = 1;
for (int i = 2; i <= n; i++)
x[sa[i]] = y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k] ? p : ++p;
if (p >= n) break;
m = p;
}
REP(i,n) rank[sa[i]] = i;
for (int i = 1,k = 0,j; i <= n; i++){
if (k) k--;
j = sa[rank[i] - 1];
while (r[i + k] == r[j + k]) k++;
height[rank[i]] = k;
}
}
bool check(int K){
memset(vis,-1,sizeof(vis));
int cnt = 0,flag = 1;
REP(i,n){
if (id[sa[i]] == -1) {cnt = 0; ++flag; continue;}
if (height[i] < K){
cnt = 1; ++flag;
vis[id[sa[i]]] = flag;
}
else {
if (vis[id[sa[i]]] != flag) cnt++;
vis[id[sa[i]]] = flag;
}
if (cnt == N) return true;
}
return false;
}
void print(int K){
//cout<<"K:"<<K<<endl;
memset(vis,-1,sizeof(vis));
int cnt = 0,flag = 1,pos = 0;
REP(i,n){
if (id[sa[i]] == -1) {cnt = 0; ++flag; continue;}
if (height[i] < K){
cnt = 1; ++flag; pos = sa[i];
vis[id[sa[i]]] = flag;
}
else {
if (vis[id[sa[i]]] != flag) cnt++;
vis[id[sa[i]]] = flag;
}
if (cnt == N){
for (int i = 0; i < K; i++)
putchar(r[pos + i]);
puts("");
return;
}
}
}
void solve(){
int L = 0,R = Min,mid;
while (L < R){
mid = L + R + 1 >> 1;
if (check(mid)) L = mid;
else R = mid - 1;
}
if (!L) printf("IDENTITY LOST\n");
else print(L);
}
int main(){
while (~scanf("%d",&N) && N){
m = 130; n = 0; Min = INF;
REP(i,N){
scanf("%s",A + 1);
int len = strlen(A + 1); Min = min(Min,len);
for (int j = 1; j <= len; j++) r[n + j] = A[j],id[n + j] = i;
n += len; r[++n] = ++m; id[n] = -1;
}
//REP(i,n) printf("%c",isalpha(r[i]) ? r[i] : '#');cout<<endl;
SA();
//REP(i,n) cout<<sa[i]<<' ';cout<<endl;
solve();
}
return 0;
}

POJ3450 Corporate Identity 【后缀数组】的更多相关文章

  1. POJ3450 Corporate Identity —— 后缀数组 最长公共子序列

    题目链接:https://vjudge.net/problem/POJ-3450 Corporate Identity Time Limit: 3000MS   Memory Limit: 65536 ...

  2. [poj3450]Corporate Identity(后缀数组)

    题意:多个字符串的最长公共子串. 解题关键:字符串的任何一个子串都是这个字符串的某个后缀的前缀.求A和B的最长公共子串等价于求A的后缀和B的后缀的最长公共前缀的最大值. 后缀数组的经典例题,连接在一起 ...

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

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

  4. POJ-3450 Corporate Identity (KMP+后缀数组)

    Description Beside other services, ACM helps companies to clearly state their “corporate identity”, ...

  5. POJ3450 Corporate Identity

    后缀数组. 解决多个字符串的最长公共子串. 采用对长度的二分,将子串按height分组,每次判断是否在每个字符串中都出现过. 复杂度O(NlogN) By:大奕哥 #include<cstrin ...

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

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

  7. POJ3080 POJ3450Corporate Identity(广义后缀自动机||后缀数组||KMP)

    Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...

  8. hdu2328 Corporate Identity【string库使用】【暴力】【KMP】

    Corporate Identity Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. kuangbin带你飞 后缀数组 题解

    2份模板 DC3 . 空间复杂度O3N 时间复杂度On #define F(x) ((x) / 3 + ((x) % 3 == 1 ? 0 : tb)) #define G(x) ((x) < ...

随机推荐

  1. php如何将base64数据流文件转换为图片文件?

    2017-03-07 在开发中,自己遇到一个前端在上传图片的时候,使用的base64数据流文件显示的图片. 也就是说 <img src="data:image/jpg;base64,& ...

  2. STM32CubeMx配置正交编码器遇到的问题

    配置时参考了这个哥们的方法: http://www.eemaker.com/stm32cubemx-encoder.html 然后我的配置是这样的 配置是没有问题. 调用时出现了问题. 由于配置完了, ...

  3. Flask初学者:视图函数和类视图

    当一个url请求进入后台时,一般有两种方式来进行处理:视图函数和类视图.视图函数直接使用一个函数来进行处理并返回数据给浏览器,类视图则是使用类来进行处理并返回的,所以当需要进行的处理比较简单,则可以考 ...

  4. POJ1659 可图性判定

    Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 10660   Accepted: 4 ...

  5. 004---基于TCP的套接字

    基于TCP的套接字 tcp是基于链接的,必须先启动服务端,然后再启动客户端去连接服务端. 之前实现的简单套接字就是基于TCP的,但是只能实现收发消息一次.服务器与客户端都断开了.不够过瘾. 通信循环版 ...

  6. HDU 1495 非常可乐 (只是转了个弯的广搜题)

    N - 非常可乐 =========================================================================================== ...

  7. 教你如何更改xshell中的转发规则

    使用不同的类型转发,与之对应的端口,所以如果想要使用不同类型的转发就要更改端口使其与之一一对应.本集xshell专栏文章将为大家讲解如何更改转发规则. 更改转发规则操作如下: 1.打开会话对话框. 2 ...

  8. Android 数据库 ANR的例子

    android 开启事务之后,在其他线程是不能进行增删改查操作的.例子如下: 首先,一个线程里面去开启事务,里面对数据库的任何操作都没有. DBAdapter.getInstance().beginT ...

  9. K8s集群内热改代码

    1.登录到k8s master服务器 $ ssh developer@XXX.XXX.X.XXX(IP地址) 2.查看服务容器所在的节点(以xx-server为例) $ kubectl get pod ...

  10. 传输控制层协议TCP概述---抄书

    1.TCP的主要特点 TCP是TCP/IP体系中非常复杂的一个协议.下面介绍TCP的最主要的特点. (1)TCP是面向连接的运输层协议.也就是说,应用程序在使用TCP协议之前,必须先建立TCP连接.在 ...