-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path843-CryptKicker.py
56 lines (52 loc) · 1.51 KB
/
843-CryptKicker.py
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
import sys
import re
ndict = int(sys.stdin.readline())
# agrupo las palabras por longuitud
frec = dict()
for i in range(ndict):
aux = sys.stdin.readline().strip()
if len(aux) in frec:
frec[len(aux)].append(aux)
else:
frec[len(aux)] = [aux]
criptogramas = []
while True:
linea = sys.stdin.readline().strip()
if not linea:
break
criptogramas.append(linea)
for cripto in criptogramas:
criptopals = cripto.split()
#res = backtracking(criptopals)
#variables para la clausurita
sol = []
d = dict()
# clausura bt
def bt(s, d):
if len(s) == len(criptopals):
return s
#elif len(s) < len(criptopals):
else:
crip = criptopals[len(s)]
opts = frec[len(crip)]
for o in opts:
sol_ = s[:]
#test = check_colisions(o, crip, dict(d))
test = dict(d)
for i in range(len(crip)):
if crip[i] in test and test[crip[i]] != o[i]:
test = None
break
else:
test[crip[i]] = o[i]
if test is not None and len(test.keys()) == len(set(test.values())):
sol_.append(o)
r = bt(sol_, test)
if r:
return r
return None
res = bt(sol, d)
if res is None:
print(re.sub('[a-z]', "*"," ".join(criptopals)))
else:
print(" ".join(res))