반응형

https://www.acmicpc.net/problem/4358

 

4358번: 생태학

프로그램은 여러 줄로 이루어져 있으며, 한 줄에 하나의 나무 종 이름이 주어진다. 어떤 종 이름도 30글자를 넘지 않으며, 입력에는 최대 10,000개의 종이 주어지고 최대 1,000,000그루의 나무가 주어

www.acmicpc.net

 

unordered_map으로 중복을 제거하며, 각 나무의 개수를 세주었습니다.

#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <vector>
#include <string>
#include <cmath>
using namespace std;

string t;
unordered_map<string, int> tree;
vector<pair<string, double>> sorted;
int cnt = 0;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	while (!getline(cin, t).eof()) {
		auto find = tree.find(t);
		if (find != tree.end()) find->second++;
		else tree.insert({ t, 1 });
		cnt++;
	}

	for (auto& it : tree) {
		sorted.push_back({ it.first, it.second * 100.0 / cnt });
	}

	sort(sorted.begin(), sorted.end());

	cout << fixed;
	cout.precision(4);
	for (auto& s : sorted) {
		cout << s.first << " " << s.second << "\n";
	}
}
반응형

'Algorithm' 카테고리의 다른 글

백준 1254 : 팰린드롬 만들기  (0) 2021.11.19
백준 1464 : 뒤집기 3  (0) 2021.11.19
백준 1347 : 미로 만들기  (0) 2021.11.19
백준 2669 : 직사각형 네개의 합집합의 면적 구하기  (0) 2021.11.19
백준 1535 : 안녕  (0) 2021.11.19

+ Recent posts