일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- ros
- 수영대회결승전
- 나무박멸
- 구현
- DP
- 조합
- 백준
- 왕실의기사대결
- BFS
- 마이크로프로세서
- 이진탐색
- 루돌프의반란
- Calibration
- 포탑부수기
- 코드트리
- ARM
- 순서대로방문하기
- 싸움땅
- DenseDepth
- 마법의숲탐색
- 3Dreconstruction
- 삼성기출
- dfs
- PQ
- 토끼와 경주
- 소프티어
- 코드트리빵
- ICER
- 슈퍼컴퓨터클러스터
- 시뮬레이션
Archives
- Today
- Total
from palette import colorful_colors
[백준] 20920 영단어 암기는 괴로워 with C++ 본문
https://www.acmicpc.net/problem/20920
언어: C++, 시간: 160ms
💡문제 아이디어
Hash와 PQ에서의 구조체 정렬을 연습하기 좋은 문제.
Hash를 이용해 시간복잡도를 O(1)로 빈도수를 저장하고,
PQ와 구조체를 이용해 문제의 조건으로 정렬한다.
💡문제 코드
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <unordered_map>
using namespace std;
// pq에 사용할 구조체
struct word{
int wordFreq;
int wordLen;
string wordName;
};
// pq용 정렬 구조체 - 1. 빈도 내림차순, 2. 길이 내림차순, 3.ASCHII오름차순
struct compare {
bool operator()(const word a, const word b) {
if (a.wordFreq == b.wordFreq) {
if (a.wordLen == b.wordLen) {
return a.wordName > b.wordName;
}
else return a.wordLen < b.wordLen;
}
else return a.wordFreq < b.wordFreq;
}
};
int N, M;
string inputWord;
unordered_map <string, word> wordInfo; // 단어 저장용 컨테이너
priority_queue <word, vector<word>, compare> wordList; // 정렬용 단어사전
int main() {
//freopen("sample_input.txt", "r", stdin);
cin >> N >> M;
for (int i = 0; i < N; i++){
cin >> inputWord;
int wordLen = inputWord.length();
if (wordLen < M) continue;
// hash에 빈도수 저장
if (wordInfo.count(inputWord) == 0) {
wordInfo[inputWord] = { 1, wordLen, inputWord };
}
else {
wordInfo[inputWord].wordFreq += 1;
}
}
// hash 순회하며 단어사전에 저장
for (auto it = wordInfo.begin(); it != wordInfo.end(); it++){
word curWord = { it->second.wordFreq, it->second.wordLen, it->second.wordName };
wordList.push(curWord);
}
// 단어사전 단어 출력
while (!wordList.empty()) {
word outputWord = wordList.top();
cout << outputWord.wordName << "\n";
wordList.pop();
}
return 0;
}
'알고리즘 > 문제풀이' 카테고리의 다른 글
[백준] 1707 이분 그래프 with C++ (0) | 2025.02.16 |
---|---|
[프로그래머스] 주사위 고르기 with C++ (2024 카카오 겨울 인턴십) (0) | 2024.05.05 |
[Softeer] 슈퍼컴퓨터 클러스터 with C++ (HSAT 4회 기출) (0) | 2024.05.01 |
[백준] RGB거리 with C++ (1) | 2024.05.01 |
[백준] 1715 카드 정렬하기 with C++ (1) | 2024.05.01 |