알고리즘

C++ 알고리즘 - 백준 20920 영단어 암기는 괴로워 (맵, 벡터)

마루설아 2025. 1. 17. 20:42

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

 

#include <bits/stdc++.h>
#define endl "\n"

using namespace std;

void CPP_INIT() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}

bool compare(pair<string, int> n1, pair<string, int> n2) {
	if (n1.second == n2.second) {
		if (n1.first.size() == n2.first.size()) {
			return n1.first < n2.first;
		}

		else return (n1.first.size()) > (n2.first.size());
	}

	return n1.second > n2.second;
}

int main(void) {
	CPP_INIT();

	int input1, input2;
	string str;
	map<string, int> m;

	cin >> input1 >> input2;

	for (int i = 0; i < input1; i++) {
		cin >> str;
		if (str.size() < input2) continue;

		m[str]++;
	}

	vector<pair<string, int>> v(m.begin(), m.end());

	sort(v.begin(), v.end(), compare);

	for (int i = 0; i < v.size(); i++) {
		cout << v[i].first << endl;
	}
}