// you can also use includes, for example:
// #include <algorithm>
vector<int> solution(string &S, vector<int> &P, vector<int> &Q) {
// write your code in C++98
vector<int> result(P.size());
//...keep record of the prefix count of 'A', 'C', 'G', 'T'
vector<vector<int> > prefixCnt(4, vector<int>(S.size()+1, 0));
for(int i = 0; i < S.size(); ++i)
{
for(int j = 0; j < 4; ++j) prefixCnt[j][i+1] = prefixCnt[j][i];
char curChar = S[i];
if(curChar == 'A') prefixCnt[0][i+1] += 1;
else if(curChar == 'C') prefixCnt[1][i+1] += 1;
else if(curChar == 'G') prefixCnt[2][i+1] += 1;
else if(curChar == 'T') prefixCnt[3][i+1] += 1;
//else error input
}
//...calculate the query result
for(int i = 0; i < result.size(); ++i)
{
int startIdx = P[i];
int endIdx = Q[i];
int curMinValue = 4;
for(int j = 0; j < 4; ++j)
{
int curCharCnt = prefixCnt[j][endIdx+1]-prefixCnt[j][startIdx];
if(curCharCnt != 0)
{
curMinValue = j+1;
break;
}
}
result[i] = curMinValue;
}
//...return result
return result;
}
[codility]Genomic-range-query
最新推荐文章于 2019-08-20 12:34:35 发布