EPS/2024/05/06-05-24-Bonus/a.py

49 lines
834 B
Python
Raw Normal View History

2024-05-06 15:22:19 +02:00
import sys
from munkres import Munkres
m = Munkres()
n, k = map(int, input().split())
ciphering = input()
sol = input()
def from_letter(a):
if ord(a) < ord('a'):
return 26+ord(a)-ord('A')
return ord(a)-ord('a')
def to_letter(n):
if n > 25:
return chr(n-26+ord('A'))
return chr(n+ord('a'))
mat = [
[0]*k for _ in range(k)
]
for letter1, letter2 in zip(ciphering, sol):
mat[from_letter(letter1)][from_letter(letter2)] += 1
cost_matrix = []
for row in mat:
cost_row = []
for col in row:
cost_row += [sys.maxsize - col]
cost_matrix += [cost_row]
m = Munkres()
indexes = m.compute(cost_matrix)
total = 0
for row, column in indexes:
value = mat[row][column]
total += value
print(total)
for row, column in indexes:
print(to_letter(column), end='')
print()