세가지 정렬 기준이 나온다. 이를 위해 사용자 정렬 함수를 만들어야한다.
중복제거를 위해 set, 빈도수 체크를 위해 map을 활용한다.
세가지 정렬 기준에 따라 사용자 정의 정렬 함수를 만든다.
자주 나오는 단어는 map을 통해 빈도수를 체크한다.
단어의 길이는 std::string의 size()메소드를 활용한다.
사전 순은 라이브러리에서 알아서 비교해준다.(비교 연산자 > or <)
(사전 순에서는 클수록 뒤에 있는 알파벳이다)
//백준 20920, 영단어 암기는 괴로워
#include <iostream>
#include <map>
#include <algorithm>
#include <set>
#include <vector>
int N, M;
std::set<std::string> set;
std::map<std::string, int> map;
std::vector<std::string> vec;
//a가 B보다 앞에 올때 true
bool solve(std::string a, std::string b){
auto cntA = map.find(a)->second;
auto cntB = map.find(b)->second;
if(cntA != cntB){
return cntA > cntB;
}
else if(a.length() != b.length()){
return a.length() > b.length();
}
else{
return a < b;
}
}
int main (){
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
std::cin >> N >> M;
for(int i{0}; i<N; ++i){
std::string s;
std::cin >> s;
if(s.length() < M) continue;
if(map.find(s) != map.end()) ++map[s];
else map[s] = 1;
set.insert(s);
}
for(auto n : set) vec.push_back(n);
std::sort(vec.begin(), vec.end(), solve);
for(auto n : vec) std::cout << n << '\n';
return 0;
}