题解 | #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;
}
}
}
}
}

