华为OJ——DNA序列

来源:互联网 发布:游戏优化软件排行榜 编辑:程序博客网 时间:2024/06/10 09:16

题目描述

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

  • 输入描述:

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

  • 输出描述:

    找出GC比例最高的子串,如果有多个输出第一个的子串

  • 示例1

    输入

      AACTGTGCACGACCTGA
      5
    输出

      GCACG

实现代码

package cn.c_shuang.demo60;import java.util.Scanner;/** * DNA序列 * @author Cshuang * 题意:找出固定长度中GC比例最高的子串 */public class Main {    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        while (in.hasNextLine()){            String s = in.nextLine();            int subLen=in.nextInt();            int max = 0;            int begin = 0;            for (int i = 0; i <= s.length()-subLen; i++){                int count = 0;                for (int j = i; j < i + subLen; j++){                    if (s.charAt(j) =='G'||s.charAt(j) =='C'){                        count++;                    }                }                if (count > max){                    max = count;                    begin = i;                }            }            System.out.println(s.substring(begin, begin+subLen));        }        in.close();    }}
原创粉丝点击