【发布时间】:2014-09-17 17:03:07
【问题描述】:
请解释为什么我们在下面的代码中用 next[j-1] 代替 j 的值: http://computing.dcu.ie/~humphrys/Notes/String/kmp.html 点击链接了解表格概念
i = 0
next[i] = 0
i++
j = 0
while ( i < m )
{
if ( pattern[j] == pattern[i] )
{
next[i] = j+1
i++
j++
}
else ( pattern[j] != pattern[i] )
{
if ( j > 0 )
j = next[j-1] //this part i am not able to figure out
//why don't we just decrement j-1?
else ( j == 0 )
{
next[i] = 0
i++
j = 0
}
}
}
【问题讨论】:
标签: algorithm pattern-matching substring dynamic-programming