1
- # python3
2
1
import sys
3
2
import suffix_tree
4
3
from collections import namedtuple
5
4
import threading
6
5
7
6
# DFS traversal on the tree
8
- def dfs_non_shared_substring (st , node ):
7
+ def dfs_non_shared_substring (stree , node ):
9
8
10
- if len (st .nodes [node ]) == 0 :
9
+ if len (stree .nodes [node ]) == 0 :
11
10
return (- 1 , 0 , False )
12
11
13
- text_len = (len (suffix_tree .text ) - 2 ) // 2
12
+ text_len = (len (stree .text ) - 2 ) // 2
14
13
shortest_substr_end = 0
15
14
shortest_substr_len = 2001
16
15
right_part = False
17
- for child in st .nodes [node ]:
16
+ for child in stree .nodes [node ]:
18
17
19
- if st .nodes [node ][child ].start >= text_len :
20
- right_part = True if st .nodes [node ][child ].start > text_len else right_part
18
+ if stree .nodes [node ][child ].start >= text_len :
19
+ right_part = True if stree .nodes [node ][child ].start > text_len else right_part
21
20
continue
22
21
23
- (substr_end , substr_len , subright_part ) = dfs_non_shared_substring (suffix_tree , child )
22
+ (substr_end , substr_len , subright_part ) = dfs_non_shared_substring (stree , child )
24
23
right_part = True if subright_part else right_part
25
24
26
- candidate_len = 1 if substr_len == 0 else st .nodes [node ][child ].len + substr_len
27
- candidate_end = st .nodes [node ][child ].start if substr_end == - 1 else substr_end
25
+ candidate_len = 1 if substr_len == 0 else stree .nodes [node ][child ].len + substr_len
26
+ candidate_end = stree .nodes [node ][child ].start if substr_end == - 1 else substr_end
28
27
29
28
if shortest_substr_len > candidate_len :
30
29
shortest_substr_end = candidate_end
@@ -39,10 +38,10 @@ def dfs_non_shared_substring(st, node):
39
38
def solve (p , q ):
40
39
pq_text = p + "#" + q + "$"
41
40
42
- st = suffix_tree .Suffix_Tree (pq_text )
43
- (shortest_substr_end , shortest_substr_len , right_part ) = dfs_non_shared_substring (st , 0 )
41
+ stree = suffix_tree .Suffix_Tree (pq_text )
42
+ (shortest_substr_end , shortest_substr_len , right_part ) = dfs_non_shared_substring (stree , 0 )
44
43
45
- return st .text [ shortest_substr_end - shortest_substr_len + 1 : shortest_substr_end + 1 ]
44
+ return stree .text [ shortest_substr_end - shortest_substr_len + 1 : shortest_substr_end + 1 ]
46
45
47
46
def main ():
48
47
p = sys .stdin .readline ().strip ()
@@ -55,5 +54,6 @@ def main():
55
54
56
55
sys .setrecursionlimit (10 ** 7 )
57
56
threading .stack_size (2 ** 30 )
57
+
58
58
threading .Thread (target = main ()).start ()
59
59
0 commit comments