原创作品,出自 “晓风残月xj” 博客,欢迎转载,转载时请务必注明出处(http://blog.csdn.net/xiaofengcanyuexj)。
由于各种原因,可能存在诸多不足,欢迎斧正!
在教材上学过BF算法、KMP算法、也会DFA,今天学习了下Karp_Rabin算法(简称KR算法)。
Karp Rabin 算法是利用hash函数的特性进行字符串匹配的。KR算法对模式串和循环中每一次要匹配的子串按一定的hash函数值,
如果hash值相同,才运用BF算法匹配。如果hash函数设计得好的话,不同子串hash值相同是小概率事件,且速度较快。在虽然理论上
KR算法的时间复杂度是O(m*n),但现实应用中一般是O(m+n)(模式串在主串中出现的次数较少时适合此算法)。
关键是hash函数的设计,可以用hash(str[0..m-1])=(str[0]*k^(m-1)+str[1]*k^(m-2)+……+str[m-1]*k^0)mod q;(k是进制基数,q通常取
较大的素数,如10^9+7)。不同题目具体对待。
import java.util.Scanner;
public class Karp_Rabin {
static Scanner key = new Scanner(System.in);
static int KR(String text, String word) {
int text_hash = 0, word_hash = 0;
for (int i = 0; i < word.length(); i++) {
text_hash = (text_hash << 1) + text.charAt(i);
word_hash = (word_hash << 1) + word.charAt(i);
}
int ret = 0;
for (int i = 0; i < text.length() - word.length(); i++) {
if (text_hash == word_hash) {
boolean flag=true;
for (int j = 0; j < word.length(); j++) {
if((int)(text.charAt(i+j))!=(int)(word.charAt(j)))
{
flag=false;
break;
}
}
if(flag==true)
ret++;
}
text_hash=((text_hash-(text.charAt(i)<<(word.length()-1)))<<1)+text.charAt(i+word.length());
}
return ret;
}
public static void main(String[] str) {
while(true)
{
String text = key.next();
String word = key.next();
System.out.println(word+" in "+text+",occur "+KR(text,word)+" times!");
}
}
}