当前位置:网站首页>SPOJ 2774 Longest Common Substring(两串求公共子串 SAM)
SPOJ 2774 Longest Common Substring(两串求公共子串 SAM)
2022-08-03 22:50:00 【Morgannr】
有了之前提到过的 多串最长公共子串,当然也要有 两串求公共子串
两者都可用 SAM
求解,具体解法可见 AcWing 2811. 多串最长公共子串(后缀自动机 fa 指针的性质)
#include <bits/stdc++.h>
using namespace std;
//#define map unordered_map
//#define int long long
const int N = 2.5e5 + 10, M = N << 1;
int fa[M], ch[M][26], len[M], cnt[M];
int np = 1, tot = 1;
char s[N];
int now[M];
void extend(int c) {
int p = np;
np = ++tot;
len[np] = len[p] + 1, cnt[np] = 1;
while (p && !ch[p][c]) {
ch[p][c] = np;
p = fa[p];
}
if (!p) {
fa[np] = 1;
}
else {
int q = ch[p][c];
if (len[q] == len[p] + 1) {
fa[np] = q;
}
else {
int nq = ++tot;
len[nq] = len[p] + 1;
fa[nq] = fa[q], fa[q] = fa[np] = nq;
while (p && ch[p][c] == q) {
ch[p][c] = nq;
p = fa[p];
}
memcpy(ch[nq], ch[q], sizeof ch[q]);
}
}
}
signed main()
{
cin >> s;
for (int i = 0; s[i]; ++i) {
extend(s[i] - 'a');
}
cin >> s;
int p = 1, t = 0;
for (int i = 0; s[i]; ++i) {
int c = s[i] - 'a';
while (p > 1 && !ch[p][c]) {
p = fa[p];
t = len[p];
}
if (ch[p][c]) p = ch[p][c], ++t;
now[p] = max(now[p], t);
}
int res = -1;
for (int i = 1; i <= tot; ++i) {
res = max(res, now[i]);
}
cout << res << '\n';
return 0;
}
边栏推荐
猜你喜欢
2019年10月SQL注入的两倍
What is Adobe?
Lift, Splat, Shoot: Encoding Images from Arbitrary Camera Rigs by Implicitly Unprojecting to 3D 论文笔记
2022的七夕,奉上7个精美的表白代码,同时教大家快速改源码自用
PowerMockup 4.3.4::::Crack
Causes of Mysql Disk Holes and Several Ways to Rebuild Tables
Binary search tree to solve the fallen leaves problem
Embedded systems: overview
BMN: Boundary-Matching Network for Temporal Action Proposal Generation阅读笔记
Live Preview | Build Business Intelligence, Quickly Embrace Financial Digital Transformation
随机推荐
113. 授人以渔 - 如何自行查询任意 SAP UI5 控件属性的文档和技术实现细节
pikachu Over permission 越权
Research status of target detection at home and abroad
Recognized by International Authorities | Yunzhuang Technology was selected in "RPA Global Market Pattern Report, Q3 2022"
关于IDO预售系统开发技术讲解丨浅谈IDO预售合约系统开发原理分析
start with connect by implements recursive query
什么是memoization,它有什么用?
Golang Chapter 1: Getting Started
encapsulation, package, access modifier, static variable
为什么我们需要回调
走迷宫 BFS
.NET6之MiniAPI(十四):跨域CORS(上)
【RYU】rest_router.py源码解析
PowerMockup 4.3.4::::Crack
剑指offer第22题-链表中倒数第K个节点
如何创建一个Web项目
图的基础概念
With the rise of concepts such as metaverse and web3.0, many digital forms such as digital people and digital scenes have begun to appear.
易观分析:2022年Q2中国网络零售B2C市场交易规模达23444.7亿元
Cisco ike2 IPSec配置