使用rabin karp算法在某些情况下匹配字符串,而在其他情况下不匹配?

时间:2018-10-29 11:08:53

标签: c++11 codeblocks string-matching hash-function rabin-karp

public static void waitUntilVisible(By locator) {
    final long startTime = System.currentTimeMillis();
    final Duration duration = Duration.ofSeconds(2);
    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
        .pollingEvery(duration)
        .ignoring(StaleElementReferenceException.class);

    while ((System.currentTimeMillis() - startTime) < 91000) {
        try {
            wait.until(ExpectedConditions.presenceOfElementLocated(locator));
            break;
        } catch (StaleElementReferenceException e) {
            log.info("", e);
        }
    }
}

func计算图案的哈希值和文本大小为m的初始幻灯片 使用hash_formula

// n -> length of the text
// m -> length of the pattern    
void rabin_karp_check(int n, int m){
        int h_p = hash_value(pattern, m, 0);       
        int h_t = hash_value(text, m, 0); 
        int x = 0;
        int i = 0,k;
        while(i < n - m + 1){
            if(x > 0){
                h_t = rolling_hash(h_t, m, i);  
                }
            x++;
            int j = i;
            if(h_t == h_p){   
                int match_count = 0;
                k = 0;
                while( match_count < m){        
                    if(pattern[k] == text[j]){
                        match_count++;  k++; j++;
                    }
                    else
                        break;
                }
                if(match_count == m)
                    cout<<"found at "<<i<<"\n";
            }
            i++;
        }
    }

使用上一个计算出的下一个哈希值的功能

//(256^m-1 * p[0] + 256^m-2 * p[1] + 256^m-3 * p[2] + .... + 256^0 * p[m-1]) % q

int hash_value(string &p, int &m, int i){

        int q = 101,k = 0;    
        long long int l = 0;
        for(int j = i;j < i + m ; j++){
            l = l + pow(256, m - 1 - k) * p[j];
            k++;
        }
        return l % q;               
    }

输出#1:

int rolling_hash(int h_t, int m, int i){   
        int x = (   ( (h_t - ((long int)pow(256, m - 1) * text[i-1])) * 256)  +  text[i + m - 1] ) % 101;
            return (x < 0) ? x + 101 : x;
    }

输出#2:未检测到

enter the text: platform for showcasing
enter the pattern: for
found at 4
found at 9

输出#3:已检测到

enter the text: karp rabin
enter the pattern: rabin

输出#4:未检测到

enter the text: best practices in
enter the pattern: ic
found at 10

我无法弄清楚为什么会这样。 我认为在某些情况下,通过滚动哈希函数从先前存储的哈希值计算出的哈希值不等于该size(m)文本的实际哈希值。 我在哪里做错了?

预先感谢

0 个答案:

没有答案