题解 | #DNA序列#
DNA序列
https://www.nowcoder.com/practice/e8480ed7501640709354db1cc4ffd42a
// 利用滑动窗口的解法,效率更高 #include<iostream> #include<string> using namespace std; int main() { string s; int n; while (cin >> s >> n) { int len = s.length(); int resindex = 0, max = 0; int count = 0; for (int i = 0; i < n; i++) //录入最前面的窗口 if (s[i] == 'C' || s[i] == 'G') count++; max = count; //录下第一个窗口的CG数量作为最大 int left = 1, right = n; //从录入窗口的左右点右移一位开始 while (right < len) { //直到右窗口结束 if (s[left - 1] == 'C' || s[left - 1] == 'G') //窗口左边出去的是CG count--; if (s[right] == 'C' || s[right] == 'G') //窗口右边进来的是CG count++; if (count > max) { //更新,取最大值 max = count; resindex = left; } left++; right++; } cout << s.substr(resindex, n) << endl; //根据下标和n输出 } return 0; }