Karp_Rabin算法

原创作品,出自 “晓风残月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!");
		}
	}
}


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值