Running on an ancient PII 266 this takes less than a minute to process the standard unix dictionary. It uses a fairly efficient algorithm. Basically we bulid a hash which has the sorted letters of the words as its keys and the corresponding words (as an array ref) as its values. Thus we have a hash like:
...
'aet' => [ ate eat ]
...
As we have represented all the words as their sorted letters in a hash we make the search quick and easy. We work from the longest keys to the shortest and use recursion to test all the possibilities. We keep a list of 'winners' - any word which is in a winning list need not be tested again when we descend to it's level.
#!usr/bin/perl -w
use strict;
# get words into an array and a hash keyed on sorted letters
open DICT, "c:/windows/desktop/unixdict.txt" or die "Oops $!\n";
my (%words,%winners);
while(<DICT>) {
chomp;
my $sort = join '', (sort split'',$_);
push @{$words{$sort}}, $_;
}
# test the words by length - longest first, don't repeat known winners
for my $word (sort {length $b <=> length $a} keys %words) {
defined $winners{$word} ? next : test($word);
}
sub test {
my $word = $_[-1];
if (length $word <=3) { winner(@_); return; }
my @letters = sort split '', $word;
for my $i (0..$#letters) {
my @temp = @letters;
$temp[$i] = '';
my $short = join '', @temp;
test(@_,$short) if exists $words{$short};
}
}
sub winner {
local $, = " ";
for (@_) {
$winners{$_}++;
print @{$words{$_}}, "\n";
}
print "\n";
}
__DATA__
planetarium
manipulate
aluminate
laminate
matinal
animal manila
milan
main
min
[blah]
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
-
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.