fork(3) download
  1. #!/usr/bin/perl
  2. use 5.016;
  3. use warnings;
  4. use utf8;
  5. binmode STDOUT => ':encoding(utf8)';
  6.  
  7. sub f {
  8. my @lists = map{ [split //] } @_;
  9.  
  10. my @queue = ([ [], [ (0) x @lists ] ]);
  11. while(@queue){
  12. my ($pattern, $pos) = @{ shift @queue };
  13.  
  14. if (not grep{ @{$lists[$_]} > $pos->[$_] } (0 .. $#lists)){
  15. foreach(@lists){
  16. my @p = @{$_};
  17. foreach(@{$pattern}){
  18. @p or last;
  19. print($p[0] eq $_ ? shift(@p) : '□');
  20. }
  21. print "\n";
  22. }
  23. }
  24.  
  25. my %memo;
  26. foreach my $n (0 .. $#lists){
  27. my $x = $lists[$n]->[$pos->[$n]];
  28. defined $x or next;
  29. if (not exists $memo{$x}){
  30. $memo{$x} = [ [ @{$pattern}, $x ], [ @{$pos} ] ];
  31. push @queue, $memo{$x};
  32. }
  33. $memo{$x}->[1]->[$n]++;
  34. }
  35. }
  36. }
  37.  
  38. f(qw(123 134));
  39. print "\n";
  40. f(qw(1212 1322 1122));
  41. print "\n";
  42.  
Success #stdin #stdout 0.03s 5000KB
stdin
Standard input is empty
stdout
123
1□34

121□2
1□□322
1□1□22