Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I suspect there is no "good and fast" solution. This problem, to me, has the flavor of a permutation or "traveling salesman" problem, and is probably NP-hard (or one of the other NP classes).

So here's an attempt (with a cheat) that at least gets a solution in a sort of reasonable time for this problem.

#!/usr/bin/perl use strict; # use warnings; my @list=("set abcde-efghi 12345", "set abcde-ijkl 12345", "clr abcde-efghi+123", "clr abcde-ijkl 12345"); #my @expected_substrings=("set","clr"," abcde-","efghi", # "ijkl"," 12345","+123"); #my $len=@expected_substrings*2; #$len+=length($_) foreach @expected_substrings; $_ = join "\n", @list; my $max = 3; ########################################### BIG CHEAT FOR + RUNTIME sub score { 2 * @_ + length join '', @_; } my $best = score( @list ); try( $_ ); print "\n"; sub try { (local $_, my @sofar) = @_; if( !/[ -~]/ ) { my $score = score @sofar; if( $score < $best ) { print "\n"; use Data::Dump 'dd'; dd $score, @sofar; $best = $score; } return; } score(@sofar) >= $best and return; for my $n ( reverse 0 .. $#list ) { my %d; /([ -~]{3,})(?:.*?\1){$n}(?{ $d{$1}++ })(*FAIL)/s; my @d = sort { length $b <=> length $a } sort keys %d; @d > $max and $#d = $max - 1; for my $string ( @d ) { try( s/\Q$string\E/\t/gr, @sofar, $string ); } } }


(46, " abcde-", " 12345", "efghi", "ijkl", "clr", "set", "+123")

where the "46" is the score and the rest are the substrings. As it finds better scores, it will print them, but so far always seems to find a best solution first.

In reply to Re^3: Divide a list of string into substrings (updated) by tybalt89
in thread Divide a list of string into substrings by Anonymous Monk

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, 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, 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.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (7)
    As of 2021-03-04 22:58 GMT
    Find Nodes?
      Voting Booth?
      My favorite kind of desktop background is:

      Results (108 votes). Check out past polls.