Advertisement
Guest User

Untitled

a guest
Nov 9th, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. Don't you hate it when there's something wrong with brackets in your code? Don't know if our proposed solution is good in terms of user experience, but we won't know untill we try!
  2.  
  3. I'll give you a string that consists only of 2 type of brackets: square ('[', ']') and round ('(', ')'). We want this string to be turned into a balanced string by zero or more operations. We have only one kind of operations: replace a bracket by another bracket, and it has a cost of 1.
  4.  
  5. A balanced string is either:
  6.  
  7. an empty string.
  8.  
  9. the concatenation of 2 balanced strings.
  10.  
  11. the concatenation of the string "[", a balanced string, and the string "]".
  12.  
  13. the concatenation of the string "(", a balanced string, and the string ")".
  14.  
  15. Yes, the definition above is recursive. If you prefer formal notation, that's its BNF:
  16.  
  17. balancedString = emptyString | balancedString balancedString | [balancedString] | (balancedSstring)
  18.  
  19. The '|' means OR.
  20.  
  21. For example, "()[()](())([[]()()[[]]()])", "()", "()[]", "(())", "[][]" are all balanced strings. While ")", "(", "[(])", "[([]])" are not.
  22.  
  23. What is the minimum cost needed to balance the given string?
  24.  
  25. Input
  26.  
  27. The input file consists of t (1 ≤ t ≤ 128) test cases.
  28.  
  29. Each test case consists of a single line - the given string s (1 ≤ s ≤ 50).
  30.  
  31. si in {'(' , ')', '[' , ']'}.
  32.  
  33. Output
  34.  
  35. The minimum cost needed to balance the given string or -1 if it's impossible.
  36.  
  37. STDIN
  38.  
  39. This is the content of the STDIN.
  40.  
  41. 3
  42. [(])
  43. (
  44. ([])
  45.  
  46. STDOUT
  47.  
  48. Your solution should produce a similar result.
  49.  
  50. 2
  51. -1
  52. 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement