File tree Expand file tree Collapse file tree 2 files changed +143
-0
lines changed Expand file tree Collapse file tree 2 files changed +143
-0
lines changed Original file line number Diff line number Diff line change
1
+ // KMP Algorithm - Pattern Searching Algorithm
2
+
3
+ #include < iostream>
4
+ using namespace std ;
5
+
6
+ // Function to implement the KMP algorithm
7
+ void KMP (string t, string p)
8
+ {
9
+ int tlen = t.length ();
10
+ int plen = p.length ();
11
+
12
+ // if `text` is an empty string
13
+ if (plen == 0 )
14
+ {
15
+ cout << " The pattern at index 0" ;
16
+ return ;
17
+ }
18
+
19
+ // if X's length is less than that of Y's
20
+ if (tlen < plen)
21
+ {
22
+ cout << " Pattern not found" ;
23
+ return ;
24
+ }
25
+
26
+ // `next[i]` stores the index of the next best partial match
27
+ int next[plen + 1 ];
28
+
29
+ for (int i = 0 ; i < plen + 1 ; i++) {
30
+ next[i] = 0 ;
31
+ }
32
+
33
+ for (int i = 1 ; i < plen; i++)
34
+ {
35
+ int j = next[i + 1 ];
36
+
37
+ while (j > 0 && p[j] != p[i]) {
38
+ j = next[j];
39
+ }
40
+
41
+ if (j > 0 || p[j] == p[i]) {
42
+ next[i + 1 ] = j + 1 ;
43
+ }
44
+ }
45
+
46
+ for (int i = 0 , j = 0 ; i < tlen; i++)
47
+ {
48
+ if (t[i] == p[j])
49
+ {
50
+ if (++j == plen) {
51
+ cout << " The pattern occurs at index " << i - j + 1 << endl;
52
+ }
53
+ }
54
+ else if (j > 0 )
55
+ {
56
+ j = next[j];
57
+ i--; // since `i` will be incremented in the next iteration
58
+ }
59
+ }
60
+ }
61
+
62
+ // Program to implement the KMP algorithm in C++
63
+ int main ()
64
+ {
65
+ string text;
66
+ cout<<" Enter text: " ;
67
+ cin>>text;
68
+ string pattern;
69
+ cout<<" Enter pattern: " ;
70
+ cin>>pattern;
71
+ KMP (text, pattern);
72
+ return 0 ;
73
+ }
Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+ const int t = 256 ;
4
+ const int w = 1283 ;
5
+ void Rabin_Karp_Algo (string text,string pattern)
6
+ {
7
+
8
+ // Length of text string.
9
+ int tlen = text.length ();
10
+
11
+ // Length of pattern string.
12
+ int plen = pattern.length ();
13
+
14
+ int flag;
15
+ int c=1 ,i=0 ;
16
+
17
+ // Calculate hash_p(hash value of pattern) and hash_0
18
+ int hash_p=0 ,hash=0 ;
19
+ for (int i=0 ;i<plen;i++)
20
+ {
21
+ hash_p=(hash_p*t + pattern[i])%w;
22
+ hash=(hash*t + text[i])%w;
23
+
24
+ }
25
+
26
+ // Calculate (t^(plen-1))%w
27
+ for (int i=1 ;i<=plen-1 ;i++)
28
+ c=(c*t)%w;
29
+
30
+ i=0 ;
31
+ while (i<=tlen-plen){
32
+ if (hash_p==hash){
33
+ flag=1 ;
34
+ for (int j=0 ;j<plen;j++){
35
+ if (pattern[j]==text[i+j]){
36
+ continue ;
37
+ }
38
+ else {
39
+ flag = 0 ;
40
+ break ;
41
+ }
42
+ }
43
+ if (flag==1 )
44
+ cout<<" Index of occurrence is " <<i<<" " ;
45
+ }
46
+
47
+
48
+ // Calculate hash value of next window
49
+ // hash_i+1 = t * {hash_i - text[i] * t^(plen-1)} + text[i+plen]
50
+ if (i<tlen-plen){
51
+ hash=((t*(hash-text[i]*c))+text[i+plen])%w;
52
+ if (hash<0 ){
53
+ hash = hash + w;
54
+ }
55
+ }
56
+ i++;
57
+ }
58
+ }
59
+
60
+ int main ()
61
+ {
62
+ string text;
63
+ cout<<" Enter the text: " ;
64
+ cin>>text;
65
+ string pattern;
66
+ cout<<" Enter the pattern: " ;
67
+ cin>>pattern;
68
+ Rabin_Karp_Algo (text,pattern);
69
+ return 0 ;
70
+ }
You can’t perform that action at this time.
0 commit comments