-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19.py
78 lines (63 loc) · 2.25 KB
/
19.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from collections import Counter
from itertools import chain
from tqdm import tqdm
AMIN_TABLE = ['G', 'A', 'S', 'P', 'V', 'T', 'C', 'L', 'N', 'D', 'Q', 'E', 'M', 'H', 'F', 'R', 'Y', 'W']
MASS_TABLE = [57, 71, 87, 97, 99, 101, 103, 113, 114, 115, 128, 129, 131, 137, 147, 156, 163, 186]
AMIN_MASS = dict(zip(AMIN_TABLE, MASS_TABLE))
LEADER_PEPTIDE = ''
def extend(peptids):
for p in peptids:
for amin in AMIN_TABLE:
yield p + amin
def cyclospectrum(peptide):
spectrum = [0]
prefix_sum = [0]
for amin in peptide:
prefix_sum.append(prefix_sum[-1] + AMIN_MASS[amin])
n = len(peptide)
for i in range(n):
for j in range(i + 1, n + 1):
spectrum.append(prefix_sum[j] - prefix_sum[i])
if i > 0 and j < n:
spectrum.append(prefix_sum[n] - prefix_sum[j] + prefix_sum[i])
return sorted(spectrum)
def count_mass(peptide):
return sum(AMIN_MASS[amin] for amin in peptide)
def score(given_spectrum, peptide):
spectrum = cyclospectrum(peptide)
c = Counter(spectrum)
score = 0
for amin, cnt in c.items():
if amin in given_spectrum:
score += min(cnt, given_spectrum[amin])
return score
def cut_leaderboard(given_spectrum, parent_mass, peptids, n):
global LEADER_PEPTIDE
best = []
for p in peptids:
p_mass = count_mass(p)
if p_mass > parent_mass:
continue
best.append(p)
if p_mass == parent_mass:
if score(given_spectrum, p) > score(given_spectrum, LEADER_PEPTIDE):
LEADER_PEPTIDE = p
best = sorted(best, key=lambda p: score(given_spectrum, p), reverse=True)
if len(best) <= n:
return best
s = score(given_spectrum, best[n - 1])
while n < len(best) and score(given_spectrum, best[n]) == s:
n += 1
return best[:n]
def main():
n = int(input())
given_spectrum = list(map(int, input().split()))
parent_mass = max(given_spectrum)
given_spectrum = Counter(given_spectrum)
peptids = ['']
while peptids:
peptids = extend(peptids)
peptids = cut_leaderboard(given_spectrum, parent_mass, peptids, n)
print('-'.join(map(lambda amin: str(AMIN_MASS[amin]), LEADER_PEPTIDE)))
if __name__ == '__main__':
main()