POJ3450 Corporate Identity
后缀数组。
解决多个字符串的最长公共子串。
采用对长度的二分,将子串按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的更多相关文章
- POJ3450 Corporate Identity —— 后缀数组 最长公共子序列
题目链接:https://vjudge.net/problem/POJ-3450 Corporate Identity Time Limit: 3000MS Memory Limit: 65536 ...
- POJ-3450 Corporate Identity (KMP+后缀数组)
Description Beside other services, ACM helps companies to clearly state their “corporate identity”, ...
- POJ3450 Corporate Identity 【后缀数组】
Corporate Identity Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 7662 Accepted: 264 ...
- [poj3450]Corporate Identity(后缀数组)
题意:多个字符串的最长公共子串. 解题关键:字符串的任何一个子串都是这个字符串的某个后缀的前缀.求A和B的最长公共子串等价于求A的后缀和B的后缀的最长公共前缀的最大值. 后缀数组的经典例题,连接在一起 ...
- hdu 2328 Corporate Identity(kmp)
Problem Description Beside other services, ACM helps companies to clearly state their “corporate ide ...
- hdu2328 Corporate Identity【string库使用】【暴力】【KMP】
Corporate Identity Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu2328 Corporate Identity 扩展KMP
Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...
- (KMP 暴力)Corporate Identity -- hdu -- 2328
http://acm.hdu.edu.cn/showproblem.php?pid=2328 Corporate Identity Time Limit: 9000/3000 MS (Java/Oth ...
- hdu2328 Corporate Identity
地址:http://acm.hdu.edu.cn/showproblem.php?pid=2328 题目: Corporate Identity Time Limit: 9000/3000 MS (J ...
随机推荐
- cms替换主页
cms替换主页的步骤 1.先做好静态页面: 2.在D:\wamp\www\phpcms\install_package\phpcms\templates文件夹下建新的文件夹tianqiwangluo( ...
- docker ubuntu容器更换阿里源(转)
问题:使用docker 利用下载的ubuntu镜像启动容器时,使用的源下载更新软件的速度较慢. 解决这个问题的方法是跟新ubuntu容器的源 示例:以ubuntu为基础镜像 启动一个名称为 test0 ...
- 关于servlet中重定向、转发的地址问题
先写一个正斜杠"/",再判断是服务器使用该地址还是网站使用该地址. 访问网络资源用/,访问硬盘资源用\. 例如: 转发: request.getRequestDispat ...
- 转:修改shape的文字
Sub 修改shape的文字()'' 修改shape的文字 宏' ' ActiveSheet.Shapes.Range(Array("Flowchart: Connector 193& ...
- Mysql存储之ORM框架SQLAlchemy(一)
上一篇我们说了mysql存储的原生语句方式,因为原生语句每次写都比较的复杂,所以这里我们说一种引用实体类的方式来操作数据库. 什么是ORM ORM技术:Object-Relational Mappin ...
- Effective C++笔记(二):构造/析构/赋值运算
参考:http://www.cnblogs.com/ronny/p/3740926.html 条款05:了解C++默默编写并调用哪些函数 如果自定义一个空类的话,会自动生成默认构造函数.拷贝构造函数. ...
- python_异常处理
常用异常种类 AttributeError 试图访问一个对象没有的树形,比如foo.x,但是foo没有属性x IOError 输入/输出异常:基本上是无法打开文件 ImportError 无法引入模块 ...
- ~Delphi const 杂谈~
来自:http://www.cnblogs.com/tibetwolf/articles/1785744.html ------------------------------------------ ...
- 20165301 2017-2018-2 《Java程序设计》第三周学习总结
20165301 2017-2018-2 <Java程序设计>第三周学习总结 教材学习内容总结 第四章:类与对象 类: 类的声明:class+类名 类体:成员变量的声明+方法(局部变量+语 ...
- php强制输出到浏览器下载
$file_name="test.mp3"; $mp3_url = "";header( "Pragma: public" );header ...