正确实现Boyer Moore算法

编程入门 行业动态 更新时间:2024-10-13 08:25:44
本文介绍了正确实现Boyer Moore算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我尝试使用多种实现,但是所有这些实现都有错误。 在SO上搜索给我 www-igm.univ-mlv.fr/~lecroq/string/node14.html - 看起来不错,但是这个实现给我错误的结果 - 有时它没有找到一个字符串。 我花了几个小时来找到错误。

I tried to use several implementations, but all of them had bugs. Search at SO gave me www-igm.univ-mlv.fr/~lecroq/string/node14.html - looks nice, but this implementation gave me wrong results - sometimes it doesn't found a string. I spent couple hours to find the bug.

下面一行看起来很好:

j += MAX(bmGs[i], bmBc[y[i + j]] - m + 1 + i);

但 y 强>!这意味着y [i + j]可能是负的(在我的一个测试中发生了什么)。

but y is char * and char is signed! It means that y[i + j] could be negative (what happens in one of my tests).

我的问题是:在哪里可以找到Boyer Moore算法的正确实现?

My question is: Where to find correct implementation of Boyer Moore algorithm?

推荐答案

char 没有明确签名或未签名 - 未指定,由实现定义。

char isn't definitively signed or unsigned - it's unspecified, and left up to the implementation to define.

如果算法依赖于char是无符号的,那么它应该显式地将输入指针转换为 unsigned char 库字符串处理函数被定义为工作 - 所有的比较都是通过处理字符串中的字符为 unsigned char )。

If the algorithm depends on char being unsigned, then it should explicitly cast the input pointers to unsigned char (which is how the C standard library string handling functions are defined to work - all comparisons are done treating the characters in the string as unsigned char).

更多推荐

正确实现Boyer Moore算法

本文发布于:2023-11-30 09:06:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649461.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:算法   正确   Boyer   Moore

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!