fork download
  1. from collections import deque
  2.  
  3. def ent(a):
  4. if ord(a) == ord('(') or ord(a) == ord(')'):
  5. return 0
  6. elif ord(a) >= ord('0') and ord(a) <= ord('9'):
  7. return 1
  8. else:
  9. return 2
  10.  
  11.  
  12. def check(s):
  13. n = len(s)
  14. now = 0
  15. st = deque([])
  16. que = deque([])
  17.  
  18. for i in range(n):
  19. if ent(s[i]) == 1:
  20. if now == 0:
  21. que.append('1')
  22. now = 1
  23. elif ent(s[i]) == 2:
  24. que.append('+')
  25. now = 0
  26.  
  27. if len(que) == 0:
  28. return 0
  29.  
  30. now = 0
  31.  
  32. while len(que):
  33. tp = que.popleft()
  34.  
  35. if now == 0:
  36. if tp == '+':
  37. return 0
  38. else:
  39. if tp == '1':
  40. return 0
  41.  
  42. now = 1 - now
  43.  
  44. for i in range(n):
  45. if s[i] == '(':
  46. st.append('(')
  47. elif s[i] == ')':
  48. if len(st) == 0:
  49. return 0
  50.  
  51. st.pop()
  52.  
  53. if len(st):
  54. return 0
  55.  
  56. for i in range(n):
  57. if ent(s[i]) == 1:
  58. if i < n - 1 and s[i + 1] == '(':
  59. return 0
  60.  
  61. if i > 0 and s[i - 1] == ')':
  62. return 0
  63. elif ent(s[i]) == 2:
  64. if i < n - 1 and s[i + 1] == ')':
  65. return 0
  66.  
  67. if i > 0 and s[i - 1] == '(':
  68. return 0
  69.  
  70. return 1
  71.  
  72.  
  73. def back(s):
  74. n = len(s)
  75. cnt = 0
  76. news, ln = [], []
  77. st = deque([])
  78.  
  79. for i in range(n):
  80. if ent(s[i]) == 1:
  81. cnt += 1
  82. news.append(s[i])
  83. else:
  84. if cnt:
  85. ln.append(cnt)
  86. cnt = 0
  87.  
  88. if s[i] == '(':
  89. st.append('(')
  90. elif s[i] == ')':
  91. while len(st) and st[len(st) - 1] != '(':
  92. news.append(st.pop())
  93.  
  94. st.pop()
  95. elif s[i] == '*' or s[i] == '/':
  96. while len(st) and (st[len(st) - 1] == '*' or st[len(st) - 1] == '/'):
  97. news.append(st.pop())
  98.  
  99. st.append(s[i])
  100. else:
  101. while len(st) and ent(st[len(st) - 1]):
  102. news.append(st.pop())
  103.  
  104. st.append(s[i])
  105.  
  106. if cnt:
  107. ln.append(cnt)
  108.  
  109. while len(st):
  110. news.append(st.pop())
  111.  
  112. return "".join(news), ln
  113.  
  114.  
  115. def cal(s, ln):
  116. idx, lnidx = 0, -1
  117. st, tmp = deque([]), deque([])
  118.  
  119. while idx < len(s):
  120. if ent(s[idx]) == 1:
  121. lnidx += 1
  122. now, ten = 0, 1
  123.  
  124. for i in range(ln[lnidx]):
  125. tmp.append(s[idx])
  126. idx += 1
  127.  
  128. while len(tmp):
  129. now += ten * (ord(tmp.pop()) - ord('0'))
  130. ten *= 10
  131.  
  132. st.append(now)
  133. else:
  134. tp2 = st.pop()
  135. tp = st.pop()
  136.  
  137. if s[idx] == '+':
  138. tp += tp2
  139. elif s[idx] == '-':
  140. tp -= tp2
  141. elif s[idx] == '*':
  142. tp *= tp2
  143. else:
  144. tp //= tp2
  145.  
  146. st.append(tp)
  147. idx += 1
  148.  
  149. return st.pop()
  150.  
  151.  
  152. s = input()
  153.  
  154. if check(s) == 0:
  155. print("ROCK")
  156. else:
  157. news, ln = back(s)
  158.  
  159. print(cal(news, ln))
  160.  
Success #stdin #stdout 0.03s 9784KB
stdin
()(1)
stdout
1