正如标题所说,我正在做一个项目,在这个项目中我正在搜索给定的文本,在这种情况下是 moby dick,以查找关键字。然而,我们试图通过跳跃距离(而不是 cat,寻找 c---a---t)找到它,而不是线性的单词。
我尝试了多种方法,但似乎无法让它真正完成一个跳跃距离,让它不起作用,并调用下一个允许的距离(递增 1 直到达到预设限制)
以下是完成此搜索的当前方法,也许这只是我缺少的一些愚蠢的东西?
private int[] search()
throws IOException
{
/*
tlength is the text file length,
plength is the length of the
pattern word (cat in the original post),
text[] is a character array of the text file.
*/
int i=0, j;
int match[] = new int[2];
int skipDist = 2;
while(skipDist <= 100)
{
while(i<=tlength-(plength * skipDist))
{
j=plength-1;
while(j>=0 && pattern[j]==text[i+(j * skipDist)])j--;
if (j<0)
{
match[0] = skipDist;
match[1] = i;
return match;
}
else
{
i++;
}
}
skipDist = skipDist + 1;
}
System.out.println("There was no match!");
System.exit(0);
return match;
}