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

Re^2: Divide a list of string into substrings (updated)

by Anonymous Monk
on Jun 21, 2020 at 09:18 UTC ( [id://11118304]=note: print w/replies, xml ) Need Help??


in reply to Re: Divide a list of string into substrings (updated)
in thread Divide a list of string into substrings

Thanks for the nice script !

The example should just illustrate the problem. The strings in the list can be completely different and your solution will only work with this specific example.

There even might exists a better solution (with a lower score ($len)) for this example as the given one. I am looking for an idea how to come to a good and fast solution, accepting that this solution is not the best one, which can be found. (See also the answer of Bill)

Replies are listed 'Best First'.
Re^3: Divide a list of string into substrings (updated)
by tybalt89 (Monsignor) on Jun 21, 2020 at 16:16 UTC

    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; # https://perlmonks.org/?node_id=11118281 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 ); } } }

    Outputs:

    (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.

      Many thanks for this nice solution.

      Especially I like the way you search for equal substrings: /([ -~]{3,})(?:.*?\1){$n}(?{ $d{$1}++})(*FAIL)/s;

      I will use a lot of your ideas in my solution ...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-03-28 21:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found