How to find minimal-length subsequence that contains all element of a sequence

后端 未结 7 1595
无人共我
无人共我 2021-02-01 07:29

Given a sequence such as S = {1,8,2,1,4,1,2,9,1,8,4}, I need to find the minimal-length subsequence that contains all element of S (no duplicates, order does n

7条回答
  •  没有蜡笔的小新
    2021-02-01 08:12

    If you need to do this quite often for the same sequence and different sets you can use inverted lists for this. You prepare the inverted lists for the sequence and then collect all the offsets. Then scan the results from the inverted lists for a sequence of m sequential numbers.

    With n the length of the sequence and m the size of the query the preparation would be in O(n). The response time for the query would be in O(m^2) if I am not miscalculating the merge step.

    If you need more details have a look at the paper by Clausen/Kurth from 2004 on algebraic databases ("Content-Based Information Retrieval by Group Theoretical Methods"). This sketches out a general database framework that can be adapted to your task.

提交回复
热议问题