Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use a <a href="http://en.wikipedia.org/wiki/Rolling_hash" rel="noreferrer"><strong><em>rolling hash</em></strong></a> to achieve linear time complexity and O(1) space complexity (I think this is the case, since I don't believe you can have an infinite repeating sequence with two frequencies which are not multiples of each other).</p> <p>Algorithm: You just keep two rolling hashes which expand like this:</p> <pre><code> _______ _______ _______ / \/ \/ \ ...2038975623895769874883301010883301010883301010 . . . || . . . [][] . . . [ ][ ] . . .[ ][ ] . . [. ][ ] . . [ . ][ ] . . [ .][ ] . . [ ][ ] . [ ][ ] </code></pre> <p>Keep on doing this for the entire sequence. The first pass will only detect repetitions repeated 2*n times for some value of n. However that's not our goal: our goal in the first pass is to detect all possible periods, which this does. As we go along the sequence performing this process, we also keep track of all relatively prime periods we will need to later check:</p> <pre><code>periods = Set(int) periodsToFurthestReach = Map(int -&gt; int) for hash1,hash2 in expandedPairOfRollingHashes(sequence): L = hash.length if hash1==hash2: if L is not a multiple of any period: periods.add(L) periodsToFurthestReach[L] = 2*L else L is a multiple of some periods: for all periods P for which L is a multiple: periodsToFurthestReach[P] = 2*L </code></pre> <p>After this process, we have a list of all periods and how far they've reached. Our answer is probably the one with the furthest reach, but we check all other periods for repetition (fast because we know the periods we're checking for). If this is computationally difficult, we can optimize by pruning away periods (which stop repeating) as we're going through the list, very much like the sieve of Eratosthenes, by keeping a priority queue of when we next expect a period to repeat.</p> <p>At the end, we double-check the result to make sure there was no hash collision (in unlikely even there is, blacklist and repeat).</p> <p>Here I assumed your goal was to minimize non-repeating-length, and not give a repeating element which can be further factored; you can modify this algorithm to find all other compressions, if they exist.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload