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