小红背单词

实现代码:

java 复制代码
 public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        scanner.nextLine(); // 读掉换行符
        
        Map<String, Integer> countMap = new HashMap<>();
        Set<String> remembered = new HashSet<>();
        int k = 0; // 当前记住的单词数
        
        for (int i = 0; i < n; i++) {
            String word = scanner.nextLine();
            
            if (remembered.contains(word)) {
                continue; // 已经记住的单词,直接跳过
            }
            
            // 更新该单词的计数
            int count = countMap.getOrDefault(word, 0) + 1;
            countMap.put(word, count);
            
            // 检查是否满足记住条件
            if (count == k + 1) {
                remembered.add(word);
                k++;
                // 可以移除 countMap 中的记录,因为之后遇到会跳过;不移除也不影响
            }
        }
        
        System.out.println(k);
    }
相关推荐
程序员黑豆5 小时前
Java 注释详解:单行、多行与文档注释的完整指南
java·前端·ai编程
江畔柳前堤6 小时前
大语言模型分布式训练:从并行策略到万卡工程的系统梳理
人工智能·分布式·深度学习·算法·目标检测·机器学习·语言模型
hey_sml6 小时前
JAVA每日一学---CompletableFuture中thenApply与thenCompose区别详解
java·开发语言
Doraemomo6 小时前
数据结构-环形链表
java·数据结构·链表
Forever Nore7 小时前
LeetCode 4 寻找两个正序数组的中位数 - 二分
算法·leetcode
萧瑟余晖7 小时前
Java深入解析篇十七之SpringCloud
java·开发语言
是未才8 小时前
从输入 URL 到页面返回:DNS、路由、TLS 与 HTTP 完整链路
java·后端·计算机网络
ttod_qzstudio8 小时前
Java 常用语法极简通关(五):类与对象——字段、方法、构造器、this 与 static
java·开发语言·python
罗西的思考8 小时前
【OpenClaw具身硬件】MiniClaw 阅读笔记---(1)基础
人工智能·算法·机器学习
蛋先生DX9 小时前
大模型参数存储格式揭秘:BF不是男朋友
深度学习·算法·llm