题解 | #DNA序列#
DNA序列
http://www.nowcoder.com/practice/e8480ed7501640709354db1cc4ffd42a
屎堆写法
import java.lang.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String in = sc.nextLine();
int n = sc.nextInt();//子串
List<String> list = new ArrayList<String>();
list = getAllChild(in,n);
float maxrat = 0;
int maxindex = 0;
boolean first = true;
for(int i=0;i<list.size();i++){
if(first){
maxrat = gcRatio(list.get(i),n);
maxindex = i;
first = false;
}else{
if(gcRatio(list.get(i),n)>maxrat){
maxrat = gcRatio(list.get(i),n);
maxindex = i;
}
}
}
System.out.println(list.get(maxindex));
}
public static List<String> getAllChild(String in,int len){
List<String> list = new ArrayList<String>();
for(int i = 0;i<in.length()-len+1;i++){
list.add(in.substring(i,i+len));
}
return list;
}
public static float gcRatio(String str,int len){
int count = 0;
for(int i =0;i<str.length();i++){
if(str.charAt(i)=='C'||str.charAt(i)=='G')
count++;
}
float rat = (float)count/(float)len;
return rat;
}
}