算法每一题,成长每一天~
C0E35 英文输入法
真题链接:【持续更新】2024华为 OD 机试E卷 机考真题库清单(全真题库)
思路
Java
package com.ccr.paper_f;
import java.util.*;
public class C0E35 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.nextLine();
String s = in.nextLine();
str = str.replaceAll("'", " "); // 先处理 don't这种,分为两个单词
String[] split = str.split(" "); // 按空格切分
Set<String> set = new HashSet<>();
for (String s1 : split) {
if (!isLetter(s1.charAt(s1.length() - 1))) { // 最后一个是标点,就去掉
s1 = s1.substring(0, s1.length() - 1);
}
set.add(s1); // set 会去掉重复单词
}
List<String> match = new ArrayList<>();
for (String s1 : set) {
if (s1.startsWith(s)) { // 前缀联想
match.add(s1);
}
}
if (match.isEmpty()) {
System.out.println(s);
} else {
match.sort(Comparator.naturalOrder());
for (String s1 : match) {
System.out.print(s1 + " ");
}
}
}
// 是字母
public static boolean isLetter(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
}
总结
~
算法要多练多练多练!!