Description

求两个串的最长连续公共字串

Solution

后缀数组入门题吧

把两个串连在一起,中间加一个分隔符,然后跑一遍后缀数组,得到 height 和 sa

一个 height[i] 对答案有贡献的充要条件是 sa[i] 和 sa[i-1] 分别在两个串中

Code

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 200200;
char s1[N], s2[N], S[N];
int n, tmpn, cnt[N], ans, sa[N], rk[N], height[N];
struct node { int id, x, y; } a[N], b[N];
int main() {
scanf("%s %s", s1, s2); tmpn = strlen(s1);
for(int i = 0; s1[i]; i++) S[++n] = s1[i]; S[++n] = '#';
for(int i = 0; s2[i]; i++) S[++n] = s2[i];
for(int i = 1; i <= n; i++) cnt[S[i]] = 1;
for(int i = 0; i <= 128; i++) cnt[i] += cnt[i - 1];
for(int i = 1; i <= n; i++) rk[i] = cnt[S[i]];
for(int L = 1; L <= n; L *= 2) {
for(int i = 1; i <= n; i++)
a[i].id = i, a[i].x = rk[i], a[i].y = rk[i + L];
for(int i = 1; i <= n; i++) cnt[i] = 0;
for(int i = 1; i <= n; i++) cnt[a[i].y]++;
for(int i = 1; i <= n; i++) cnt[i] += cnt[i - 1];
for(int i = 1; i <= n; i++) b[cnt[a[i].y]--] = a[i];
for(int i = 1; i <= n; i++) cnt[i] = 0;
for(int i = 1; i <= n; i++) cnt[a[i].x]++;
for(int i = 1; i <= n; i++) cnt[i] += cnt[i - 1];
for(int i = n; i >= 1; i--) a[cnt[b[i].x]--] = b[i];
for(int i = 1; i <= n; i++)
if(a[i].x == a[i - 1].x && a[i].y == a[i - 1].y)
rk[a[i].id] = rk[a[i - 1].id];
else rk[a[i].id] = rk[a[i - 1].id] + 1;
} for(int i = 1; i <= n; i++) sa[rk[i]] = i;
int k = 0;
for(int i = 1; i <= n; i++) {
int j = sa[rk[i] - 1]; if(k) k--;
while(i + k <= n && j + k <= n && S[i + k] == S[j + k]) k++;
height[rk[i]] = k;
} for(int i = 1; i <= n; i++)
if(sa[i] <= tmpn && sa[i - 1] > tmpn ||
sa[i] > tmpn && sa[i - 1] <= tmpn)
ans = max(ans, height[i]);
printf("%d\n", ans);
return 0;
}

题解【poj2774 Long Long Message】的更多相关文章

  1. POJ2774 Long Long Message —— 后缀数组 两字符串的最长公共子串

    题目链接:https://vjudge.net/problem/POJ-2774 Long Long Message Time Limit: 4000MS   Memory Limit: 131072 ...

  2. POJ2774 Long Long Message 【SAM】

    POJ2774 Long Long Message 找两个串的最长公共字串 对其中一个串\(s\)建\(SAM\),然后我们如何找到最长公共字串,办法就是枚举\(t\)串所有的前缀,然后找各个前缀的最 ...

  3. [POJ2774]Long Long Message 解题报告

    Long Long Message Description The little cat is majoring in physics in the capital of Byterland. A p ...

  4. POJ2774 Long Long Message [后缀数组]

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 29277   Accepted: 11 ...

  5. poj2774 Long Long Message(后缀数组or后缀自动机)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Long Long Message Time Limit: 4000MS   Me ...

  6. 题解 CF950B 【Intercepted Message】

    题目链接 先吐槽一番:本宝宝好久没写过题解了...首先我们想一个贪心策咯.就是我们预处理出前缀和,然后一边扫过去,记录一个l1,l2和一个n1,n2.分别表示我们现在第一个数组切到l1,上一次切是在n ...

  7. poj2774 Long Long Message(后缀数组)

    [题目链接] http://poj.org/problem?id=2774 [题意] A & B的最长公共子序列. [思路] 拼接+height数组.将AB拼接成一个形如A$B的串,枚举hei ...

  8. poj2774 Long Long Message 后缀数组求最长公共子串

    题目链接:http://poj.org/problem?id=2774 这是一道很好的后缀数组的入门题目 题意:给你两个字符串,然后求这两个的字符串的最长连续的公共子串 一般用后缀数组解决的两个字符串 ...

  9. [POJ2774]Long Long Message

    vjudge 一句话题意 给两个串,求最长公共子串. sol 把两个串接在一起求后缀数组.其实中间最好用一个没有出现过的字符连接起来. 判断如果\(SA[i]\)和\(SA[i-1]\)不属于同一个串 ...

随机推荐

  1. [SHELL]shell中变量的使用

    1.输出变量 : #! /bin/bash my_var=BOB echo $my_var echo "hi,$my_var" echo "the price is \$ ...

  2. CSP201703-1:分蛋糕

    引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...

  3. 解决CentOS: Failed to start The Apache HTTP Server.

    使用systemctl status httpd.service命令查看服务状态,发现有报错 然后将此配置文件/etc/httpd/conf.d/wordpress.conf的内容全部清空,修改为: ...

  4. oraclize预言机资料

    oraclize预言机资料 智能合约如何可信的与外部世界交互: https://blog.csdn.net/sportshark/article/details/77477842 国外一篇讲得很详细的 ...

  5. FZU 1844 Earthquake Damage(最大流最小割)

    Problem Description Open Source Tools help earthquake researchers stay a step ahead. Many geological ...

  6. Cannot retrieve repository metadata (repomd.xml) for repository: base. Please verify its path and try again YUM报错

        1.挂盘 ----- 2.# mount /dev/sr0 /media/ mount: block device /dev/sr0 is write-protected, mounting ...

  7. BluetoothAdapter解析

    这篇文章将会详细解析BluetoothAdapter的详细api, 包括隐藏方法, 每个常量含义. 一 BluetoothAdapter简介 1.继承关系 该类仅继承了Object类; 2.该类作用 ...

  8. Alpha冲刺——第一天

    Alpha第一天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...

  9. ACM 第十四天

    字符串: 1.KMP算法(模式串达到1e6) 模式串达到1e4直接暴力即可. 字符串哈希 字符串Hash的种类还是有很多种的,不过在信息学竞赛中只会用到一种名为“BKDR Hash”的字符串Hash算 ...

  10. 3DMAX2016安装教程【图文】

    下载安装包之后,双击setup.exe. 下面是安装图片教程: 点击安装 点击下一步. 如图输入序列号和产品密钥. 填写安装路径,然后下一步. 开始安装,等待. 安装成功.