본문 바로가기
python-algorithm

백준 1251 단어 나누기

by 무적김두칠 2022. 10. 17.

https://www.acmicpc.net/problem/1251

 

1251번: 단어 나누기

알파벳 소문자로 이루어진 단어를 가지고 아래와 같은 과정을 해 보려고 한다. 먼저 단어에서 임의의 두 부분을 골라서 단어를 쪼갠다. 즉, 주어진 단어를 세 개의 더 작은 단어로 나누는 것이다

www.acmicpc.net

 

1
2
3
4
5
6
7
8
9
10
11
def sol(s):
    len_s = len(s)
    strings = []
    for i in range(1, len_s - 1):
        for j in range(1, len_s - 1):
            if i + j <= len_s - 1:
                strings.append(s[:i][::-1+ s[i:i + j][::-1+ s[i + j:][::-1])
    return(sorted(strings)[0])
 
s=input()
print(sol(s))
cs

2중 for문으로 단어 쪼개고 쪼개진 단어 역으로 list에 넣고 정렬
Using double for clause split Words, and append splitted in to list , sort

반응형

댓글