알고리즘
[백준, 파이썬, 1544번] 사이클 단어
hminor
2024. 2. 15. 10:59
반응형
풀이
- 그냥 단순하게 생각해서 받아오는 입력값 중
- 첫 단어를 기준으로 하나씩 순회하며 첫 단어와 같은지 확인하고자 했으며
- 첫 단어를 2배로 늘려 좀 더 쉽게 확인했고,
- 해당 단어와 같다면 li 배열에 추가하여 탐색이 끝난 뒤 word 배열에서 제거하여 해결
import sys
input = sys.stdin.readline
word = [input().rstrip('\n') for _ in range(int(input()))]
result = 0
while len(word) != 0:
result += 1
a = word.pop(0)
a = a+a
li = []
for i in range(len(word)):
i_ln = len(word[i])
if len(a)//2 == i_ln:
for j in range(i_ln+1):
if "".join(a[j:j+i_ln]) == word[i]:
li.append(word[i])
break
for i in li: word.remove(i)
print(result)