From eb4bc75111da45a17604fdff2f9eed0977f93dff Mon Sep 17 00:00:00 2001 From: MZuenni Date: Tue, 14 Feb 2023 16:41:24 +0100 Subject: moved more stuff --- string/kmp.cpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'string/kmp.cpp') 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 kmpPreprocessing(const string& sub) { vector 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 kmpSearch(const string& s, const string& sub) { - vector pre = kmpPreprocessing(sub), result; - int i = 0, j = 0; - while (i < sz(s)) { + vector 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)) { -- cgit v1.2.3