当前位置:网站首页>[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;
}
边栏推荐
- xpath中的position()函数使用
- Linked list interview questions (Graphic explanation)
- How can word delete English only and keep Chinese or delete Chinese and keep English
- edge瀏覽器 路徑獲得
- Select all the lines with a symbol in word and change them to titles
- TS 体操 &(交叉运算) 和 接口的继承的区别
- 珠海金山面试复盘
- HTTP cache, forced cache, negotiated cache
- Full Score composition generator: living on code
- Sélectionnez toutes les lignes avec un symbole dans Word et changez - les en titre
猜你喜欢
MEX有关的学习
opencv学习笔记八--答题卡识别
合规、高效,加快药企数字化转型,全新打造药企文档资源中心
Jerry's ad series MIDI function description [chapter]
链表面试题(图文详解)
烧录场景下的源代码防泄密方案分享
TypeScript接口与泛型的使用
Leecode-c language implementation -15 Sum of three ----- ideas to be improved
leecode-C語言實現-15. 三數之和------思路待改進版
Three no resumes in the software testing industry. What does the enterprise use to recruit you? Shichendahai's resume
随机推荐
Solution: intelligent site intelligent inspection scheme video monitoring system
GET/POST/PUT/PATCH/DELETE含义
NiO programming introduction
Ble of Jerry [chapter]
word中如何删除某符号前面或后面所有的文字
Get the path of edge browser
Significance and measures of encryption protection for intelligent terminal equipment
Is the super browser a fingerprint browser? How to choose a good super browser?
Luogu p4127 [ahoi2009] similar distribution problem solution
Linked list interview questions (Graphic explanation)
[1. Delphi foundation] 1 Introduction to Delphi Programming
链表面试题(图文详解)
Simulation of holographic interferogram and phase reconstruction of Fourier transform based on MATLAB
HTTP cache, forced cache, negotiated cache
Opencv learning notes 8 -- answer sheet recognition
edge浏览器 路径获得
onie支持pice硬盘
【Redis】NoSQL数据库和redis简介
Typescript interface and the use of generics
实现精细化生产, MES、APS、ERP必不可少