strstr() 用于非空终止的字符串
如何对计数字符串(即非 null-)执行strstr()
的就地等效操作终止)在C?
How do I do the in-place equivalent of strstr()
for a counted string (i.e. not null-terminated) in C?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果你害怕 O(m*n) 行为 - 基本上,你不需要,这种情况不会自然发生 - 这是我已经修改过的 KMP 实现,我已经对其进行了修改以获取干草堆的长度。也是一个包装纸。如果您想重复搜索,请编写自己的搜索并重用
borders
数组。不能保证没有错误,但它似乎仍然有效。
If you're afraid of O(m*n) behaviour - basically, you needn't, such cases don't occur naturally - here's a KMP implementation I had lying around which I've modified to take the length of the haystack. Also a wrapper. If you want to do repeated searches, write your own and reuse the
borders
array.No guarantees for bug-freeness, but it seems to still work.
看看下面的功能是否适合您。我还没有彻底测试过,所以我建议你这样做。
See if the function below works for you. I haven't tested it thoroughly, so I would suggest you do so.
我刚刚遇到这个,我想分享我的实现。它认为它相当快,我没有任何子调用。
它返回在大海捞针中找到针的索引,如果没有找到则返回 -1。
I just came across this and I'd like to share my implementation. It think it quite fast a I don't have any subcalls.
It returns the index in the haystack where the needle is found or -1 if it was not found.
我用了这个方法
I used this method