题解 | #DNA序列#

DNA序列

https://www.nowcoder.com/practice/e8480ed7501640709354db1cc4ffd42a

想法是递推往前走,记录当前子序列的CG个数,往后滑动的时候减去头加上尾
#include <iostream>
#include <string>
using namespace std;

int main() {
    string s;
    int N;
    cin >> s >> N;
    int cnt = 0;
    
    bool head_is_CG = (s[0] == 'C' || s[0] == 'G') ? true : false;
    for(int idx = 0 ; idx < N ; idx++) {
        if(s[0] == 'C' || s[0] == 'G') cnt++;
    }
    int index = 1, res = 0;
    int maxcnt = cnt;
    for(int idx = N ; idx < s.size(); idx++) {
        if(head_is_CG) cnt--;
        if(s[idx] == 'C' || s[idx] == 'G') cnt++;
        head_is_CG = (s[index] == 'C' || s[index] == 'G') ? true : false;
        if(cnt > maxcnt) {
            maxcnt = cnt;
            res = index;
        }
        index++;
    }
    cout << s.substr(res,N);
}


全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务