fork(9) 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 3608KB
stdin
stol stul dverj krovatj kompjuter
vatjkrotulsredvjlostkompterju
stdout
 vatjkro tuls redvj lost kompterju
 krovatj stul dverj stol kompjuter