https://www.acmicpc.net/problem/10597
따라서 코드로 작성하면 다음과 같다.
n = len(word) if len(word) <= 9 else 9 + (len(word) - 9) // 2
순열을 저장하는 리스트를 만들어 관리한다.
3번을 처리 안해줘서 계속 오답이 났었다.
import sys
input = sys.stdin.readline
word = input().rstrip()
n = len(word) if len(word) <= 9 else 9 + (len(word) - 9) // 2
answer = list()
def backtracking(index: int):
if index == len(word):
print(' '.join(answer))
exit(0)
if word[index] != '0' and word[index] not in answer:
answer.append(word[index])
backtracking(index + 1)
answer.pop()
if word[index] != '0' and int(word[index: index + 2]) <= n and word[index:index + 2] not in answer:
answer.append(word[index:index + 2])
backtracking(index + 2)
answer.pop()
backtracking(0)
exit(0)
으로 한 번 출력하고 프로그램을 종료시킬 수 있다.