poj 2774 Long Long Message 后缀数组LCP理解

题目链接

题意:给两个长度不超过1e5的字符串,问两个字符串的连续公共子串最大长度为多少?

思路:两个字符串连接之后直接后缀数组+LCP,在height中找出max同时满足一左一右即可;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<time.h>
using namespace std;
typedef long long ll;
const int MAXN = 200007;
char s[MAXN],str[MAXN];
int sa[MAXN],t[MAXN],t2[MAXN],c[MAXN],n;
void build_sa(int m,int n) // m为字符ASCII码的最大值+1;n = strlen(s) + 1;
{
    int i,*x = t, *y = t2;
    for(i = 0;i < m; i++) c[i] = 0;
    for(i = 0;i < n; i++) c[x[i] = s[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(int k = 1;k <= n;k <<= 1){
        int p = 0;
        for(i = n - k;i < n;i++) y[p++] = i;
        for(i = 0;i < n;i++) if(sa[i] >= k) y[p++] = sa[i] - k;

        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);
        x[sa[0]] = 0;// 将字符彻底转变为序号;
        for(i = 1,p = 1;i < n;i++)
            x[sa[i]] = y[sa[i]] == y[sa[i-1]] && y[sa[i]+k] == y[sa[i-1]+k]?p-1:p++;
        if(p >= n) break;
        m = p;
    }
}
int rk[MAXN],height[MAXN];
void getHeight()
{
    int i,j,k = 0;
    for(i = 1;i <= n;i++) rk[sa[i]] = i; // rk[i]:后缀i在sa[]中的下标
    for(i = 0;i < n;i++){
        if(k) k--;
        if(rk[i] == 0) continue;
        j = sa[rk[i] - 1];
        while(i+k<n && j+k<n && s[i+k] == s[j+k]) k++;
        height[rk[i]] = k; // h[i] = height[rk[i]]; h[i] >= h[i-1] - 1;
    }
}
int main()
{
    while(scanf("%s%s",s,str) == 2){
        int len = strlen(s);
        s[len] = ‘#‘;s[len+1] = ‘\0‘;
        strcat(s,str);
        n = strlen(s);
        s[n] = ‘#‘;
        build_sa(‘z‘+1,n+1);
        getHeight();
        int ans = -1;
        for(int i = 2;i <= n;i++)
            if(height[i] > ans && ((sa[i] < len && sa[i-1] > len) || (sa[i] > len && sa[i-1] < len)))
                ans = height[i];
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-12-23 18:25:47

poj 2774 Long Long Message 后缀数组LCP理解的相关文章

poj 2774 Long Long Message(后缀数组入门题)

1 /****************************************************************** 2 题目: Long Long Message(poj 2774) 3 链接: http://poj.org/problem?id=2774 4 题意: 给两个字符串,找最长的公共子串 5 算法: 后缀数组 6 算法思想: 后缀数组就是套模板求先应得数组,这题用到了两个数组,分 7 别是sa[],height[];sa[i]表示所有后缀按字典数排序后以s[i

poj 2774 Long Long Message 后缀数组基础题

Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 24756   Accepted: 10130 Case Time Limit: 1000MS Description The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to him these days: his mother is ge

poj 2774 Long Long Message 后缀数组

点击打开链接题目链接 Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 23327   Accepted: 9566 Case Time Limit: 1000MS Description 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数组RMQ的性质去求最长的公共子串. 要求sa[i]和sa[i-1]必须在两个串中,然后取height的MAX. 利用中间的字符来连接两个字符串的思想很巧妙,记得最后还需要空一个位置避免冲突. [代码] #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <map> #include <set>

PKU 2774 Long Long Message (后缀数组练习模板题)

题意:给你两个字符串,求最长公共字串的长度. by:罗穗骞模板 #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; #define M 303 #define inf 0x3fffffff #define maxn 500000 #define ws ww #define rank RANK #define F

poj 1743 最长不重叠重复子串 后缀数组+lcp+二分

题比较容易读懂,但是建模需动点脑子: 一个子串加常数形成的子串认为跟子串相同,求最长不重叠重复子串 题目中说 is disjoint from (i.e., non-overlapping with) at least one of its other appearance(s) 意味着不能重叠,举个例子 1, 2,3,  52, 53,54 1,2, 3和 52, 53,54满足题意,差值为51 枚举差值肯定不行------看了题解明白的:: 后项减去前一项得到: 1,1,1,49,1,1  

POJ 2774 Long Long Message (最长公共子串)

Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 27062   Accepted: 11010 Case Time Limit: 1000MS Description The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to him these days

HUID 5558 Alice&#39;s Classified Message 后缀数组+单调栈+二分

http://acm.hdu.edu.cn/showproblem.php?pid=5558 对于每个后缀suffix(i),想要在前面i - 1个suffix中找到一个pos,使得LCP最大.这样做O(n^2) 考虑到对于每一个suffix(i),最长的LCP肯定在和他排名相近的地方取得. 按排名大小顺序枚举位置,按位置维护一个递增的单调栈,对于每一个进栈的元素,要算一算栈内元素和他的LCP最大是多少. 如果不需要输出最小的下标,最大的直接是LCP(suffix(st[top]),  suff

POJ 3729 Facer’s string (后缀数组)

题目大意: 串1中有多少个后缀和 串2中的某个后缀 的lcp 为 k 思路分析: 先找出 长度至少为k的对数有多少. 再找出 至少为k+1的有多少 然后相减. #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <map> #include <string> #define maxn 110005 using na