与Boyer-Moore-Horspool一起向后搜索
Searching backwards with Boyer-Moore-Horspool
我已经实现了Boyer-Moore-Horspool搜索算法,当我想在我的文本中向前搜索时,它工作得很好。我正在尝试使其适应反向工作,但似乎无法正确处理。
有没有使用 BMH 向后搜索的示例?
作为参考,这是我的代码:
POSITION_T cDocument::FindNext(std::string needle, POSITION_T start)
{
size_t buffersize = mBuffer.GetByteSize() ;
size_t needlesize = needle.length() ;
vector<int> badchars(256, -1) ;
BadCharHueristic(needle, badchars) ;
size_t s = mBuffer.ConvertPositionToByte(start) ;
while(s <= (buffersize - needlesize))
{
ssize_t j = needlesize - 1 ;
while(j >= 0 && needle[j] == mBuffer.GetByte(s + j))
{
j-- ;
}
if(j < 0)
{
// s holds our position in bytes
return mBuffer.ConvertByteToPosition(s) ;
}
else
{
ssize_t b = j - badchars[mBuffer.GetByte(s + j)] ;
s += Max(1, b) ;
}
}
return GetTextSize() ;
}
//Java Solution
public static int findLastBoore(char[] text, char[] pattern) {
int n = text.length;
int m = pattern.length;
if (m == 0) return 0;
Map<Character, Integer> last = new HashMap<Character, Integer>();
for (int i = 0; i < n; i++) {
last.put(text[i], -1);
}
for (int k = 0; k < m; k++){
last.put(pattern[k], k);
}
int i = n - m;
int k = 0;
while (i > 0) {
if (text[i] == pattern[k]) {
if (k == m - 1) return i;
i++;
k++;
} else {
// Step backwards when there is a mismatch
i-= m - Math.max(k + 1, last.get(text[i]));
k = 0;
}
}
return -1;
}
'
相关文章:
- 有根的二进制搜索树.保留与其父级的链接
- 如何将enable-if与模板参数和参数包一起使用
- 如何将PERF_AMPLE_READ与mmap一起使用
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 在C++中搜索嵌套多映射值
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 如何将C++中的库和头与MinGW一起使用
- 二进制搜索树叶数问题
- 为什么二进制搜索在我的测试中不起作用
- 将--whole archive链接器选项与CMake和具有其他库依赖项的库一起使用
- 为什么我不能将 rand() 与数组的大小一起使用?
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 正在尝试重载二进制搜索树分配运算符
- 不能将复制初始化与隐式转换的多个步骤一起使用
- c++binary_search函数排序数组(流行名称搜索)出现问题
- 将fold表达式与std::一起用于两个元组
- spdlog标头仅与外部fmt一起使用.spdlog错误:'内部':不是'fmt'
- 向量上的线性搜索
- 与Boyer-Moore-Horspool一起向后搜索
- 将二叉搜索与向量一起使用