문제
2022-03-23
어떤 수식이 주어졌을 때, 괄호를 제거해서 나올 수 있는 서로 다른 식의 개수를 계산하는
프로그램을 작성하시오.
((2)) 같은경우 바깥괄호제거할 때랑 안쪽 괄호제거한 결과가 겹친다.
set을 써서 제거해버리자.
코드
import itertools
exp = list(input())
give_num = {}
stk = []
count = 0
for i in range(len(exp)):
if exp[i] == "(":
stk.append(i)
elif exp[i] == ")":
give_num[count] = [stk[-1],i]
count += 1
del stk[-1]
count = 0
to_del = []
for i in range(1,len(give_num.keys())+1):
for comb in itertools.combinations(give_num.values(),i):
to_del.append(comb)
answer = []
for comb in to_del:
temp_del = []
temp_answer = []
for i in range(len(comb)):
temp_del.append(comb[i][0])
temp_del.append(comb[i][1])
for i in range(len(exp)):
if i not in temp_del:
temp_answer.append(exp[i])
answer.append("".join(temp_answer))
temp_set = set(answer)
answer = list(temp_set)
answer.sort()
for i in answer:
print(i)