您的位置:首页 > 其它

SPOJ 题目1811 LCS - Longest Common Substring(后缀自动机求最长公共子串)

2015-09-03 14:40 651 查看


LCS - Longest Common Substring

no tags

A string is finite sequence of characters over a non-empty finite set Σ.
In this problem, Σ is the set of lowercase letters.
Substring, also called factor, is a consecutive sequence of characters occurrences at least once in a string.
Now your task is simple, for two given strings, find the length of the longest common substring of them.
Here common substring means a substring of two or more strings.


Input

The input contains exactly two lines, each line consists of no more than 250000 lowercase letters, representing a string.


Output

The length of the longest common substring. If such string doesn't exist, print "0" instead.


Example

Input:
alsdfkjfjkdsal
fdjskalajfkdsla

Output:
3
sam确实快,sa超时了=_=,给a串建个sam,b串跑一遍就行了
ac代码



#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define max(a,b) (a>b?a:b)
#define N 510005
struct sam
{
sam *pre,*son[26];
int len,g;
}que
,*root,*tail,*b
;
int tot;
void add(int c,int l)
{
sam *p=tail,*np=&que[tot++];
np->len=l;
tail=np;
while(p&&p->son[c]==NULL)
{
p->son[c]=np;
p=p->pre;
}
if(p==NULL)
np->pre=root;
else
{
sam *q=p->son[c];
if(p->len+1==q->len)
np->pre=q;
else
{
sam *nq=&que[tot++];
*nq=*q;
nq->len=p->len+1;
np->pre=q->pre=nq;
while(p&&p->son[c]==q)
{
p->son[c]=nq;
p=p->pre;
}
}
}
}
char str1[N>>1],str2[N>>1];
int dp[N>>1];
int main()
{
while(scanf("%s%s",str1,str2)!=EOF)
{
tot=0;
root=tail=&que[tot++];
int l=0;
int len=strlen(str1),i;
for(i=0;i<len;i++)
{
add(str1[i]-'a',i+1);
}
len=strlen(str2);
sam *p=root;
int ans=0;
for(i=0;i<len;i++)
{
int now=str2[i]-'a';
if(p->son[now])
{
p=p->son[now];
l++;
}
else
{
while(p&&p->son[now]==NULL)
p=p->pre;
if(p==NULL)
{
p=root;
l=0;
}
else
{
l=p->len+1;
p=p->son[now];
}
}
if(l>ans)
ans=l;
}
printf("%d\n",ans);
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: