Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: How can I get the longest and all other common substrings from a set of strings?

by thundergnat (Deacon)
on Sep 21, 2012 at 18:28 UTC ( #994966=note: print w/replies, xml ) Need Help??


in reply to How can I get the longest and all other common substrings from a set of strings?

Here you go.

@s = qw(ABABC BABCA ABCBA); sub l{length@_[0]};for$i(0..$#s){$l=l$s[$i];for$s(0..$l-1){for$o(1..$l +-$s) {$t=substr$s[$i],$s,$o;if(!$i||exists$c{$t}){$c{$t}|=1<<$i}}}};print j +oin$/, 'Common subsequences:',@s,'-'x30,sort{l($b)<=>l$a}grep{$c{$_}==(1<<@s) +-1}keys%c;
Common subsequences:
ABABC
BABCA
ABCBA
------------------------------
ABC
BC
BA
AB
C
A
B

  • Comment on Re: How can I get the longest and all other common substrings from a set of strings?
  • Download Code