超过 1 个哈希值指向相同的模式

问题描述

我已经构建了一个散列函数来散列字符串及其子字符串,但是在测试它时,我得到了超过 1 个相同模式的散列值,我无法确定原因,有人能帮我找出问题吗

   static class Hash {
    int N = (int) (1e6 + 6);
    int mod = (int) (1e9 + 9);
    final int base = 256;

    int add(int a,int b,int mod) {
        int res = (a + b) % mod;
        if (res < 0) {
            res += mod;
        }
        return res;
    }

    int mult(int a,int mod) {
        int res = (int) ((a * (long) 1 * b) % mod);
        if (res < 0) {
            res += mod;
        }
        return res;
    }

    int power(int a,int mod) {
        int res = 1;
        a = a % mod;
        if (a == 0) {
            return 0;
        } else {
            while (b > 0) {
                if (b % 2 == 1) {
                    res = mult(res,a,mod);
                }
                a = mult(a,mod);
                b /= 2;
            }
        }
        return res;
    }

    int[] pw = new int[N]; // power
    int[] inv = new int[N]; // inverse
    int[] hash = new int[N]; // string hash

    // precalculation for creatinf power,inveres,string hash array
    void precalc() {
        pw[0] = 1;
        for (int i = 1; i < N; i++) {
            pw[i] = mult(pw[i - 1],base,mod);
        }

        int pw_inv = power(base,mod - 2,mod);
        // int pw_inv = (int)Math.pow(base,mod-2)%mod;
        inv[0] = 1;
        for (int i = 1; i < N; i++) {
            inv[i] = mult(inv[i - 1],pw_inv,mod);

        }

    }

    // buid hash of a string
    void build(String s) {
        int n = s.length();
        for (int i = 0; i < n; ++i) {
            hash[i] = add((i == 0) ? 0 : hash[i - 1],mult(pw[i],s.charat(i) - 'A' + 1,mod),mod);
        }
    }

    // getting hash of a substring
    int getHash(int x,int y) {
        int res = add(hash[y],(x == 0) ? 0 : -hash[x - 1],mod);
        res = mult(res,inv[x],mod);
        return res;
    }
}


    public static ArrayList<Integer> pattern_search(String s,String pat) {
    Hash hash = new Hash();
    ArrayList<Integer> indexes = new ArrayList<>();

    // initializing hash function for easy calculation of substring hash
    hash.precalc();
    hash.build(pat);
    hash.build(s);

    int hashpat = hash.getHash(0,pat.length());

    //
    System.out.println("pattern");
    System.out.println(pat + " " + hashpat);

    // since hash of string is alreay calculation we just need to calculate the
    // substring hash
    System.out.println("substrings");
    for (int i = 0; i < s.length() - pat.length() + 1; i++) {
        int x = i + pat.length();
        System.out.println(s.substring(i,x) + " " + hash.getHash(i,x));

        if (hash.getHash(i,x) == hashpat) {
            indexes.add(i);
        }
    }

    return indexes;

}

input:-
AABAABACAADAABAABAABA
AABA

output:-

pattern
**AABA 311875805**
substrings
**AABA 311875805**
ABAA 606777785
BAAB 328587486
**AABA 901810325**
ABAC 345364957
BACA 311941342
ACAA 196712552
CAAD 362141919
AADA 312006877
ADAA 606778297
DAAB 328587488
**AABA 311875805**
ABAA 606777785
BAAB 328587486
**AABA 311875805**
ABAA 606777785
BAAB 328587486
**AABA 509992049**

如您所见,带星号的输出是相同的子字符串,但输出不同

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...