您的位置:首页 > 其它

POJ-2774-Long Long Message(后缀数组)

2016-04-05 15:40 393 查看
Input

Two strings with lowercase letters on two of the input lines individually. Number of characters in each one will never exceed 100000.
Output

A single line with a single integer number – what is the maximum length of the original text written by the little cat.
Sample Input
yeshowmuchiloveyoumydearmotherreallyicannotbelieveit
yeaphowmuchiloveyoumydearmother

Sample Output
27


题意 :求最长连续公共子串

解法 :两串连接,求其后缀数组 后求height数组, 连接节点取特殊值标记

code:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;
#define MAXN 200010
int t1[MAXN],t2[MAXN],c[MAXN];
int sa[MAXN],rank[MAXN],height[MAXN];
int r[MAXN];
char s1[MAXN],s2[MAXN];
int s[MAXN];
bool cmp(int *r, int a, int b,int l)
{
return r[a]==r[b]&&r[a+l]==r[b+l];
}
void DA(int str[], int sa[], int rank[], int height[], int n, int m)
{
// n++;
int i,j,p,*x=t1,*y=t2;
for(i=0; i<m; ++i)c[i]=0;
for(i=0; i<n; ++i)c[x[i]=str[i]]++;
for(i=1; i<m; ++i)c[i]+=c[i-1];
for(i=n-1; i>=0; --i)sa[ --c[ x[i] ] ]=i;
for(j=1; j<=n; j<<=1)
{
p=0;
for(i=n-j; i<n; ++i)y[p++]=i;
for(i=0; i<n; ++i)if(sa[i]>=j)y[p++]=sa[i]-j;

for(i=0; i<m; ++i)c[i]=0;
for(i=0; i<n; ++i)c[x[y[i]]]++;
for(i=1; i<m; ++i)c[i]+=c[i-1];
for(i=n-1; i>=0; --i)sa[--c[x[y[i]]]]=y[i];
swap(x,y);
p=1;
x[sa[0]]=0;
for(i=1; i<n; ++i)
x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
//if(p>=n)break;
m=p;
}
// for(i=0; i<n; ++i)
//    cout<<sa[i]<<" ";
//cout<<endl;
// cin>>i;
// cout<<"this\n";
int k=0;
n--;
for(i=0; i<=n; ++i)rank[sa[i]]=i;
for(i=0; i<n; ++i)
{
if(k)--k;
j=sa[rank[i]-1];
while(str[i+k]==str[j+k])k++;
height[rank[i]]=k;
}
}
/*
int main()
{
int n;
cin>>n;
for(int i=0; i<n; ++i)
cin>>s[i];
s
=0;
DA(s,sa,rank,height,n+1,30);
return 0;
}*/
int main()
{
while(~scanf("%s",s1))
{
scanf("%s",s2);
int i,j,k,len1,len2,len;
len1=strlen(s1);
len2=strlen(s2);
for(i=0; i<len1; ++i)
s[i]=s1[i]-'a'+1;
s[i++]=27;
for(j=0; j<len2; ++j)
s[i++]=s2[j]-'a'+1;
s[i++]=0;
len=i;
DA(s,sa,rank,height,len,30);
int maxx=0;
for(i=2; i<len; ++i)
{
if(height[i]>maxx)
{
if(sa[i]<len1 && sa[i-1]>len1)maxx=height[i];
if(sa[i-1]<len1 && sa[i]>len1)maxx=height[i];
}
}
cout<<maxx<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: