当前位置:网站首页>[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;
}
边栏推荐
- 剪映的相关介绍
- 【Redis】NoSQL数据库和redis简介
- Three no resumes in the software testing industry. What does the enterprise use to recruit you? Shichendahai's resume
- Get/post/put/patch/delete meaning
- How Navicat imports MySQL scripts
- Comparison of usage scenarios and implementations of extensions, equal, and like in TS type Gymnastics
- Luogu p4127 [ahoi2009] similar distribution problem solution
- How can word delete English only and keep Chinese or delete Chinese and keep English
- 解决方案:智慧工地智能巡检方案视频监控系统
- C # display the list control, select the file to obtain the file path and filter the file extension, and RichTextBox displays the data
猜你喜欢
Excel的相关操作
If Jerry needs to send a large package, he needs to modify the MTU on the mobile terminal [article]
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
Do you really think binary search is easy
Force buckle day31
How to prevent Association in cross-border e-commerce multi account operations?
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
How to delete all the words before or after a symbol in word
Set picture annotation in markdown
Key value judgment in the cycle of TS type gymnastics, as keyword use
随机推荐
word删除括号里内容
Select all the lines with a symbol in word and change them to titles
剪映的相关介绍
Iterator Foundation
Games101 Lesson 7 shading 1 Notes
Cf1036c class numbers solution
Scala语言学习-08-抽象类
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
Force buckle day31
GET/POST/PUT/PATCH/DELETE含义
位运算异或
Emo diary 1
Wonderful use of TS type gymnastics string
In the era of digital economy, how to ensure security?
Ali's redis interview question is too difficult, isn't it? I was pressed on the ground and rubbed
[online problem processing] how to kill the corresponding process when the MySQL table deadlock is caused by the code
Three no resumes in the software testing industry. What does the enterprise use to recruit you? Shichendahai's resume
Ble of Jerry [chapter]
软件开发的一点随记
Relevant introduction of clip image