알고리즘
백준 20920번 영단어 암기는 괴로워 C++
영춘권의달인
2022. 12. 23. 12:32
https://www.acmicpc.net/problem/20920
20920번: 영단어 암기는 괴로워
첫째 줄에는 영어 지문에 나오는 단어의 개수 $N$과 외울 단어의 길이 기준이 되는 $M$이 공백으로 구분되어 주어진다. ($1 \leq N \leq 100\,000$, $1 \leq M \leq 10$) 둘째 줄부터 $N+1$번째 줄까지 외울 단
www.acmicpc.net
map을 통해서 각 단어가 몇번 나왔는지 기록하고 조건에 맞게 정렬하였다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
int n, m;
struct Info {
string str;
int cnt;
bool operator<(const Info& other) const {
if (cnt != other.cnt) return cnt > other.cnt;
else {
if (str.length() == other.str.length()) return str < other.str;
return str.length() > other.str.length();
}
}
};
map<string, int> m1;
vector<Info> v;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
for (int i = 0; i < n; i++) {
string str;
cin >> str;
if (str.length() < m) continue;
m1[str]++;
}
for (auto a : m1) {
v.push_back({ a.first,a.second });
}
sort(v.begin(), v.end());
for (auto a : v) {
cout << a.str << "\n";
}
}