pku 2774 , ural 1517( 后缀数组 最长公共子串 )
来源:互联网 发布:图标软件下载 编辑:程序博客网 时间:2024/04/28 04:02
题意 : 求两个字符串的最大公共子串
思路 : 将两个字符串拼接起来之后求height , 如果直接取height的最大值,那么这两个串可能来自同一个串,为了防止这样情况,对于height[i] 我们要求 sa[i-1] 和 sa[i] 分别来自不同的串,求满足上述条件的height[i]的最大值即可。
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; #define maxn 210005 int wa[maxn],wb[maxn],wv[maxn],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++; } } 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;i++) { if(k)k--; int j = sa[Rank[i]-1]; while(r[i+k]==r[j+k]) k++ ; height[Rank[i]] = k ; } return; } int r[maxn] , sa[maxn] ; char str[maxn] , str2[maxn] ;int main(){while( scanf( "%s" , str ) != EOF ) {scanf( "%s" , str2 ) ;int len = strlen( str ) ;str[len] = '$' ; str[len+1] = 0 ;strcat( str , str2 ) ;int len2 = strlen( str ) ;for( int i = 0 ; i < len2 ; i ++ ) r[i] = str[i] ;r[len2] = 0 ;da( r , sa , len2 + 1 , 200 ) ;calheight( r , sa , len2 ) ;int Max = 0 ;for( int i = 1 ; i <= len2 ; i ++ ) {if( (long long)(sa[i-1] - len ) * ( sa[i] - len ) < 0 ) {Max = max( Max , height[i] ) ;}}printf( "%d\n" , Max ) ;}return 0 ;}
0 0
- pku 2774 , ural 1517( 后缀数组 最长公共子串 )
- PKU 2774(后缀数组求最长公共子串)
- PKU 2774 Long Long Message 后缀数组 最长公共子串
- URAL 1517 Freedom of Choice (后缀数组 输出两个串最长公共子串)
- URAL 1517. Freedom of choice(后缀数组:最长公共连续子串)
- URAL 1517. Freedom of choice(后缀数组:最长公共连续子串)
- 最长公共子串--后缀数组实现
- HDU1403(后缀数组--最长公共子串)
- 最长公共子串(后缀数组)
- 最长公共子串(后缀数组)
- 【poj2774】 后缀数组最长公共子串
- 后缀数组 最长公共子串
- 【后缀数组求最长公共子串】POJ 2774
- poj 2774 后缀数组 最长公共子串
- 后缀数组之最长公共子串 poj 2774
- poj 2774 最长公共子串 后缀数组
- POJ 2774 后缀数组:求最长公共子串
- poj 2774 最长公共子串 后缀数组
- 计算斐波纳契数,分析算法复杂度
- Best Time to Buy and Sell Stock II
- 与屏幕亮度调节相关的各种方法整理
- 关联容器 map
- 嵌入式课程设计实践(五)——pthread
- pku 2774 , ural 1517( 后缀数组 最长公共子串 )
- 2014-06-30 日志
- 设计模式之十四:Decorator(装饰)—对象结构型模式
- OCP-1Z0-051-题目解析-第44题
- Oracle_11gR2_SCAN_IP scan ip配置详解
- 自己学习整理的常用知识点代码demo
- ASP.Net 文件上传大小限制解决方案修改IIS7/7.5配置
- 我的博客--序
- Java高并发高负载简单设计