fork download
  1. sub solve {
  2. push @{$anagram{join'', sort split//, $_}}, $_
  3. foreach @{+shift};
  4.  
  5. foreach (split//, $_[0]) {
  6. push @w, $_ and $w = join'', sort @w;
  7. push @r, pop @{$anagram{$w}} => join'', @w and @w = ()
  8. if (@{ $anagram{$w} } > 0);
  9. }
  10. return @r;
  11. }
  12.  
  13. (($f = !$f) ? $r1 : $r0) .= " ".$_
  14. foreach solve [split /\s+/, <>], <>;
  15. print $r0 . "\n" . $r1;
Success #stdin #stdout 0s 3564KB
stdin
abc abcd abcjk
abcjkdabcabc
stdout
 abc
 abc