建立长度为k的双端队列,队尾进,队首出。进出之前进行检查,相应的增减G和C的总个数。
遍历中,记录下最大的个数和起始位置,便于输出

#include <bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    int k;
    while(cin >> s >> k)
    {
        deque<char> sub(s.begin(), s.begin() + k);
        int cnt = count(sub.begin(),sub.end(), 'G') + count(sub.begin(),sub.end(), 'C');
        int maxcount = cnt, stapos = 0;
        for(int i = k; i < s.size(); i++)
        {
            auto &c = s[i];
            if(c == 'G' || c == 'C')
            {
                cnt++;
            }
            sub.push_back(c);
            auto f = sub.front();
            if(f == 'G' || f == 'C')
            {
                cnt--;
            }
            sub.pop_front();
            if(cnt > maxcount)
            {
                maxcount = cnt;
                stapos = i - k + 1;
            }
        }
        cout << s.substr(stapos, k) << endl;
    }
}