Two Way算法是子字符串搜索算法(primary paper, 1.4 MB PDF)。
它将搜索模式 x 分成两部分: x = x l x r ,首先它尝试将 x r 与文本匹配,如果成功,则算法规定匹配 x l in反向(即从右到左的顺序)。
问题的原因很简单:订单未指定的比较已经可用并且可能更高效,请考虑优化的memcmp
或展开的循环。
答案 0 :(得分:1)
From an efficiency point of view it obviously doesn't matter. The only other reason I can think of is: in case of non-matching, a right-to-left attempt potentially leaves the algorithm with more information about a partial match. So going RTL, if we match 2 characters in xl and then fail, we know we have a partial, contiguous match of 2 chars + xr. If we match xl LTR and fail, we know nothing more than the xr match.