This returns all maximal ordered subsets:

sub MaxOrderedSubList { my @order= @{ shift @_ }; my %order; @order{@order}= 0..$#order; my @list= @{ shift @_ }; my @wins= ( [] ); my @idx= ( -1 ); my $i= 0; while( 1 ) { do { ++$idx[$i]; if( $#list < $idx[$i] ) { if( --$i < 0 ) { return @wins; } redo; } } while( 0 < $i && $order{$list[$idx[$i]]} <= $order{$list[$idx[$i-1]]} ); if( $#{$wins[0]} <= $i ) { @wins= () if $#{$wins[0]} < $i; push @wins, [ @list[@idx[0..$i]] ]; } $idx[$i+1]= $idx[$i]; ++$i; } } for my $win ( MaxOrderedSubList( [1,2,3,8,4,5,6], [2,1,3,5,4] ) ) { print "@$win\n"; } for my $win ( MaxOrderedSubList( [qw( t h i s w o r k a )], [qw( w h a t i s o k )] ) ) { print "@$win\n"; }
Output:
2 3 5 2 3 4 1 3 5 1 3 4 h i s o k t i s o k
The maximum size for ordered subsets would be 0 + @{( MaxOrderedSubList( [...], [...] ) )[0]} (updated)

        - tye

In reply to (tye)Re2: Items in Order by tye
in thread Items in Order by artist

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



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.