后缀数组:HDU1043 Longest Common Substring

Longest Common Substring

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5375    Accepted Submission(s): 1910


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.
 

 

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.
 

 

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
 
  这题求最长公共字串,简单的DP并不可以搞定,因为数据范围len<=10^5,所以我用后缀数组做的。
  这里将两个字符串连起来,中间用特殊符号隔开,只要两个在排序上连续的后缀的起点在断点两边,就用其更新答案。
 
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 
 5 
 6 const int maxn=200010;
 7 int Ws[maxn],Wv[maxn],Wa[maxn],Wb[maxn],lcp[maxn],sa[maxn],rank[maxn],r[maxn];
 8 bool cmp(int *p,int i,int j,int l)
 9 {return p[i]==p[j]&&p[i+l]==p[j+l];}
10 
11 void DA(int n,int m)
12 {
13     int i,j,p,*x=Wa,*y=Wb;
14     for(i=0;i<m;i++)Ws[i]=0;
15     for(i=0;i<n;i++)++Ws[x[i]=r[i]];
16     for(i=1;i<m;i++)Ws[i]+=Ws[i-1];
17     for(i=n-1;i>=0;i--)
18         sa[--Ws[x[i]]]=i;
19     for(j=1,p=1;p<n;j*=2,m=p)
20     {
21         for(p=0,i=n-j;i<n;i++)y[p++]=i;
22         for(i=0;i<n;i++)
23             if(sa[i]>=j)
24                 y[p++]=sa[i]-j;
25         
26         for(i=0;i<m;i++)Ws[i]=0;
27         for(i=0;i<n;i++)Wv[i]=x[y[i]];
28         for(i=0;i<n;i++)++Ws[Wv[i]];
29         for(i=1;i<m;i++)Ws[i]+=Ws[i-1];
30         for(i=n-1;i>=0;i--)sa[--Ws[Wv[i]]]=y[i];
31         for(std::swap(x,y),p=1,i=1,x[sa[0]]=0;i<n;i++)
32             x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;        
33     }    
34 }
35 void LCP(int n)
36 {
37     int i,j,k=0;
38     for(i=1;i<=n;i++)
39         rank[sa[i]]=i;
40     
41     for(i=0;i<n;lcp[rank[i++]]=k)
42         for(k?--k:k,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);    
43 }
44 char str1[maxn>>1],str2[maxn>>1];
45 int main()
46 {
47     int i,len1,len2,ans;
48     while(~scanf("%s",str1)){
49         for(len1=0;str1[len1];len1++)
50             r[len1]=str1[len1];
51         r[len1]='$';
52         scanf("%s",str2);
53         for(len2=0;str2[len2];len2++)
54             r[len1+len2+1]=str2[len2];
55         DA(len1+len2+2,128);
56         LCP(len1+len2+1);
57         ans=0;
58         for(i=1;i<len1+len2+1;i++)
59             if(sa[i]<len1&&sa[i-1]>len1||sa[i-1]<len1&&sa[i]>len1)
60                 ans=std::max(ans,lcp[i]);
61         printf("%d\n",ans);    
62     }
63     return 0;
64 }

 

 

转载于:https://www.cnblogs.com/TenderRun/p/5198379.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值