当前位置:网站首页>[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;
}
边栏推荐
- 学go之路(二)基本类型及变量、常量
- 解决方案:智慧工地智能巡检方案视频监控系统
- C # connect to SQLite database to read content
- 【Redis】NoSQL数据库和redis简介
- QT color is converted to string and uint
- [MySQL learning notes 30] lock (non tutorial)
- [MySQL learning notes 29] trigger
- 实现精细化生产, MES、APS、ERP必不可少
- Set picture annotation in markdown
- 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
猜你喜欢
![[online problem processing] how to kill the corresponding process when the MySQL table deadlock is caused by the code](/img/93/ec9de907cae4714038bb5f95aba52b.png)
[online problem processing] how to kill the corresponding process when the MySQL table deadlock is caused by the code

In the era of digital economy, how to ensure security?

Related operations of Excel

opencv学习笔记八--答题卡识别
![datax自检报错 /datax/plugin/reader/._drdsreader/plugin.json]不存在](/img/17/415e66d67afb055e94a966de25c2bc.png)
datax自检报错 /datax/plugin/reader/._drdsreader/plugin.json]不存在

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
![Ble of Jerry [chapter]](/img/d8/d080ccaa4ee530ed21d62755808724.png)
Ble of Jerry [chapter]

Linked list interview questions (Graphic explanation)

word中如何删除某符号前面或后面所有的文字
随机推荐
Three treasures of leeks and Chinese men's football team
学go之路(二)基本类型及变量、常量
C # display the list control, select the file to obtain the file path and filter the file extension, and RichTextBox displays the data
数字IC设计笔试题汇总(一)
【mysql学习笔记29】触发器
octomap averageNodeColor函数说明
Database addition, deletion, modification and query
js对象获取属性的方法(.和[]方式)
Do you really think binary search is easy
合规、高效,加快药企数字化转型,全新打造药企文档资源中心
Leecode-c language implementation -15 Sum of three ----- ideas to be improved
Word setting directory
Methods for JS object to obtain attributes (. And [] methods)
Word delete the contents in brackets
How MySQL merges data
HTTP cache, forced cache, negotiated cache
Iterator Foundation
数字经济时代,如何保障安全?
OpenJudge NOI 2.1 1661:Bomb Game
C intercept string