华为OD机考-英文输入法-逻辑分析(JAVA 2025B卷)


java 复制代码
import java.util.*;

public class EnglishSpell {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextLine()){
            String line = sc.nextLine();
            // don't-> don t
            if(line.contains("'")){
                line  = line.replace("'", " ");
            }
            String[] source = line.split("\\s+");
            String target = sc.nextLine();
            test(source,target);
        }
    }

    private static void test(String[] source,String target){
        HashSet<String> set = new HashSet<>();
        for(String s:source){
            // delete comma and dot
            if(!Character.isLetter(s.charAt(s.length()-1))){
                set.add(s.substring(0,s.length()-1));
            }else{
                set.add(s);
            }
        }
        List<String> sb = new ArrayList<>();
        for(String s:set){
            // find target word
            if(s.startsWith(target)){
                sb.add(s);
            }
        }
        if(sb.isEmpty()){
            System.out.println(target);
            return;
        }
        sb.sort(String::compareTo);
        for(int i=0;i<sb.size();i++){
            System.out.print(sb.get(i));
            if(i!=sb.size()-1){
                System.out.print(" ");
            }
        }
    }
}
相关推荐
lly20240643 分钟前
jQuery Mobile 表格
开发语言
惊讶的猫1 小时前
探究StringBuilder和StringBuffer的线程安全问题
java·开发语言
jmxwzy1 小时前
Spring全家桶
java·spring·rpc
Halo_tjn1 小时前
基于封装的专项 知识点
java·前端·python·算法
m0_748233171 小时前
30秒掌握C++核心精髓
开发语言·c++
Fleshy数模2 小时前
从数据获取到突破限制:Python爬虫进阶实战全攻略
java·开发语言
Duang007_2 小时前
【LeetCodeHot100 超详细Agent启发版本】字母异位词分组 (Group Anagrams)
开发语言·javascript·人工智能·python
像少年啦飞驰点、2 小时前
零基础入门 Spring Boot:从“Hello World”到可上线的 Web 应用全闭环指南
java·spring boot·web开发·编程入门·后端开发
苍煜2 小时前
万字详解Maven打包策略:从基础插件到多模块实战
java·maven
froginwe112 小时前
Redis 管道技术
开发语言