summaryrefslogtreecommitdiff
path: root/string/kmp.cpp
diff options
context:
space:
mode:
authorMZuenni <michi.zuendorf@gmail.com>2023-02-14 16:41:24 +0100
committerMZuenni <michi.zuendorf@gmail.com>2023-02-14 16:41:24 +0100
commiteb4bc75111da45a17604fdff2f9eed0977f93dff (patch)
treeffd990c0cc12a73c897a6e5c0d8216ce178a78c5 /string/kmp.cpp
parentf07738a30c46f0a277af5609a3b4c4b01674ad84 (diff)
moved more stuff
Diffstat (limited to 'string/kmp.cpp')
-rw-r--r--string/kmp.cpp11
1 files changed, 4 insertions, 7 deletions
diff --git a/string/kmp.cpp b/string/kmp.cpp
index 12ae3eb..421479e 100644
--- a/string/kmp.cpp
+++ b/string/kmp.cpp
@@ -1,18 +1,15 @@
vector<int> kmpPreprocessing(const string& sub) {
vector<int> b(sz(sub) + 1);
b[0] = -1;
- int i = 0, j = -1;
- while (i < sz(sub)) {
+ for (int i = 0, j = -1; i < sz(sub);) {
while (j >= 0 && sub[i] != sub[j]) j = b[j];
- i++; j++;
- b[i] = j;
+ b[++i] = ++j;
}
return b;
}
vector<int> kmpSearch(const string& s, const string& sub) {
- vector<int> pre = kmpPreprocessing(sub), result;
- int i = 0, j = 0;
- while (i < sz(s)) {
+ vector<int> result, pre = kmpPreprocessing(sub);
+ for (int i = 0, j = 0; i < sz(s);) {
while (j >= 0 && s[i] != sub[j]) j = pre[j];
i++; j++;
if (j == sz(sub)) {