当前位置:网站首页>[KMP] template
[KMP] template
2022-07-06 07:39:00 【Code chess】
️s
For longer strings to match
️p
It is a pattern string with small length ne[i]
: Indicates that in the pattern string i
The length of the longest common prefix with subscript as the end point j
: Always represents the end position that can match the common pre suffix
Be careful :ne[1]=0
, therefore i
from 2 Start
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
char s[N], p[N];
int ne[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
cin >> s + 1 >> p + 1;
for(int i = 2, j = 0; i <= m; i++)
{
while(j && s[i] != s[j + 1]) j = ne[j];
if(p[i] == p[j + 1]) j++;
ne[i] = j;
}
for(int i = 1, j = 0; i <= n; i++)
{
while(j && s[i] != p[j + 1]) j = ne[j];
if(s[i] == p[j + 1]) j++;
if(j == m) j = ne[j]; // The match is successful
}
return 0;
}
边栏推荐
- [window] when the Microsoft Store is deleted locally, how to reinstall it in three steps
- TS 类型体操 之 extends,Equal,Alike 使用场景和实现对比
- Position() function in XPath uses
- Launch APS system to break the problem of decoupling material procurement plan from production practice
- Sharing of source code anti disclosure scheme under burning scenario
- Description of octomap averagenodecolor function
- http缓存,强制缓存,协商缓存
- PHP Coding Standard
- Get the path of edge browser
- [MySQL learning notes 32] mvcc
猜你喜欢
Ble of Jerry [chapter]
数字经济时代,如何保障安全?
【Redis】NoSQL数据库和redis简介
[cf gym101196-i] waif until dark network maximum flow
Redis builds clusters
DataX self check error /datax/plugin/reader/_ drdsreader/plugin. Json] does not exist
链表面试题(图文详解)
Excel的相关操作
How to prevent Association in cross-border e-commerce multi account operations?
[1. Delphi foundation] 1 Introduction to Delphi Programming
随机推荐
js对象获取属性的方法(.和[]方式)
word设置目录
861. Score after flipping the matrix
Get the path of edge browser
[非线性控制理论]9_非线性控制理论串讲
qt颜色与字符串、uint相互转换
How to delete all the words before or after a symbol in word
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Typescript indexable type
超级浏览器是指纹浏览器吗?怎样选择一款好的超级浏览器?
珠海金山面试复盘
(lightoj - 1410) consistent verbs (thinking)
Emo diary 1
How MySQL merges data
Ali's redis interview question is too difficult, isn't it? I was pressed on the ground and rubbed
Full Score composition generator: living on code
TypeScript 变量作用域
DataX self check error /datax/plugin/reader/_ drdsreader/plugin. Json] does not exist
TypeScript 接口属性
Launch APS system to break the problem of decoupling material procurement plan from production practice