package Others;/** @author Prateek Kumar Oraon (https://github.com/prateekKrOraon) */import java.util.Scanner;// An implementation of Rabin-Karp string matching algorithm// Program will simply end if there is no matchpublic class RabinKarp {public static Scanner scanner = null;public static final int d = 256;public static void main(String[] args) {scanner = new Scanner(System.in);System.out.println("Enter String");String text = scanner.nextLine();System.out.println("Enter pattern");String pattern = scanner.nextLine();int q = 101;searchPat(text, pattern, q);}private static void searchPat(String text, String pattern, int q) {int m = pattern.length();int n = text.length();int t = 0;int p = 0;int h = 1;int j = 0;int i = 0;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 patternp = (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 addedif (p == t) {// if hash value matches then the individual characters are matchedfor (j = 0; j < m; j++) {// if not matched then break out of the loopif (text.charAt(i + j) != pattern.charAt(j)) break;}// if all characters are matched then pattern exist in the stringif (j == m) {System.out.println("Pattern found at index " + i);}}// 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 textif (i < n - m) {t = (d * (t - text.charAt(i) * h) + text.charAt(i + m)) % q;// if hash value becomes less than zero than q is added to make it positiveif (t < 0) t = (t + q);}}}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。