Gratitude

Ben heard about studies by Emmons and McCullough that suggest that intentionally practicing gratitude has a lasting effect on people’s happiness. Since he wants to be happy too, he decided that at the end of each day he will think back over the past day and write down three things he is thankful for, one thing per line. At the end of N days in which he practiced this exercise, he was curious to know which things appear the most on his list. Help Ben get the K things he was grateful for most frequently.

Input
The input begins with one line containing two space-separated integers, N and K, in that order. Then follow 3N lines containing Ben’s notes from N days. You may assume that the three lines that correspond to the same day contain no repetitions. That is, if you partition the input into N chunks of 3 consecutive lines, no chunk contains two identical lines.

Limits

1≤K≤3N≤100000
Each input line contains at most 50 (ASCII) characters.
Output
The output should represent the list of things that Ben is grateful for, ordered by frequency of appearance in Ben’s list (with the most frequent item first). In case of two items with equal frequency, the most recent item should appear first. That is, in case of a tie in the number of appearances, the item whose last appearance is later in the input should appear earlier in the output. Finally, if there are more than K different items in Ben’s list, your output should contain only the K first items (according to the required order).

input

2 2
Supportive parents
Being able to solve a hard problem
Good food
Fun game with friends
Good food
Being healthy

output

Good food
Being healthy

input

2 6
Supportive parents
Being able to solve a hard problem
Good food
Fun game with friends
Good food
Being healthy

output

Good food
Being healthy
Fun game with friends
Being able to solve a hard problem
Supportive parents

1,相同的串只输入一次,并且记录当前串第一次出现的序号,排序是先按照出现次数排序,其次是输入序号从大到小排序
2,这次拓展学习了unorder_map用法

#include<map>
#include<iostream>
#include<string>
#include<algorithm>
#include<unordered_map>
using namespace std;
struct NODE
{
	string str;
	int js;
	int ord;
}st[400010];
bool cmp(NODE a, NODE b)
{
	if (a.js != b.js)
		return a.js > b.js;
	else
		return a.ord > b.ord;
}
int main()
{
	int n, k;
	cin >> n >> k;
	unordered_map<string ,int > map1;
	string q;
	int cnt = 1;
	getchar();
	for (int i = 1; i <= 3 * n; i++)
	{
		getline(cin,q);
		if (map1[q] == 0)
		{
			st[cnt].str = q;
			st[cnt].ord = i;
			st[cnt].js = 1;
			map1[q] = cnt;
			cnt++;
		}
		else
		{
			st[map1[q]].ord = i;
			st[map1[q]].js++;
		}
	}
	sort(st + 1, st + cnt + 1, cmp);
	for(int i=1;i<=k;i++)
	{
		cout << st[i].str<< endl;
	}

                            
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Prime me

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值