题目链接: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 后缀数组的更多相关文章

  1. POJ 2774 后缀数组 || 二分+哈希

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 35607   Accepted: 14 ...

  2. POJ 2774 后缀数组:查找最长公共子

    思考:其实很easy.就在两个串在一起.通过一个特殊字符,中间分隔,然后找到后缀数组的最长的公共前缀.然后在两个不同的串,最长是最长的公共子串. 注意的是:用第一个字符串来推断是不是在同一个字符中,刚 ...

  3. poj 2774 后缀数组 两个字符串的最长公共子串

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 31904   Accepted: 12 ...

  4. 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 ...

  5. POJ 2774 (后缀数组 最长公共字串) Long Long Message

    用一个特殊字符将两个字符串连接起来,然后找最大的height,而且要求这两个相邻的后缀的第一个字符不能在同一个字符串中. #include <cstdio> #include <cs ...

  6. [poj 2274]后缀数组+最长公共子串

    题目链接:http://poj.org/problem?id=2774 后缀数组真的太强大了,原本dp是O(nm)的复杂度,在这里只需要O(n+m). 做法:将两个串中间夹一个未出现过的字符接起来,然 ...

  7. poj 3693 后缀数组 重复次数最多的连续重复子串

    Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8669   Acc ...

  8. POJ - 2774~POJ - 3415 后缀数组求解公共字串问题

    POJ - 2774: 题意: 求解A,B串的最长公共字串 (摘自罗穗骞的国家集训队论文): 算法分析: 字符串的任何一个子串都是这个字符串的某个后缀的前缀. 求 A 和 B 的最长 公共子串等价于求 ...

  9. POJ 3415 后缀数组

    题目链接:http://poj.org/problem?id=3415 题意:给定2个串[A串和B串],求两个串公共子串长度大于等于k的个数. 思路:首先是两个字符串的问题.所以想用一个'#'把两个字 ...

随机推荐

  1. 【leetcode】Plus One (easy)

    Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...

  2. C/S love自编程序

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  3. win7下安装openpyxl

    想使用python来操作Excel,看资料据说openpyxl非常好用,于是到https://pypi.python.org/pypi/openpyxl下载了安装包.下面就来说说安装步骤,也算是对自己 ...

  4. selinux

    root@lujie ~]# vim /etc/sysconfig/selinux # This file controls the state of SELinux on the system. # ...

  5. 将txt多行文本合并成一行

    1.用word打开txt文本2.打开“替换”功能,查找内容“^p”,替换内容为“,”(均无双引号).即可把多列文字合并为一行.

  6. September 21st 2016 Week 39th Wednesday

    Don't try so hard, the best things come when you least expect them. 不要着急,最好的总会在最不经意的时候出现. Always tur ...

  7. July 3rd, Week 28th Sunday, 2016

    2016-07-03 First-hand experience is often necessary for personal growth. 亲身体验通常是成长所必须的. Although som ...

  8. Linux Shell多命令执行

    有三种: :只是顺序执行,命令之间没有任何关联,不相互影响.如  ls;date;cd /etc/ 如,创建100M的文件. && 命令之间有关系,只有前一条命令正确执行才会执行下面一 ...

  9. http协议之request

    一.请求的基本格式 请求的基本格式包括请求行,请求头,请求实体三部分.例如:GET /img/bd_logo1.png HTTP/1.1Accept: */*Referer: http://www.b ...

  10. NYOJ题目766回文数

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsgAAAHaCAIAAACSPygsAAAgAElEQVR4nO3dO3LqSheG4X8S5AyEWB ...