题解 | #DNA序列#
DNA序列
https://www.nowcoder.com/practice/e8480ed7501640709354db1cc4ffd42a
定长的滑动窗口
import java.io.IOException; import java.util.*; public class Main { public static void main(String[] args) throws IOException { Scanner scanner = new Scanner(System.in); char[] chars = scanner.nextLine().toCharArray(); int n = scanner.nextInt(); String maxStr = ""; int lastMaxCount = 0; int curCount = 0; for (int start = 0; start <= chars.length - n; start++) { int end = start + n - 1; if (start == 0) { for (int j = start; j <= end; j++) { if (isGC(chars[j])) { curCount++; } } } else { if (isGC(chars[start - 1])) { curCount--; } if (isGC(chars[end])) { curCount++; } } if (curCount > lastMaxCount) { lastMaxCount = curCount; maxStr = new String(chars, start, end - start + 1); } } System.out.println(maxStr); } static boolean isGC(char c) { return c == 'G' || c == 'C'; } }