41 lines
965 B
C++
41 lines
965 B
C++
//
|
|
// Created by grtsinry43 on 5/8/25.
|
|
//
|
|
|
|
#include "problem4.h"
|
|
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
std::vector<int> naive_string_match(const std::string &text, const std::string &pattern) {
|
|
std::vector<int> 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<int> 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;
|
|
} |