poj 3450 Corporate Identity

时间:2023-03-09 01:20:09
poj 3450  Corporate Identity

题目链接:http://poj.org/problem?id=3450

题目分类:后缀数组

题意:求n个串的最长公共字串(输出字串)

//#include<bits/stdc++.h>
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string.h> using namespace std; #define N 200005 int wa[N],wb[N],wsf[N],wv[N],sa[N];
int rank[N],height[N],s[N];
//sa:字典序中排第i位的起始位置在str中第sa[i]
//rank:就是str第i个位置的后缀是在字典序排第几
//height:字典序排i和i-1的后缀的最长公共前缀
int cmp(int *r,int a,int b,int k)
{
return r[a]==r[b]&&r[a+k]==r[b+k];
}
void getsa(int *r,int *sa,int n,int m)//n要包含末尾添加的0
{
int i,j,p,*x=wa,*y=wb,*t;
for(i=; i<m; i++) wsf[i]=;
for(i=; i<n; i++) wsf[x[i]=r[i]]++;
for(i=; i<m; i++) wsf[i]+=wsf[i-];
for(i=n-; i>=; i--) sa[--wsf[x[i]]]=i;
p=;
j=;
for(; 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++) wsf[i]=;
for(i=; i<n; i++) wsf[wv[i]]++;
for(i=; i<m; i++) wsf[i]+=wsf[i-];
for(i=n-; i>=; i--) sa[--wsf[wv[i]]]=y[i];
t=x;
x=y;
y=t;
x[sa[]]=;
for(p=,i=; i<n; i++)
x[sa[i]]=cmp(y,sa[i-],sa[i],j)? p-:p++;
}
}
void getheight(int *r,int n)//n不保存最后的0
{
int i,j,k=;
for(i=; i<=n; i++) rank[sa[i]]=i;
for(i=; i<n; i++)
{
if(k)
k--;
else
k=;
j=sa[rank[i]-];
while(r[i+k]==r[j+k])
k++;
height[rank[i]]=k;
}
} char str[N],ans[N];
int id[N],vis[]; bool check(int mid,int n,int k)
{
int i,j,cnt = ;
memset(vis,,sizeof(vis));
for(i = ; i<=n; i++)
{
if(height[i]<mid)
{
memset(vis,,sizeof(vis));
cnt = ;
continue;
}
if(!vis[id[sa[i-]]])
{
cnt++;
vis[id[sa[i-]]] = ;
}
if(!vis[id[sa[i]]])
{
cnt++;
vis[id[sa[i]]] = ;
}
if(cnt == k)
{
for(j = ; j<mid; j++)
ans[j] = s[sa[i]+j];
ans[mid] = '\0';
return ;
}
}
return ;
} int main()
{
int n,i,j,k,len;
while(~scanf("%d",&k),k)
{
n = ;
for(i = ; i<k; i++)
{
scanf("%s",str);
len = strlen(str);
for(j = ; j<len; j++)
{
s[n] = str[j];
id[n] = i;
n++;
}
s[n] = '#'+i;
id[n] = '#'+i;
n++;
}
s[n] = ;
getsa(s,sa,n+,);
getheight(s,n);
int l = ,r = len,mid,flag = ;
while(l<=r)
{
mid = (l+r)/;
if(check(mid,n,k))
{
flag = ;
l=mid+;
}
else
r=mid-;
}
if(flag)
printf("%s\n",ans);
else
printf("IDENTITY LOST\n");
} return ;
}