题解 | #DNA序列#

DNA序列

https://www.nowcoder.com/practice/e8480ed7501640709354db1cc4ffd42a

import java.util.*;

// 奥里给
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            String next = scanner.nextLine();
            int n = scanner.nextInt();
            String[] split = next.split("");
            LinkedHashMap<String, Integer> lhm = new LinkedHashMap<>();
            for (int i = 0; i <split.length; i++) {
                if (split[i].matches("[CG]{1}")){
                    String five="";
                    if (i+n<= split.length) {
                        five = next.substring(i, i + n);
                    }else {
                        five = next.substring(i);
                    }
                    String[] split1 = five.split("");
                    int value=0;
                    for (int j = 0; j < split1.length; j++) {
                        if (split1[j].equals("C")||split1[j].equals("G")){
                            value=value+1;
                        }
                    }
                    lhm.put(five,value);
                }
            }
            ArrayList<Integer> integers = new ArrayList<>();
            for (String key:lhm.keySet()){
                integers.add(lhm.get(key));
            }
            Collections.sort(integers);
            for (String key:lhm.keySet()){
                if (lhm.get(key)==integers.get(integers.size()-1)){
//                    System.out.println(key);
                    String result = "";
                    char[] chars = key.toCharArray();
                    for (int i = chars.length - 1; i >= 0; i--) {
                        result = result + chars[i];
                    }
//                    System.out.println("颠倒"+result);
                    String[] split2 = result.split("");
                    int sub=0;
                    for (int i = 0; i <split2.length; i++) {
                        if (!split2[i].matches("[C]")&&!split2[i].matches("[G]")){
                            sub=sub+1;
                        } else if (split2[i].matches("[C]")||split2[i].matches("[G]")) {
                            break;
                        }
                    }
//                    System.out.println(sub);
                    key=key.substring(0,key.length()-sub);
//                    System.out.println("原初"+key);
                    String[] split3 = next.split(key);
                    String s ="";
                    String s1 ="";//右侧拆分
                    if (split3.length!=0) {
                        s = split3[0];
                        s1= split3[1];
                    }
//                    System.out.println("左侧拆分"+s);
                    String success="";
                    int count=0;
                    if (!s.equals("")) {
                        for (int i = s.length() - 1; i >= 0; i--) {
                            count++;
                            if (count <= sub) {
                                key = s.charAt(i) + key;
                            } else {
                                break;
                            }
                        }
                    }

                    if (key.length()<n){
                        key=key+s1.substring(0,0+(n- key.length()));
                    }
                    System.out.println(key);
                    break;
                }
            }
            
        }
    }
}

全部评论

相关推荐

10-14 23:01
已编辑
中国地质大学(武汉) Java
CUG芝士圈:虽然是网上的项目,但最好还是包装一下,然后现在大部分公司都在忙校招,十月底、十一月初会好找一些。最后,boss才沟通100家,别焦虑,我去年暑假找第一段实习的时候沟通了500➕才有面试,校友加油
点赞 评论 收藏
分享
爱看电影的杨桃allin春招:我感觉你在炫耀
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务