Algorithm/BOJ

[백준 20291] 파일 정리

giiro 2020. 12. 11. 13:05

문제

www.acmicpc.net/problem/20291

 

20291번: 파일 정리

친구로부터 노트북을 중고로 산 스브러스는 노트북을 켜자마자 경악할 수밖에 없었다. 바탕화면에 온갖 파일들이 정리도 안 된 채 가득했기 때문이다. 그리고 화면의 구석에서 친구의 메시지를

www.acmicpc.net

 

풀이

STL map을 사용하면 문자열인 확장자들의 개수를 관리할 수 있고, 출력할 때는 맵을 순회하면서 확장자명과 개수를 출력하여 간단하게 해결할 수 있었습니다.

코드

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

int n;
string s;
map<string, int> mp;

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

	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> s;
		s = s.substr(s.find('.') + 1);
		mp[s]++;
	}
	for (auto it = mp.begin(); it != mp.end(); it++)
		cout << (*it).first << " " << (*it).second << '\n';
}