hdu1403(后缀数组)
来源:互联网 发布:郭敬明 梦里花落知多少 编辑:程序博客网 时间:2024/06/07 15:47
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
bananacianaic
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 200100char 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;}
- hdu1403 poj2774(后缀数组)
- hdu1403(后缀数组)
- hdu1403 简单的后缀数组
- 后缀数组poj2774(hdu1403)ural1517
- poj2774,hdu1403 lcs 后缀数组,后缀自动机
- HDU1403 Longest Common Substring 后缀数组
- HDU1403(后缀数组--最长公共子串)
- hdu1403 求最长公共前缀 后缀数组
- hdu1403(后缀数组求lcs模板)
- 后缀数组模板(求最长公共子串,hdu1403)
- hdu1403 后缀数组求最长公共子串
- HDU1403 Longest Common Substring —— 后缀数组
- hdu1403 && poj 2774 最长公共子串 后缀数组
- poj 2774 Long Long Message,后缀数组,求最长公共子串 hdu1403
- HDU1403 - Longest Common Substring(后缀数组求nlog(n)的最长公共子序列)
- hdu1403---Longest Common Substring(后缀数组求2个字符串的最长公共子串)
- HDU1403 Longest Common Substring(最长公共子串、后缀数组入门)
- hdu1403
- 转:Android app项目开发步骤总结
- 如何测试数据库
- Linq学习笔记
- [ webservice ] webservice系列1
- [FAQ04205][FAQ04204][Power] mtk android某个核的 频率和控制在某个频率
- hdu1403(后缀数组)
- poj 3114 Countries in War(缩点+最短路)
- CreateDIBSection 是怎么使用内存的?
- grunt学习(四)——css资源的合并和压缩
- SQL Server优化50法
- 实现割绳子游戏(来源网络)
- [FAQ04241][BMT]充电低电量(少于5%)的时候开 机充电图标默认是没有显示动画,如果需要充电 动画,该如何配置
- Android中asset文件夹和raw文件夹区别
- 存储系统性能 - 带宽计算