首页 > 试题广场 >

DNA序列

[编程题]DNA序列
  • 热度指数:124507 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

一个 DNA 序列由 A/C/G/T 四个字母的排列组合组成。 G 和 C 的比例(定义为 GC-Ratio )是序列中 G 和 C 两个字母的总的出现次数除以总的字母数目(也就是序列长度)。在基因工程中,这个比例非常重要。因为高的 GC-Ratio 可能是基因的起始点。

给定一个很长的 DNA 序列,以及限定的子串长度 N ,请帮助研究人员在给出的 DNA 序列中从左往右找出 GC-Ratio 最高且长度为 N 的第一个子串。
DNA序列为 ACGT 的子串有: ACG , CG , CGT 等等,但是没有 AGT , CT 等等

数据范围:字符串长度满足  ,输入的字符串只包含 A/C/G/T 字母

输入描述:

输入一个string型基因序列,和int型子串的长度



输出描述:

找出GC比例最高的子串,如果有多个则输出第一个的子串

示例1

输入

ACGT
2

输出

CG

说明

ACGT长度为2的子串有AC,CG,GT3个,其中AC和GT2个的GC-Ratio都为0.5,CG为1,故输出CG   
示例2

输入

AACTGTGCACGACCTGA
5

输出

GCACG

说明

虽然CGACC的GC-Ratio也是最高,但它是从左往右找到的GC-Ratio最高的第2个子串,所以只能输出GCACG。    
public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) { // 注意 while 处理多个 case
            String str = in.nextLine();
            int n = in.nextInt();
            int max = 0;
            String maxChild = "";
            for(int i=0;i+n<=str.length();i++){
                String child = str.substring(i,i+n);
                String temp = child.replaceAll("C","").replaceAll("G","");
                int len = child.length() - temp.length();
                if(max < len || "".equals(maxChild)){
                    max = len;
                    maxChild = child;
                }
            }

            System.out.println(maxChild);
        }
    }
发表于 2023-09-12 08:19:08 回复(0)
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();
        int n = Integer.parseInt(br.readLine());

        // 业务场景为存取有序,所以使用linkedHashMap
        LinkedHashMap<String, Double> map = new LinkedHashMap<>();
        for (int i = 0; i <= line.length() - n; i++) {
            String sub = line.substring(i, i + n);
            map.put(sub, getGCRatio(sub));
        }

        // 计算最大值
        Collection<Double> values = map.values();
        double max = Collections.max(values);

        // 遍历找出第一个gc最大的字符串
        for (Map.Entry<String, Double> entry : map.entrySet()) {
            double value = entry.getValue();
            String key = entry.getKey();
            if (value == max) {
                System.out.println(key);
                break;
            }
        }
    }


    /**
     * 计算一个字符串GC的比例
     *
     * @param line
     * @return
     */
    private static double getGCRatio(String line) {
        int count = 0;
        for (int i = 0; i < line.length(); i++) {
            char c = line.charAt(i);
            if (c == 'C' || c == 'G') {
                count++;
            }
        }

        return count * 1.0 / line.length();
    }
}

发表于 2023-08-11 10:15:19 回复(0)
咱比较菜啊,也就是按照给定的长度来不定的截取字符串,然后在截取到的字符串里计算GC-Ratio,把得到的GC-Ratio值存储在数组里,最后获取数组最大值索引,根据索引截取字符串输出就好。
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String dna = in.next();
            int n = in.nextInt();
            System.out.println(highRate(dna, n));
        }
    }
    public static String highRate(String s, int n) {
        int size = s.length();
        int[] count = new int[size - n + 1];
        for (int i = 0; i < size - n + 1; i++) {
            for (int j = i; j < n + i; j++) {
                if (s.charAt(j) == 'C' || s.charAt(j) == 'G') {
                    count[i]++;
                }
            }
        }
        int index = getMaxIndex(count);
        String res = s.substring(index, index + n);
        return res;
    }
    public static int getMaxIndex(int[] arr) {
        if (arr == null || arr.length == 0) {
            throw new IllegalArgumentException("数组不能为空");
        }

        int maxIndex = 0;
        int maxValue = arr[0];

        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > maxValue) {
                maxValue = arr[i];
                maxIndex = i;
            }
        }

        return maxIndex;
    }
}

发表于 2023-06-18 22:41:19 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String str=in.nextLine();
        int num=in.nextInt();
        double maxRatio=0.0;
        //符合题目答案要求的开始索引
        int index=0;
        //计算答案
        for(int i=0;i<str.length()-num;i++){
            String strTmp=str.substring(i,i+num);
            //G,C出现的次数
            int sum=0;
            //求当前的GC-Ratio
            double ratio=0.0;
            for(int j=0;j<strTmp.length();j++){
                if(strTmp.charAt(j)=='G'){
                    sum++;
                }
                if(strTmp.charAt(j)=='C'){
                    sum++;
                }
            }
            //获取GC-Ratio
            ratio=Double.valueOf(sum)/Double.valueOf(num);
            //如果GC-Ratio大于最大GC-Ratio,就更新
            if(ratio>maxRatio){
                index=i;
                maxRatio=ratio;
            }
        }
        System.out.print(str.substring(index,index+num));
    }
}

发表于 2023-06-04 18:32:35 回复(0)
//使用前缀和优化,时间复杂度O(N)


import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String dna = in.next();
        int n = in.nextInt();
        int [] prefixSum = new int[dna.length()+1];
        for(int i=0; i<dna.length(); i++){
            if(dna.charAt(i) == 'G' || dna.charAt(i) == 'C'){
                prefixSum[i+1] = prefixSum[i] + 1;
            }else{
                prefixSum[i+1] = prefixSum[i];
            }
        }
        int maxStartIndex = 0, maxGCNum = 0;
        for(int i=0; i<=dna.length()-n; i++){
            if(prefixSum[i+n]-prefixSum[i] > maxGCNum){
                maxGCNum = prefixSum[i+n]-prefixSum[i];
                maxStartIndex = i;
            }
        }
        System.out.println(dna.substring(maxStartIndex,maxStartIndex+n));

    }
}

发表于 2023-04-27 12:54:45 回复(0)
Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String str = in.next();
            int n = in.nextInt();
            int strLen = str.length();
            TreeMap<Integer,String> treeMap = new TreeMap<Integer,String>();
            for(int i=0;i<strLen;i++){
                for(int j=strLen;j>i;j--){
                    if(j-i==n){
                        String cutStr = str.substring(i,j);
                        if(!(cutStr.contains("CG") || cutStr.contains("GC"))){
                            continue;
                        }
                        int num=0;
                        for(int k=0;k<cutStr.length();k++){
                            if(cutStr.charAt(k)=='C' || cutStr.charAt(k)=='G'){
                                num++;
                            }
                        }
                        if(!treeMap.containsKey(num)){
                             treeMap.put(num,cutStr);
                        }
                    }
                }
            }
            System.out.println(treeMap.lastEntry().getValue());
        }
发表于 2023-04-16 19:45:54 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String s = reader.readLine();
        int k = Integer.parseInt(reader.readLine());
        Main main = new Main();
        List<String> list = new ArrayList<>();
        main.getSubStr(s, list,k);
        int[] ratio = new int[list.size()];
        int max = 0;
        for (int i = 0; i < list.size(); i++) {
            int num = 0;
            String temp = list.get(i);
            for (int j = 0; j < temp.length(); j++) {
                if (temp.charAt(j) == 'G' || temp.charAt(j) == 'C') {
                    num++;
                }
            }

            max = Math.max(max, num);
            ratio[i] = num;
        }
        for (int i = 0; i < ratio.length; i++) {
            if (max == ratio[i]) {
                System.out.println(list.get(i));
                break;
            }
        }
    }

    public void getSubStr(String s, List<String> list, int k) {
        for (int i = 0; i <= s.length() - k; i++) {
            list.add(s.substring(i, i + k));
        }
    }
}

发表于 2023-04-06 15:50:20 回复(0)
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        int n = sc.nextInt();
        // 注意:这里要用LinkedHashMap,HashMap是无序的
        Map<String, Double> map = new LinkedHashMap<>();
        for (int i = 0; i <= str.length() - n; i++) {
            String s = str.substring(i, i + n);
            // 被除数+0.0转为double
            map.put(s, (s.length() - s.replaceAll("[GC]", "").length() + 0.0) / n);
        }
        Double max = map.values().stream().sorted(Comparator.comparingDouble(Double::doubleValue).reversed()).findFirst().get();
        Set<Map.Entry<String, Double>> entries = map.entrySet();
        for (Map.Entry<String, Double> entry : entries) {
            if (max.compareTo(entry.getValue()) == 0) {
                System.out.println(entry.getKey());
                break;
            }
        }
    }
}

发表于 2023-02-28 09:33:46 回复(0)
双指针,字符串长度一定,就是看哪段包含的CG多,一起右移记录个数
import java.io.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s;
        while ((s = br.readLine()) != null) {
            int n = Integer.parseInt(br.readLine());
            // int[] cg = new int[s.length()];
            int count = 0;
            for (int i = 0; i < n; i++) {
                char c = s.charAt(i);
                if (c == 'G' || c == 'C') count++;
            }
            // cg[n-1] = count;
            int res = count;
            int maxIndex = n - 1;
            int l = 0, r = n;
            while (r < s.length()) {
                char cl = s.charAt(l);
                char cr = s.charAt(r);
                if (cr == 'C' || cr == 'G') {
                    if (cl != 'C' && cl != 'G') count++;
                } else {
                    if (cl == 'C' || cl == 'G') count--;
                }
                if (count > res) {
                    res = count;
                    maxIndex = r;
                }
                l++;
                r++;
            }
            System.out.println(s.substring(maxIndex + 1 - n, maxIndex + 1));
        }
    }
}


发表于 2022-09-30 22:25:44 回复(0)
使用LinkedHashMap解决
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.*;

public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String originalStr = br.readLine();
        int len = Integer.parseInt(br.readLine());
        LinkedHashMap<String, Double> hashMap = new LinkedHashMap<>();
        for(int i = 0; i < originalStr.length() - len + 1; i++){
            String temp = originalStr.substring(i, i + len);
            double count = 0;
            for(char ch : temp.toCharArray()){
                if(ch == 'C' || ch == 'G'){
                    count++;
                }
            }
            double ratio = count / len;
            hashMap.put(temp, ratio);
        }
        double max = 0.0;
        String result = null;
        for(String key : hashMap.keySet()){
            if(hashMap.get(key) > max){
                max = hashMap.get(key);
                result = key;
            }
        }
        System.out.println(result);
    }
}


发表于 2022-08-16 15:24:50 回复(0)
import java.util.*;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        int num = sc.nextInt();
        String result = "";
        int total = 0;
        for(int i = 0; i < str.length() - num + 1; i++){
            String temp = str.substring(i,i+num);
            int nn = 0;
            for(int j = 0; j < temp.length(); j++){
                if(temp.charAt(j) == 'C' || temp.charAt(j) == 'G'){
                    nn++;
                }
            }
            if(total < nn){
                total = nn;
                result = temp;
            }
        }
        System.out.println(result);
    }
}

发表于 2022-07-28 17:36:23 回复(0)
标准的滑动窗口 ,Java实现
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String dna = sc.nextLine();
        int n = Integer.parseInt(sc.nextLine());


        List<Character> window = new ArrayList<>();
        //一个窗口内 gc的个数
        int currWindowGcCount = 0;
        int maxGcCount;
        String maxGcStr;

        //先把开始的 n个 加入到窗口中
        for (int i = 0; i < n ; i++) {
            if ('G' == dna.charAt(i) || 'C' == dna.charAt(i)) {
                currWindowGcCount++;
            }
            window.add(dna.charAt(i));
        }

        //当前第一个就是最大 gc
        maxGcCount = currWindowGcCount;
        maxGcStr = characterToString(window);

        //从第 n 个开始滚动窗口
        for (int i = n; i < dna.length(); i++) {
            // 往后滚动,如果是GC 则当前窗口GC数+1
            if ('G' == dna.charAt(i) || 'C' == dna.charAt(i)) {
                currWindowGcCount++;
            }
            window.add(dna.charAt(i));
            //第一个元素出队 ,如果是 GC,则窗口GC 数-1
            if ('G' == window.get(0) || 'C' == window.get(0)){
                currWindowGcCount--;
            }
            window.remove(0);
            if (currWindowGcCount >= maxGcCount){
                maxGcStr = characterToString(window);
                maxGcCount = currWindowGcCount;
            }
        }
        System.out.println(maxGcStr);
    }

    public static String characterToString(List<Character> characters){
        StringBuilder stringBuilder = new StringBuilder();
        characters.stream().forEach(stringBuilder::append);
        return stringBuilder.toString();
    }
}


发表于 2022-06-27 11:23:17 回复(1)
思路| 题解:
子串长度固定为n,从右往左依次循环长度为n的子串并记录其中C/G的个数,并维护好right索引值,以便取出最左侧的符合要求的子串


/***
子串长度固定为n,从右往左依次循环长度为n的子串并记录其中C/G的个数,并维护好right索引值,以便取出最左侧的符合要求的子串
***/
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) { 
            String str = sc.nextLine();
            int n = sc.nextInt();
            int max = 0;
            int right= 0; 
            //1.从右往左,固定长度为n,进行循环
            for(int i = str.length()-1; i-n>=-1; i--){
                char[] ch = str.substring(i-n+1,i+1).toCharArray();
                // 2.统计'C' 和 'G'出现的次数
                int count = 0; 
                for(char c : ch){
                    if(c == 'C' || c == 'G'){
                        count = count + 1;
                    }
                }
                //3.维护right坐标:
                //3.1若count超过已存储的max值,则将其存储到max里,同时right坐标也要更新
                if(count >= max){
                    max = count;
                    right = i;
                //3.2若count不超过已存储的max值,则right保持不变
                }else{
                    right = right;
                }
            }
            System.out.println(str.substring(right-n+1,right+1));
        }
    }
}


发表于 2022-05-03 10:20:30 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = null;
        while ((str = br.readLine()) != null){
            // DNA长度
            int DNAlen = str.length();
            // 限定的子串长度 N
            Integer n = Integer.parseInt(br.readLine());
            // 限定的子串
            List<String> dnaChildStr = new ArrayList<>();
            // 子串切割
            int index = 0;
            while ((index+n) < DNAlen){
                dnaChildStr.add(str.substring(index,index+n));
                index++;
            }
            // 获取GC-Ratio最高且长度为N的第一个子串
            int rowIn = 0;
            // GC-Ratio最高且长度为N的第一个子串
            BigDecimal maxGcRatio= BigDecimal.ZERO;
            for (int i = 0; i < dnaChildStr.size(); i++) {
                char[] chars = dnaChildStr.get(i).toCharArray();
                int GC = 0;
                for (int j = 0; j < chars.length; j++) {
                    if (chars[j] == 'C' || chars[j] == 'G'){
                        GC++;
                    }
                }
                // DNA子串GC-Ratio
                BigDecimal GcRtaio =new BigDecimal(GC).divide(new BigDecimal(n),5,BigDecimal.ROUND_HALF_UP);
                if (maxGcRatio.compareTo(GcRtaio) == -1){
                    maxGcRatio = GcRtaio;
                    rowIn = i;
                }
            }
            // 打印GC-Ratio最高且长度为N的第一个子串
            String dnaStr = null;
            if (rowIn == 0 && DNAlen == n){
                dnaStr = str;
            }else {
                dnaStr = dnaChildStr.get(rowIn);
            }
            System.out.println(dnaStr);
        }
    }
}

发表于 2022-04-30 22:42:33 回复(0)
应该算是动态规划吧(笑)
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String dna = sc.nextLine();
        int num = sc.nextInt();
        int[] dp = new int[dna.length()];
        
        if(dna.charAt(0) == 'C' || dna.charAt(0) == 'G') dp[0] = 1;
        else dp[0] = 0;
        for(int i=1; i<num; i++){
            char c = dna.charAt(i);
            if(c == 'C' || c == 'G') dp[i] = dp[i-1]+1;
            else dp[i] = dp[i-1];
        }
        
        for(int i=num; i<dna.length(); i++){
            char head = dna.charAt(i);
            char tail = dna.charAt(i-num);
            if(head == 'C' || head == 'G') dp[i] = dp[i-1]+1;
            else dp[i] = dp[i-1];
            if(tail == 'C' || tail == 'G') dp[i] -= 1;
        }
        
        int maxValue = Integer.MIN_VALUE;
        int maxIndex = Integer.MIN_VALUE;
        for(int i=num-1; i<dna.length(); i++){
            if(dp[i]>maxValue){
                maxValue = dp[i];
                maxIndex = i;
            }
        }
        System.out.println(dna.substring(maxIndex-num+1, maxIndex+1));
    }
}


发表于 2022-04-28 22:00:59 回复(0)
import java.util.LinkedHashMap;
import java.util.Scanner;

/**
 * 题目:DNA序列
 * 一个 DNA 序列由 A/C/G/T 四个字母的排列组合组成。 G 和 C 的比例(定义为 GC-Ratio )是序列中 G 和 C 两个字母的总的出现次数除以总的字母数目(也就是序列长度)。在基因工程中,这个比例非常重要。因为高的 GC-Ratio 可能是基因的起始点。
 *  给定一个很长的 DNA 序列,以及限定的子串长度 N ,请帮助研究人员在给出的 DNA 序列中从左往右找出 GC-Ratio 最高且长度为 N 的第一个子串。
 * DNA序列为 ACGT 的子串有: ACG , CG , CGT 等等,但是没有 AGT , CT 等等
 */
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        int n = scanner.nextInt();

        //找出子字符串中,CG个数最多的
        int maxCount = 0;
        String substring = "";
        LinkedHashMap<Integer, String> map = new LinkedHashMap<>();


        if(str.length() == n){
            System.out.println(str);
            return;
        }

        for (int i = 0; i < str.length()-n; i++) {
             substring = str.substring(i, i+n);
//             maxCount = Math.max(maxCount,getCount(substring));
            int count = getCount(substring);
            //不能=,不然会被替换掉
            if(count > maxCount){
                map.put(count,substring);
                maxCount = count;
            }
        }


        System.out.println(map.get(maxCount));



    }

    public static int getCount(String str){
        int count = 0;
        for (char c : str.toCharArray()) {
            if(c == 'C' || c == 'G'){
                count++;
            }
        }
        return count;
    }
}
我感觉比其他题难多了
发表于 2022-04-23 21:49:11 回复(0)
/**
 * @author zhouheng
 * @date 2022年03月28日 14:27
 */



import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

public class Main {
    private static final int NUM = 3;

    public static void main(String[] args) {
        Scanner scanner =new Scanner(System.in);
//        while (scanner.hasNext()) {
//            String line = scanner.nextLine();
//
//        }

        Map<Integer , Integer> map = new HashMap<>();
        String line = scanner.nextLine();
        Integer num = scanner.nextInt();
        for (int i = 0; i < line.length()-num; i++) {
            String substring = line.substring(i, i+num);
            if (substring.equals("AGT") || substring.equals("CT")) {
                continue;
            }
            int cgNum = calcSubStrRate(substring);
            map.put(i, cgNum);
        }

        Collection<Integer> values = map.values();
        IntSummaryStatistics collect = values.stream()
            .collect(Collectors.summarizingInt(Integer::intValue));

        int max = collect.getMax();
        // sort
        List<Integer> list = new ArrayList();
        map.forEach((key, val) -> {
            if (val == max){
                list.add(key);
            }
        });
        Optional<Integer> min = list.stream().min(Comparator.comparing(Integer::intValue));
        int index = 0;
        if (min.isPresent()) {
            index = min.get();
        }

        System.out.println(line.substring(index, index+num));
    }

    private static int calcSubStrRate(String substring) {
        // c包含C G的个数
        int containCGnum = 0;
        char[] chars = substring.toCharArray();
        for (char aChar : chars) {
            if (aChar == 'C' || aChar == 'G') {
                containCGnum++;
            }
        }

        return containCGnum;
    }

   
}

发表于 2022-03-29 23:08:39 回复(0)
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        int n = sc.nextInt();
        
        ArrayList<String> list = new ArrayList<>();
        for(int i = 0 ; i < str.length() - n + 1 ; i ++){
            String temp = str.substring(i,i+n);
            list.add(temp);
        }
        
        ArrayList<Double> ratio = new ArrayList<>();
        for(int i = 0 ; i < str.length() - n + 1 ; i ++){
            double count = 0;
            String temp = list.get(i);
            for(int j = 0 ; j < n ; j ++){
                if(temp.charAt(j) == 'G' || temp.charAt(j) == 'C') count++;
            }
            double res = count / n;
            ratio.add(res);
        }
        
        int index = 0;
        double max = 0;
        for(int i = 0 ; i < str.length() - n + 1 ; i ++){
            if(ratio.get(i) > max){
                max = Math.max(max,ratio.get(i));
                index = i;
            }
        }
        
        System.out.print(list.get(index));
    }
}

发表于 2022-03-26 16:22:40 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args){
        Scanner in=new Scanner(System.in);
        String str=in.nextLine();
        int len=in.nextInt();
        float[] num=new float[str.length()-len];
        for(int i=0;i<str.length()-len;i++){
            String temp=str.substring(i,i+len);
            int appear=findAppear(temp);
            num[i]=(float)appear/len;
        }
        float max=0;
        int index=0;
        for(int i=0;i<str.length()-len;i++){
            if(num[i]>max){
                max=num[i];
                index=i;
            }
        }
        String res=str.substring(index,index+len);
        System.out.print(res);
    }
    public static int findAppear(String temp){
        int apNums=0;
        for(int i=0;i<temp.length();i++){
            char ca=temp.charAt(i);
            if(ca=='C'||ca=='G') apNums++;
        }
        return apNums;
    }
}
发表于 2022-03-23 10:40:01 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            String s = sc.nextLine();
            int n = sc.nextInt();
           //if (s.length()<n)   System.out.println(s);
            int lg = 0;
            int max = 0;
            int pos = 0; //右边界
            for (int i = 0; i<s.length(); i++) {
                char cur = s.charAt(i);
                if (cur=='C'||cur =='G') lg++;
                if (i >=n) {
                    char last = s.charAt(i-n);
                    if (last=='C'||last =='G') lg--;
                    
                }
                if (lg > max) {
                        max = lg;
                        pos = i+1;
                    }
               
            }
             if (pos <= n)  System.out.println(s.substring(0,n));
            else  System.out.println(s.substring(pos-n, pos));
           
        }
    }
}

发表于 2022-03-16 00:17:02 回复(0)

问题信息

难度:
45条回答 30557浏览

热门推荐

通过挑战的用户

查看代码