Rabin Carp Algorithm
unknown
c_cpp
3 years ago
979 B
8
Indexable
void search(char pat[], char txt[], int q) { int M = strlen(pat); int N = strlen(txt); int i, j; int p = 0; // hash value for pattern int t = 0; // hash value for txt 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) { /* Check for characters one by one */ for (j = 0; j < M; j++) { if (txt[i + j] != pat[j]) break; } // if p == t and pat[0...M-1] = txt[i, i+1, ...i+M-1] if (j == M) printf("Pattern found at index %d \n", i); } if (i < N - M) { t = (d * (t - txt[i] * h) + txt[i + M]) % q; if (t < 0) t = (t + q); } } }
Editor is loading...