# 
# Here is my solution.
# 
#!/usr/bin/perl -w
use strict;

my $QUESTIONSIZE = 4; # how long are the questions?
my $ANSWERCOUNT  = 1; # how many answers (words) can share a question?

my $dict = $ARGV[0] || "/usr/share/dict/words";
open IN, "$dict" or die "error: $! in $dict";

my %result;

while (my $word = lc <IN>){
	chomp $word;
	my @a = split / */, $word;
	local $" = '';
	while (@a >= $QUESTIONSIZE) {
	    my $q = "@a[0..$QUESTIONSIZE-1]";
		push @{$result{$q}->{answer}},$word;
		$result{$q}->{cnt}++;
		shift @a;
	}
}

open Q, ">q.txt" or die "can't open q.txt $!\n";
open A, ">a.txt" or die "can't open a.txt $!\n";
local $" = ", ";
foreach my $q (sort keys %result) {
	next if $result{$q}->{cnt} != $ANSWERCOUNT;
	print Q "$q\n";
	print A "@{$result{$q}->{answer}}\n";
}
close Q;
close A;

