728x90
320x100
20920번: 영단어 암기는 괴로워
첫째 줄에는 영어 지문에 나오는 단어의 개수 $N$과 외울 단어의 길이 기준이 되는 $M$이 공백으로 구분되어 주어진다. ($1 \leq N \leq 100\,000$, $1 \leq M \leq 10$) 둘째 줄부터 $N+1$번째 줄까지 외울 단
www.acmicpc.net
반응형
📌 접근 방법
- N : 입력받을 단어의 개수
- M : 길이가 M 이상인 단어만 단어장에 적는다
- map 을 이용해 key에 단어, value에 ++1을 해준다.
- 중복입력되면 value의 값이 증가!
- 벡터로 단어장을 만들어준 후 map에 있는 단어와 단어가 나온 횟수를 넣어준다.
- sort 함수를 이용해 조건에 맞게 정렬해줌!
✅ Pass Code
#include<iostream>
#include<string>
#include<cmath>
#include<algorithm>
#include<vector>
#include<iterator>
#include<map>
#include<set>
#include<unordered_set>
using namespace std;
bool cmp(pair<int, string> a, pair<int, string> b){
if(a.first==b.first){
if(a.second.length()==b.second.length()){
return a.second<b.second;
}
else return a.second.length()>b.second.length();
}
else return a.first>b.first;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N, M;
cin>>N>>M;
map<string, int>check;
for(int i=0; i<N; i++){
string x;
cin>>x;
if(x.size()<M){
continue;
}
check[x]++;
}
vector<pair<int, string>> words;
for(auto&& word : check){
words.emplace_back(word.second,word.first);
}
sort(words.begin(),words.end(),cmp);
for(auto&& word : words){
cout<<word.second<<'\n';
}
return 0;
}
![](https://t1.daumcdn.net/keditor/emoticon/friends1/large/011.gif)
728x90
반응형
'Coding Test > Baekjoon' 카테고리의 다른 글
[BOJ/백준/C++] 11866번 요세푸스 문제 0 (1) | 2024.01.28 |
---|---|
[BOJ/백준/C++] 1874번 (0) | 2024.01.28 |
[BOJ/백준/C++] 2108번 통계학 (1) | 2024.01.27 |
[BOJ/백준/C++] 1010번 다리 놓기 (1) | 2024.01.27 |
[BOJ/백준/C++] 4134번 다음 소수 (1) | 2024.01.27 |