Skip to content
This repository has been archived by the owner on Nov 3, 2024. It is now read-only.

Commit

Permalink
Rabin-Karp Algorithm for Pattern Searching
Browse files Browse the repository at this point in the history
  • Loading branch information
gauravbhaskar080 committed Oct 19, 2024
1 parent 5aefa58 commit cef14d6
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 29 deletions.
18 changes: 18 additions & 0 deletions .vscode/c_cpp_properties.json
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
}
24 changes: 24 additions & 0 deletions .vscode/launch.json
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
}
]
}
]
}
59 changes: 59 additions & 0 deletions .vscode/settings.json
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
}
57 changes: 28 additions & 29 deletions Beginner Level 📁/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,15 @@
"name": "Swapna Devi",
"githubUsername": "Swapna804"
},

{
"name": "Shyam Sundar Vashishtha",
"githubUsername": "shyam640"

},


{
"name": "Surbhi Mayank",
"githubUsername": "surbhi2408"

},

{
Expand Down Expand Up @@ -86,45 +83,45 @@
"githubUsername": "parthkgh24"
},

{
"name": "Afzal Khan",
"githubUsername": "afzall-khan",
},
{
"name": "Afzal Khan",
"githubUsername": "afzall-khan"
},

{
"name": "Thejasree Kilari",
"githubUsername": "ThejasreeKilari"
},
{
{
"name": "Reidho Satria",
"githubUsername": "Eilaluth",
},
{
"githubUsername": "Eilaluth"
},
{
"name": "Jill Oestreicher",
"githubUsername": "jilloestreicher",
},
"githubUsername": "jilloestreicher"
},
{
"name": "Debargha Mukherjee",
"githubUsername": "Debargha-arch",
"name": "Debargha Mukherjee",
"githubUsername": "Debargha-arch"
},
{
"name": "Aryan Singh",
"githubUsername": "chipper1211"
},
{
"name": "Amit Zope",
"githubUsername": "amitsat27"
"name": "Amit Zope",
"githubUsername": "amitsat27"
},
{
"name": "Siddhant Kaura",
"githubUsername": "SiddhantKaura"
"githubUsername": "SiddhantKaura"
},
{
{
"name": "Atherv Patil",
"githubUsername": "ColonelAVP",
},
{
"githubUsername": "ColonelAVP"
},

{
"name": "Lisa V Cherian",
"githubUsername": "LisaVCherian"
},
Expand All @@ -133,18 +130,20 @@
"githubUsername": "gktc"
},

{
{
"name": "Vinayaka D",
"githubUsername": "vd-37"
},
{
{
"name": "Ramanpreet Kaur",
"githubUsername": "1998ramanpreet"
},
{
"name": "Vimal M",
"githubUsername": "vimall03"
},

]

{
"name": "Gaurav Bhaskar",
"githubUsername": "gauravbhaskar080"
}
]
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
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;
}

0 comments on commit cef14d6

Please sign in to comment.