华为OJ自测系统
一个DNA序列由A/C/G/T四个字母的排列组合组成。G和C的比例(定义为GC-Ratio)是序列中G和C两个字母的总的出现次数除以总的字母数目(也就是序列长度)。在基因工程中,这个比例非常重要。因为高的GC-Ratio可能是基因的起始点。
给定一个很长的DNA序列,以及要求的最小子序列长度,研究人员经常会需要在其中找出GC-Ratio最高的子序列。
package com.guoguo.basic; import java.util.Scanner; //enum Word {C,G,default} public class DNAList { public static void main(String[] args){ Scanner scan = new Scanner(System.in); String str = scan.next(); int len = scan.nextInt(); int max = 0; int k = 0; String str1 = "";
for(int i=0; i<str.length()-5; i++){ int num = 0; for(int j=i; j<i+len; j++){ // if(str.charAt(j)=='G' || str.charAt(j)=='C') // num++; // char ch = str.charAt(j); // Word w = Word.default; // if(ch == 'C') // w = Word.C; // if(ch == 'G') // w = Word.G; // switch(w){ switch(str.charAt(j)){ case 'C': case 'G': {num++;break;} default: break; } } if(num > max){ max = num; k = i; } } for(int n=k; n<k+len; n++){ str1 += str.charAt(n); } System.out.println(str1); } }