Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Here's a simplistic approach using regexps:

my @a = ( [2,5,10,5,12,6,21,5,10,12,23], [5,6,11,10,5,10,6,21,5,1,9], [6,5,10,15,21] ); my($m, $n) = (2, 2); my $joined = join ';', map join(',', @$_), @a; while ($joined =~ / \b (?= ( @{[ join ',', ('\d+') x $n ]} ) \b @{[ '(?> .*? \b \1 \b )' x ($m - 1) ]} ) /xg) { print "($1)\n" unless $seen{$1}++; }

I suspect this isn't as fast as you'd need: on my machine, it took about 30 seconds to search an array of 100 x 100 random numbers for $m = 3, $n = 4, and runtime will be O(n^2) the total number of integers in your list of sequences.

Also this finds only common subsequences of the exact length specified, so in this case it returns (5,10), (10,5), (6,21), (21,5). I can't see a way to adapt this to return directly only the longest common sequences, but you could maybe save all the length-2 sequences, then search for length-3 sequences and discard their subsequences, iteratively until you've found the longest subsequence.

Hugo


In reply to Re: Seeking algorithm for finding common continous sub-patterns by hv
in thread Seeking algorithm for finding common continous sub-patterns by johnnywang

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-19 14:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found