POJ 2774 后缀数组
题目链接:http://poj.org/problem?id=2774
题意:给定两个只含小写字母的字符串,求字符串的最长公共子串长度。
思路:根据<<后缀数组——处理字符串的有力工具>>的思路,字符串的任何一个子串都是这个字符串的某个后缀的前缀 。求 A和 B的最长公共子串等价于求 A的后缀和 B的后缀的最长公共前缀的最大值。 如果枚举 A 和 B的所有的后缀,那么这样做显然效率低下。由于要计算 A的后缀和 B的后缀的最长公共前缀, 所以先将第二个字符串写在第一个字符串后面, 中问用一个没有出現过的字符隔开, 再求这个新的字符串的后缀数组。
那么是不是所有的 height值中的最大值就是答案呢?不一定!有可能这两个后缀是在同一个字符串中的, 所以实际上只有当 suffix(sa[i-1])和suffix(sa[i])不是同一个字符串中的两个后缀时, height[i]才是满是条件的 。 而这其中的最大值就是答案。记字符串 A和字符串 B的长度分别为lAl和lBl 。求新的字符串的后缀数组和 height数组的时间是 0( IAl+l Bl ) , 然后求排名相邻但原来不在同一个字符串中的两个后缀的 height值的最大值, 时间也是0(lAl+lBl) , 所以整个做法的时间复杂度为 0(lAl+lBl) 。
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<time.h>
#include<cmath>
using namespace std;
typedef long long int LL;
const int MAXN = + ;
int cmp(int *r, int a, int b, int l){
return r[a] == r[b] && r[a + l] == r[b + l];
}
int wa[MAXN], wb[MAXN], wv[MAXN], WS[MAXN];
void da(int *r, int *sa, int n, int m){
int i, j, p, *x = wa, *y = wb, *t;
for (i = ; i < m; i++) { WS[i] = ; }
for (i = ; i < n; i++) { WS[x[i] = r[i]]++; }
for (i = ; i < m; i++) { WS[i] += WS[i - ]; }
for (i = n - ; i >= ; i--) { sa[--WS[x[i]]] = i; }
for (j = , p = ; p<n; j *= , m = p)
{
for (p = , i = n - j; i < n; i++) { y[p++] = i; }
for (i = ; i < n; i++) {
if (sa[i] >= j){ y[p++] = sa[i] - j; }
}
for (i = ; i < n; i++) { wv[i] = x[y[i]]; }
for (i = ; i < m; i++) { WS[i] = ; }
for (i = ; i < n; i++) { WS[wv[i]]++; }
for (i = ; i < m; i++) { WS[i] += WS[i - ]; }
for (i = n - ; i >= ; i--) { sa[--WS[wv[i]]] = y[i]; }
for (t = x, x = y, y = t, p = , x[sa[]] = , i = ; i < n; i++){
x[sa[i]] = cmp(y, sa[i - ], sa[i], j) ? p - : p++;
}
}
return;
}
int Rank[MAXN], height[MAXN], sa[MAXN];
void calheight(int *r, int *sa, int n){
int i, j, k = ;
for (i = ; i <= n; i++) { Rank[sa[i]] = i; }
for (i = ; i < n; height[Rank[i++]] = k){
for (k ? k-- : , j = sa[Rank[i] - ]; r[i + k] == r[j + k]; k++);
}
return;
}
int r[MAXN], Ca = , len,index;
char str[MAXN],ch[MAXN];
void solve(){
int ans = ;
for (int i = ; i < len; i++){
int L = height[i]; //公共前缀
int Fidx = min(sa[i - ], sa[i]);
int Sidx = max(sa[i - ], sa[i]);
if (Fidx<index&&Sidx>index){ //该两个后缀的起点分布在'#'左右两端
ans = max(ans, L);
}
}
printf("%d\n", ans);
}
int main(){
//#ifdef kirito
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
//#endif
// int start = clock();
while (~scanf("%s%s", str,ch)){
index = strlen(str);
strcat(str, "#"); strcat(str, ch);
len = strlen(str);
for (int i = ; i < len; i++){
if (str[i] == '#'){ r[i] = ; continue; }
r[i] = str[i]-'a'+;
}
da(r, sa, len, );
calheight(r, sa, len-);
solve();
}
//#ifdef LOCAL_TIME
// cout << "[Finished in " << clock() - start << " ms]" << endl;
//#endif
return ;
}
POJ 2774 后缀数组的更多相关文章
- POJ 2774 后缀数组 || 二分+哈希
Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 35607 Accepted: 14 ...
- POJ 2774 后缀数组:查找最长公共子
思考:其实很easy.就在两个串在一起.通过一个特殊字符,中间分隔,然后找到后缀数组的最长的公共前缀.然后在两个不同的串,最长是最长的公共子串. 注意的是:用第一个字符串来推断是不是在同一个字符中,刚 ...
- poj 2774 后缀数组 两个字符串的最长公共子串
Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 31904 Accepted: 12 ...
- Long Long Message POJ - 2774 后缀数组
The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to him ...
- POJ 2774 (后缀数组 最长公共字串) Long Long Message
用一个特殊字符将两个字符串连接起来,然后找最大的height,而且要求这两个相邻的后缀的第一个字符不能在同一个字符串中. #include <cstdio> #include <cs ...
- [poj 2274]后缀数组+最长公共子串
题目链接:http://poj.org/problem?id=2774 后缀数组真的太强大了,原本dp是O(nm)的复杂度,在这里只需要O(n+m). 做法:将两个串中间夹一个未出现过的字符接起来,然 ...
- poj 3693 后缀数组 重复次数最多的连续重复子串
Maximum repetition substring Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8669 Acc ...
- POJ - 2774~POJ - 3415 后缀数组求解公共字串问题
POJ - 2774: 题意: 求解A,B串的最长公共字串 (摘自罗穗骞的国家集训队论文): 算法分析: 字符串的任何一个子串都是这个字符串的某个后缀的前缀. 求 A 和 B 的最长 公共子串等价于求 ...
- POJ 3415 后缀数组
题目链接:http://poj.org/problem?id=3415 题意:给定2个串[A串和B串],求两个串公共子串长度大于等于k的个数. 思路:首先是两个字符串的问题.所以想用一个'#'把两个字 ...
随机推荐
- hiho一下第一周 最长回文子串
题目链接:http://hihocoder.com/contest/hiho1/problem/1 #include <iostream> #include <cstdio> ...
- LeetCode 453 Minimum Moves to Equal Array Elements
Problem: Given a non-empty integer array of size n, find the minimum number of moves required to mak ...
- 一次工作中用到的Highcharts.Chart
一般动态获取图表信息都是通过ajax交互传送数据. 这次是一次性从后台返回集合后,直接在页面取数据绘制图表 引用js <script type="text/javascript&quo ...
- powerdesigner新建模型教程
1.现在开始使用PowerDesigner创建数据库,首先运行程序,进入主界面:
- 地图API文档
目录 腾讯地图API 2 1.API概览... 2 1.1 WebService API(官网注明是beta版本,可能不稳定,慎用):... 2 1.2 URL API:... 2 1.3 静态图AP ...
- 129. Sum Root to Leaf Numbers
题目: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a nu ...
- 在cmd窗口中运行php命令
1.首先安装php.我使用的是wamp,里面包含php5.5.12 2.将C:\wamp\bin\php\php5.5.12添加到环境变量Path中 3.在cmd中运行php -v可以查看php版本相 ...
- iOS - 二维码扫描和应用跳转
序言 前面我们已经调到过怎么制作二维码,在我们能够生成二维码之后,如何对二维码进行扫描呢? 在iOS7之前,大部分应用中使用的二维码扫描是第三方的扫描框架,例如ZXing或者ZBar.使用时集成麻烦, ...
- 动手动脑之小程序:TryAndCatch
源代码 import java.util.InputMismatchException;import java.util.Scanner;public class TryAndCatch {publi ...
- EF – 2.EF数据查询基础(上)查询数据的实用编程技巧
目录 5.4.1 查询符合条件的单条记录 EF使用SingleOrDefault()和Find()两个方法查询符合条件的单条记录. 5.4.2 Entity Framework中的内部数据缓存 DbS ...