163. 괄호 제거
1) 어떤 전략(알고리즘)으로 해결?
2) 코딩 설명
<내 풀이>
import sys
from itertools import combinations
inp=list(map(str,sys.stdin.readline().rstrip()))
save=[]
stk=[]
for i in range(len(inp)) :
if inp[i]=="(" :
stk.append(i)
elif inp[i]==")" :
save.append([stk.pop(),i])
res=[]
for i in range(1,len(save)+1):
aftercombination= list(combinations(save, i))
for j in aftercombination :
inpcpy2 = inp.copy()
for e,s in j :
inpcpy2[e]=""
inpcpy2[s]=""
res.append("".join(inpcpy2))
res=list(set(res))
res.sort()
for i in res:
print(i)
- 처음에 순서쌍 찾는 것을 틀렸었나 보다.
- 스택을 사용해서 바꿔주었다.
<내 틀렸던 풀이, 문제점>
import sys
from itertools import combinations
inp=list(map(str,sys.stdin.readline().rstrip()))
save=[]
inpcpy0=inp.copy()
for i in range(len(inp)-1,-1,-1) :
if inpcpy0[i]==")" :
for j in range(i-1) :
if inpcpy0[j]=="(" :
save.append([i,j])
inpcpy0.pop(j)
inpcpy0.insert(j,'-1')
break
res=[]
for i in range(1,len(save)+1):
aftercombination= list(combinations(save, i))
for j in aftercombination :
inpcpy2 = inp.copy()
for e,s in j :
inpcpy2[e]=""
inpcpy2[s]=""
res.append("".join(inpcpy2))
res=list(set(res))
res.sort()
for i in res:
print(i)
<반성 점>
<배운 점>