in reply to Challenge: "Words" In A String
Very rough draft (pseudocode)
Hope I don't get it all wrong, it's quite late here in Europe. I'd definitely use dynamic programming to save the partial results.sub wordify($string) { my $results = []; $min_end = length($string) for $pos (0 .. $min_end) { for $len (1 .. length($string)) { $word = substr($string, $pos, $len) if exists($dictionary{$word}) { $min_end = $pos+$len if $pos+$len < $min_end $prefix = substr($string, 0, $pos) . " " . $word # nee +ds special treatment for $pos = 0 $endings = wordify(substr($string, $pos+1)) push(@$results, map("$prefix $_", @$endings)) } } } return [$string] unless @$results return $results }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Challenge: "Words" In A String
by pjotrik (Friar) on Sep 19, 2008 at 10:21 UTC | |
by Limbic~Region (Chancellor) on Sep 19, 2008 at 13:46 UTC |
In Section
Seekers of Perl Wisdom