题解 | #DNA序列#
DNA序列
https://www.nowcoder.com/practice/e8480ed7501640709354db1cc4ffd42a
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String a = in.nextLine(); int b = in.nextInt(); int gc = 0; String tmp = a.substring(0, b); for (int i = 0; i < b; i++) { if (tmp.charAt(i) == 'C' || tmp.charAt(i) == 'G') { gc++; } } int max = gc; int maxidx = 0; for (int i = b; i < a.length(); i++) { if (a.charAt(i) == 'C' || a.charAt(i) == 'G') { if (a.charAt(i - b) != 'C' && a.charAt(i - b) != 'G') { gc++; } }else if (a.charAt(i) != 'C' && a.charAt(i) != 'G') { if (a.charAt(i - b) == 'C' || a.charAt(i - b) == 'G') { gc--; } } if (gc > max) { max = gc; maxidx = i - b + 1; } } System.out.println(a.substring(maxidx, maxidx + b)); } }