Algorithm to find the smallest snippet from searching a document?

前端 未结 7 1429
太阳男子
太阳男子 2021-01-30 07:57

I\'ve been going through Skiena\'s excellent \"The Algorithm Design Manual\" and got hung up on one of the exercises.

The question is: \"Given a search string of three w

相关标签:
7条回答
  • 2021-01-30 08:21

    Unless I've overlooked something, here's a simple, O(n) algorithm:

    1. We'll represent the snippet by (x, y) where x and y are where the snippet begins and ends respectively.
    2. A snippet is feasible if it contains all 3 search words.
    3. We will start with the infeasible snippet (0,0).
    4. Repeat the following until y reaches end-of-string:
      1. If the current snippet (x, y) is feasible, proceed to the snippet (x+1, y)
        Else (the current snippet is infeasible) proceed to the snippet (x, y+1)
    5. Choose the shortest snippet among all feasible snippets we went through.

    Running time - in each iteration either x or y is increased by 1, clearly x can't exceed y and y can't exceed string length so total number of iterations is O(n). Also, feasibility can be checked at O(1) in this case since we can track how many occurences of each word are within the current snippet. We can maintain this count at O(1) with each increase of x or y by 1.

    Correctness - For each x, we calculate the minimal feasible snippet (x, ?). Thus we must go over the minimal snippet. Also, if y is the smallest y such that (x, y) is feasible then if (x+1, y') is a feasible snippet y' >= y (This bit is why this algorithm is linear and the others aren't).

    0 讨论(0)
提交回复
热议问题