-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd14.py
409 lines (334 loc) · 8.54 KB
/
d14.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# class Node:
# def __init__(self):
# self.count_word = 0
# self.child = dict()
# def add_word(root, s):
# tmp = root
# for ch in s:
# if ch not in tmp.child:
# tmp.child[ch] = Node()
# tmp = tmp.child[ch]
# tmp.count_word += 1
# def find_word(root, s):
# temp = root
# for ch in s:
# if ch not in temp.child:
# return False
# temp = temp.child[ch]
# return temp.count_word > 0
# def is_word(node):
# return node.count_word != 0
# def is_empty(node):
# return len(node.child) == 0
# def remove_word(root, s, level, leng):
# if root == None:
# return False
# if level == leng:
# if root.count_word > 0:
# root.count_word -= 1
# return True
# return False
# ch = s[level]
# if ch not in root.child:
# return False
# flag = remove_word(root.child[ch], s, level + 1, leng)
# if flag == True and is_word(root.child[ch]) == False and is_empty(root.child[ch]) == True:
# del root.child[ch]
# return flag
# def print_word(root, s):
# if is_word(root):
# print(s)
# for ch in root.child:
# print_word(root.child[ch], s + ch)
# root = Node()
# add_word(root, 'the')
# add_word(root, 'then')
# add_word(root, 'bigo')
# print(find_word(root, 'there'))
# print(find_word(root, 'ththere'))
# print(find_word(root, 'the'))
# print_word(root, '')
# remove_word(root, 'bigo', 0, len('bigo'))
# remove_word(root, 'the', 0, len('the'))
# remove_word(root, 'then', 0, len('then'))
# # Search Engine
# class Node:
# def __init__(self):
# self.max_value = -1
# self.child = dict()
# def add_word(root, s, value):
# tmp = root
# for ch in s:
# if ch not in tmp.child:
# tmp.child[ch] = Node()
# tmp = tmp.child[ch]
# tmp.max_value = max(tmp.max_value, value)
# def get_max_value(root, s):
# tmp = root
# for ch in s:
# if ch not in tmp.child:
# return -1
# tmp = tmp.child[ch]
# return tmp.max_value
# n, q = list(map(int, input().split()))
# root = Node()
# for _ in range(n):
# word, priority = list(input().split())
# add_word(root, word, int(priority))
# for _ in range(q):
# word = str(input())
# print(get_max_value(root, word))
# # DNA Prefix
# class Node:
# def __init__(self):
# self.th_word = 1
# self.max_value = -1
# self.child = dict()
# def add_word(root, s):
# global result
# tmp = root
# for i in range(len(s)):
# ch = s[i]
# if ch not in tmp.child:
# tmp.child[ch] = Node()
# else:
# tmp.child[ch].th_word += 1
# tmp = tmp.child[ch]
# tmp.max_value = i + 1
# if tmp.th_word != 0:
# result = max(result, tmp.max_value * tmp.th_word)
# T = int(input())
# for case in range(T):
# N = int(input())
# root = Node()
# result = 0
# for i in range(N):
# word = input()
# add_word(root, word)
# print('Case {}: {}'.format(case +1, result))
# # Consistency Checker
# class Node:
# def __init__(self):
# self.count_word = 0
# self.child = dict()
# def add_word(root, s):
# tmp = root
# for i in range(len(s)):
# ch = s[i]
# if ch not in tmp.child:
# tmp.child[ch] = Node()
# else:
# if tmp.child[ch].count_word >= 1 and i <= len(s) - 1:
# return False
# tmp = tmp.child[ch]
# tmp.count_word += 1
# if len(tmp.child) > 0:
# return False
# return True
# T = int(input())
# for case in range(T):
# N = int(input())
# root = Node()
# result = True
# words = []
# for i in range(N):
# word = input()
# words.append(word)
# for word in words:
# result = add_word(root, word)
# if result == False:
# break
# final_res = 'YES' if result == True else 'NO'
# print('Case {}: {}'.format(case +1, final_res))
# # Contacts
# class Node:
# def __init__(self):
# self.count_word = 0
# self.child = dict()
# def add_word(root, s):
# tmp = root
# for ch in s:
# if ch not in tmp.child:
# tmp.child[ch] = Node()
# tmp = tmp.child[ch]
# tmp.count_word += 1
# def count_word(root, s):
# temp = root
# for ch in s:
# if ch not in temp.child:
# return 0
# else:
# temp = temp.child[ch]
# return temp.count_word
# n = int(input())
# root = Node()
# for _ in range(n):
# op, word = list(input().split())
# if op == 'add':
# add_word(root, word)
# else:
# result = count_word(root, word)
# print(result)
# # Diccionario Portunol
# import string
# import sys
# sys.setrecursionlimit(10000000)
# class TrieNode:
# def __init__(self):
# self.count_leaf = 0
# self.child = dict()
# class Trie:
# def __init__(self):
# self.root = TrieNode()
# def add_word(self, word):
# cur = self.root
# for c in word:
# if c not in cur.child:
# cur.child[c] = TrieNode()
# cur = cur.child[c]
# cur.count_leaf += 1
# def suffix_traversal(root, level, start_with):
# cnt = 1
# for c in root.child:
# if level > 0:
# start_with[c] += 1
# cnt += suffix_traversal(root.child[c], level + 1, start_with)
# return cnt
# def prefix_traversal(root, level, suffix_state_count, start_with):
# res = 0
# if level > 0:
# res = suffix_state_count
# for c in root.child:
# if level > 0:
# res -= start_with[c]
# res += prefix_traversal(root.child[c], level + 1, suffix_state_count, start_with)
# return res
# while True:
# p, s = map(int, input().split())
# if p == s == 0:
# break
# suffix_trie = Trie()
# prefix_trie = Trie()
# for i in range(p):
# prefix_trie.add_word(input())
# for i in range(s):
# suffix_trie.add_word(input()[::-1])
# start_with = {c: 0 for c in string.ascii_lowercase}
# suffix_state_count = suffix_traversal(suffix_trie.root, 0, start_with) - 1
# result = prefix_traversal(prefix_trie.root, 0, suffix_state_count, start_with)
# print(result)
# # No Prefix Set
# class Node:
# def __init__(self):
# self.count_word = 0
# self.child = dict()
# def add_word(root, s):
# tmp = root
# for i in range(len(s)):
# ch = s[i]
# if ch not in tmp.child:
# tmp.child[ch] = Node()
# else:
# if tmp.child[ch].count_word >= 1:
# return False
# tmp = tmp.child[ch]
# tmp.count_word += 1
# return len(tmp.child) == 0
# N = int(input())
# root = Node()
# words = []
# for i in range(N):
# word = input()
# words.append(word)
# for word in words:
# result = add_word(root, word)
# if result == False:
# print('BAD SET')
# print(word)
# exit()
# print('GOOD SET')
# # Bank and Vulnerable Passwords
# class Node:
# def __init__(self):
# self.count_word = 0
# self.child = dict()
# def add_word(root, s):
# tmp = root
# for i in range(len(s)):
# ch = s[i]
# if ch not in tmp.child:
# tmp.child[ch] = Node()
# else:
# if tmp.child[ch].count_word >= 1:
# return False
# tmp = tmp.child[ch]
# tmp.count_word += 1
# return len(tmp.child) == 0
# N = int(input())
# root = Node()
# words = []
# for i in range(N):
# word = input()
# words.append(word)
# for word in words:
# result = add_word(root, word)
# if result == False:
# print('vulnerable')
# exit()
# print('non vulnerable')
# Old Berland Language
class Node:
def __init__(self):
self.child = [None, None]
self.parent = None
self.is_blocked = False
def add_word(root, leng):
if root.is_blocked:
return ''
temp = root
s = ""
for i in range(leng):
next = -1
l = temp.child[0]
r = temp.child[1]
# Check if node 0 or 1 is blocked
if l == None or l.is_blocked == False:
next = 0
elif r == None or r.is_blocked == False:
next = 1
if next == -1:
return ''
# If not create new node and assign parent for new node
if temp.child[next] == None:
temp.child[next] = Node()
temp.child[next].parent = temp
temp = temp.child[next]
# Add new node for building string
s = s + str(next)
temp.is_blocked = True
# Update parent to blocked node if it can be
while temp.parent != None:
temp = temp.parent
l = temp.child[0]
r = temp.child[1]
if l != None and r != None:
if l.is_blocked == True and r.is_blocked == True:
temp.is_blocked = True
return s
root = Node()
N = int(input())
len_words = list(map(int, input().split()))
word_positions = []
for i in range(len(len_words)):
word_positions.append((len_words[i], i))
word_positions = sorted(word_positions)
ans = ['' for i in range(N)]
for (leng, id) in word_positions:
s = add_word(root, leng)
if s == '':
print('NO')
exit()
ans[id] = s
print('YES')
for w in ans:
print(w)