关于Sunday算法的某些理解..

public static int indexOf(String Text, String Key, int i) {		
		char[] Textchars = Text.toCharArray();
		char[] Keychars = Key.toCharArray();
		int iter_Text, iter_Key;
		int Num_Text = Text.length(), Num_Key = Key.length(); 
		int[] Increase = new int[26];
		for (int iter_Increase = 0; iter_Increase < Increase.length; iter_Increase++) {
			Increase[iter_Increase] = Num_Key + 1;
		}
		for (iter_Key = 0; iter_Key < Num_Key; iter_Key++) {
			Increase[Keychars[iter_Key] - 'a'] = Num_Key - iter_Key;
		}
		
		int pos = i;
		while(pos < (Num_Text - Num_Key + 1)){
			iter_Text = pos;
			for (iter_Key = 0; iter_Key < Num_Key; iter_Key++, iter_Text++) {
				if (Textchars[iter_Text] != Keychars[iter_Key]) {
					pos += Increase[Textchars[pos + Num_Key] - 'a'];
					break;
				}
			}
			if (iter_Key == Num_Key) {
				return pos;
			}
		}		
		return false;
	}


代码很简单...思路也很简单...通过ASCII码的性质,在数组Increase建立 字母——增量 之间的映射(有点像map容器中的键值对)。在使用时(遇到不匹配时)根据Increase的增量跃迁匹配串,然后从头开始继续匹配,如此往复,直到找到目标串...

当然了,Sunday算法的核心在于如何构建increase增量数组(这也是string matching algorithm的核心),详情请自行去看paper..

以上..

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值