horspool算法问题,代码没问题,就是不太明白Horspool-Match函数的语句的意思,例如short skip[256],求此函数的详细注释
#include <iostream>#include <vector>
#include <string>
#include <cstdlib>
using namespace std;
const m=100;
const n=50;
int HorspoolMatch(char S[], int SSize,char T[], int TSize)
{
if (TSize>SSize)
{
return -1;
}
short skip[256];
int i;
for(i = 0; i < 256; i++)
{
skip[i]=TSize;
}
for(i = 0; i<TSize - 1; i++)
{
skip[T[i]] = TSize - i - 1;
}
int pos = 0;
while(pos <=SSize - TSize)
{
int j = TSize -1;
while(j >= 0 && S[pos + j] == T[j])
{
j--;
}
if(j < 0 )
{
break;
}
pos = pos + skip[S[pos + TSize -1]];
}
return pos;
}
int main()
{
char S[m];
char T[n];
int i,j;
cout<<"请输入需要匹配的字符串:"<<endl;
cin.getline(S,m-1);
for(i=0;S[i]!=0;i++);
cout<<"需要匹配的字符串的长度为"<<i<<endl;
cout<<"请输入模板字符串:"<<endl;
cin.getline(T,n-1);
for(j=0;T[j]!=0;j++);
cout<<"模板字符串的长度为"<<j<<endl;
int k;
k=HorspoolMatch(S,i,T,j);
if(k==-1)
cout<<"匹配失败"<<endl;
else
cout<<"匹配成功,匹配成功的位置为\n"<<k+1<<endl;
return 0;
}