一個DNA序列由A/C/G/T四個字母的排列組合組成。G和C的比例(定義爲GC-Ratio)是序列中G和C兩個字母的總的出現次數除以總的字母數目(也就是序列長度)。在基因工程中,這個比例非常重要。因爲高的GC-Ratio可能是基因的起始點。
給定一個很長的DNA序列,以及要求的最小子序列長度,研究人員經常會需要在其中找出GC-Ratio最高的子序列
輸入:一個string型基因序列,和int型子串的長度
輸出:找出GC比例最高的字串
#include<iostream>
#include<string>
using namespace std;
int main()
{
int n,ratio,count0=0,count,mark=0;
string str;
cin >> str;
cin >> n;
int len = str.size();
for (int i = 0;i < n;i++)
{
if (str[i] == 'G' || str[i] == 'C')
count0++;
}
for (int i = 0;i < len-n;i++)
{
count = 0;
for (int j = i;j < i + n;j++)
{
if (str[j] == 'G' || str[j] == 'C')
count++;
}
if (count > count0)
{
mark = i;count0 = count;
}
}
for (int j = mark;j < mark + n;j++)
{
cout << str[j];
}
return 0;
}
樣例輸入 | AACTGTGCACGACCTGA 5 |
---|---|
樣例輸出 | GCACG |