Rolling Hash(Rabin-Karp 算法)

// 闲言少许,直奔主题,我是宋鹏举。

import java.io.PrintStream;

public class RollingHash
{
  private static final int R = 31;
  private int _hash;
  private int _rK;
  private CharSequence _c;
  private int _k;
  private int _offset;

  public RollingHash(int k)
  {
    if (k <= 0) {
      throw new IllegalArgumentException("k must be positive");
    }
    this._k = k;

    this._rK = 1;
    for (int i = 1; i < k; i++) {
      this._rK *= 31;
    }
  }

  public void init(CharSequence c)
  {
    if (this._k > c.length()) {
      throw new IllegalArgumentException("k is greater then lenght of content");
    }
    this._c = c;
    this._offset = 0;

    this._hash = 0;
    for (int i = 0; i < this._k; i++) {
      this._hash = (this._hash * 31 + this._c.charAt(i));
    }
  }

  public boolean hasNext()
  {
    if (this._offset + this._k >= this._c.length()) {
      return false;
    }
    return true;
  }

  public int next()
  {
    if (this._offset + this._k >= this._c.length()) {
      throw new IllegalStateException("Past the end of content.");
    }
    int currentHash = this._hash;

    this._hash = ((this._hash - this._rK * this._c.charAt(this._offset)) * 31 + this._c.charAt(this._offset + this._k));

    this._offset += 1;

    return currentHash;
  }

  public static void main(String[] args)
  {
    String text = "01234567890123456789012345678901234567890123456789testesteskdjfgdkfjghdkfjghdkfjghdkjgh";

    int k = 20;

    RollingHash rh = new RollingHash(k);
    rh.init(text);
    for (int i = 0; rh.hasNext(); i++) {
      System.out.println("Hash " + i + ": " + rh.next());
    }
  }
}
时间: 2024-10-11 17:35:45

Rolling Hash(Rabin-Karp 算法)的相关文章

Rabin Karp 算法实战

关键字 Rabin karp 算法, C++, ubuntu 14.04, linux, big integer, gmp 为了计算冗余度, 我写出了如下算法 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

Rolling Hash(Rabin-Karp算法)匹配字符串

您可以在我的个人博客中访问此篇文章: http://acbingo.cn/2015/08/09/Rolling%20Hash(Rabin-Karp%E7%AE%97%E6%B3%95)%E5%8C%B9%E9%85%8D%E5%AD%97%E7%AC%A6%E4%B8%B2/ 该算法常用的场景 字符串中查找子串,字符串中查找anagram形式的子串问题. 关于字符串查找与匹配 字符串可以理解为字符数组.而字符可以被转换为整数,他们具体的值依赖于他们的编码方式(ASCII/Unicode).这意味

hdu2389二分图之Hopcroft Karp算法

You're giving a party in the garden of your villa by the sea. The party is a huge success, and everyone is here. It's a warm, sunny evening, and a soothing wind sends fresh, salty air from the sea. The evening is progressing just as you had imagined.

负载均衡算法(四)IP Hash负载均衡算法

/// <summary> /// IP Hash负载均衡算法 /// </summary> public static class IpHash { static Dictionary<string, int> dic = new Dictionary<string, int> { { "192.168.1.12", 1}, {"192.168.1.13", 1 }, { "192.168.1.14&quo

一致性hash与CRUSH算法总结

相同之处:都解决了数据缓存系统中数据如何存储与路由. 不同之处:区别在于虚拟节点和物理节点的映射办法不同 由于一般的哈希函数返回一个int(32bit)型的hashCode.因此,可以将该哈希函数能够返回的hashCode表示成一个范围为0---(2^32)-1 环 数据和节点使用相同的hash函数来保证 把数据和节点映射到相同的hash空间上.这样,按照顺时针方向,数据存放在它所在的顺时针方向上的那个机器上.这就是一致性哈希算法分配数据的方式! 物理节点:  使用ip或者唯一机器标识为key

memcache通过hash取模算法,实现多服务器存取值

<?php //封装一个hash算法类 class Mem{ //存储memcache的服务器个数 private $hostCount=''; //多个服务器 private $host=[]; //构造方法用来给接收值,给属性赋值 public function __construct($hostServer) { $this->hostCount = count($hostServer); $this->host = $hostServer; } //计算key的位置,返回的是当前

HASH处理KMP算法

模拟赛当天,YZR大佬告诉我可以用HASH来做KMP,然后当场没做出来,今天正好没事干,打了个HASH试试看,结果真把KMP的题给过了 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 const int Bas = 131, M = 1e6 + 10; 6 char a[M],b[M]; 7 long long la,lb; 8 lon

LCS problem using Binary-Search\Rolling hash

One TopCoder article introduces a very interesting alternative solution to Longest Common Sequences problem. It is based on this statement (http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=stringSearching): The important point that a

模式字符串匹配问题(KMP算法)

这两天又看了一遍<算法导论>上面的字符串匹配那一节,下面是实现的几个程序,可能有错误,仅供参考和交流. 关于详细的讲解,网上有很多,大多数算法及数据结构书中都应该有涉及,由于时间限制,在这就不重复了. 需要说明的是: stra:主串,及需要从中寻找模式串的字符串 strb:模式串 <算法导论>上面包括严蔚敏老师<数据结构>,字符串下表是按从1开始,并且<数据结构>一书中貌似吧字符串的第一个字符用来储存字符串长度.这里我改成了0. maxlen :字符串的最长