Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- https://codeforces.com/problemset/problem/1175/B
- """
- import re
- def st(s):
- flag = True
- while(flag):
- if(len(s)>2 and s[-2].isdigit() and s[-1].isdigit()):
- a,b = int(s[-2]), int(s[-1])
- s = s[:-2]
- s.append(str(a*10+b))
- elif(len(s)>=3 and s[-3] == '(' and s[-2].isdigit() and s[-1]== ')'):
- a = s[-2]
- s = s[:-3]
- s.append(a)
- elif(len(s)>=3 and s[-3].isdigit() and s[-2]=='+' and s[-1].isdigit()):
- a,b = int(s[-1]),int(s[-3])
- s = s[:-3]
- s.append(str(a+b))
- elif(len(s)>=3 and s[-3].isdigit() and s[-2]=='*' and s[-1].isdigit()):
- a,b = int(s[-1]),int(s[-3])
- s = s[:-3]
- s.append(str(a*b))
- else:
- flag = False
- return s
- l = int(input())
- x=''
- for i in range(l):
- x+=input()
- #print(x)
- x = re.sub('for (\d+)',r'(\1*(',x)
- x = re.sub(r'add',r'1+',x)
- x = re.sub(r'end',r'))+',x)
- x = re.sub(r'\(\)',r'0',x)
- x = re.sub(r'\*\)',r'*0)',x)
- x = re.sub(r'\+\)',r')',x)
- x = re.sub(r'\+$',r'',x)
- #print(x)
- s=[]
- for i in x:
- #print(i)
- s.append(i)
- s = st(s)
- ans = int(s[0])
- print("OVERFLOW!!!") if ans>2**32-1 else print(ans)
Add Comment
Please, Sign In to add comment