【华为机试063】DNA序列

题目描述:

一个DNA序列由A/C/G/T四个字母的排列组合组成。G和C的比例(定义为GC-Ratio)是序列中G和C两个字母的总的出现次数除以总的字母数目(也就是序列长度)。在基因工程中,这个比例非常重要。因为高的GC-Ratio可能是基因的起始点。

给定一个很长的DNA序列,以及要求的最小子序列长度,研究人员经常会需要在其中找出GC-Ratio最高的子序列。

Java实现:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String str = sc.nextLine();
            int n = sc.nextInt();
            int beginIndex = 0;
            int max = 0;
            for (int i = 0; i <= str.length()-n; i++) {
                int count = 0;
                for (int j = i; j < i+n; j++) {
                    if (str.charAt(j) == 'G' || str.charAt(j) == 'C')
                        count++;
                }
                if (count > max) {
                    max = count;
                    beginIndex = i;
                }
            }
            System.out.println(str.substring(beginIndex, beginIndex+n));
        }
    }
}

关键点:

  • 子序列长度固定,不是最小

猜你喜欢

转载自blog.csdn.net/heyiamcoming/article/details/81098184