题解 | #DNA序列#简单解法,遍历即可
DNA序列
http://www.nowcoder.com/practice/e8480ed7501640709354db1cc4ffd42a
#include <bits/stdc++.h>
using namespace::std;
int main()
{
string str;
int num;
while (cin >> str >> num) {
string s;
string subs; //保存最大值对应的字串
int max = 0; // 最大值
for (int i = 0;i < str.size();i++) {
int cnt = 0; // 记录当前长度
s = str.substr(i, num); // 子串
for (auto const &c : s) {
if (('C' == c) || ('G' == c)) cnt++;
}
if (cnt > max) {
max = cnt;
subs = s;
}
}
cout << subs << endl;
}
return 0;
}
using namespace::std;
int main()
{
string str;
int num;
while (cin >> str >> num) {
string s;
string subs; //保存最大值对应的字串
int max = 0; // 最大值
for (int i = 0;i < str.size();i++) {
int cnt = 0; // 记录当前长度
s = str.substr(i, num); // 子串
for (auto const &c : s) {
if (('C' == c) || ('G' == c)) cnt++;
}
if (cnt > max) {
max = cnt;
subs = s;
}
}
cout << subs << endl;
}
return 0;
}