[BOJ-Code] 20920 - 영단어 암기는 괴로워

Posted by DHniyeo Blog on August 30, 2024

문제 링크

💡 자료 구조/해시를 사용한 집합과 맵/정렬/문자열/트리를 사용한 집합과 맵

Memory 15008KB Time 160ms Code Length 1085B

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include<vector>
#include <string>
#include <algorithm>
#include <map>
using namespace std;

struct info {
	string word;
	int time = 0;
};
int n, m;
vector<info> dic;
map<string, int> data_map;
bool cmp(info first, info second) {
	if (first.time > second.time) {
		return true;
	}
	else if (first.time == second.time) {
		if (first.word.size() > second.word.size()) {
			return true;
		}
		else if (first.word.size() == second.word.size()) {
			if (first.word < second.word) {
				return true;
			}
		}
	}
	return false;

}

void init() {
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		string s;
		cin >> s;
		if (s.size() >= m) {
			if (data_map.find(s) != data_map.end()) { // 값이 있다면
				dic[data_map[s]].time++;
			}
			else { // 처음 나온 단어라면??
				dic.push_back({ s,1 });
				data_map[s] = dic.size() - 1;
			}

		}
	}
}
void solve() {
	// vector 내부 정렬
	sort(dic.begin(), dic.end(), cmp);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	init();
	solve();
	for (info now : dic) {
		cout << now.word << '\n';
	}

}

이 코드는 단어를 입력받아서 길이가 m 이상인 단어들을 카운트하고, 그 카운트된 단어들을 정렬하여 출력하는 프로그램이다. 입력된 단어들은 구조체 info에 저장되고, map을 사용하여 각 단어의 등장 횟수를 저장한다. 그 후, 등장 횟수와 단어의 길이, 알파벳 순서대로 정렬하여 출력한다.