Algorithm/BOJ
[BOJ] 1251 단어 나누기
SolB
2022. 5. 28. 23:38

word = input()
stack = []
for i in range(len(word)-2):
for j in range(i+1, len(word)-1):
for k in range(j+1, len(word)):
t = word[:j][::-1] + word[j:k][::-1] + word[k:][::-1]
stack.append(t)
print(min(stack))
<코드 설명>
먼저 word를 input받아주었다. 이를 세 부분으로 나누어야 한다.
나눠지는 단어의 부분이 1이상이어야하므로 첫번째 for문을 len(word)-2까지, 두번째 for문을 i+1부터 len(word)-1까지, 마지막 for문을 j+1부터 len(word)까지 검사해주도록 하였다.
변수 t에 각각 반대로 뒤집어진 결과를 합하여 저장해주었고 이를 stack에 append해주었다.
이 중 사전 순으로 가장 앞에 있는 단어를 출력해주었다.
<실행 결과>

