Skip to content

Instantly share code, notes, and snippets.

@nariaki3551
Created December 10, 2016 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nariaki3551/5a48a9cde58cdcca1cbe4fa2a3f55243 to your computer and use it in GitHub Desktop.
Save nariaki3551/5a48a9cde58cdcca1cbe4fa2a3f55243 to your computer and use it in GitHub Desktop.
# -----------
# yukicoder
# No.457 (^^*)
# http://yukicoder.me/problems/no/457
# -------
# TLE もっと早い実装はできないのか........
s = input()
left_pos = [i for i, k in enumerate(s) if k == '(']
def left_solve():
res = 0
tmp_stock = None
first_index = None
for i in left_pos:
if tmp_stock is not None:
if i < first_index:
res += tmp_stock
continue
else:
tmp_stock = None
first_index = None
tmp = s[i+1:]
count = 0
for j, elm in enumerate(tmp):
if count < 2:
if elm == '^':
if first_index is None: first_index = i + j
count += 1
else:
if elm == '*':
tmp_stock = tmp[j+1:].count(')')
res += tmp_stock
break
return res
def right_solve():
res = 0
tmp_stock = None
first_index = None
for i in left_pos:
if tmp_stock is not None:
if i < first_index:
res += tmp_stock
continue
else:
tmp_stock = None
first_index = None
tmp = s[i+1:]
count = 0
for j, elm in enumerate(tmp):
if count < 1:
if elm == '*':
if first_index is None: first_index = i + j
count += 1
elif count < 2:
if elm == '^':
count += 1
else:
if elm == '^':
tmp_stock = tmp[j+1:].count(')')
res += tmp_stock
break
return res
print(left_solve(), right_solve())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment