当前位置:网站首页>String match KMP BF
String match KMP BF
2022-04-21 09:40:00 【SVIP_ Quanw】
string matching KMP BF
string matching ?
Simply put : Given two strings S, T stay Main string S Find Pattern T
BF
C++ Code
/** * @brief brutal force violence * * @param str character string * @param pat Pattern * @return int Initial subscript or -1 I didn't find */
int bf(string str, string pat) {
for (size_t i = 0; i < str.length(); i++) {
size_t j = 0, k = i;
for (; j < pat.size();) {
if (str[k] == pat[j])
++k, ++j;
else
break;
}
if (j == pat.size())
return i;
}
return -1;
}
Java Code
public static int indexOf(byte[] value, int valueCount, byte[] str, int strCount, int fromIndex) {
byte first = str[0];
int max = (valueCount - strCount);
for (int i = fromIndex; i <= max; i++) {
// Look for first character.
if (value[i] != first) {
while (++i <= max && value[i] != first);
}
// Found first character, now look at the rest of value
if (i <= max) {
int j = i + 1;
int end = j + strCount - 1;
for (int k = 1; j < end && value[j] == str[k]; j++, k++);
if (j == end) {
// Found whole string.
return i;
}
}
}
return -1;
}
KMP
The core idea is that the main string does not go back
use next After the array records the current position mismatch , The pattern string should start there again
The difficulty is how to find next Array , Here are two ways
C++ Code
/** * @brief kmp Use next Array * * @param str character string * @param pat Pattern * @return int */
int kmp(string str, string pat) {
const int N = pat.length();
int next[N];
// Initial Hu next
next[0] = -1;
for (int k = 0, j = 1; j < N; ++j) {
next[j] = k;
while (k > 0 && pat[j] != pat[k])
k = next[k];
if (pat[j] == pat[k])
++k;
}
// matching
int i = 0, j = 0;
for (; i < str.length();) {
if (j == -1 || str[i] == pat[j])
++j, ++i; // success ++
else
j = next[j]; // Failure backtracking
if (j == pat.length())
return i - j; // Initial subscript
}
return -1;
}
/** * @brief Get the "next" array * * @param next Array * @param t Pattern */
void get_next(int next[], string t) {
next[0] = -1;
for (int j = 0, k = -1; j < t.length() - 1;) {
if (k == -1 || t[j] == t[k]) {
j++;
k++;
next[j] = k;
} else
k = next[k];
}
}
Test code
int main(int argc, char const* argv[]) {
int a = bf("sdsdfaaregwr", "sdf");
cout << a << '\n';
a = bf("gsfwesgfvsfghj", "sdf");
cout << a << '\n';
string str = "abcbababcabcdf";
string pat = "bababcabc";
int idx = kmp(str, pat);
cout << idx;
return 0;
}
版权声明
本文为[SVIP_ Quanw]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210937049369.html
边栏推荐
- Install redis in docker under CentOS
- Multithreaded copy set (New 4)
- 1168: Bill (pointer topic)
- [summary] 1296 - summarize 12 common mobile terminal H5 and hybrid development problems
- 1165: decimal part of real number (pointer)
- ZABBIX 5.4 server installation
- Uniapp style penetration
- C语言进阶-动态内存管理
- Install MySQL in docker under CentOS
- [hand in hand to prepare you for the video game] detailed explanation of April tag tag tracking (3D positioning)
猜你喜欢

My life of Honker Security Commando

Operation of simulation test platform for test questions of refrigeration and air conditioning equipment operation test in 2022
Serviceworker cache and HTTP cache

2022年制冷与空调设备运行操作考试试题模拟考试平台操作

【手拉手 带你准备电赛】April Tag标记跟踪(3D定位)详解

CC00019. CloudJenkins—————————————

The display problem of gltf model with transparent map

【DL-图像分类】
![[notes] Launch file syntax record](/img/68/cbd3d6173535223c54e1dcf9ecc142.png)
[notes] Launch file syntax record

Grid layout -- grid
随机推荐
You are using pip version 20.2.3; however, version 22.0.4 is available. You should consider
CC00026. CloudJenkins—————————————
Redis deployment and use
【愚公系列】2022年04月 微信小程序-地图的使用之线聚合
Dark blue - Visual slam - Section 6 exercise
Operation of simulation test platform for test questions of refrigeration and air conditioning equipment operation test in 2022
1167: number of reversals (pointer topic)
CentOS下Docker中安装MySQL
【愚公系列】2022年04月 微信小程序-项目篇(公交查询)-01周边站点
【手拉手 带你准备电赛】近期小总结
Multithreaded copy set (New 4)
【云驻共创】华为云数据库之数据库原理及应用课程11、数据库系统控制
PicLite 开发日志 (v0.0.3)
组合总和-Leetcode
广州期货交易所的产品有哪些呢?
Promise处理复杂异步
Implementation of multithreaded lock free queue
Interview question of a small factory: what is false awakening?
Installation de MySQL dans docker sous CentOS
Operation of simulated examination platform of special operation certificate examination question bank for safety production management personnel of hazardous chemical production units in 2022