안녕하세요. 오늘은 영단어를 외울 거예요.

문제

https://www.acmicpc.net/problem/20920

아이디어

문제에 나온 그대로 정렬을 구현해주면 됩니다.
이때 개수를 세는것은 map으로 할겁니다.
정렬은 vector로 하면 됩니다.

소스코드

#include <iostream>
#include <map>
#include <algorithm>
#include <string>#
#include <vector>
#define ll long long
using namespace std;

bool cmp(pair <string, ll> A, pair <string, ll> B)
{
	if (A.second != B.second) return A.second > B.second;
	if (A.first.length() != B.first.length()) return A.first.length() > B.first.length();
	return A.first < B.first;
}

int main(void)
{
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	map <string, ll> m;
	ll N, M, i;
	string s;

	cin >> N >> M;
	for (i = 1; i <= N; i++)
	{
		cin >> s;
		if (s.length() < M) continue;
		if (m.find(s) != m.end()) m[s]++;
		else m[s] = 1;
	}

	vector <pair <string, ll> > v;
	for (auto temp : m)
		v.push_back(temp);

	sort(v.begin(), v.end(), cmp);
	for (auto temp : v)
		cout << temp.first << "\n";
}


감사합니다.

0개의 댓글