Quantcast
Channel: Active questions tagged ocaml - Stack Overflow
Viewing all articles
Browse latest Browse all 527

find longest repeating sequence in list

$
0
0

If I have a List like this:

[i;i;i;a;b;b;a;i;i;c] (*the longest repeating sequence would be [i;i]*)[i;i;i;i] (*here the max_pattern would be [i;i] (has to repeat, no overlapping*)[t;f;f;t] (*here it would be [t] as t is, in this case, the first element that has a repeating pattern in the list*)

my idea:

  • take the first element from the list and divide the list- where list_one has all the elements to the left of the first element. and list_two all the elements to the right.

  • then check if the element has a match in either one of the two lists.

  • if it does set the current max to the element.

  • now concatenate the next element to the right of the current element in the original list to the current element and again look in list_one and list_two if there is a match.

  • after the length of the concatenation reaches a point where it is >(size_of list / 2) stop.

  • now go to the first step but with the next element and repeat until every element in the list is checked.

example:

[t;f;f;t](*first round*)[t][][f;f;t](*match in last elem*)current_max = [t](*second round*)[t;f][][f;t](*from here on no more matches*)(*go to the next element, split lists again, and proceed with mentioned steps*)[f][t][f;t](*match in f*) (*repeat from here on...*) 

I don't know if this algorithm has flaws.I am trying to implement this in OCaml but I think there might bean easier way to do this.


Viewing all articles
Browse latest Browse all 527

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>