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 pair->벡터로 복사후에 sort를 해줘야 시간 초과가 발생안하네여 ...
하아 ㅋㅋ 진짜 뭔가 문자열 문제들 다 왜이런거죠? ㅋㅋ
for (const auto& pair : wordCounts) {
wordList.push_back({ pair.first, pair.second });
}
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
struct Word {
string word;
int count;
};
bool compare(const Word& a, const Word& b) {
if (a.count != b.count) {
return a.count > b.count;
}
if (a.word.length() != b.word.length()) {
return a.word.length() > b.word.length();
}
return a.word < b.word;
}
int main() {
int n, m;
cin >> n >> m;
map<string, int> wordCounts;
for (int i = 0; i < n; i++) {
string word;
cin >> word;
if (word.length() >= m) {
wordCounts[word]++;
}
}
vector<Word> wordList;
for (const auto& pair : wordCounts) {
wordList.push_back({ pair.first, pair.second });
}
sort(wordList.begin(), wordList.end(), compare);
for (const auto& word : wordList) {
cout << word.word <<endl;
}
return 0;
}