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
|
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
vector<int> solution( const string &S, const vector<int> &P, const vector<int> &Q )
{
string SI = S;
replace( SI.begin(), SI.end(), 'A', '0' );
replace( SI.begin(), SI.end(), 'C', '1' );
replace( SI.begin(), SI.end(), 'G', '2' );
replace( SI.begin(), SI.end(), 'T', '3' );
int N = S.size();
vector< vector<int> > bases(N,vector<int>(4,-1)); // will hold the LATEST index with these bases
bases[0][SI[0]-'0'] = 0;
for ( int i = 1; i < N; i++ )
{
bases[i] = bases[i-1];
bases[i][SI[i]-'0'] = i;
}
int M = Q.size();
vector<int> result( M );
for ( int k = 0; k < M; k++ )
{
int p = P[k], q = Q[k];
for ( int j = 0; j < 4; j++ )
{
if ( bases[q][j] >= p )
{
result[k] = j + 1;
break;
}
}
}
return result;
}
int main()
{
string S = "CAGCCTA";
vector<int> P = { 2, 5, 0 }, Q = { 4, 5, 6 };
for ( int e : solution( S, P, Q ) ) cout << e << ' ';
}
|