This repository has been archived by the owner on Nov 3, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rabin-Karp Algorithm for Pattern Searching
- Loading branch information
1 parent
5aefa58
commit cef14d6
Showing
6 changed files
with
187 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"configurations": [ | ||
{ | ||
"name": "windows-gcc-x86", | ||
"includePath": [ | ||
"${workspaceFolder}/**" | ||
], | ||
"compilerPath": "C:/MinGW/bin/gcc.exe", | ||
"cStandard": "${default}", | ||
"cppStandard": "${default}", | ||
"intelliSenseMode": "windows-gcc-x86", | ||
"compilerArgs": [ | ||
"" | ||
] | ||
} | ||
], | ||
"version": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "C/C++ Runner: Debug Session", | ||
"type": "cppdbg", | ||
"request": "launch", | ||
"args": [], | ||
"stopAtEntry": false, | ||
"externalConsole": true, | ||
"cwd": ".", | ||
"program": "build/Debug/outDebug", | ||
"MIMode": "gdb", | ||
"miDebuggerPath": "gdb", | ||
"setupCommands": [ | ||
{ | ||
"description": "Enable pretty-printing for gdb", | ||
"text": "-enable-pretty-printing", | ||
"ignoreFailures": true | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
{ | ||
"C_Cpp_Runner.cCompilerPath": "gcc", | ||
"C_Cpp_Runner.cppCompilerPath": "g++", | ||
"C_Cpp_Runner.debuggerPath": "gdb", | ||
"C_Cpp_Runner.cStandard": "", | ||
"C_Cpp_Runner.cppStandard": "", | ||
"C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat", | ||
"C_Cpp_Runner.useMsvc": false, | ||
"C_Cpp_Runner.warnings": [ | ||
"-Wall", | ||
"-Wextra", | ||
"-Wpedantic", | ||
"-Wshadow", | ||
"-Wformat=2", | ||
"-Wcast-align", | ||
"-Wconversion", | ||
"-Wsign-conversion", | ||
"-Wnull-dereference" | ||
], | ||
"C_Cpp_Runner.msvcWarnings": [ | ||
"/W4", | ||
"/permissive-", | ||
"/w14242", | ||
"/w14287", | ||
"/w14296", | ||
"/w14311", | ||
"/w14826", | ||
"/w44062", | ||
"/w44242", | ||
"/w14905", | ||
"/w14906", | ||
"/w14263", | ||
"/w44265", | ||
"/w14928" | ||
], | ||
"C_Cpp_Runner.enableWarnings": true, | ||
"C_Cpp_Runner.warningsAsError": false, | ||
"C_Cpp_Runner.compilerArgs": [], | ||
"C_Cpp_Runner.linkerArgs": [], | ||
"C_Cpp_Runner.includePaths": [], | ||
"C_Cpp_Runner.includeSearch": [ | ||
"*", | ||
"**/*" | ||
], | ||
"C_Cpp_Runner.excludeSearch": [ | ||
"**/build", | ||
"**/build/**", | ||
"**/.*", | ||
"**/.*/**", | ||
"**/.vscode", | ||
"**/.vscode/**" | ||
], | ||
"C_Cpp_Runner.useAddressSanitizer": false, | ||
"C_Cpp_Runner.useUndefinedSanitizer": false, | ||
"C_Cpp_Runner.useLeakSanitizer": false, | ||
"C_Cpp_Runner.showCompilationTime": false, | ||
"C_Cpp_Runner.useLinkTimeOptimization": false, | ||
"C_Cpp_Runner.msvcSecureNoWarnings": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
Beginner Level 📁/rabinKarpAlgorithmForPatternSearching/question.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
Rabin-Karp Algorithm for Pattern Searching | ||
Given a text T[0. . .n-1] and a pattern P[0. . .m-1], write a function search(char P[], char T[]) that prints all occurrences of P[] present in T[] using Rabin Karp algorithm. You may assume that n > m. | ||
|
||
Examples: | ||
Input: T[] = “THIS IS A TEST TEXT”, P[] = “TEST” | ||
Output: Pattern found at index 10 | ||
|
||
|
||
Input: T[] = “AABAACAADAABAABA”, P[] = “AABA” | ||
Output: Pattern found at index 0 | ||
Pattern found at index 9 | ||
Pattern found at index 12 |
46 changes: 46 additions & 0 deletions
46
Beginner Level 📁/rabinKarpAlgorithmForPatternSearching/solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
#define d 256 | ||
void search(char pat[], char txt[], int q) | ||
{ | ||
int M = strlen(pat); | ||
int N = strlen(txt); | ||
int i, j; | ||
int p = 0; | ||
int t = 0; | ||
int h = 1; | ||
|
||
for (i = 0; i < M - 1; i++) | ||
h = (h * d) % q; | ||
|
||
for (i = 0; i < M; i++) { | ||
p = (d * p + pat[i]) % q; | ||
t = (d * t + txt[i]) % q; | ||
} | ||
|
||
for (i = 0; i <= N - M; i++) { | ||
if (p == t) { | ||
for (j = 0; j < M; j++) { | ||
if (txt[i + j] != pat[j]) { | ||
break; | ||
} | ||
} | ||
if (j == M) | ||
cout << "Pattern found at index " << i | ||
<< endl; | ||
} | ||
if (i < N - M) { | ||
t = (d * (t - txt[i] * h) + txt[i + M]) % q; | ||
if (t < 0) | ||
t = (t + q); | ||
} | ||
} | ||
} | ||
|
||
int main(){ | ||
char txt[] = "GEEKS FOR GEEKS"; | ||
char pat[] = "GEEK"; | ||
int q = INT_MAX; | ||
search(pat, txt, q); | ||
return 0; | ||
} |