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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.