HDU1403 Longest Common Substring —— 后缀数组
来源:互联网 发布:金字塔软件免费版 编辑:程序博客网 时间:2024/06/15 19:00
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1403
Longest Common Substring
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6792 Accepted Submission(s): 2401
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.将两个字符串接在一起,并用特殊分隔符分开。然后跑一下后缀数组,得出height[]数组。
2.枚举height[]数组,如果排位相邻的两个后缀字符串的开头分别在两边(原先的两个字符串),则更新答案。
后缀数组学习推荐:
http://blog.csdn.net/yxuanwkeith/article/details/50636898
http://blog.csdn.net/j_sure/article/details/41777097
代码如下:
#include <bits/stdc++.h>using namespace std;typedef long long LL;const int INF = 2e9;const LL LNF = 9e18;const int mod = 1e9+7;const int MAXN = 2e5+10;bool cmp(int *r, int a, int b, int l){ return r[a]==r[b] && r[a+l]==r[b+l];}int t1[MAXN], t2[MAXN], c[MAXN];void DA(int str[], int sa[], int Rank[], int height[], int n, int m){ int i, j, p, *x = t1, *y = t2; for(i = 0; i<m; i++) c[i] = 0; for(i = 0; i<n; i++) c[x[i] = str[i]]++; for(i = 1; i<m; i++) c[i] += c[i-1]; for(i = n-1; i>=0; i--) sa[--c[x[i]]] = i; for(j = 1; j<=n; j <<= 1) { p = 0; for(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<m; i++) c[i] = 0; for(i = 0; i<n; i++) c[x[y[i]]]++; for(i = 1; i<m; i++) c[i] += c[i-1]; for(i = n-1; i>=0; i--) sa[--c[x[y[i]]]] = y[i]; swap(x, y); p = 1; x[sa[0]] = 0; for(i = 1; i<n; i++) x[sa[i]] = cmp(y, sa[i-1], sa[i], j)?p-1:p++; if(p>=n) break; m = p; } int k = 0; for(i = 0; i<=n; i++) Rank[sa[i]] = i; for(i = 0; i<n; i++) { if(k) k--; j = sa[Rank[i]-1]; while(str[i+k]==str[j+k]) k++; height[Rank[i]] = k; }}char a[MAXN], b[MAXN];int r[MAXN], sa[MAXN], Rank[MAXN], height[MAXN];// sa[]下标的有效值:0~n-1; height[]下标的有效值:1~n-1int main(){ while(scanf("%s%s", a, b)!=EOF) { int lena, lenb, len; lena = strlen(a); lenb = strlen(b); len = 0; for(int i = 0; i<lena; i++) r[len++] = a[i]; r[len++] = 1; for(int i = 0; i<lenb; i++) r[len++] = b[i]; r[len++] = 0; DA(r, sa, Rank, height, len, 128); int ans = 0; for(int i = 1; i<len; i++) if( (sa[i]<lena && sa[i-1]>lena) || (sa[i]>lena && sa[i-1]<lena) ) ans = max(ans, height[i]); printf("%d\n", ans); } return 0;}
阅读全文
0 0
- HDU1403 Longest Common Substring —— 后缀数组
- HDU1403 Longest Common Substring 后缀数组
- HDU1403 Longest Common Substring
- HDU1403-Longest Common Substring
- HDU1403 - Longest Common Substring(后缀数组求nlog(n)的最长公共子序列)
- hdu1403---Longest Common Substring(后缀数组求2个字符串的最长公共子串)
- HDU1403 Longest Common Substring(最长公共子串、后缀数组入门)
- HDU 1403 Longest Common Substring 【 后缀数组】
- hdu 1403 Longest Common Substring - 后缀数组
- 【后缀数组】 HDOJ 1403 Longest Common Substring
- hdu 1403 Longest Common Substring (后缀数组)
- 后缀数组 spoj LCS - Longest Common Substring
- HDOJ 1403:Longest Common Substring 后缀数组的用处
- hdu 1403 Longest Common Substring (后缀数组模板题)
- hdu 1403 Longest Common Substring (后缀数组模板)
- hdu 1403 Longest Common Substring 后缀数组da算法
- HDU 1403 Longest Common Substring(后缀数组入门)
- LCS(longest common subsequence)与LCS(longest common substring)以及后缀数组
- linux常用基础命令
- c++日记——WIndows和Linux下左移模板函数的不同
- Hello world
- 腾讯大牛教你web前后端漏洞分析与防御-接入层上传问题
- Java中的三元运算符?: error: not a statement
- HDU1403 Longest Common Substring —— 后缀数组
- 多线程多个实例
- Eclipse安装反编译工具Eclipse Class Decompiler:实现不下载源码,查看源文件
- Linux setup kafka
- UVA
- Java基础知识
- Java中为什么int、double首字母小写而String大写
- 排序算法之交换排序(Java)
- 如何压缩图片大小但不失真