当前位置:网站首页>Huawei machine test hj63: DNA sequence

Huawei machine test hj63: DNA sequence

2021-09-15 07:11:51 Zhai Tianbao Steven

author :Steven
Copyright notice : The copyright belongs to the author , Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source

Title Description :

One DNA The sequence consists of A/C/G/T It's a combination of four letters .G and C The proportion of ( Defined as GC-Ratio) It's in the sequence G and C The total number of occurrences of two letters divided by the total number of letters ( That's the length of the sequence ). In genetic engineering , This ratio is very important . Because of the tall GC-Ratio It could be the starting point of the gene .

Given a very long DNA Sequence , And the limited substring length N, Please help the researchers in the given DNA Find... From left to right in the sequence GC-Ratio The maximum length is N The first substring of .

DNA The sequence is ACGT There are :ACG,CG,CGT wait , But no AGT,CT wait

Input description :

Enter a string Genotype sequence , and int Length of type substring

Output description :

find GC The highest proportion of substrings , If there are more than one, output the substring of the first

Example :

Input :

ACGT
2

Output :

CG

explain :

ACGT The length is 2 There are AC,CG,GT3 individual , among AC and GT2 One of the GC-Ratio All for 0.5,CG by 1, Therefore, the output CG 

Their thinking :

This problem is called string operation problem . Input string After gene sequence and substring length , Next to substring analysis GC The proportion of , Locate the maximum GC-Ratio Substring output of .

Test code :

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    string str;
    int length;
    while(cin>>str>>length)
    {
        vector<string> ls;
        int size=str.size();
        float max=0.f;
        string result;
        for(int i=0;i<size-length+1;++i)
        {
            string t=str.substr(i,length);
            float sum=0.f;
            for(int j=0;j<t.size();++j)
            {
                if(t[j]=='G'||t[j]=='C')
                    sum++;
            }
            float R=float(sum/t.size());
            if(R>max)
            {
                max=R;
                result=t;
            }
        }
        cout<<result<<endl;
    }
    return 0;
}

版权声明
本文为[Zhai Tianbao Steven]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/09/20210909125005427V.html

随机推荐