-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLesson 5 - GenomicRangeQuery.cpp
55 lines (51 loc) · 1.41 KB
/
Lesson 5 - GenomicRangeQuery.cpp
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
/* ● GenomicRangeQuery
Find the minimal nucleotide from a range of sequence DNA. */
vector<int> solution(string &S, vector<int> &P, vector<int> &Q) {
unsigned int i, len = S.length(), M = P.size();
vector<int> a(len, 0);
vector<int> c(len, 0);
vector<int> g(len, 0);
vector<int> t(len, 0);
vector<int> result;
if(S[0] == 'A'){
a[0] = 1;
}else if(S[0] == 'C'){
c[0] = 1;
}else if(S[0] == 'G'){
g[0] = 1;
}else if(S[0] == 'T'){
t[0] = 1;
}
for (i = 1; i < len; i++) {
switch(S[i]){
case 'A':
a[i] = 1;
break;
case 'C':
c[i] = 1;
break;
case 'G':
g[i] = 1;
break;
case 'T':
t[i] = 1;
break;
}
a[i] += a[i-1];
c[i] += c[i-1];
g[i] += g[i-1];
t[i] += t[i-1];
}
for(i = 0; i < M; i++){
if(a[Q[i]] > a[P[i]] || S[P[i]] == 'A'){
result.push_back(1);
}else if(c[Q[i]] > c[P[i]] || S[P[i]] == 'C'){
result.push_back(2);
}else if(g[Q[i]] > g[P[i]] || S[P[i]] == 'G'){
result.push_back(3);
}else if(t[Q[i]] > t[P[i]] || S[P[i]] == 'T'){
result.push_back(4);
}
}
return result;
}