Python字符串“in”运算符实现算法和时间复杂性

2024-04-25 07:08:08 发布

您现在位置:Python中文网/ 问答频道 /正文

例如,我正在考虑in运算符如何实现

>>> s1 = 'abcdef'
>>> s2 = 'bcd'
>>> s2 in s1
True

在CPython中,哪种算法用于实现字符串匹配,时间复杂度是多少?有关于这个的官方文件或维基吗?


Tags: 文件字符串in算法true官方时间运算符
1条回答
网友
1楼 · 发布于 2024-04-25 07:08:08

它是Boyer-MooreHorspool的组合。

您可以查看C代码here

Fast search/count implementation, based on a mix between Boyer-Moore and Horspool, with a few more bells and whistles on the top. For some more background, see: http://effbot.org/zone/stringlib.htm.

从上面的链接:

When designing the new algorithm, I used the following constraints:

  • should be faster than the current brute-force algorithm for all test cases (based on real-life code), including Jim Hugunin’s worst-case test
  • small setup overhead; no dynamic allocation in the fast path (O(m) for speed, O(1) for storage)
  • sublinear search behaviour in good cases (O(n/m))
  • no worse than the current algorithm in worst case (O(nm))
  • should work well for both 8-bit strings and 16-bit or 32-bit Unicode strings (no O(σ) dependencies)
  • many real-life searches should be good, very few should be worst case reasonably simple implementation

相关问题 更多 >

    热门问题