結果

問題 No.708 (+ー)の式
ユーザー eQeeQe
提出日時 2020-05-07 16:34:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 112 ms / 2,000 ms
コード長 823 bytes
コンパイル時間 1,020 ms
コンパイル使用メモリ 87,088 KB
実行使用メモリ 73,232 KB
最終ジャッジ日時 2023-09-16 07:57:27
合計ジャッジ時間 3,713 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 108 ms
72,908 KB
testcase_01 AC 107 ms
73,036 KB
testcase_02 AC 108 ms
72,672 KB
testcase_03 AC 107 ms
72,708 KB
testcase_04 AC 108 ms
73,092 KB
testcase_05 AC 110 ms
73,232 KB
testcase_06 AC 109 ms
72,912 KB
testcase_07 AC 112 ms
72,916 KB
testcase_08 AC 110 ms
73,144 KB
testcase_09 AC 109 ms
73,020 KB
testcase_10 AC 110 ms
72,908 KB
testcase_11 AC 109 ms
72,932 KB
testcase_12 AC 109 ms
72,912 KB
testcase_13 AC 108 ms
73,232 KB
testcase_14 AC 109 ms
72,856 KB
権限があれば一括ダウンロードができます

ソースコード

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