Can i check if subsequence faster then O(n*n)

↘锁芯ラ 提交于 2019-12-05 20:15:37

Here's a recursive characterization of David Eisenstat's algorithm. (Note that this algorithm is tail recursive and can therefore be written as a loop; I describe it as recursive because doing so is a nice way to understand the algorithm.)

Define a sequence as either empty, or an item followed by a sequence.

Take two sequences, A and B. The question is if B is or is not a subsequence of A.

If B is empty then B is a subsequence of A.

If B is not empty and A is empty then B is not a subsequence of A.

If we've made it this far, neither A nor B are empty. Suppose that A is item X followed by sequence C and B is item Y followed by sequence D.

If X is the same as Y then the answer to the question "is B a subsequence of A?" is the same as the answer to the smaller question "is D a subsequence of C?". Answer that question.

If X is not the same as Y then the answer to the question "is B a subsequence of A?" is the same as the answer to the smaller question "is B a subsequence of C?". Answer that question.

This procedure terminates and clearly its worst case is in the length of the sequence A.

Yes, it can be done faster than O(n^2) with an algorithm by Knuth, Morris and Pratt. However note that the implementation provided by the framework might already implement this algorithm.

Yes, the following greedy algorithm is correct and runs in time O(n). For each element of B in order, scan forward in A from the previous stopping point (initially the beginning of A) for the first occurrence.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!