小红每天都要背单词,然后她会把每天记住了多少单词记录下来,并在小红书上打卡。
当小红背单词时,如果她已经记住了
个单词,且背了一个没有记住的新单词
次,则她就会记住这个新的单词。
例如,当她按顺序背["you","thank","thank"]时,她第一次背单词"you"时她就能记住"you"。而由于她已经记住了一个单词,所以需要背两次"thank"才能记住"thank"。
第一行一个整数。
接下来行,每行一个字符串,保证每个字符串长度不超过 10。
输出一个整数,表示她记住了多少个单词。
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() 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());
}
} #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;
}
#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") #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") 通不过试试下面的数据: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);
}
} //程序已通过所有用例
#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;
} 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);
}
}()
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)