hdu 1403 Longest Common Substring 后缀数组求最长公共子串
2016-08-13 11:36
543 查看
Longest Common Substring
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5823 Accepted Submission(s): 2063
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的方法求复杂度是o(n^2),显然会超时。这个时候我们使用后缀数组,由于所有子串分别为母串某一个后缀的前缀,所以只需将两串组合在一起形成一条母串,然后求最大的height[i]即可。入门后缀数组应用。
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5823 Accepted Submission(s): 2063
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的方法求复杂度是o(n^2),显然会超时。这个时候我们使用后缀数组,由于所有子串分别为母串某一个后缀的前缀,所以只需将两串组合在一起形成一条母串,然后求最大的height[i]即可。入门后缀数组应用。
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn=200050; char s1[maxn],s2[maxn],str[maxn]; int sa[maxn],r[maxn]; int wa[maxn],wb[maxn],wv[maxn]; int ws[maxn]; int cmp(int *r,int a,int b,int l) {return r[a]==r[b]&&r[a+l]==r[b+l];} void da(int *r,int *sa,int n,int m) { int i,j,p,*x=wa,*y=wb,*t; for(i=0;i<m;i++) ws[i]=0; for(i=0;i<n;i++) ws[x[i]=r[i]]++; for(i=1;i<m;i++) ws[i]+=ws[i-1]; for(i=n-1;i>=0;i--) sa[--ws[x[i]]]=i; for(j=1,p=1;p<n;j*=2,m=p) { for(p=0,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<n;i++) wv[i]=x[y[i]]; for(i=0;i<m;i++) ws[i]=0; for(i=0;i<n;i++) ws[wv[i]]++; for(i=1;i<m;i++) ws[i]+=ws[i-1]; for(i=n-1;i>=0;i--) sa[--ws[wv[i]]]=y[i]; for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++) x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++; } return; } int Rank[maxn],height[maxn]; void calheight(int *r,int *sa,int n) { int i,j,k=0; for(i=1;i<=n;i++) Rank[sa[i]]=i; for(i=0;i<n;height[Rank[i++]]=k) for(k?k--:0,j=sa[Rank[i]-1];r[i+k]==r[j+k];k++); return; } int ans; int main() { while(scanf("%s",str)!=EOF) { /*int len1=strlen(s1); scanf("%s",str); //int len2=strlen(s2); //strcat(str,s1); str[len1]='0'; scanf("%s",str+len1+1); //strcat(str,s2); //str[len1+len2+1]='\0'; int n=strlen(str); //puts(str);*/ int n=strlen(str); int len1=n; str ='9'; scanf("%s",str+len1+1); //puts(str); n=strlen(str); for(int i=0;i<n;i++) r[i]=str[i]; r =0; da(r,sa,n,300); calheight(r,sa,n); /*for(int i=0;i<=n;i++) { printf("%d %d\n",sa[i],height[i]); }*/ int Max=-1; for(int i=2;i<n;i++) { if(((sa[i]>=0&&sa[i]<len1)&&(sa[i-1]>len1&&sa[i-1]<n))||((sa[i-1]>=0&&sa[i-1]<len1)&&(sa[i]>len1&&sa[i]<n))) { Max=max(Max,height[i]); } } printf("%d\n",Max); } }
相关文章推荐
- HDU 1403 Longest Common Substring(后缀数组,最长公共子串)
- HDU 1403 Longest Common Substring(后缀数组 最长公共子串)
- hdu 1403 Longest Common Substring 后缀数组求最长公共子串
- hdu1403---Longest Common Substring(后缀数组求2个字符串的最长公共子串)
- POJ 2774 Long Long Message+Hdu 1403 Longest Common Substring (后缀数组 最长公共子串)
- HDU1403 Longest Common Substring(最长公共子串、后缀数组入门)
- HDU 1403 Longest Common Substring(最长公共子串)
- HDU 1403 & POJ 2774 Longest Common Substring (后缀数组啊 求最长公共子串 模板题)
- hdu_1403_Longest Common Substring(后缀数组的应用)
- SPOJ 题目1812 LCS2 - Longest Common Substring II(后缀自动机求多个串的最长公共子串)
- HDU1403 - Longest Common Substring(后缀数组求nlog(n)的最长公共子序列)
- SPOJ 题目1811 LCS - Longest Common Substring(后缀自动机求最长公共子串)
- SPOJ 1811 Longest Common Substring (后缀自动机第一题,求两个串的最长公共子串)
- SPOJ 1812 Longest Common Substring II 后缀自动机求多字符串最长公共子串
- hdu 1403 Longest Common Substring (后缀数组模板题)
- hdu1403 后缀数组求最长公共子串
- hdu 1403 Longest Common Substring - 后缀数组
- poj2774 Long Long Message && hdu 1403 Longest Common Substring 最长公共字串【后缀数组(倍增)】
- hdu1403 && poj 2774 最长公共子串 后缀数组
- HDU1403(后缀数组--最长公共子串)