Skip to content

Commit c2514de

Browse files
author
Hamid Gasmi
committed
#166 starters are added
1 parent b02e927 commit c2514de

File tree

7 files changed

+92
-0
lines changed

7 files changed

+92
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
A$
2+
1 0
3+
0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
1 2
2+
0 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
AAA$
2+
3 2 1 0
3+
0 1 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
3 4
2+
0 1
3+
3 4
4+
1 2
5+
3 4
6+
2 4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
GTAGT$
2+
5 2 3 0 4 1
3+
0 0 2 0 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
5 6
2+
2 6
3+
3 5
4+
5 6
5+
2 6
6+
4 5
7+
5 6
8+
2 6

0 commit comments

Comments
 (0)