-
Notifications
You must be signed in to change notification settings - Fork 3
/
wordlewithfriends.py
92 lines (79 loc) · 2.41 KB
/
wordlewithfriends.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import sys
n, w = map(int, input().split())
kb, kb2, freq = {}, {}, {}
wv = []
for line in sys.stdin:
wv.append(line.strip().split())
n -= 1
if n == 0:
break
for word, verdict in wv:
temp = {}
for idx in range(len(word)):
if verdict[idx] != '-':
if word[idx] not in temp:
temp[word[idx]] = 0
temp[word[idx]] += 1
for k in temp:
if k not in freq:
freq[k] = set(range(len(word)))
freq[k] -= set(range(temp[k]))
for idx in range(len(word)):
if verdict[idx] == '-':
if word[idx] in temp:
freq[word[idx]] = {temp[word[idx]]}
else:
freq[word[idx]] = {0}
for word, verdict in wv:
for idx in range(len(word)):
if verdict[idx] == 'Y':
if word[idx] not in kb:
kb[word[idx]] = set(range(len(word)))
kb[word[idx]] -= {idx}
for word, verdict in wv:
for idx in range(len(word)):
if verdict[idx] == 'G':
if word[idx] not in kb2:
kb2[word[idx]] = set()
kb2[word[idx]].add(idx)
illegal = set()
for word, verdict in wv:
for idx in range(len(word)):
if verdict[idx] == '-' and word[idx] not in kb and word[idx] not in kb2:
illegal.add(word[idx])
possible = []
for line in sys.stdin:
check = line.strip()
def do():
kb2c = {}
for k in kb2:
kb2c[k] = kb2[k].copy()
for idx in range(len(check)):
if check[idx] in illegal:
return
try:
kb2c[check[idx]].remove(idx)
if not kb2c[check[idx]]:
del kb2c[check[idx]]
except:
if check[idx] in kb and idx not in kb[check[idx]]:
return
freq2 = {}
for k in check:
if k not in freq2:
freq2[k] = 0
freq2[k] += 1
for k in freq2:
if k in freq and freq2[k] not in freq[k]:
return
for k in freq:
if k in freq2:
if min(freq[k]) > freq2[k]:
return
elif min(freq[k]) > 0:
return
if not kb2c:
possible.append(check)
do()
for w in possible:
print(w)