# 
# My solution didn't accept duplicate substrings inside a word that are in exactly
# one word. Corrected.
# I remembered that there is a exists() function so I use it.
# 
#!/usr/bin/perl

while (<>) {

	chomp $_;
	
        @subs = extract_subs($_);	

	foreach $s (@subs) {

		if ( (exists $good{$s}) and ($_ ne $good{$s})) {

			delete $good{$s};
		} else {

			$good{$s} = $_;
		}
	}
}


open(QUESTIONS, ">questions") || die "can't open the file questions for writing:
$!"; open(ANSWERS, ">answers")     || die "can't open the file answers for
writing: $!";

foreach $l (sort keys %good) {

	print QUESTIONS "$l\n";
	print ANSWERS "$good{$l}\n";
}

close(QUESTIONS) || die "can't close the file questions: $!";
close(ANSWERS)   || die "can't close the file answers: $!";


sub extract_subs () {
	
	my $string = $_[0];

	my @subs;
	
	$steps = length($string) - 3;

	for (1 .. $steps) {

		if ($string =~ /^(\w{4})/) {
			$substring = $1;
		}
		
		push @subs, $substring;
		substr($string, 0, 1) = "";
		
 	}	
	
	return @subs;
}
