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 = ;
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 = ;i < m; i++) c[i] = ;
for(i = ;i < n; i++) c[x[i] = s[i]]++;
for(i = ;i < m; i++) c[i] += c[i-];
for(i = n - ;i >= ; i--) sa[--c[x[i]]] = i;
for(int k = ;k <= n;k <<= ){
int p = ;
for(i = n - k;i < n;i++) y[p++] = i;
for(i = ;i < n;i++) if(sa[i] >= k) y[p++] = sa[i] - k; for(i = ;i < m;i++) c[i] = ;
for(i = ;i < n;i++) c[x[y[i]]]++;
for(i = ;i < m;i++) c[i] += c[i-];
for(i = n - ;i >= ;i--) sa[--c[x[y[i]]]] = y[i]; swap(x,y);
x[sa[]] = ;// 将字符彻底转变为序号;
for(i = ,p = ;i < n;i++)
x[sa[i]] = y[sa[i]] == y[sa[i-]] && y[sa[i]+k] == y[sa[i-]+k]?p-:p++;
if(p >= n) break;
m = p;
}
}
int rk[MAXN],height[MAXN];
void getHeight()
{
int i,j,k = ;
for(i = ;i <= n;i++) rk[sa[i]] = i; // rk[i]:后缀i在sa[]中的下标
for(i = ;i < n;i++){
if(k) k--;
if(rk[i] == ) continue;
j = sa[rk[i] - ];
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) == ){
int len = strlen(s);
s[len] = '#';s[len+] = '\0';
strcat(s,str);
n = strlen(s);
s[n] = '#';
build_sa('z'+,n+);
getHeight();
int ans = -;
for(int i = ;i <= n;i++)
if(height[i] > ans && ((sa[i] < len && sa[i-] > len) || (sa[i] > len && sa[i-] < len)))
ans = height[i];
printf("%d\n",ans);
}
return ;
}
poj 2774 Long Long Message 后缀数组LCP理解的更多相关文章
- POJ 2774 Long Long Message 后缀数组
Long Long Message Description The little cat is majoring in physics in the capital of Byterland. A ...
- poj 2774 Long Long Message 后缀数组基础题
Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 24756 Accepted: 10130 Case Time Limi ...
- POJ - 2774 Long Long Message (后缀数组/后缀自动机模板题)
后缀数组: #include<cstdio> #include<algorithm> #include<cstring> #include<vector> ...
- POJ 2774 Long Long Message 后缀数组模板题
题意 给定字符串A.B,求其最长公共子串 后缀数组模板题,求出height数组,判断sa[i]与sa[i-1]是否分属字符串A.B,统计答案即可. #include <cstdio> #i ...
- POJ 2774 Long Long Message (后缀数组+二分)
题目大意:求两个字符串的最长公共子串长度 把两个串接在一起,中间放一个#,然后求出height 接下来还是老套路,二分出一个答案ans,然后去验证,如果有连续几个位置的h[i]>=ans,且存在 ...
- POJ 2774 Long Long Message ——后缀数组
[题目分析] 用height数组RMQ的性质去求最长的公共子串. 要求sa[i]和sa[i-1]必须在两个串中,然后取height的MAX. 利用中间的字符来连接两个字符串的思想很巧妙,记得最后还需要 ...
- PKU 2774 Long Long Message (后缀数组练习模板题)
题意:给你两个字符串.求最长公共字串的长度. by:罗穗骞模板 #include <iostream> #include <stdio.h> #include <stri ...
- [POJ 2774] Long Long Message 【后缀数组】
题目链接:POJ - 2774 题目分析 题目要求求出两个字符串的最长公共子串,使用后缀数组求解会十分容易. 将两个字符串用特殊字符隔开再连接到一起,求出后缀数组. 可以看出,最长公共子串就是两个字符 ...
- 后缀数组(模板题) - 求最长公共子串 - poj 2774 Long Long Message
Language: Default Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 21 ...
随机推荐
- Header 与 Footer 的 DIV 高度固定, 中间内容 DIV高度自适应,内容不满一页时,默认填满屏幕。
一.需求: 页面布局分三大块: Header Body Footer 1.内容不满一页时,Footer 在屏幕最底部,Body 填充满 Header 与 Footer 中间的部分. 2.当缩小浏览器时 ...
- python解决SNIMissingWarning和InsecurePlatformWarning警告
在想要获取https站点的资源时,会报出SNIMissingWarning和InsecurePlatformWarning警告 SNIMissingWarning: An HTTPS request ...
- Java 8 Stream API Example Tutorial
Stream API Overview Before we look into Java 8 Stream API Examples, let’s see why it was required. S ...
- C++编译器的函数名修饰规则
我们知道在C++中有函数重载这样一个东西,当我们定义了几个功能类似且函数名是一样的函数的时候,只要它的参数列表不同,编译是可以通过的,但是在C中是不可以的. double add(double a, ...
- CF Tavas and Karafs (二分)
Tavas and Karafs time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- 关于Winform发布时,资源文件缺失的解决方案
今天和大家分享一下,我这几天一直困惑的问题,今天不经意间解决了这个问题,接下来描述一下这个问题: 问题: 最近公司给人家做二次开发,是C/S结构的,我在做Winform时发现,当我选择一个项目发布时, ...
- HDOJ2014青年歌手大奖赛_评委会打分
青年歌手大奖赛_评委会打分 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- Android中Universal Image Loader开源框架的简单使用
UIL (Universal Image Loader)aims to provide a powerful, flexible and highly customizable instrument ...
- MVC 提交表单
public ActionResult UserLogin() { var UserName = Request["username"]; var Password = Reque ...
- Html.ActionLink 几种重载方式说明及例子
本文整理了该方法的几种重载形式:一 Html.ActionLink("linkText","actionName")该重载的第一个参数是该链接要显示的文字,第二 ...