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 ...
随机推荐
- jQuery 写的简单打字游戏
var off_x; //横坐标 var count=0; //总分 var speed=5000; //速度,默认是5秒. var keyErro=0; //输入错误次数 var keyRight= ...
- 【HDU】6110 路径交(2017百度之星) 线段树+RMQ-LCA+树链的交
[题目]2017"百度之星"程序设计大赛 - 初赛(A) [题意]给定n个点的带边权树,m条编号1~m的路径,Q次询问编号区间[L,R]所有链的交集的长度.n<=500000 ...
- indexof()函数
js 判断字符串是否包含某字符串,String对象中查找子字符,indexOf, 成功,返回索引值,失败返回 -1. 转载: http://www.cnblogs.com/fishtreeyu/arc ...
- H5小游戏——看你有多色
使用了封装了canvas的create.js库来实现的. 最终效果: 工程: Rect.js /* * 方块类 */ function Rect(n,color,specialColor){ crea ...
- 原 jQuery中document的ready和load事件的区别?
概述: 大家在工作中用jQuery的时候一定会在使用之前这样: 1 2 3 4 5 6 7 8 //document ready $(document).ready(function(){ ...
- hdu 3729 I'm Telling the Truth(二分匹配_ 匈牙利算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3729 I'm Telling the Truth Time Limit: 2000/1000 MS ( ...
- 简单漂亮的php验证码函数
/* *说明:函数功能是生成验证码 * 参数说明:输入 长度,宽度,高度 */ function vcode($_code_length = , $_width = , $_height = ){ $ ...
- Android稳定性测试工具Monkey的使用
综述 Monkey是一个命令行工具,它可以运行在我们的模拟器或者设备当中.它可以发送一些伪随机的用户事件流,例如点击.触摸.手势等.我们能够使用Monkey工具来对我们所开发的应用进行压力测试. ...
- perl6 登录phpmyadmin
use HTTP::UserAgent; my $ua = HTTP::UserAgent.new; my $url = 'http://localhost/phpMyAdmin/index.php' ...
- H5移动端视频问题(苹果全屏播放问题等)
iphone上,手动.自动.窗口化等问题 iphone窗口化 解决方案: 通过canvas + video标签结合处理 原理: 获取video的原图帧,通过canavs绘制到页面. 我们一般在苹果上在 ...