Advertisement
Guest User

Untitled

a guest
Jun 27th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.97 KB | None | 0 0
  1. import io.Source.stdin
  2. import scala.collection.mutable.Stack
  3.  
  4. object cycle {
  5.   def main(args: Array[String]): Unit = {
  6.     val strings = stdin.getLines().toList
  7.     val correct = strings
  8.       .map { x => balance_stack(x.toList, new Stack[Char]) }
  9.       .map { if(_) '1' else '0' }
  10.       .toString()
  11.     print(Integer.parseInt(correct, 2))
  12.   }
  13.  
  14.   def balance_stack(chars: List[Char], stack: Stack[Char]): Boolean = {
  15.     if(chars.isEmpty) stack.isEmpty
  16.     else {
  17.       chars.head match {
  18.         case '(' | '[' | '{' => { stack.push(chars.head); balance_stack(chars.tail, stack) }
  19.         case _ => {
  20.           val top = stack.pop()
  21.           chars.head match {
  22.             case ')' => { if(top == '(') balance_stack(chars.tail, stack) else false }
  23.             case '}' => { if(top == '{') balance_stack(chars.tail, stack) else false }
  24.             case ']' => { if(top == '[') balance_stack(chars.tail, stack) else false }
  25.           }
  26.         }
  27.       }
  28.     }
  29.   }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement