逆向因子算法

时间:2018-04-13 15:41:59

标签: algorithm search string-search

我是新手。我已经学习了两个月的算法了。我一般都没事,但我不擅长理解搜索算法,也不擅长实现它们。我特别坚持这种模式搜索算法,反向因子。我已经研究了一个星期了,但我仍然没有完全理解它,更不用说实现它了。我没有任何人可以问,但我不想跳过任何算法。 到目前为止,我已经找到了这个算法。但我不太了解它。我也不是母语人士。你能救我吗?

目的是“在字符串t中搜索模式p”。

Algorithm RF /* reverse factor string matching */
    /* denote t[i + j.. i + m] by x;
         it is the last-scanned part of the text */

    i:= 0; 
    while i _< n - m do
    { 
        j:= m; 
        while j > 1 and x ϵ FACT(p) 
            do j:=j- 1;
        /* in fact, we check the equivalent condition x^R ϵ FACT(p^R) */
        if x = p then 
            report a match at position i;
        shift := RF shift[x];
        i := i + shift;
    }
end.

事实(p)是p的所有因子(子串)的集合。

提前谢谢你。

1 个答案:

答案 0 :(得分:1)

我会试一试:

i:= 0; 
while i _< n - m do //start at character 0
{ 
    j:= m; //start at character i + m (the potentially last character)
    whilej > 1 and x ϵ FACT(p)
        do j:=j- 1; //step back as long as t[i+j,i+m] is a substring of the pattern p
    /* in fact, we check the equivalent condition x^R ϵ FACT(p^R) */
    if x = p then // x=[i+0, i+m] == p
        report a match at position i; 
    shift := RF shift[x]; // look up the number of chars to advance
    i := i + shift; // advance
}

数组shift的构造非常困难。我不记得这是怎么做到的。但是,我可以说在shift[x]会找到什么。

shift[x] = the number of save character shifts such that the next search does not miss a match.

示例:拥有字符串abcabcdab和模式bcd| is i+m, * is i+j):

abc*|abcdab // start with i=0,j=3
ab*c|abcdab // c is a factor => continue
a*bc|abcdab // bc is a factor => continue
*abc|abcdab // abc is not a factor => shift = shift[bc] = 1
abca*|bcdab 
abc*a|bcdab // a is not a factor => shift = shift[] = 3
abcabcd*|ab 
abcabc*d|ab // d is a factor => continue
abcab*cd|ab // cd is a factor => continue
abca*bcd|ab // bcd is a factor and j = 0 => report match

点击此处查看example for debugging in Java。它不像您的伪代码那么简单,但您可以调试它以便更好地理解。