题解 | #DNA序列#
DNA序列
http://www.nowcoder.com/practice/e8480ed7501640709354db1cc4ffd42a
//其实就是求CG出现最多的子串 ,所以要先截取出来子串,存储到list中
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String s = sc.next();
int n = sc.nextInt();
ArrayList<String> list = new ArrayList<>();
for(int i=0;i<=s.length()-n;i++){
//截取子串到与n范围内就可以,每次都是从当前那个数截取到后面n
String str = s.substring(i,i+n);
//System.out.println(str);
list.add(str);
}
int CGMax=0;
String str1 ="";
for(int i=0;i<list.size();i++){
String str2 =list.get(i);
int CGCount=0;
for(int j=0;j<str2.length();j++){
if(str2.charAt(j)=='C' || str2.charAt(j)=='G'){
CGCount++;
}
}
//遍历list中每个串,如果CG出现次数最多,则赋值到最终串
if(CGCount>CGMax){
CGMax = CGCount;
str1 = str2;
}
}
System.out.println(str1);
}
}
}