in reply to Re^4: longest common substring (with needed tweaks)
in thread longest common substring (with needed tweaks)

sub f { @a = split //, shift; $ih = shift; for $i (0 .. $#a){ $e = $a[$i]; ${$ih}{$e} = 1; for $y ($i+1 .. $#a){ $e .= $a[$y]; ${$ih}{$e} = 1; } } } ($l, $m) = split /\s/, <DATA>; $_ = <DATA>; chomp; %h = (); f($_, \%h); while(<DATA>){ chomp; %th = (); f($_, \%th); $h{$_}++ foreach (keys %th); } foreach $key (keys %h){ if($h{$key} == $m){ $r[length($key)] = [] if ! exists $r[length($key)]; push $r[length($key)], $key; } } print "@{$r[$#r]}\n"; __DATA__ 3 2 ac bc b
1: creates sub named f, which takes two parameters: string and a reference to a hash
that sub puts all combinations of substrings out of the given string into the hash
2: splits the first string from the file into two variables $l $m
3: takes the next line from the file and sends it into sub f with the reference to an empty hash %h
4: at this point the first line from the file is split on all its substrings which are put into
hash %h and have a value 1
5: then we read the rest of the file line by line and send these lines to the sub f along the reference
to an empty hash %th, right after that the two hashes are compared and the %h hash is incremented on the doubled values
6: runs through the %h hash and if the value of the key is amount of overlaps we need, then put it into an @r array of arrays
7: the last line prints all the longest overlaps with the same length