|
| 1 | +# python3 |
| 2 | +import sys |
| 3 | + |
| 4 | + |
| 5 | +def suffix_array_to_suffix_tree(sa, lcp, text): |
| 6 | + """ |
| 7 | + Build suffix tree of the string text given its suffix array suffix_array |
| 8 | + and LCP array lcp_array. Return the tree as a mapping from a node ID |
| 9 | + to the list of all outgoing edges of the corresponding node. The edges in the |
| 10 | + list must be sorted in the ascending order by the first character of the edge label. |
| 11 | + Root must have node ID = 0, and all other node IDs must be different |
| 12 | + nonnegative integers. Each edge must be represented by a tuple (node, start, end), where |
| 13 | + * node is the node ID of the ending node of the edge |
| 14 | + * start is the starting position (0-based) of the substring of text corresponding to the edge label |
| 15 | + * end is the first position (0-based) after the end of the substring corresponding to the edge label |
| 16 | +
|
| 17 | + For example, if text = "ACACAA$", an edge with label "$" from root to a node with ID 1 |
| 18 | + must be represented by a tuple (1, 6, 7). This edge must be present in the list tree[0] |
| 19 | + (corresponding to the root node), and it should be the first edge in the list (because |
| 20 | + it has the smallest first character of all edges outgoing from the root). |
| 21 | + """ |
| 22 | + tree = {} |
| 23 | + # Implement this function yourself |
| 24 | + return tree |
| 25 | + |
| 26 | + |
| 27 | +if __name__ == '__main__': |
| 28 | + text = sys.stdin.readline().strip() |
| 29 | + sa = list(map(int, sys.stdin.readline().strip().split())) |
| 30 | + lcp = list(map(int, sys.stdin.readline().strip().split())) |
| 31 | + print(text) |
| 32 | + # Build the suffix tree and get a mapping from |
| 33 | + # suffix tree node ID to the list of outgoing Edges. |
| 34 | + tree = suffix_array_to_suffix_tree(sa, lcp, text) |
| 35 | + """ |
| 36 | + Output the edges of the suffix tree in the required order. |
| 37 | + Note that we use here the contract that the root of the tree |
| 38 | + will have node ID = 0 and that each vector of outgoing edges |
| 39 | + will be sorted by the first character of the corresponding edge label. |
| 40 | + |
| 41 | + The following code avoids recursion to avoid stack overflow issues. |
| 42 | + It uses two stacks to convert recursive function to a while loop. |
| 43 | + This code is an equivalent of |
| 44 | + |
| 45 | + OutputEdges(tree, 0); |
| 46 | + |
| 47 | + for the following _recursive_ function OutputEdges: |
| 48 | + |
| 49 | + def OutputEdges(tree, node_id): |
| 50 | + edges = tree[node_id] |
| 51 | + for edge in edges: |
| 52 | + print("%d %d" % (edge[1], edge[2])) |
| 53 | + OutputEdges(tree, edge[0]); |
| 54 | + |
| 55 | + """ |
| 56 | + stack = [(0, 0)] |
| 57 | + result_edges = [] |
| 58 | + while len(stack) > 0: |
| 59 | + (node, edge_index) = stack[-1] |
| 60 | + stack.pop() |
| 61 | + if not node in tree: |
| 62 | + continue |
| 63 | + edges = tree[node] |
| 64 | + if edge_index + 1 < len(edges): |
| 65 | + stack.append((node, edge_index + 1)) |
| 66 | + print("%d %d" % (edges[edge_index][1], edges[edge_index][2])) |
| 67 | + stack.append((edges[edge_index][0], 0)) |
0 commit comments