mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-08-02 23:00:35 +08:00
@ -35,21 +35,19 @@ public class RabinKarp {
|
||||
h = (int) Math.pow(d, m - 1) % q;
|
||||
|
||||
for (i = 0; i < m; i++) {
|
||||
// hash value is calculated for each character and then added with the hash value of the next
|
||||
// character for pattern
|
||||
// as well as the text for length equal to the length of pattern
|
||||
// hash value is calculated for each character and then added with the hash value of the
|
||||
// next character for pattern as well as the text for length equal to the length of
|
||||
// pattern
|
||||
p = (d * p + pattern.charAt(i)) % q;
|
||||
t = (d * t + text.charAt(i)) % q;
|
||||
}
|
||||
|
||||
for (i = 0; i <= n - m; i++) {
|
||||
// if the calculated hash value of the pattern and text matches then
|
||||
// all the characters of the pattern is matched with the text of length equal to length of the
|
||||
// pattern
|
||||
// if all matches then pattern exist in string
|
||||
// if not then the hash value of the first character of the text is subtracted and hash value
|
||||
// of the next character after the end
|
||||
// of the evaluated characters is added
|
||||
// all the characters of the pattern is matched with the text of length equal to length
|
||||
// of the pattern if all matches then pattern exist in string if not then the hash value
|
||||
// of the first character of the text is subtracted and hash value of the next character
|
||||
// after the end of the evaluated characters is added
|
||||
if (p == t) {
|
||||
// if hash value matches then the individual characters are matched
|
||||
for (j = 0; j < m; j++) {
|
||||
@ -65,10 +63,9 @@ public class RabinKarp {
|
||||
}
|
||||
}
|
||||
|
||||
// if i<n-m then hash value of the first character of the text is subtracted and hash value of
|
||||
// the next character after the end
|
||||
// of the evaluated characters is added to get the hash value of the next window of characters
|
||||
// in the text
|
||||
// if i<n-m then hash value of the first character of the text is subtracted and hash
|
||||
// value of the next character after the end of the evaluated characters is added to get
|
||||
// the hash value of the next window of characters in the text
|
||||
if (i < n - m) {
|
||||
t = (d * (t - text.charAt(i) * h) + text.charAt(i + m)) % q;
|
||||
|
||||
|
Reference in New Issue
Block a user