题解 | #DNA序列#

DNA序列

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

#include <iostream>
#include <string>
using namespace std;

class DNASequence {
  private:
    //DNA序列
    string s;

    //所需字串长度
    int n;

  public:
    //构造函数
    DNASequence(const string s, const int n);

    //析构函数
    ~DNASequence();

    //找出子串
    const string search(void)const;

    //统计字符串s中C和G的总数
    const int count(const string s)const;
};

DNASequence::DNASequence(const string s, const int n) {
    this->s = s;
    this->n = n;
    return;
}

DNASequence::~DNASequence() {
}

const string DNASequence::search(void) const {
    string s1;
    int num = 0;
  
    //比较所有长度符合的子串
    for (int i = 0; i <= this->s.size() - this->n; i++) {
        string si = this->s.substr(i, this->n);
        int ni = this->count(si);
        if (ni > num) {
            s1 = si;
            num = ni;
        }
    }
    return s1;
}

const int DNASequence::count(const string s) const {
    int num = 0;

    //遍历字符串
    for (char ch : s)
        if ((ch == 'C') || (ch == 'G'))
            num++;
    return num;
}

int main() {
    string a;
    int b;
    while (cin >> a >> b) { // 注意 while 处理多个 case
        cout << DNASequence(a, b).search() << endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

点赞 评论 收藏
分享
半解316:内容充实,细节需要修改一下。 1,整体压缩为一页。所有内容顶格。 2,项目描述删除,直接写个人工作量 修改完之后还需要建议,可以私聊
点赞 评论 收藏
分享
08-07 11:47
门头沟学院 Java
快手你的进度好快啊,可是我感觉我还没做好准备8.4投递8.7hr初筛-用人部门筛选
瞒着老板找实习:2号投敌 4号约面 今天一面已挂 哈哈
投递快手等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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