后缀数组。

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

采用对长度的二分,将子串按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. TreeSet的特性

    TreeSet在Set的元素不重复的基础之上引入排序的概念,其中对自身拥有Comparable的元素,可以直接进行排序,比如字符串,按照字母的自然顺序排序,此处说下对于自定义对象排序的方式. 1.存储 ...

  2. 老版本ubuntu更新源地址以及sources.list的配置方法 转

    转自(http://blog.csdn.net/snaking616/article/details/52966634) 1.国内可用的更新源地址: (1)中科大地址 http://mirrors.u ...

  3. linux wc命令的作用。

    Linux系统中的wc(Word Count)命令的功能为统计指定文件中的字节数.字数.行数,并将统计结果显示输出. 1.命令格式: wc [选项]文件... 2.命令功能: 统计指定文件中的字节数. ...

  4. Python 根据地址获取经纬度及求距离

    方法一: 使用Geopy包 : https://github.com/geopy/geopy   (仅能精确到城镇,具体街道无结果返回) from geopy.geocoders import Nom ...

  5. 如何优雅地使用vim编辑器

    如何优雅地使用vim编辑器大赞..原文链接:http://jackiekuo.com/code/2014/05/07/use-vim-the-pragmatic-way/ 接上篇如何优雅地使用Vim? ...

  6. Linux中常用命令 <一>

    本笔记中记录的命令来源于 <Linux C 编程实战> ------------------------------------------------------------------ ...

  7. P1084 疫情控制

    Solution 二分答案, 尽量往上跳, 不能跳到根节点. 仍然能跳的拿出来.看剩下的点没有覆盖哪个? 贪心的分配一下. Code 70 #include<iostream> #incl ...

  8. MVC开发模式与javaEE三层架构

    1.MVC开发模式 1. M:Model,模型.JavaBean        * 完成具体的业务操作,如:查询数据库,封装对象2. V:View,视图.JSP        * 展示数据3. C:C ...

  9. js获取系统时间

    //------------------------------------获取系统日期时间 var oDate=new Date(); //alert(oDate.getFullYear());// ...

  10. LeetCode解题报告—— Minimum Window Substring && Largest Rectangle in Histogram

    1. Minimum Window Substring Given a string S and a string T, find the minimum window in S which will ...