把s1,s2拼接,求Height。相邻的Height判断左右串起点是否在两个串中,另外对Height和s1.length()-SA[i-1]取min。

#include <iostream>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <iomanip>
#include <stdlib.h>
#include <time.h>
#define LL long long
using namespace std;
const LL mod = ;
const LL N = ;
int s1l, s2l;
class SF
{
//N:数组大小
public:
int x[N], y[N], c[N];
int Height[N],str[N], SA[N], Rank[N];//Height数组从2开始
int slen;
int m=;//字符集处理大小(传入如果不是数字,需要做位移转换)
bool cmp(int* r, int a, int b, int l) {
return r[a] == r[b] && r[a + l] == r[b + l];
} void Suffix(int n) {
++n;
int i, j, p;
for (i = ; i < m; ++i) c[i] = ;
for (i = ; i < n; ++i) c[x[i] = str[i]]++;
for (i = ; i < m; ++i) c[i] += c[i - ];
for (i = n - ; i >= ; --i) SA[--c[x[i]]] = i;
for (j = ; j <= n; j <<= ) {
p = ;
for (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 < 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]] = cmp(y, SA[i - ], SA[i], j) ? p - : p++;
}
if (p >= n)break;
m = p;
} int k = ;
n--;
for (i = ; i <= n; ++i) Rank[SA[i]] = i;
for (i = ; i < n; ++i) {
if (k)--k;
j = SA[Rank[i] - ];
while (str[i + k] == str[j + k])++k;
Height[Rank[i]] = k;
//cout << k << endl;
}
}
void init(string s)//不论什么参数,用引用传入
{
slen = s.length();
for (int i = ; i < slen; i++)
str[i] =s[i]-'a'+;//如果是字符,映射成从1开始的序列
str[slen] = ;//0作为结束符,防止越界
Suffix(slen);
}
LL solve()
{
int ans = ;
for (int i = ; i <= slen; i++)
{
int sa1 = SA[i - ],sa2=SA[i];
if (sa1 > sa2) swap(sa1, sa2);
if (sa1 >= s1l || sa2 < s1l) continue;
ans = max(ans, min(s1l - sa1, Height[i]));
}
return ans;
}
}sf;
LL dp[][];
LL n;
int main() {
cin.sync_with_stdio(false);
string s1,s2;
while (cin >> s1>>s2)
{
s1l = s1.length();
s2l = s2.length();
sf.init(s1+s2);
cout << sf.solve() << endl;
}
return ;
}

hihocoder-1415 后缀数组三·重复旋律3 两个字符串的最长公共子串的更多相关文章

  1. hihocoder #1415 : 后缀数组三·重复旋律3

    #1415 : 后缀数组三·重复旋律3 Time Limit:5000ms Case Time Limit:1000ms Memory Limit:256MB 描述 小Hi平时的一大兴趣爱好就是演奏钢 ...

  2. hiho一下122周 后缀数组三·重复旋律

    后缀数组三·重复旋律3 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为长度为 N 的数构成的数列.小Hi ...

  3. hihoCoder 1403 后缀数组一·重复旋律(后缀数组+单调队列)

    #1403 : 后缀数组一·重复旋律 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为长度为 N 的数构成 ...

  4. hihocoder #1419 : 后缀数组四·重复旋律4

    #1419 : 后缀数组四·重复旋律4 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为长度为 N 的数构 ...

  5. hihocoder #1407 : 后缀数组二·重复旋律2

    #1407 : 后缀数组二·重复旋律2 Time Limit:5000ms Case Time Limit:1000ms Memory Limit:256MB 描述 小Hi平时的一大兴趣爱好就是演奏钢 ...

  6. HiHocoder1415 : 后缀数组三·重复旋律3 & Poj2774:Long Long Message

    题面 HiHocoder1415 Poj2774 Sol 都是求最长公共子串,\(hihocoder\)上讲的很清楚 把两个串拼在一起,中间用一个特殊字符隔开 那么答案就是排序后相邻两个不同串的后缀的 ...

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

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

  8. SPOJ 1811 Longest Common Substring (后缀自动机第一题,求两个串的最长公共子串)

    题目大意: 给出两个长度小于等于25W的字符串,求它们的最长公共子串. 题目链接:http://www.spoj.com/problems/LCS/ 算法讨论: 二分+哈希, 后缀数组, 后缀自动机. ...

  9. hihocoder1415 后缀数组三·重复旋律3

    传送门:http://hihocoder.com/problemset/problem/1415 [题解] 考虑求出两串合在一起(中间加分隔符)后缀数组,就是要求任意在两个串中的$i, j$,$\mi ...

随机推荐

  1. Bootstrap3基础 clearfix pull-left/right 辅助类样式 快速左右浮动

      内容 参数   OS   Windows 10 x64   browser   Firefox 65.0.2   framework     Bootstrap 3.3.7   editor    ...

  2. CF776B Sherlock and his girlfriend

    题目地址 题目链接 题解 这题很有意思啊qwq.本来是写算出每个数的质约数的,然后写到一半发现,质约数互相不影响,有质约数的数肯定是合数. 所以合数染一种色,质数染一种色就好 #include < ...

  3. MySQL的启动和关闭

    1.Windows下 启动服务 mysqld --console 或 net start mysql 关闭服务 mysqladmin -uroot shudown 或 net stop mysql 2 ...

  4. Using git-flow to automate your git branching workflow

    Using git-flow to automate your git branching workflow Vincent Driessen’s branching model is a git b ...

  5. jd面试之感

    一面问题:问题都回答的很好,顺利进入二面 1.单点登录的改造和原理 2.hashmap 3.jvm:堆.方法区.栈,本地方法栈,gc,gc的方式 4.spring的ioc.aop的实现方式cglib和 ...

  6. 中文目录对 sublime text 有什么影响?

    用了这软件好几个月了,一直没出现问题.最近做精简时,发现一个奇怪的问题. 相同的配置,为什么两个程序表现得不一样? 难道是哪里的配置不一样? 难道是插件被我精简得太厉害了? 难道是插件有依赖文件被我删 ...

  7. HDU 5573 Binary Tree(构造题)

    http://acm.hdu.edu.cn/showproblem.php?pid=5573 题意:给出一个满二叉树,根节点权值为1,左儿子为2*val,右儿子为2*val+1.现在有只青蛙从根节点出 ...

  8. 【Mysql】【环境配置】Mac

    参看:http://www.cnblogs.com/chenmo-xpw/p/6102933.html     一.下载dmg包安装 1.下载MySQL dmg 包, 从官网 : http://www ...

  9. VR外包公司—2016中国VR开发者论坛第一期

    由VR界网和暴风魔镜联合举办的2016中国VR开发者论坛第一期已于3月2日下午5点在吉林动画学院圆满落幕,本次论坛云集了VR相关领域的精英,邀请了VR社交<极乐王国>.暴风魔镜.南京睿悦. ...

  10. Codeforces Round #200 (Div. 1) D. Water Tree 树链剖分+线段树

    D. Water Tree time limit per test 4 seconds memory limit per test 256 megabytes input standard input ...