DNA序列, 华为, 双指针算法

题目描述存在错误, 应该是固定子序列长度。
双指针算法,时间复杂度O(N)。

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            char[] s = sc.next().toCharArray();
            int n = s.length;
            int m = sc.nextInt();
            if(m > n) m = n;
            int cnt = 0, maxL = 0, start = 0;
            for(int i=0; i < n; i++) {
                if(s[i] == 'G' || s[i] == 'C') cnt ++;
                if(i >= m && (s[i-m] == 'G' || s[i-m] == 'C')) cnt --;
                if(cnt > maxL) {
                    maxL = cnt; 
                    start = Math.max(0,i-m+1); // 一定注意
                }
            }
            for(int i=start; i < Math.min(start + m, n); i++)
                System.out.print(s[i]);
            System.out.println();
        }
    }
}
原文地址:https://www.cnblogs.com/lixyuan/p/13263461.html