문제
2022-03-20
풀이
l = 4
c = 6
jaem = 0
moem = 0
arr = ['a','t','c','i','s','w']
arr.sort()
now_arr = []
answer = []
def dfs(current):
global c,l,jaem,moem,now_arr
if current == c:
return
if arr[current] in ['a','e','i','o','u']:
if jaem >=2 and moem >= 0 and len(now_arr) == l-1:
now_arr.append(arr[current])
answer.append("".join(now_arr))
now_arr.pop()
else:
if jaem >=1 and moem >= 1 and len(now_arr) == l-1:
now_arr.append(arr[current])
answer.append("".join(now_arr))
now_arr.pop()
print('------------------------')
dfs(current+1)
if arr[current] in ['a','e','i','o','u']:
moem += 1
else:
jaem += 1
now_arr.append(arr[current])
dfs(current+1)
if arr[current] in ['a','e','i','o','u']:
moem -= 1
else:
jaem -= 1
now_arr.pop()
dfs(0)
answer.sort()
for i in answer:
print(i)
주목해서 봐야할 부분은
dfs(current+1)
if arr[current] in ['a','e','i','o','u']:
moem += 1
else:
jaem += 1
now_arr.append(arr[current])
dfs(current+1)
if arr[current] in ['a','e','i','o','u']:
moem -= 1
else:
jaem -= 1
now_arr.pop()
이 부분이다. 주어진 알파벳을 선택안하거나 선택하는 경우를 각각 재귀함수를 호출해서 실행하고
두 재귀함수가 끝난후에는 해당 알파벳을 선택하기 전으로 되돌려 놓은다음 함수를 끝낸다.