Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://994966]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (18)
As of 2015-07-29 14:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (263 votes), past polls