Longest Common Substring
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3218 Accepted Submission(s): 1164
Problem Description
Given two strings, you have to tell the length of the Longest Common Substring of them.
For example:
str1 = banana
str2 = cianaic
So the Longest Common Substring is "ana", and the length is 3.
For example:
str1 = banana
str2 = cianaic
So the Longest Common Substring is "ana", and the length is 3.
Input
The input contains several test cases. Each test case contains two strings, each string will have at most 100000 characters. All the characters are in lower-case.
Process to the end of file.
Process to the end of file.
Output
For each test case, you have to tell the length of the Longest Common Substring of them.
Sample Input
banana cianaic
Sample Output
3
Author
Ignatius.L
本题要求两个字符串的最长公共子串。求两个字符串的最长公共子串是个经典的问题。本问题可以由三种想法:
1.基本思想,即枚举两个字符串的所有后缀,求出他们最长的前缀,时间复杂度为O(n^3);
2.动态规划,dp[i][j]表示以str1[i]和str2[j]结尾的最长公共子串的长度,状态转移方程为:
若str1[i]==str2[j],dp[i][j]=dp[i-1][j-1]+1;否则,dp[i][j]=0;
3.使用后缀树(大多数时候使用它的替代品-后缀数组)。
本题使用后缀数组:欲掌握后缀数组,请参考牛人的论文;算法合集之《后缀数组——处理字符串的有力工具》
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define maxn 200100
char str[maxn];
int h2,ans,len1,B[maxn];//B某个桶空余空间尾部的索引
int *S1,*S2,*R1,*R2;
int mem[4][maxn],height[maxn];
void CreateSuffixArray(char* szText,int L)
{
int i,h;
S1=mem[0]; //h阶后缀数组
S2=mem[1]; //2h阶后缀数组
R1=mem[2]; //h阶Rank数组
R2=mem[3]; //2h阶Rank数组
for(i=0;i<L;i++)//求对应桶中元素个数
B[szText[i]]++;
for(i=1;i<256;i++)//
B[i]+=B[i-1];
for(i=L-1;i>=0;i--)
S1[--B[szText[i]]]=i;
for(R1[S1[0]]=0,i=1;i<L;i++)
{
if(szText[S1[i]]==szText[S1[i-1]])
R1[S1[i]]=R1[S1[i-1]];
else
R1[S1[i]]=R1[S1[i-1]]+1;
}
for(h=1;h<L&&R1[S1[L-1]]<L-1;h*=2)
{
for(i=0;i<L;i++)
B[R1[S1[i]]]=i;
for(i=L-1;i>=0;i--)
{
if(h<=S1[i])
S2[B[R1[S1[i]-h]]--]=S1[i]-h;
}
for(i=L-h,h2=L-(h>>1);i<h2;i++)
S2[B[R1[i]]]=i;
for(R2[S2[0]],i=1;i<L;i++)
{
if(R1[S2[i]]!=R1[S2[i-1]]||R1[S2[i]+h]!=R1[S2[i-1]+h])
R2[S2[i]]=R2[S2[i-1]]+1;
else
R2[S2[i]]=R2[S2[i-1]];
}
swap(S1,S2);
swap(R1,R2);
}
}
void calheight(char* r,int L)
{
int i, j, k;
for(i=0,k=0;i<L;i++)
{
if(R1[i]==L-1) height[R1[i]]=k=0;
else
{
if (k>0)k--;
j=S1[R1[i]+1];
for(;r[i+k]==r[j+k];k++);
height[R1[i]]=k;
}
}
}
int main()
{
int i,n;
while (~scanf("%s",str))
{
ans=0;
memset(height,0,sizeof(height));
memset(B,0,sizeof(B));
memset(mem,0,sizeof(mem));
len1=strlen(str);
str[len1++]='#';
scanf("%s",str+len1);
n=strlen(str);
str[n++]=0;
CreateSuffixArray(str,n);
calheight(str,n);
for (i=0;i<n-1;i++)
{
if ((S1[i]-len1)*(S1[i+1]-len1)<=0&&height[i]>ans)
ans=height[i];
}
printf("%d\n",ans);
}
return 0;
}