1
這是我執行KMP字符串匹配算法。 當我檢查pi陣列,它存儲0,1,2,3,4,5,6。但根據算法書它應該是0,0,1,2,3,0,1。我的代碼也給出了正確的結果。我不明白爲什麼會發生這種情況,或者我做錯了什麼?如果是這樣,請糾正我。KMP字符串匹配算法:輔助陣列輸出
謝謝。
#include<iostream>
#include<string>
#include<string.h>
using namespace std;
int* ComputePrefix(char P[])
{
size_t m = strlen(P);
int *pi = new int[m];
pi[0] = 0;
int k = 0;
for(int q =0; q < m; q++)
{
if(k > 0 && P[k+1] != P[q])
k = pi[k];
if(P[k+1] == P[q])
{
pi[q] = k;
k = k + 1;
}
pi[q]=k;
}
return (pi);
}
void KMP_Matcher(char T[], char P[])
{
size_t n = strlen(T);
size_t m = strlen(P);
int *pi = new int[m];
pi = ComputePrefix(P);
cout<<endl;
int q =0;
for (int i = 0; i <= n; i++)
{
if(q > 0 && P[q] != T[i])
{
q = pi[q - 1];
}
else if(P[q] == T[i])
{
if(q == m-1)
{
cout<<"Shift occurs at : "<< i-q <<endl;
q = pi[q];
}
else q = q + 1;
}
else q++;
}
}
int main()
{
char T[] = "abababacaba";
char P[] = "ababaca";
KMP_Matcher(T,P);
return 0;
}