当前位置:网站首页>[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;
}
边栏推荐
- DataX self check error /datax/plugin/reader/_ drdsreader/plugin. Json] does not exist
- MES, APS and ERP are essential to realize fine production
- Qualitative risk analysis of Oracle project management system
- TypeScript 接口属性
- [1. Delphi foundation] 1 Introduction to Delphi Programming
- [MySQL learning notes 32] mvcc
- 861. Score after flipping the matrix
- onie支持pice硬盘
- Memory error during variable parameter overload
- Compliance and efficiency, accelerate the digital transformation of pharmaceutical enterprises, and create a new document resource center for pharmaceutical enterprises
猜你喜欢
JMeter performance test steps practical tutorial
[CF Gym101196-I] Waif Until Dark 网络最大流
[window] when the Microsoft Store is deleted locally, how to reinstall it in three steps
Related operations of Excel
Three no resumes in the software testing industry. What does the enterprise use to recruit you? Shichendahai's resume
[online problem processing] how to kill the corresponding process when the MySQL table deadlock is caused by the code
解决方案:智慧工地智能巡检方案视频监控系统
Simulation of Michelson interferometer based on MATLAB
Do you really think binary search is easy
TypeScript接口与泛型的使用
随机推荐
Basics of reptile - Scratch reptile
超级浏览器是指纹浏览器吗?怎样选择一款好的超级浏览器?
C intercept string
edge浏览器 路径获得
How MySQL merges data
word中把带有某个符号的行全部选中,更改为标题
位运算异或
P3047 [USACO12FEB]Nearby Cows G(树形dp)
Typescript function definition
Games101 Lesson 7 shading 1 Notes
解决方案:智慧工地智能巡檢方案視頻監控系統
Key value judgment in the cycle of TS type gymnastics, as keyword use
[computer skills]
Ble of Jerry [chapter]
Fundamentals of C language 9: Functions
edge瀏覽器 路徑獲得
Leecode-c language implementation -15 Sum of three ----- ideas to be improved
智能终端设备加密防护的意义和措施
[online problem processing] how to kill the corresponding process when the MySQL table deadlock is caused by the code
Sharing of source code anti disclosure scheme under burning scenario