题意:

  给你两串字符,要你找出在这两串字符中都出现过的最长子串

解析:

  先用个分隔符将两个字符串连接起来,再用后缀数组求出height数组的值,找出一个height值最大并且i与i-1的sa值分别在两串字符中就好了

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff;
int s[maxn];
int sa[maxn], t[maxn], t2[maxn], c[maxn], n;
int ran[maxn], height[maxn]; void get_sa(int m)
{
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);
p = ; x[sa[]] = ;
for(i = ; 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 k = ;
for(i = ; i < n; i++) ran[sa[i]] = i;
for(i = ; i < n; i++)
{
if(k) k--;
int j = sa[ran[i]-];
while(s[i+k] == s[j+k]) k++;
height[ran[i]] = k;
}
} char s1[maxn], s2[maxn];
int main()
{
while(~scanf("%s%s", s1, s2)){
n = ;
int len1 = strlen(s1);
rep(i, , len1)
s[n++] = s1[i] - 'a' + ; s[n++] = ;
int len2 = strlen(s2);
rep(i, , len2)
s[n++] = s2[i] - 'a' + ;
s[n++] = ;
get_sa();
int maxx = -INF;
rep(i, , n)
{
if(height[i] > maxx && sa[i] < len1 && sa[i-] > len1 && sa[i] >= )
maxx = height[i];
else if(height[i] > maxx && sa[i-] < len1 && sa[i] > len1 && sa[i-] >= )
maxx = height[i];
}
cout<< maxx <<endl;
} return ;
}

Long Long Message POJ - 2774(最长公共子串)的更多相关文章

  1. poj 2774 最长公共子串 后缀数组

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 25752   Accepted: 10 ...

  2. POJ 2774 最长公共子串

    一定好好学SAM...模板在此: #include<iostream> #include<cstdio> #include<cmath> #include<a ...

  3. POJ-2774-Long Long Message(后缀数组-最长公共子串)

    题意: 给定两个字符串 A 和 B,求最长公共子串. 分析: 字符串的任何一个子串都是这个字符串的某个后缀的前缀. 求 A 和 B 的最长公共子串等价于求 A 的后缀和 B 的后缀的最长公共前缀的最大 ...

  4. poj 2774 最长公共子--弦hash或后缀数组或后缀自己主动机

    http://poj.org/problem?id=2774 我想看看这里的后缀数组:http://blog.csdn.net/u011026968/article/details/22801015 ...

  5. POJ 2774 Long Long Message [ 最长公共子串 后缀数组]

    题目:http://poj.org/problem?id=2774 Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total ...

  6. 后缀数组(模板题) - 求最长公共子串 - poj 2774 Long Long Message

    Language: Default Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 21 ...

  7. poj 2774 Long Long Message,后缀数组,求最长公共子串 hdu1403

    题意:给出两个字符串,求最长公共子串的长度. 题解:首先将两个字符串连在一起,并在中间加一个特殊字符(字串中不存在的)切割,然后两个串的最长公共字串就变成了全部后缀的最长公共前缀.这时就要用到heig ...

  8. POJ 2774 Long Long Message (二分 + Hash 求最长公共子串)题解

    题意:求最长公共子串 思路:把两个串Hash,然后我们把短的作为LCS的最大可能值,然后二分长度,每次判断这样二分可不可以.判断时,先拿出第一个母串所有len长的子串,排序,然后枚举第二个母串len长 ...

  9. 字符串hash + 二分答案 - 求最长公共子串 --- poj 2774

    Long Long Message Problem's Link:http://poj.org/problem?id=2774 Mean: 求两个字符串的最长公共子串的长度. analyse: 前面在 ...

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

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

随机推荐

  1. 大数据中Hadoop集群搭建与配置

    前提环境是之前搭建的4台Linux虚拟机,详情参见 Linux集群搭建 该环境对应4台服务器,192.168.1.60.61.62.63,其中60为主机,其余为从机 软件版本选择: Java:JDK1 ...

  2. Git积累

    1.使用git config命令进行配置(此配置为全局配置,这些是在提交commit时的签名): $ git config --global user.name "填写github的用户名& ...

  3. [Lua] try catch实现

    参考了https://blog.csdn.net/waruqi/article/details/53649634这里的代码,但实际使用时还有些问题,修改后在此记录一下. -- 异常捕获 functio ...

  4. 《杜增强讲Unity之Tanks坦克大战》6-发射子弹

    6 发射子弹 本节完成发射子弹的功能,最终代码如下:   image 首先,发射子弹得确定发射的位置和方向,还有发射的初始速度.具体的发射速度和按下发射按键的时间长短有关,这个关于子弹的蓄力我们在第九 ...

  5. 转载:GBDT算法梳理

    学习内容: 前向分布算法 负梯度拟合 损失函数 回归 二分类,多分类 正则化 优缺点 sklearn参数 应用场景 转自:https://zhuanlan.zhihu.com/p/58105824 G ...

  6. EF多个上下文迁移

    步骤: 1. Enable-Migrations 2. add-migration Initial -ConfigurationTypeName ModelOneDbConfig 3. update- ...

  7. js中 null, undefined, 0,空字符串,false,不全等比较

    null == undefined // true null == ''  // false null == 0 // false null == false // false undefined = ...

  8. [zabbix] zabbix从内部检测web页面

    环境说明: 两台机器各运行一个tomcat实例,通过阿里云slb到后端,假设后端服务挂了一个,从外部访问整个服务还是可用的,所以需要从内部检测web页面. zabbix自带的web场景都是从外部检测w ...

  9. "prefs:root" or "App-Prefs:root"

    iOS 苹果审核也是看心情的吗?已经上线几个版本了,新版本提交审核居然被查出来了! Guideline 2.5.1 - Performance - Software Requirements Your ...

  10. Daily Scrumming* 2015.10.26(Day 7)

    一.总体情况总结 今天我们开会具体讨论了一下接下来的任务.还详细讨论了一下分数的分配,具体分数分配我们会在下一篇博客中详细说明. 我们下一周大致的工作安排如下: 1.UI:完成社团后台界面的设计,以及 ...