【发布时间】:2014-05-02 04:33:02
【问题描述】:
KMP algorithm for string matching。
以下是我在网上找到的用于计算最长前缀-后缀数组的code:
定义:
lps[i] = the longest proper prefix of pat[0..i]
which is also a suffix of pat[0..i].
代码:
void computeLPSArray(char *pat, int M, int *lps)
{
int len = 0; // length of the previous longest prefix suffix
int i;
lps[0] = 0; // lps[0] is always 0
i = 1;
// the loop calculates lps[i] for i = 1 to M-1
while(i < M)
{
if(pat[i] == pat[len])
{
len++;
lps[i] = len;
i++;
}
else // (pat[i] != pat[len])
{
if( len != 0 )
{
// This is tricky. Consider the example AAACAAAA and i = 7.
len = lps[len-1]; //*****************
// Also, note that we do not increment i here
}
else // if (len == 0)
{
lps[i] = 0;
i++;
}
}
}
}
我可以用len = len-1代替len = lps[len-1]吗?
因为 len 总是从 [0 .. someIndex] 开始计算前缀长度。那为什么在这里使用 lps 进行赋值呢?以下是我测试过哪些工作正常的案例(第一行是模式,随后两行是原始分配和修改后分配给 len 的结果):
a a a b a b c
0 1 2 0 1 0 0
0 1 2 0 1 0 0
a b c b a b c
0 0 0 0 1 2 3
0 0 0 0 1 2 3
a a b c b a b
0 1 0 0 0 1 0
0 1 0 0 0 1 0
在此处编写两种变体的代码:http://ideone.com/qiSrUo
【问题讨论】:
标签: c++ algorithm string-matching