-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy path(hard) Genome Sequencing.js
59 lines (52 loc) · 1.9 KB
/
(hard) Genome Sequencing.js
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
/**
* Genome Sequencing (hard)
* https://www.codingame.com/training/hard/genome-sequencing
* Statement:
* The goal of this puzzle is calculate the length of the shortest string
* sequence that contains all the sub-sequences of the input data. You will
* have to compute the list of all possible permutations and merge strings
* together. This is also a good exercise for optimization and to discover
* dynamic programming.
* Story:
* Guanine, Thymine, Cytosine... You might have heard of those things in
* biology class, but forgot them on the spot. Don't worry, we all have. The
* goal of this exercise is to find how to combine chains of nucleotides
* (sorry, characters) in a way in which they take the least possible room.
*/
let N = +readline();
const subseq = [];
while (N--) {
subseq.push(readline());
}
const results = [];
function mutate(arr, firstpart) {
if (arr.length) {
for(let i = 0; i < arr.length; i++) {
const chain = arr.slice();
const part = chain.splice(i, 1);
mutate(chain, (firstpart || []).concat(part));
}
} else {
let totalLength = firstpart.join``.length;
if (firstpart.length === 1) {
return results.push(firstpart[0].length);
}
for (let i=1; i<firstpart.length; i++) {
const [curr, prev] = [firstpart[i], firstpart[i-1]];
const [cLength, pLength] = [curr.length, prev.length];
if (~prev.indexOf(curr)) {
totalLength -= cLength;
} else {
for(let j = Math.max(0, pLength - cLength); j < pLength; j++) {
if (curr.indexOf(prev.slice(j)) === 0) {
totalLength -= pLength - j;
break;
}
}
}
}
results.push(totalLength);
}
}
mutate(subseq);
print(Math.min(...results));