KMP
1. KMP的原理
原理
- 注意:p和s的下标都是从 1 开始的。
代码模板
- C++
#include <iostream>
using namespace std;
const int N = 100010, M = 1000010;
int n, m;
char s[M], p[N];
int ne[N];
int main() {
cin >> n >> p + 1 >> m >> s + 1;
// 求next数组, ne[1]=0表示如果p[1]没有匹配上,从头开始匹配
for (int i = 2, j = 0; i <= n; i++) {
while (j && p[i] != p[j + 1]) j = ne[j];
if (p[i] == p[j + 1]) j++;
ne[i] = j;
}
// 匹配过程
for (int i = 1, j = 0; i <= m; i++) {
while (j && s[i] != p[j + 1]) j = ne[j];
if (s[i] == p[j + 1]) j++;
if (j == n) {
printf("%d ", i - n);
j = ne[j];
}
}
return 0;
}
- 时间复杂度: O ( m ) O(m) O(m),m是模式串(被匹配的串)长度。对于每一外层循环,j 最多加1,因此j最多加m次,里层的while循环,会使得 j 变小,最多让 j 减少m次,因此时间复杂度为 O ( 2 m ) O(2m) O(2m) 。
2. 力扣上的KMP题目
Leetcode 0028 实现strStr()
问题描述
-
问题链接:网址
分析
- 直接使用
KMP
算法即可。
代码
- C++
/**
* 执行用时:0 ms, 在所有 C++ 提交中击败了100.00%的用户
* 内存消耗:7.1 MB, 在所有 C++ 提交中击败了8.43%的用户
*/
class Solution {
public:
int strStr(string haystack, string needle) {
if (needle.empty()) return 0;
if (needle.size() > haystack.size()) return -1;
int m = haystack.size(), n = needle.size();
string s = ' ' + haystack, p = ' ' + needle;
vector<int> ne(n + 10, 0);
for (int i = 2, j = 0; i <= n; i++) {
while (j && p[i] != p[j + 1]) j = ne[j];
if (p[i] == p[j + 1]) j++;
ne[i] = j;
}
for (int i = 1, j = 0; i <= m; i++) {
while (j && s[i] != p[j + 1]) j = ne[j];
if (s[i] == p[j + 1]) j++;
if (j == n) return i - n;
}
return -1;
}
};
/**
* 执行用时:4 ms, 在所有 C++ 提交中击败了62.45%的用户
* 内存消耗:6.7 MB, 在所有 C++ 提交中击败了61.60%的用户
*/
class Solution {
public:
int strStr(string haystack, string needle) {
return haystack.find(needle);
}
};
- Java
/**
* Date: 2020/9/19 21:22
* Content: kmp算法
* 执行用时:3 ms, 在所有 Java 提交中击败了39.16%的用户
* 内存消耗:38.4 MB, 在所有 Java 提交中击败了38.65%的用户
*/
public class Solution {
public int strStr(String haystack, String needle) {
if (needle == null || needle.length() == 0) return 0;
if (needle.length() > haystack.length()) return -1;
int m = haystack.length(), n = needle.length();
char[] s = (" " + haystack).toCharArray(), p = (" " + needle).toCharArray();
int[] ne = new int[n + 10];
for (int i = 2, j = 0; i <= n; i++) {
while (j != 0 && p[i] != p[j + 1]) j = ne[j];
if (p[i] == p[j + 1]) j++;
ne[i] = j;
}
for (int i = 1, j = 0; i <= m; i++) {
while (j != 0 && s[i] != p[j + 1]) j = ne[j];
if (s[i] == p[j + 1]) j++;
if (j == n) return i - n;
}
return -1;
}
}
/**
* Date: 2021/4/20 10:00
* Content:
* 执行用时:0 ms, 在所有 Java 提交中击败了100.00%的用户
* 内存消耗:37.2 MB, 在所有 Java 提交中击败了57.92%的用户
*/
public class Solution {
public int strStr(String haystack, String needle) {
return haystack.indexOf(needle);
}
}
Leetcode 0686 重复叠加字符串匹配
问题描述
-
问题链接:网址
代码
- C++
class Solution {
public:
int repeatedStringMatch(string a, string p) {
string s;
while (s.size() < p.size()) s += a;
s += a;
int n = s.size(), m = p.size();
s = ' ' + s, p = ' ' + p;
vector<int> next(m + 1);
for (int i = 2, j = 0; i <= m; i++) {
while (j && p[i] != p[j + 1]) j = next[j];
if (p[i] == p[j + 1]) j++;
next[i] = j;
}
// a/b上取整 == (a + b - 1)/b下取整
for (int i = 1, j = 0; i <= n; i++) {
while (j && s[i] != p[j + 1]) j = next[j];
if (s[i] == p[j + 1]) j++;
if (j == m) return (i + a.size() - 1) / a.size();
}
return -1;
}
};
- Java
class Solution {
public int repeatedStringMatch(String a, String b) {
StringBuilder sb = new StringBuilder();
while (sb.length() < b.length()) sb.append(a);
sb.append(a);
int n = sb.length(), m = b.length();
String t = " " + sb; b = " " + b;
char[] s = t.toCharArray(), p = b.toCharArray();
int[] next = new int[m + 1];
for (int i = 2, j = 0; i <= m; i++) {
while (j != 0 && p[i] != p[j + 1]) j = next[j];
if (p[i] == p[j + 1]) j++;
next[i] = j;
}
for (int i = 1, j = 0; i <= n; i++) {
while (j != 0 && s[i] != p[j + 1]) j = next[j];
if (s[i] == p[j + 1]) j++;
if (j == m) return (i + a.length() - 1) / a.length();
}
return -1;
}
}