-
Notifications
You must be signed in to change notification settings - Fork 0
/
13.py
60 lines (48 loc) · 1.59 KB
/
13.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
import sys
sys.setrecursionlimit(10000)
ALPHABET = 'ACGT'
def dfs(cur_vertex, graph, visited_set):
max_depth = 0
best_end = []
visited_set.add(cur_vertex)
for next_vertex in graph[cur_vertex]:
if next_vertex not in visited_set:
depth, end = dfs(next_vertex, graph, visited_set)
if depth > max_depth:
max_depth = depth
best_end = end
visited_set.remove(next_vertex)
return max_depth + 1, [cur_vertex] + best_end
def collect_result(kmers_chain, k, d):
kmers_chain = [kmer.split('#') for kmer in kmers_chain]
res = [kmers_chain[0][0]]
for i in range(d):
res.append(kmers_chain[i + 1][0][-1])
res.append(kmers_chain[0][1])
for _, kmer in kmers_chain[1:]:
res.append(kmer[-1])
return ''.join(res)
def main():
k, d = map(int, input().split())
kmers = sys.stdin.read().strip().split('\n')
kmers = [kmer.split('|') for kmer in kmers]
n = len(kmers) + 2 * k + d - 1
edges = {}
for l, r in kmers:
edges[l + '#' + r] = []
for l, r in kmers:
cur_kmer = l + '#' + r
for x in ALPHABET:
for y in ALPHABET:
next_kmer = l[1:] + x + '#' + r[1:] + y
if next_kmer not in edges:
continue
edges[cur_kmer].append(next_kmer)
for l, r in kmers:
cur_kmer = l + '#' + r
depth, best_chain = dfs(cur_kmer, edges, set())
if depth == len(kmers):
print(collect_result(best_chain, k, d))
break
if __name__ == '__main__':
main()