Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
85 lines (70 loc) · 1.74 KB

File metadata and controls

85 lines (70 loc) · 1.74 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <string>
#include <vector>
#include <iostream>
namespace cppcode { namespace common {
enum class SUB_STRING_ALGORITHM : uint8_t
{
BRUTE_FORCE,
KMP
};
int32_t sub_string_brute_force(const std::string& s, const std::string& needle);
int32_t sub_string_KMP(const std::string& s, const std::string& needle);
int32_t sub_string(const std::string& s, const std::string& needle, const SUB_STRING_ALGORITHM sub_string_algorithm = SUB_STRING_ALGORITHM::BRUTE_FORCE)
{
switch (sub_string_algorithm)
{
case SUB_STRING_ALGORITHM::BRUTE_FORCE:
break;
case SUB_STRING_ALGORITHM::KMP:
return sub_string_KMP(s, needle);
default:
break;
}
return sub_string_brute_force(s, needle);
}
int32_t sub_string_brute_force(const std::string& s, const std::string& needle)
{
auto* p = s.data();
for (size_t i = 0; i < (s.size() - needle.size() + 1); i++)
{
if (std::string_view{p++, needle.size()} == needle)
{
return i;
}
}
return -1;
}
int32_t sub_string_KMP(const std::string& s, const std::string& needle)
{
std::vector<uint32_t> lps(needle.size(), 0);
int k = 0, i;
for (i = 1; i < needle.size(); i++)
{
while (k > 0 && needle[k] != needle[i])
{
k = lps[k - 1];
}
if (needle[k] == needle[i])
{
lps[i] = ++k;
}
}
k = 0;
for (i = 0; i < s.size(); i++)
{
while (k > 0 && s[i] != needle[k])
{
k = lps[k - 1];
}
if (s[i] == needle[k])
{
k++;
}
if (k == needle.size())
{
return i - needle.size() + 1;
}
}
return -1;
}
}}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.