-
Notifications
You must be signed in to change notification settings - Fork 74
/
non_shared_substring.py
52 lines (40 loc) · 1.09 KB
/
non_shared_substring.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
# python3
import sys
def build_trie(patterns):
tree = dict()
tree[0] = {}
index = 1
for pattern in patterns:
current = tree[0]
for letter in pattern:
if letter in current:
current = tree[current[letter]]
else:
current[letter] = index
tree[index] = {}
current = tree[index]
index = index + 1
current['$'] = {}
return tree
def solve(p, q):
patterns = [p, q]
for i in range(1, len(p)):
patterns.append(p[i:])
patterns.append(q[i:])
trie = build_trie(patterns)
print(trie)
current = trie[0]
substring = ""
while '$' not in current or current == trie[0]:
for child in current:
substring += child
if child not in q:
return substring
else:
current = trie[current[child]]
substring = substring[:-1]
return p
p = sys.stdin.readline().strip()
q = sys.stdin.readline().strip()
ans = solve(p, q)
sys.stdout.write(ans + '\n')