From 06f16eb09a9de96f1e18e575f2b5a7891e4e916d Mon Sep 17 00:00:00 2001 From: S R Pranav Suriya <91081814+pranavsuriya-sr@users.noreply.github.com> Date: Sun, 9 Oct 2022 15:00:55 +0530 Subject: [PATCH 1/2] Add files via upload --- String/PatternMatching/KMPalgorithm.c++ | 85 +++++++++++++++++++ String/PatternMatching/RabinKarpAlgorithm.c++ | 56 ++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 String/PatternMatching/KMPalgorithm.c++ create mode 100644 String/PatternMatching/RabinKarpAlgorithm.c++ diff --git a/String/PatternMatching/KMPalgorithm.c++ b/String/PatternMatching/KMPalgorithm.c++ new file mode 100644 index 0000000..3425ef3 --- /dev/null +++ b/String/PatternMatching/KMPalgorithm.c++ @@ -0,0 +1,85 @@ +#include "bits/stdc++.h" +using namespace std; + +void findLpsArray(char* pattern, int* lps, int m) +{ + + int len = 0; + + lps[0] = 0; + int i = 1; + + while (i < m) + { + if (pattern[i] == pattern[len]) + { + len++; + lps[i] = len; + i++; + } + else + { + if (len != 0) + { + len = lps[len - 1]; + } + else + { + lps[i] = 0; + i++; + } + } + } +} + +void patternSearchKMP(char* pattern, char* text) +{ + int m = strlen(pattern); + int n = strlen(text); + + int lps[m]; //longest-prefix-suffix array + + findLpsArray(pattern, lps, m); //calling findLpsArray function + + int i = 0; + int j = 0; + + while (i < n) + { + if (pattern[j] == text[i]) + { + j++; + i++; + } + + if (j == m) + { + cout<<"Pattern found at index: "<>pattern; + + cout<<"Enter the string in which the pattern is to be found: "<>text; + + patternSearchKMP(pattern, text); //Function call + + return 0; +} + diff --git a/String/PatternMatching/RabinKarpAlgorithm.c++ b/String/PatternMatching/RabinKarpAlgorithm.c++ new file mode 100644 index 0000000..ccef543 --- /dev/null +++ b/String/PatternMatching/RabinKarpAlgorithm.c++ @@ -0,0 +1,56 @@ +#include "bits/stdc++.h" +using namespace std; + +void rabinKarpAlgo(char* text, char* pattern) +{ + int m = strlen(text); + int n = strlen(pattern); + + int prime = 31; + int mod = 1e9 + 9; + + vector p_pow(m); + p_pow[0] = 1; + + for (int i = 1; i < m; i++) + { + p_pow[i] = (p_pow[i-1] * prime) % mod; + } + + vector h(m + 1, 0); + + for (int i = 0; i < m; i++) + { + h[i+1] = (h[i] + (text[i] - 'a' + 1) * p_pow[i]) % mod; + } + + long long hash_pattern = 0; + + for (int i = 0; i < n; i++) + { + hash_pattern = (hash_pattern + (pattern[i] - 'a' + 1) * p_pow[i]) % mod; + } + + for (int i = 0; i + n - 1 < m; i++) + { + long long curr_hash = (h[i+n] + mod - h[i]) % mod; + + if (curr_hash == hash_pattern * p_pow[i] % mod) + cout<<"Pattern found at index: "<>pattern; + + cout<<"Enter the string in which the pattern is to be found: "<>text; + + rabinKarpAlgo(text, pattern); //Function call + + return 0; +} From 828b051a3bace969c1d076a4b361334cc46b4afc Mon Sep 17 00:00:00 2001 From: S R Pranav Suriya <91081814+pranavsuriya-sr@users.noreply.github.com> Date: Sun, 9 Oct 2022 15:21:10 +0530 Subject: [PATCH 2/2] Create README.md --- String/PatternMatching/README.md | 40 ++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 String/PatternMatching/README.md diff --git a/String/PatternMatching/README.md b/String/PatternMatching/README.md new file mode 100644 index 0000000..032333f --- /dev/null +++ b/String/PatternMatching/README.md @@ -0,0 +1,40 @@ +KMP Algorithm: + +The Knuth-Morris-Pratt algorithm is used to find a given text from a specific word. + +This algorithm uses the degenerating property which primarily works based on sub-strings. + +The basic idea of this algorithm is to not match a character which is already known to match for sure. + +Example: + +Output: Enter the pattern to be found: + +Input: cktober + +Output : Enter the string in which the pattern is to be found: + +Input: hacktoberfest + +Output/Answer: Pattern found at index: 2 + +Rabin Karp Algorithm + +The Rabin karp Algorithm is used to search a text from a given word using hash function. + +This algorithm doesnt search slide through every character but filters the unlike characters and then does the comparing. + +Example: + +Enter the pattern to be found: + +program + +Enter the string in which the pattern is to be found: + +competitveprogramming + +Pattern found at index: 10 + + +