in reply to Re^4: longest common substring (with needed tweaks)
in thread longest common substring (with needed tweaks)
1: creates sub named f, which takes two parameters: string and a reference to a hashsub 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
|
|---|