โ 20920 ์๋จ์ด ์๊ธฐ๋ ๊ดด๋ก์ โ
//20920
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
using namespace std;
int N,M;
string word;
map<string,int> freq;
vector<string> vocas;
bool compare(string a, string b){
if(freq[a] != freq[b]){
return freq[a] > freq[b];
}
else{
if(a.size() != b.size()){
return a.size() > b.size();
}
else{
return a < b;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> N >> M;
while(N--){
cin >> word;
if(M<=word.length()){
if(freq[word]){
freq[word] += 1;
}
else{
freq[word] = 1;
vocas.push_back(word);
}
}
}
sort(vocas.begin(), vocas.end(), compare);
for(int i = 0; i < vocas.size(); i++){
cout << vocas[i] << '\n';
}
return 0;
}
๐ง๐ป๐จ ๋ค์ค ์ ๋ ฌ ์กฐ๊ฑด์ด๋ผ๋ฉด ์กฐ๊ฑด์ ์์๋ฅผ 1์์ - 2์์ - 3์์ ๋ฐ์ ธ์ ํ์ธ ํ์. sort()์ ์ธ๋ฒ์งธ ์ธ์ compare() ํจ์ ์์ฑ ์ ๋จผ์ ์ฐ์ ์์ 1์์ ์งํ. ๊ทธ ๋ค์ 2์์ - 3์์ ์์ผ๋ก compare() ํจ์ ๋ด์ฉ ๋ง๋ ๋ค.(a์ b๋ ๊ณ ์ . ๋ถ๋ฑํธ์ ๋ฐ๋ผ ์ค๋ฆ์ฐจ์ / ๋ด๋ฆผ์ฐจ์ ๊ฒฐ์ )
๐ง๐ป๐จ frequency map๊ณผ vector ๊ฐ๋ณ 1์ฐจ์ ๋ฐฐ์ด ๊ฐ๊ฐ ๋ง๋ค์ด์ ์งํ.
'C, C++ > ๐ฅ BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
(C++) โ DP Upper-Intermediate I - 2 Solvedโ (0) | 2024.11.15 |
---|---|
(C++) โ Backtracking Intermediate I - 3 Solvedโ (2) | 2024.11.15 |
(C++) โ Stack & Queue & Deque Intermediate I - 4 Solvedโ (0) | 2024.11.15 |
(C++) โ Number Theory Intermediate I - 2 Solvedโ (0) | 2024.11.15 |
(C++) โ Set/Map Intermediate I - 5 Solvedโ (2) | 2024.11.14 |
๋๊ธ