Algorithm/Data Structure
[BOJ 20920] S3 영단어 암기는 괴로워 { C++ }
surimi🍥
2022. 7. 4. 14:55
반응형
20920번: 영단어 암기는 괴로워
첫째 줄에는 영어 지문에 나오는 단어의 개수 $N$과 외울 단어의 길이 기준이 되는 $M$이 공백으로 구분되어 주어진다. ($1 \leq N \leq 100\,000$, $1 \leq M \leq 10$) 둘째 줄부터 $N+1$번째 줄까지 외울 단
www.acmicpc.net
C++
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <vector>
using namespace std;
bool cmp(pair<string, int> &a, pair<string, int> &b)
{
// 두 int 값 이 같으면 두번째 정렬 조건으로
if (a.second == b.second)
{
// 두 문자열의 길이가 같으면 세번째 정렬 조건으로
if (a.first.length() == b.first.length())
// 두 문자열 알파벳 순 정렬
return (a.first < b.first);
return (a.first.length() > b.first.length());
}
else return (a.second > b.second);
}
int main(void)
{
cin.tie(0)->sync_with_stdio(0);
unordered_map<string, int> M;
string str;
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++)
{
cin >> str;
if (str.length() >= m)
M[str]++;
}
// Map의 원소를 vector로 복사
vector<pair<string, int>> V(M.begin(), M.end());
sort(V.begin(), V.end(), cmp);
for (auto p : V)
cout << p.first << "\\n";
}
반응형