formatting source-code for d7af6fdc8c

This commit is contained in:
github-actions
2020-05-29 23:26:30 +00:00
parent edb3d51ec2
commit 7ad1f171c1
176 changed files with 5342 additions and 4288 deletions

View File

@@ -17,14 +17,18 @@
* @return Index where the pattern starts in the text
* @return -1 if the pattern was not found.
*/
int brute_force(const std::string &text, const std::string &pattern) {
int brute_force(const std::string &text, const std::string &pattern)
{
size_t pat_l = pattern.length();
size_t txt_l = text.length();
int index = -1;
if (pat_l <= txt_l) {
for (size_t i = 0; i < txt_l - pat_l + 1; i++) {
if (pat_l <= txt_l)
{
for (size_t i = 0; i < txt_l - pat_l + 1; i++)
{
std::string s = text.substr(i, pat_l);
if (s == pattern) {
if (s == pattern)
{
index = i;
break;
}
@@ -40,8 +44,10 @@ const std::vector<std::vector<std::string>> test_set = {
{"bba", "bb", "0"}, {"bbca", "c", "2"}, {"ab", "b", "1"}};
/** Main function */
int main() {
for (size_t i = 0; i < test_set.size(); i++) {
int main()
{
for (size_t i = 0; i < test_set.size(); i++)
{
int output = brute_force(test_set[i][0], test_set[i][1]);
if (std::to_string(output) == test_set[i][2])

View File

@@ -26,14 +26,17 @@
* \param[in] pattern text for which to create the partial match table
* \returns the partial match table as a vector array
*/
std::vector<int> getFailureArray(const std::string &pattern) {
std::vector<int> getFailureArray(const std::string &pattern)
{
int pattern_length = pattern.size();
std::vector<int> failure(pattern_length + 1);
failure[0] = -1;
int j = -1;
for (int i = 0; i < pattern_length; i++) {
while (j != -1 && pattern[j] != pattern[i]) {
for (int i = 0; i < pattern_length; i++)
{
while (j != -1 && pattern[j] != pattern[i])
{
j = failure[j];
}
j++;
@@ -49,13 +52,16 @@ std::vector<int> getFailureArray(const std::string &pattern) {
* \returns `true` if pattern was found
* \returns `false` if pattern was not found
*/
bool kmp(const std::string &pattern, const std::string &text) {
bool kmp(const std::string &pattern, const std::string &text)
{
int text_length = text.size(), pattern_length = pattern.size();
std::vector<int> failure = getFailureArray(pattern);
int k = 0;
for (int j = 0; j < text_length; j++) {
while (k != -1 && pattern[k] != text[j]) {
for (int j = 0; j < text_length; j++)
{
while (k != -1 && pattern[k] != text[j])
{
k = failure[k];
}
k++;
@@ -66,21 +72,28 @@ bool kmp(const std::string &pattern, const std::string &text) {
}
/** Main function */
int main() {
int main()
{
std::string text = "alskfjaldsabc1abc1abc12k23adsfabcabc";
std::string pattern = "abc1abc12l";
if (kmp(pattern, text) == true) {
if (kmp(pattern, text) == true)
{
std::cout << "Found" << std::endl;
} else {
}
else
{
std::cout << "Not Found" << std::endl;
}
text = "abcabc";
pattern = "bca";
if (kmp(pattern, text) == true) {
if (kmp(pattern, text) == true)
{
std::cout << "Found" << std::endl;
} else {
}
else
{
std::cout << "Not Found" << std::endl;
}

View File

@@ -21,9 +21,11 @@
* \param[in] n length of substring to hash
* \returns hash integer
*/
int64_t create_hash(const std::string& s, int n) {
int64_t create_hash(const std::string& s, int n)
{
int64_t result = 0;
for (int i = 0; i < n; ++i) {
for (int i = 0; i < n; ++i)
{
result += (int64_t)(s[i] * (int64_t)pow(PRIME, i));
}
return result;
@@ -39,7 +41,8 @@ int64_t create_hash(const std::string& s, int n) {
* \returns new hash integer
*/
int64_t recalculate_hash(const std::string& s, int old_index, int new_index,
int64_t old_hash, int patLength) {
int64_t old_hash, int patLength)
{
int64_t new_hash = old_hash - s[old_index];
new_hash /= PRIME;
new_hash += (int64_t)(s[new_index] * (int64_t)pow(PRIME, patLength - 1));
@@ -57,12 +60,16 @@ int64_t recalculate_hash(const std::string& s, int old_index, int new_index,
* @note can this be replaced by std::string::compare?
*/
bool check_if_equal(const std::string& str1, const std::string& str2,
int start1, int end1, int start2, int end2) {
if (end1 - start1 != end2 - start2) {
int start1, int end1, int start2, int end2)
{
if (end1 - start1 != end2 - start2)
{
return false;
}
while (start1 <= end1 && start2 <= end2) {
if (str1[start1] != str2[start2]) {
while (start1 <= end1 && start2 <= end2)
{
if (str1[start1] != str2[start2])
{
return false;
}
start1++;
@@ -79,16 +86,19 @@ bool check_if_equal(const std::string& str1, const std::string& str2,
* @return -1 if pattern not found
*/
int rabin_karp(const std::string& str, const std::string& pat) {
int rabin_karp(const std::string& str, const std::string& pat)
{
int64_t pat_hash = create_hash(pat, pat.size());
int64_t str_hash = create_hash(str, pat.size());
for (int i = 0; i <= str.size() - pat.size(); ++i) {
for (int i = 0; i <= str.size() - pat.size(); ++i)
{
if (pat_hash == str_hash &&
check_if_equal(str, pat, i, i + pat.size() - 1, 0,
pat.size() - 1)) {
check_if_equal(str, pat, i, i + pat.size() - 1, 0, pat.size() - 1))
{
return i;
}
if (i < str.size() - pat.size()) {
if (i < str.size() - pat.size())
{
str_hash =
recalculate_hash(str, i, i + pat.size(), str_hash, pat.size());
}
@@ -97,7 +107,8 @@ int rabin_karp(const std::string& str, const std::string& pat) {
}
/** Main function */
int main(void) {
int main(void)
{
assert(rabin_karp("helloWorld", "world") == -1);
assert(rabin_karp("helloWorld", "World") == 5);
assert(rabin_karp("this_is_c++", "c++") == 8);