Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
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 imbibing at the Monastery: (11)
As of 2014-08-29 19:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (287 votes), past polls