解法:简单语法题
package com.sky;
import java.util.*;
public class Test1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int M = sc.nextInt();
List<String> words = new ArrayList<>(N);
for (int i = 0; i < N; i++) {
String word = sc.next();
words.add(word);
}
Comparator<String> customComparator = (s1, s2) -> {
boolean s1Contains = s1.contains("PDD");
boolean s2Contains = s2.contains("PDD");
if (s1Contains && !s2Contains) {
return -1;
} else if (!s1Contains && s2Contains) {
return 1;
} else {
return s1.compareTo(s2);
}
};
Collections.sort(words, customComparator);
for (int i = 0; i < M; i++) {
System.out.println(words.get(i));
}
}
}