ワイルドカード (単語内の任意の文字に一致) を処理する Boyer Moore Horspool アルゴリズムの一般化を実装したいと考えています。これは、パターンh _ _ s e
がこのテキストで 2 回検出されることを意味します: horsehouse
.
これを実装するには助けが必要です。アルゴリズムを十分に理解して、自分でこれを理解することはできません。いくつかのヒントはありますか?
int [] createBadCharacterTable(char [] needle) {
int [] badShift = new int [256];
for(int i = 0; i < 256; i++) {
badShift[i] = needle.length;
}
int last = needle.length - 1;
for(int i = 0; i < last; i++) {
badShift[(int) needle[i]] = last - i;
}
return badShift;
}
int boyerMooreHorsepool(String word, String text) {
char [] needle = word.toCharArray();
char [] haystack = text.toCharArray();
if(needle.length > haystack.length) {
return -1;
}
int [] badShift = createBadCharacterTable(needle);
int offset = 0;
int scan = 0;
int last = needle.length - 1;
int maxoffset = haystack.length - needle.length;
while(offset <= maxoffset) {
for(scan = last; (needle[scan] == haystack[scan+offset] || needle[scan] == (int) '_'); scan--) {
if(scan == 0) { //Match found
return offset;
}
}
offset += badShift[(int) haystack[offset + last]];
}
return -1;
}