My entry into the pot works by starting with the smallest words and building to the largest, instead of the other way. On the word list from ITA, the answers are 16 letters long (Underestimations and indeterminations).
#!perl
use strict;
use Data::Dumper;
$|++;
my %WordTree = ();
my %WordLengths = ();
my $LongWordLength = 1;
open (DATA, "Word.txt");
while (<DATA>) {
chomp;
my $Word = $_;
my $Length = length($_);
next if $Length < 3;
$LongWordLength = $Length if ($LongWordLength < $Length);
push (@{$WordLengths{$Length}}, $Word);
}
print "Finished reading Data\n";
close (DATA);
for (my $CurrentLength = 3; $CurrentLength <= $LongWordLength; $Curren
+tLength++) {
print "Working on words of length $CurrentLength\n";
my ($Word) = "";
foreach $Word (@{$WordLengths{$CurrentLength}}) {
my (@SortedLetters) = sort split //, $Word;
if ($CurrentLength > 3) {
my $TempWord = "";
my @SplicedLetters = "";
my $MatchFlag = 0;
for (my $Splice = 0; $Splice <= $#SortedLetters; $Splice++
+) {
@SplicedLetters = @SortedLetters;
splice(@SplicedLetters, $Splice, 1);
$TempWord = join '', @SplicedLetters;
if (exists($WordTree{$#SortedLetters}->{$TempWord})) {
if (! exists($WordTree{$CurrentLength}->{join '',
+@SortedLetters})) {
push (@{$WordTree{$CurrentLength}->{join '', @
+SortedLetters}}, $Word);
push (@{$WordTree{$CurrentLength}->{join '', @
+SortedLetters}},
@{$WordTree{$CurrentLength - 1}->{join '
+', @SplicedLetters}});
}
last;
}
}
} else {
if (! exists($WordTree{3}->{join '', @SortedLetters})) {
push (@{$WordTree{3}->{join '', @SortedLetters}}, $Wor
+d);
}
}
}
}
my (@LongestMatch) = sort {$a<=>$b} keys %WordTree;
my $NotFoundLongest = 1;
while ($NotFoundLongest && $#LongestMatch > 1) {
if (keys %{$WordTree{$LongestMatch[$#LongestMatch]}} > 0) {
print "The longest length is $LongestMatch[$#LongestMatch]\n";
print Dumper $WordTree{pop @LongestMatch};
$NotFoundLongest = 0;
}
pop @LongestMatch;
}
-
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.