sub repeated_substring {
  my $size     = length $_[0];    # total size of the dataset
  my $maxsize  = int ($size / 2); # maximum possible length of the match
  my $f_index  = 0;               # index of the current best match
  my $f_offset = 0;               # offset of the current best match

OUTER:
  for (my $index=0; $index < $size; $index++) {

    # The big optimizer (explained below)
    if ($f_offset > 20) {
      for (my $t_index = $index + $f_offset - 15; $t_index >= $index; $t_index =
$t_index - 10) {
        if (index($_[0], substr($_[0], $t_index, $index+$f_offset-$t_index),
$index+$f_offset) == -1) {
          $index = $t_index;
          next OUTER;
        }
      }
    }

    # The brute force algorithm
    # Keep looking for a match that is one bigger than the last match and stop once
    # we don't find a match
    for (my $offset=$f_offset+1; $offset <= $maxsize && $index + $offset <=
$size; $offset++) {
      if (index($_[0], substr($_[0], $index, $offset), $index+$offset) >= 0) {
        $f_index  = $index;
        $f_offset = $offset;
      } else {
        last;
      }
    }
  }
  return substr($_[0], $f_index, $f_offset);
}
