后缀数组。

解决多个字符串的最长公共子串。

采用对长度的二分,将子串按height分组,每次判断是否在每个字符串中都出现过。

复杂度O(NlogN)

By:大奕哥

 #include<cstring>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#define rank fank
using namespace std;
const int N=;
int r[N],wa[N],wb[N],wv[N],wu[N],sa[N],rank[N],height[N],bel[N],cnt;
int cmp(int *r,int a,int b,int l)
{
return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da(int *r,int *sa,int n,int m)
{
int i,j,p;int *x=wa,*y=wb;
for(i=;i<m;++i)wu[i]=;
for(i=;i<n;++i)wu[x[i]=r[i]]++;
for(i=;i<m;++i)wu[i]+=wu[i-];
for(i=n-;i>=;--i)sa[--wu[x[i]]]=i;
for(j=,p=;p<n;j<<=,m=p)
{
for(p=,i=n-j;i<n;++i)y[p++]=i;
for(i=;i<n;++i)if(sa[i]>=j)y[p++]=sa[i]-j;
for(i=;i<n;++i)wv[i]=x[y[i]];
for(i=;i<m;++i)wu[i]=;
for(i=;i<n;++i)wu[wv[i]]++;
for(i=;i<m;++i)wu[i]+=wu[i-];
for(i=n-;i>=;--i)sa[--wu[wv[i]]]=y[i];
for(swap(x,y),p=,x[sa[]]=,i=;i<n;++i)
x[sa[i]]=cmp(y,sa[i-],sa[i],j)?p-:p++;
}
return;
}
void calcHeight(int *rank,int *sa,int n)
{
int i,j,k=;
for(i=;i<=n;++i)rank[sa[i]]=i;
for(i=;i<n;height[rank[i++]]=k)
for(k?k--:,j=sa[rank[i]-];r[i+k]==r[j+k];++k);
return ;
}
char s[N],ans[N];
bool v[];
int tot,num;
bool check(int x,int n)
{
memset(v,,sizeof(v));
for(int i=;i<=n;++i)
{
if(height[i]<x){cnt=;memset(v,,sizeof(v));}
else
{
if(v[bel[sa[i-]]]==){
cnt++;v[bel[sa[i-]]]=;
}
if(v[bel[sa[i]]]==){
cnt++;v[bel[sa[i]]]=;
}
if(cnt==num)
{
for(int j=;j<x;++j)
ans[j]=r[sa[i]+j];
ans[x]='\0';
return ;
}
}
}
return ;
}
int main()
{
while(~scanf("%d",&num)&&num)
{
int len=;
for(int i=;i<num;++i)
{
scanf("%s",s);
int n=strlen(s);
for(int j=;j<n;++j)
{
r[len]=s[j];bel[len]=i;++len;
}
r[len]=+i;bel[len]=+i;++len;
}
--len;r[len]=;
da(r,sa,len+,);
calcHeight(rank,sa,len);
int as=,l=,r=len;
while(l<=r)
{
int mid=l+r>>;
if(check(mid,len))as=,l=mid+;
else r=mid-;
}
if(as)printf("%s\n",ans);
else
printf("IDENTITY LOST\n");
}
return ;
}

Ps:wa了好多遍,因为插进去的ascII码值没有选好,附一张ascII表。

POJ3450 Corporate Identity的更多相关文章

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

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

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

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

  3. POJ3450 Corporate Identity 【后缀数组】

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

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

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

  5. hdu 2328 Corporate Identity(kmp)

    Problem Description Beside other services, ACM helps companies to clearly state their “corporate ide ...

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

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

  7. hdu2328 Corporate Identity 扩展KMP

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

  8. (KMP 暴力)Corporate Identity -- hdu -- 2328

    http://acm.hdu.edu.cn/showproblem.php?pid=2328 Corporate Identity Time Limit: 9000/3000 MS (Java/Oth ...

  9. hdu2328 Corporate Identity

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=2328 题目: Corporate Identity Time Limit: 9000/3000 MS (J ...

随机推荐

  1. [php]禁用缓存

    header("Expires: -1"); header("Cache-Control: no_cache"); header("pragma: n ...

  2. System中关于Property的方法

    System类在java.lang包中,所有方法都是静态的,里边有很多对系统的属性和控制方法 System类有三个成员变量:out-标准输出流(默认是控制台),in-标准输入流(默认是键盘),err- ...

  3. 【BZOJ】3262: 陌上花开

    [题意]三维偏序,给定n个点(x,y,z),求每个点和(0,0,0)组成空间中的点数,有重点.1<=x,y,z<=2*10^5,1<=n<=10^5. [算法]CDQ分治+树状 ...

  4. 【洛谷 P2865】 [USACO06NOV]路障Roadblocks(最短路)

    题目链接 次短路模板题. 对每个点记录最短路和严格次短路,然后就是维护次值的方法了. 和这题一样. #include <cstdio> #include <queue> #in ...

  5. python初步学习-python模块之 logging

    logging 许多应用程序中都会有日志模块,用于记录系统在运行过程中的一些关键信息,以便于对系统的运行状况进行跟踪.在python中,我们不需要第三方的日志组件,python为我们提供了简单易用.且 ...

  6. 按键精灵MySql数据库操作

    查询 MySql服务器地址="192.168.1.166" 用户名 = "root" 密码 = " 数据库名="bookshop" ...

  7. Linux进程的创建函数fork()及其fork内核实现解析【转】

    转自:http://www.cnblogs.com/zengyiwen/p/5755193.html 进程的创建之fork() Linux系统下,进程可以调用fork函数来创建新的进程.调用进程为父进 ...

  8. Redis使用详细教程【转】

    转自 Redis使用详细教程 - wangyuyu - 博客园http://www.cnblogs.com/wangyuyu/p/3786236.html 一.Redis基础部分: 1.redis介绍 ...

  9. glom模块的使用(二)

    上次我们说到golm的简单应用这次我们继续带结构化数据的其他操作进行学习. Literal 用法:class glom.Literal(value) 这个方法的功能主要是添加自定义的键值. 例如: f ...

  10. WebClient vs HttpClient vs HttpWebRequest

    转载:http://www.diogonunes.com/blog/webclient-vs-httpclient-vs-httpwebrequest/ Just when I was startin ...