2607-s2 성공
This commit is contained in:
63
workbook_8708/2607-s2.py
Normal file
63
workbook_8708/2607-s2.py
Normal file
@@ -0,0 +1,63 @@
|
||||
# 비슷한 단어
|
||||
|
||||
import sys
|
||||
from collections import Counter
|
||||
|
||||
input = sys.stdin.readline
|
||||
|
||||
def check_similar(now_word, target_c):
|
||||
target_c_copy = target_c.copy()
|
||||
now_word_left = []
|
||||
for c in now_word:
|
||||
if len(target_c_copy) < 1:
|
||||
break
|
||||
|
||||
if target_c_copy.get(c, -1) == -1:
|
||||
now_word_left.append(c)
|
||||
continue
|
||||
|
||||
target_c_copy[c] -= 1
|
||||
if target_c_copy[c] == 0:
|
||||
del target_c_copy[c]
|
||||
|
||||
len_target_left = sum(target_c_copy.values())
|
||||
result = 1 if len_target_left <= 1 and len(now_word_left) <= 1 else 0
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def solution():
|
||||
n = int(input().rstrip())
|
||||
|
||||
result = 0
|
||||
target = input().rstrip()
|
||||
target_c = Counter(target)
|
||||
|
||||
for _ in range(n-1):
|
||||
now_word = input().rstrip()
|
||||
if len(now_word) >= len(target)+2:
|
||||
continue
|
||||
|
||||
result += check_similar(now_word, target_c)
|
||||
|
||||
print(result)
|
||||
|
||||
return
|
||||
|
||||
|
||||
solution()
|
||||
|
||||
"""
|
||||
걸린 시간: 32분
|
||||
|
||||
시간 복잡도: 단어 길이를 m이라고 하자. 중복 제거할 때는 둘 중 짧은 단어만큼 진행된다. -> O(m)
|
||||
target_c의 values 합 구하기 -> O(m)
|
||||
이 두 과정이 check_similar()이고, O(m)이다. 이제 이 과정을 n-1개의 단어만큼 진행하기 때문에 전체 시간복잡도는 O(nm)이다.
|
||||
|
||||
해설: 비슷한 단어의 조건을 수치적으로 정리하면 target과 비교하는 단어의 길이 차이가 2이상 나면 안되고,
|
||||
겹치는 글자들을 개수 기준으로 다 제거했을 때 양쪽 다 남은 글자가 1을 초과하면 안된다.
|
||||
따라서 나는 target을 Counter로 만들고, 비교하는 단어를 쭉 보면서 target_c에 있으면 개수를 줄이는 방식으로 중복을 제거했다.
|
||||
이때 target_c에 없으면 남는 글자는 따로 리스트에 보관하였다.
|
||||
마지막으로 target_c의 values 합이 남는 글자 수고, len(lst)가 비교하는 글자의 남는 글자 수이므로 이것의 길이가 둘 다 <= 1일때만
|
||||
비슷한 단어로 판정했다.
|
||||
"""
|
||||
Reference in New Issue
Block a user