Algorithm to find the smallest snippet from searching a document?

前端 未结 7 1432
太阳男子
太阳男子 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:20

    O(n)

    Pair find(int[][] indices) {
    pair.lBound = max int;
    pair.rBound = 0;
    index = 0;
    
    for i from 0 to indices.lenght{
        if(pair.lBound > indices[i][0]){
            pair.lBound = indices[i][0]
            index = i;
        }
        if(indices[index].lenght > 0)
            pair.rBound = max(pair.rBound, indices[i][0])
    }
    remove indices[index][0]
    
    return min(pair, find(indices)}
    

提交回复
热议问题