// // Created by grtsinry43 on 5/8/25. // #include "problem4.h" #include #include #include std::vector naive_string_match(const std::string &text, const std::string &pattern) { std::vector positions; int text_length = text.size(); int pattern_length = pattern.size(); for (int i = 0; i <= text_length - pattern_length; ++i) { int j = 0; while (j < pattern_length && text[i + j] == pattern[j]) { ++j; } if (j == pattern_length) { positions.push_back(i); // 记录匹配的起始位置 } } return positions; } int problem_4() { std::string text = "ababcabcacbab"; std::string pattern = "abc"; std::vector positions = naive_string_match(text, pattern); std::cout << "Pattern found at positions: "; for (int pos : positions) { std::cout << pos << " "; } std::cout << std::endl; return 0; }