33 size_t pattern_length = pattern.size();
34 std::vector<size_t> failure(pattern_length + 1);
35 failure[0] = std::string::npos;
36 size_t j = std::string::npos;
37 for (
int i = 0; i < pattern_length; i++) {
38 while (j != std::string::npos && pattern[j] != pattern[i]) {
53size_t kmp(
const std::string &pattern,
const std::string &text) {
54 if (pattern.empty()) {
58 size_t text_length = text.size();
59 size_t pattern_length = pattern.size();
61 for (
size_t j = 0; j < text_length; j++) {
62 while (k != std::string::npos && pattern[k] != text[j]) {
65 if (++k == pattern_length) {
69 return std::string::npos;