def miv Min 99 99 for in if fr Min Min fr for in range len if fr Min d

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def miv (s):
Min = 99**99
for i in s:
if i['fr'] < Min:
Min = i['fr']
for i in range(len(s)):
if s[i]['fr'] == Min:
t = s[i]
del s[i]
return t
def hafTree(s2):
while len(s2) != 1:
x = miv(s2)
y = miv(s2)
fr = x['fr'] + y['fr']
name = x['name'] + y['name']
s2.append({'fr': fr, 'name': name, 0: x, 1: y})
return s2
def treeRead(s, codes, code=''):
if not s[0] and not s[1]:
if code == '': code = '0'
codes[s['name']] = code
return
for i in range(2):
treeRead(s[i], codes, code=code+str(i))
s = input()
s1 = set(s)
s2 = {}
for i in s:
if i in s2:
s2[i]+= 1
else:
s2[i] = 1
t = []
for i in s2:
t.append({'name': i, 'fr': s2[i], 0: None, 1: None})
s2 = t
s3 = hafTree(s2)
codes = {}
s2 = s2[0]
treeRead(s2, codes)
output = ''
for i in s:
output += str(codes[i])
print(len(s1), len(output))
for i in codes:
print(i + ':', codes[i])
print(output)