Little Jay

[C++] 백준 4358번 - 생태학 본문

알고리즘/BOJ

[C++] 백준 4358번 - 생태학

Jay, Lee 2022. 6. 2. 22:40

시험기간에 너무 공부가 안되서 푼 문제

KMP, Boyer-Moore, NP-Hard, NP-Complete 등등 너무 어렵다 정말

학교에서 알고리즘 과제하는데 string pattern matching 알고리즘이 너무 어려워서

string 관련 문제 하나 풀었다.

 

어렵지는 않은데 입력을 받을때 

cin >> str을 하면 틀리고, getline(cin, str)을 해주면 맞았다

이건 또 왜 그런지 잘 모르겠다

 

백분율 구하는 문제라서 크게 어렵지는 않았던 문제

#include <bits/stdc++.h>
using namespace std;

map<string, float> m;

int main() {
	ios_base::sync_with_stdio(false); 
	cin.tie(NULL); cout.tie(NULL);
	string str;
	float cnt = 0;

	while (getline(cin, str)) {
		cnt++;
		if (m.find(str) != m.end()) {
			m[str] += 1;
		}
		else
			m[str] = 1;
	}
	cout << fixed;
	cout.precision(4);
	for (auto it = m.begin(); it != m.end(); it++) {
		float val = (it->second / cnt) * 100;
		cout << it->first << " ";
		cout << val << endl;
	}
	return 0;
}
Comments