fork download
  1. #!/usr/bin/env perl
  2. my %twin = (
  3. ')' => '(',
  4. '}' => '{',
  5. ']' => '[',
  6. );
  7.  
  8. COLLECT:
  9. while (<>) {
  10. my ($max, @stack) = 0;
  11. for my $char ( split // ) {
  12. if ( $char =~ / [[({] /x ) {
  13. push @stack, $char;
  14. ( $max < 1+ $#stack ) && ( $max = 1+ $#stack );
  15. }
  16. elsif ( $char =~ / [])}] /x ) {
  17. unless ( pop @stack eq $twin{$char} ) {
  18. print "-1\n";
  19. next COLLECT;
  20. }
  21. }
  22. }
  23. print "$max\n";
  24. }
Success #stdin #stdout 0s 4952KB
stdin
()
(())
]{}
{()}[]
[{()}]
(]
stdout
1
2
-1
2
3
-1