首页 > 试题广场 >

小红背单词

[编程题]小红背单词
  • 热度指数:2820 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
小红每天都要背单词,然后她会把每天记住了多少单词记录下来,并在小红书上打卡。

当小红背单词时,如果她已经记住了i个单词,且背了一个没有记住的新单词i+1次,则她就会记住这个新的单词。
例如,当她按顺序背["you","thank","thank"]时,她第一次背单词"you"时她就能记住"you"。而由于她已经记住了一个单词,所以需要背两次"thank"才能记住"thank"。

现在你知道了小红背单词的顺序,请你求出小红今天记住了多少个单词。

输入描述:
第一行一个整数n(1 \leq n \leq 10000)
接下来n行,每行一个字符串,保证每个字符串长度不超过 10。


输出描述:
输出一个整数,表示她记住了多少个单词。

示例1

输入

5
you
thank
queue
queue
thank

输出

2

说明

小红先记住了单词"you",又因为背了两次"queue",于是记住了单词"queue"。由于已经记住了两个单词,所以背两次"thank"还不能让小红记住。
def main():
    n = int(input())
    arr = []
    for _ in range(n):
        tmp = input()
        arr.append(tmp)
    cur = 1
    set_ = set()
    cnt = {}
    ans = 0
    for i in range(n):
        s = arr[i]
        cnt[s] = cnt.get(s,0) + 1
        if cnt.get(s) == cur and not s in set_:
            set_.add(s)
            ans += 1
            cur += 1
    print(ans)

        

if __name__ == '__main__':
    main()

发表于 2025-11-25 11:46:53 回复(0)
import java.util.*;

public class Main {
    public static void main(String[] args) {
        // 创建Scanner对象,用于读取输入数据
        Scanner in = new Scanner(System.in);
        // 读取第一行输入的整数n,代表小红背诵单词的总次数
        int n = in.nextInt();

        // 定义HashSet集合,存储已经记住的单词(HashSet查询效率高,适合判断是否存在)
        Set<String> knownWords = new HashSet<>();
        // 定义HashMap,存储"未记住但已背诵过"的单词及其累计背诵次数(键:单词,值:累计次数)
        Map<String, Integer> countWords = new HashMap<>();

        // 循环遍历每一次背诵的单词(共n次)
        for (int i = 0; i < n; i++) {
            // 读取当前背诵的单词
            String word = in.next();
            
            // 若当前单词已在"记住的单词"集合中,直接跳过(无需再统计次数)
            if (knownWords.contains(word)) {
                continue;
            }

            // 若单词未记住:更新其累计背诵次数
            // getOrDefault(word, 0):若单词已在map中,取当前次数;否则取默认值0,之后+1
            countWords.put(word, countWords.getOrDefault(word, 0) + 1);

            // 获取当前已记住的单词总数k(记忆新单词的要求是:累计次数 ≥ k+1)
            int k = knownWords.size();
            // 检查当前单词的累计次数是否达到记忆要求
            if (countWords.get(word) >= k + 1) {
                // 达到要求:将单词加入"记住的单词"集合
                knownWords.add(word);
            }
        }

        // 输出最终记住的单词总数(即knownWords集合的大小)
        System.out.println(knownWords.size());
    }
}

发表于 2025-08-31 16:39:28 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
     public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = Integer.parseInt(in.nextLine());

        // 存储已经记住的单词
        Set<String> mem = new HashSet<>();
        // 记录单词背诵次数
        Map<String, Integer> cntMap = new HashMap<>();
        for (int i = 0; i < n; i++) {
            String word = in.nextLine();
            // 已经记住不再重复记
            if (mem.contains(word)) {
                continue;
            } else {
                int cnt = cntMap.getOrDefault(word, 0) + 1;
                // 记住单词 word
                if (cnt > mem.size()) {
                    mem.add(word);
                } else {
                    cntMap.put(word, cnt);
                }
            }
        }
        System.out.println(mem.size());
    }

}
发表于 2025-08-14 16:58:51 回复(0)
#include <iostream>
#include <unordered_map>
using namespace std;

int main() {
    int n;
    cin>>n;
    string s;
    int c=0;
    unordered_map<string, int> mp;
    unordered_map<string, int> fg;

    if (n>=1) {
        cin>>s;
        c=1;
        fg[s]=1;
        mp[s]++;
    }
    n=n-1;

    while (n--) {
        cin>>s;
        mp[s]++;
        if(mp[s]==c+1){
            if(fg[s]==0){
              c++;
              fg[s]=1;
            }

        }
    }
    cout<<c;
}
发表于 2025-08-06 14:40:09 回复(0)
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;

int main() {
    int n;
    cin >> n;
    string str;
    int num = 0;
    vector<string> arr;
    while (n --) {
        cin >> str;
        arr.push_back(str);
    }
    unordered_map<string, int>  m_map;
    for(int i = 0; i < arr.size(); i++) {
        m_map[arr[i]] ++;
        if(arr[i] != arr[i+1] && m_map[arr[i]] > num) {
            num++;
        }
    }
    cout << num;
}
// 64 位输出请用 printf("%lld")

发表于 2025-10-30 11:22:56 回复(0)
#include <iostream>
#include <unordered_map>
using namespace std;

int main() {
    unordered_map<string, int> dic;
    unordered_map<string, bool> remember;
    int n, cnt = 0;
    cin >> n;
    while(n--){
        string temp;
        cin >> temp;
        dic[temp]++;
        if(dic[temp] > cnt && remember[temp] == false){
            cnt++;
            remember[temp] = true;
        }
    }
    cout << cnt;
    return 0;
}
// 64 位输出请用 printf("%lld")
通不过试试下面的数据:
5
y
y
y
y
y
输出1
所以不要忘了已经记住的单词不应因为再背而重复计数
发表于 2025-10-25 01:32:20 回复(0)
这题是不是有问题啊?
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();

        int k=1;
        int sum =0;

        for(int i=0; i<n; i++){
            k++;
            sum = sum+k;
            if(sum>=n){
                break;
            }
        }
        System.out.println(k-1);

    }
}


发表于 2025-10-08 15:59:35 回复(1)
//程序已通过所有用例
#include <stdio.h>
#include <string.h>
//思路:每来一个单词记录一次这个单词记了多少次,然后对比当前记到第i个单词需要的次数,满足则标记这个单词记住了,后面再来相同的单词就可以跳过。
typedef struct{
    int remember_count;  //被背的次数
    int is_remember;  //这个单词是否记住了
    char string[11];        //单词
} node;
int main() {
    //思路:好像不需要排序啊,只需要根据每次来一个单词,然后对比这个单词满不满足记第i个单词所需要的数量,就可以标记为记住,下次遇到相同的就跳过就行了。
    int n;
    scanf("%d",&n);
    node array[n];
    memset(array, 0, sizeof(array));

    int need=1;  //满足记第i个单词需要的次数
    for(int i=0;i<n;i++)
    {
        char temp_string[11]; 
        scanf("%s",temp_string);
        for(int j=0;j<n;j++)//对比存入
        {
            if(array[j].remember_count==0)  //没有出现过这个单词
            {
                strcpy(array[j].string,temp_string);
                array[j].remember_count=1;
                //printf("%s=%d ",array[j].string,array[j].remember_count);
                if(array[j].remember_count==need)
                {
                    array[j].is_remember=1;
                    need++;  //下一个单词需要的次数
                }
                break;
            }else if(strcmp(array[j].string, temp_string)==0){
                if(array[j].is_remember==0) //没有记住才需要加
                {
                    array[j].remember_count+=1;
                    if(array[j].remember_count==need)
                    {
                        array[j].is_remember=1;
                        need++;  //下一个单词需要的次数
                    }
                    //printf("%s=%d ",array[j].string,array[j].remember_count);
                    break;
                }
                break;//找到相等的也要break;不然就会就会新建空间了吧
            }
        }
        //printf("%s",array[i].string);
    }
    printf("%d",need-1);  //输出记了多少个单词了
    return 0;
}

发表于 2025-10-07 14:51:30 回复(0)
思路:
用一个哈希Map记录每个单词出现频次,用一个哈希Set记录已经记住的单词,用变量ans记录记住的单词数目;
遍历单词,统计单词出现频次,如果当前单词不在已记住的单词集合中且频次等于ans+1,那么就ans++,并加入已记住的单词集合中。
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void async function () {
    // Write your code here
    while(line = await readline()){
        let n = Number(line);
        let ans = 0;
        let map = new Map();
        let memed = new Set();
        for (let i = 0; i < n; i++) {
            let word = await readline();
            map.set(word, (map.get(word) || 0) + 1);
            if (!memed.has(word) && map.get(word) === ans + 1) {
                memed.add(word);
                // console.log(word, map.get(word))
                ans++;
            }
        }
        console.log(ans);
    }
}()



发表于 2025-09-26 08:42:33 回复(0)
package main

import (
    "fmt"
)

func main() {
    var n int
    fmt.Scanf("%d", &n)
    var  mp1= make(map[string]bool) // 是否记住了
    var  mp2= make(map[string]int) // 记了多少次
    var s string
    var result int
    for i:=0;i<n;i++{
        fmt.Scanf("%s", &s)
        mp2[s]++
        if !mp1[s] && mp2[s]>result{
            result++
            mp1[s] = true
        }
    }
    fmt.Println(result)
}
发表于 2025-09-08 15:26:58 回复(0)
n = int(input())
count = 0
yuzhi = 0
dic = {}
lis = []
for i in range(n):
    tmp = input()
    if tmp not in dic.keys():
        dic[tmp] = 1
    else:
        dic[tmp] += 1
    if dic[tmp]>yuzhi and (tmp not in lis):
        count += 1
        yuzhi = dic[tmp]
        lis.append(tmp)

print(count)
发表于 2025-08-26 12:29:37 回复(0)
n = int(input()) 
word_count = {} 
remembered_count = 0 
for _ in range(n): 
    word = input() 
    if word not in word_count: 
        word_count[word] = 1 
    elif word_count[word] != -1: 
        word_count[word] += 1 
    else:
        continue
    if word_count[word] == remembered_count + 1: 
        remembered_count += 1 
        word_count[word] = -1
print(remembered_count)

发表于 2025-08-15 12:16:28 回复(0)
n = int(input()) word_count = {} remembered_count = 0 for _ in range(n): word = input() if word not in word_count: word_count[word] = 1 else: word_count[word] += 1 if word_count[word] == remembered_count + 1: remembered_count += 1 print(remembered_count)
发表于 2025-03-28 16:20:46 回复(1)