27 std::unordered_map<char, int>
30 for (
int i = 0; i < prototype.size();
32 if (shiftTable.find(prototype[i]) ==
34 if (i != prototype.size() - 1) {
35 shiftTable.insert(std::make_pair(
36 prototype[i], prototype.size() - i -
39 shiftTable.insert(std::make_pair(
44 if (i != prototype.size() - 1) {
45 shiftTable[prototype[i]] = prototype.size() - i - 1;
59bool horspool(
const std::string &text,
const std::string &prototype) {
63 int i =
static_cast<int>(
66 while (i < text.size()) {
70 for (
int z =
static_cast<int>(prototype.size() - 1); z >= 0 && flag;
72 if (text[j] == prototype[z]) {
84 if (shiftTable.find(text[i]) != shiftTable.end()) {
85 i += shiftTable[text[i]];
87 i += prototype.size();
101 assert(strings::horspool::horspool(
"Hello World",
"World") ==
true);
102 assert(strings::horspool::horspool(
"Hello World",
" World") ==
true);
103 assert(strings::horspool::horspool(
"Hello World",
"ello") ==
true);
104 assert(strings::horspool::horspool(
"Hello World",
"rld") ==
true);
105 assert(strings::horspool::horspool(
"Hello",
"Helo") ==
false);
106 assert(strings::horspool::horspool(
"c++_algorithms",
"c++_algorithms") ==
true);
107 assert(strings::horspool::horspool(
"c++_algorithms",
"c++_") ==
true);
108 assert(strings::horspool::horspool(
"Hello",
"Hello World") ==
false);
109 assert(strings::horspool::horspool(
"c++_algorithms",
"") ==
false);
110 assert(strings::horspool::horspool(
"c++",
"c") ==
true);
111 assert(strings::horspool::horspool(
"3458934793",
"4793") ==
true);
112 assert(strings::horspool::horspool(
"3458934793",
"123") ==
false);