Check for duplicates of subsequences of length> = N in the sequence

I have a sequence of values, and I want to know if it contains repeating subsequences of a certain minimum length. For instance:

1, 2, 3, 4, 5, 100, 99, 101, 3, 4, 5, 100, 44, 99, 101

Contains a subsequence 3, 4, 5, 100twice. It also contains a subsequence 99, 101twice, but this subsequence is double to take care of it.

Is there an effective algorithm for verifying the existence of such a subsequence? I am not particularly interested in placing sequences (although this would be useful for checking), I am primarily interested in the True / False answer, given the sequence and the minimum length of the subsequence.

Until now, my only approach is to search for brute force: for each element in the sequence, find all the other places where the element occurs (already at point O (N ^ 2)), and then go one step at a time from each place and see whether the next element matches, and keep going until I find a mismatch or find an appropriate subsequence of sufficient length.

Another thought that I had, but could not develop into a real approach, was to build a tree of all sequences, so that each number will be a node and a child of its previous number, if the node is already in the tree.

+4
source share
3 answers

O(k) (k - ) N.

№ 1:
( ).
, >= N.

№ 2:
.
, , , >= N .

№ 3:
( , , , N).

O(k) , ( ).
, - O(k log k) ( map) O(k) hashmap.

P.S string sequence.

+4

N (, , ), : KMP .

, k .

N (O (k) ):

  • ( O (N))
  • ( O (k))

, , N < k, O (k ^ 2).

+1

, .

, , , . :

Key: Indices
  1: 0 
  2: 1 
  3: 2, 8
  ....

1 0, 2 1, 3 2 8 ..

. , .

0

Source: https://habr.com/ru/post/1547010/


All Articles