题解 | #DNA序列#
DNA序列
https://www.nowcoder.com/practice/e8480ed7501640709354db1cc4ffd42a
#include <iostream>
#include <string>
using namespace std;
//滑动窗口
//每次只记录两端的CG变化
int main() {
string str;
cin >> str;
int n;
cin >> n;
int l=0;
int cg=0;
string result=str.substr(0,n);
for(int i=0;i<n;++i){
if(str[i]=='C'||str[i]=='G')
++cg;
}
int cgmax=cg;
int r=n;
while(r<str.size()){
if(str[r]=='C'||str[r]=='G')
++cg;
if(str[l]=='C'||str[l]=='G')
--cg;
++l;++r;
if(cg>cgmax){
cgmax=cg;
result=str.substr(l,n);
}
}
cout << result;
}
// 64 位输出请用 printf("%lld")
查看10道真题和解析
