首页 > 编程 > C++ > 正文

[华为OJ--C++]089-DNA序列

2019-11-08 03:00:04
字体:
来源:转载
供稿:网友

题目描述:

一个DNA序列由A/C/G/T四个字母的排列组合组成。G和C的比例(定义为GC-Ratio)是序列中G和C两个字母的总的出现次数除以总的字母数目(也就是序列长度)。在基因工程中,这个比例非常重要。因为高的GC-Ratio可能是基因的起始点。给定一个很长的DNA序列,以及要求的最小子序列长度,研究人员经常会需要在其中找出GC-Ratio最高的子序列。

输入描述:输入一个string型基因序列,和int型子串的长度

输出描述:找出GC比例最高的字串

输入例子:

AACTGTGCACGACCTGA

5

输出例子:

GCACG

算法实现:

#include<iostream>#include<vector>#include<string>using namespace std;/************************************************   * Author: 赵志乾   * Date: 2017-2-18    * Declaration: All Rigths Reserved !!!   ***********************************************/int main(){	string instr;	int len;	cin>>instr>>len;	int count=0;	for(int i=0;i<len;i++)		if(instr[i]=='C'||instr[i]=='G')			count++;	int max=count;	int left=0,right=len-1;	for(int i=len;i<instr.length();i++)	{		if(instr[i]=='C'||instr[i]=='G')		{			count++;		}		if(instr[i-len]=='C'||instr[i-len]=='G')		{			count--;		}		if(count>max)		{			left=i-len+1;			right=i;			max=count;		}	}	for(int i=left;i<=right;i++)	{		cout<<instr[i];	}	cout<<endl;	return 0;}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选