forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KMP.c
70 lines (54 loc) · 1.34 KB
/
KMP.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Program to implement KnuthMorrisPratt Algorithm in C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void KnuthMorrisPratt(const char* X, const char* Y, int m, int n)
{
if (*X == '\0' || m < n)
printf("Pattern is not found");
if (*X == '\0' || m < n)
printf("Pattern is not found");
if (*Y == '\0' || n == 0)
printf("Pattern with shift 0");
int alternate[n + 1];
for (int i = 0; i < n + 1; i++)
alternate[i] = 0;
for (int i = 1; i < n; i++)
{
int j = alternate[i + 1];
while (j > 0 && Y[j] != Y[i])
j = alternate[j];
if (j > 0 || Y[j] == Y[i])
alternate[i + 1] = j + 1;
}
for (int i = 0, j = 0; i < m; i++)
{
if (*(X + i) == *(Y + j))
{
if (++j == n)
printf("Pattern occurs with shift %d\n", i - j + 1);
}
else if (j > 0) {
j = alternate[j];
i--;
}
}
}
// Program to implement KnuthMorrisPratt Algorithm in C
int main(void)
{
char word[20];
gets(word);
char pattern[20];
gets(pattern);
int n = strlen(word);
int m = strlen(pattern);
KnuthMorrisPratt(word, pattern, n, m);
return 0;
}
///Sample IP:
// BACPARKWRPARM
// PAR
///Sample OP:
// Pattern occurs with shift 3
// Pattern occurs with shift 9