forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KMP.rb
76 lines (62 loc) · 1.49 KB
/
KMP.rb
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
71
72
73
74
75
76
# Given a text and a pattern, we need to find all occurrences of pattern in text
def kmp_search(pattern, text)
pattern_size = pattern.length
text_size = text.length
lps = Array.new(pattern_size) # longest_prefix_suffix array
lps = calculate_lps(pattern, lps) # compute lps[]
i = 0 # index for pattern[]
j = 0 # index for text[]
while i < text_size
if pattern[j] == text[i]
i += 1
j += 1
end
if j == pattern_size
puts "Pattern found at #{i - j + 1}"
j = lps[j - 1]
elsif i < text_size && pattern[j] != text[i]
if j != 0
j = lps[j - 1]
else
i += 1
end
end
end
end
# function to preprocess lps[] to hold the longest prefix suffix values for pattern
def calculate_lps(pattern, lps)
len = 0
lps[0] = 0
i = 1
while i < pattern.length
if pattern[i] == pattern[len]
len += 1
lps[i] = len
i += 1
else
if len != 0
len = lps[len - 1]
else
lps[i] = 0
i += 1
end
end
end
return lps
end
# Driver Code
puts "Enter the text:"
text = gets.chomp
puts "Enter the pattern:"
pattern = gets.chomp
kmp_search(pattern, text)
=begin
Enter the text:
namanchamanbomanamansanam
Enter the pattern:
aman
Output
Pattern found at 2
Pattern found at 8
Pattern found at 17
=end