문제 확인하기
2. 정답 코드 (Python, memory: 31256KB, time: 44ms)
def hamming_distance(str1, str2):
return sum(s1 != s2 for s1, s2 in zip(str1, str2))
def find_closest_pattern(patterns, target):
min_distance = float('inf')
closest_index = -1
for i, pattern in enumerate(patterns):
distance = hamming_distance(pattern, target)
if distance < min_distance:
min_distance = distance
closest_index = i
if min_distance == 1:
return closest_index
else:
return -1
patterns = [
'000000',
'001111',
'010011',
'011100',
'100110',
'101001',
'110101',
'111010'
]
pattern_labels = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
n = int(input())
arr = input().strip()
ans = []
err = False
while arr:
found = False
for i, pattern in enumerate(patterns):
if arr.startswith(pattern):
ans.append(pattern_labels[i])
arr = arr[len(pattern):]
found = True
break
if not found:
closest_index = find_closest_pattern(patterns, arr[:6])
if closest_index == -1:
err = True
m = len(ans) + 1
break
else:
ans.append(pattern_labels[closest_index])
arr = arr[6:]
if err:
print(m)
else:
print(''.join(ans))