题解 | DNA序列
DNA序列
https://www.nowcoder.com/practice/e8480ed7501640709354db1cc4ffd42a
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
int n;
cin >> str >> n;
int len = str.length();
string res;
double maxp = 0; // 最大占比
for (int i = 0; i <= len - n; ++i) {
int cnt = 0;
for (int j = i; j < i + n; ++j) {
if (str[j] == 'G' || str[j] == 'C')
cnt++;
}
if (maxp < (double)cnt / n) {
res = str.substr(i, n);
maxp = (double)cnt / n;
}
}
cout << res << endl;
return 0;
}
查看23道真题和解析
