結果

問題 No.708 (+ー)の式
ユーザー eQe
提出日時 2020-05-07 16:34:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 823 bytes
コンパイル時間 173 ms
コンパイル使用メモリ 82,228 KB
実行使用メモリ 62,488 KB
最終ジャッジ日時 2024-07-03 09:28:45
合計ジャッジ時間 1,539 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

import math
from math import sqrt
import sys
from collections import deque, defaultdict
from heapq import heappop, heappush
from copy import deepcopy

INF = 10 ** 18
MOD = 10 ** 9 + 7
MAX = 10 ** 5 + 7

mp=defaultdict(int)
def rec(t):
  if len(t)==3:
    a=int(t[0])
    b=int(t[2])
    if t[1]=='+': return a+b
    else: return a-b

  res=0
  r=0

  i=0
  while i<len(t):
    c=t[i]
    if c=='+': r=0
    elif c=='-': r=1
    elif c=='(':
      if r==0: res+=rec(t[i+1:mp[i]])
      else : res-=rec(t[i+1:mp[i]])
      i=mp[i]
    else:
      if r==0: res+=int(c)
      else: res-=int(c)
    i+=1

  return res

def main():
  global s, mp
  s=input()

  st=[]
  for i,c in enumerate(s):
    if c=='(':
      st.append(i)
    elif c==')':
      mp[st.pop()]=i

  print(rec(s))

  
if __name__ == '__main__':
    main()



0