ThihaNyein

Catch Overflow!

Oct 22nd, 2020 (edited)
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.14 KB | None | 0 0
  1. """
  2. https://codeforces.com/problemset/problem/1175/B
  3. """
  4. import re
  5.  
  6. def st(s):
  7.   flag = True
  8.   while(flag):
  9.     if(len(s)>2 and s[-2].isdigit() and s[-1].isdigit()):
  10.       a,b = int(s[-2]), int(s[-1])
  11.       s = s[:-2]
  12.       s.append(str(a*10+b))
  13.     elif(len(s)>=3 and s[-3] == '(' and s[-2].isdigit() and s[-1]== ')'):
  14.       a = s[-2]
  15.       s = s[:-3]
  16.       s.append(a)
  17.     elif(len(s)>=3 and s[-3].isdigit() and s[-2]=='+' and s[-1].isdigit()):
  18.       a,b = int(s[-1]),int(s[-3])
  19.       s = s[:-3]
  20.       s.append(str(a+b))
  21.     elif(len(s)>=3 and s[-3].isdigit() and s[-2]=='*' and s[-1].isdigit()):
  22.       a,b = int(s[-1]),int(s[-3])
  23.       s = s[:-3]
  24.       s.append(str(a*b))
  25.     else:
  26.       flag = False
  27.  
  28.   return s
  29.  
  30. l = int(input())
  31. x=''
  32. for i in range(l):
  33.   x+=input()
  34. #print(x)
  35.  
  36. x = re.sub('for (\d+)',r'(\1*(',x)
  37. x = re.sub(r'add',r'1+',x)
  38. x = re.sub(r'end',r'))+',x)
  39. x = re.sub(r'\(\)',r'0',x)
  40. x = re.sub(r'\*\)',r'*0)',x)
  41. x = re.sub(r'\+\)',r')',x)
  42. x = re.sub(r'\+$',r'',x)
  43. #print(x)
  44.  
  45. s=[]
  46. for i in x:
  47.   #print(i)
  48.   s.append(i)  
  49.   s = st(s)
  50. ans = int(s[0])
  51. print("OVERFLOW!!!") if ans>2**32-1 else print(ans)
Add Comment
Please, Sign In to add comment