Algorithm/백준과 프로그래머스

[C++] 20291. 파일정리

young_3060 2024. 3. 13. 20:30
728x90

 

map을 이용하면 쉽게 풀리는 구현문제이다.

string 입력에서 어떻게 해야할지만 안다면 바로 풀린다.

문자열 일부를 return하는 substr함수를 이용해서 '.' 다음 글자부터 끝까지 return받는다.

그렇게 받은 값을 맵에 저장한다. (default가 오름차순이므로)

 

#include <iostream>
#include <map>
using namespace std;

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

    int N; cin >> N;

    map<string, int> m;

    for(int i=0; i<N; i++) {
        string t; cin >> t;
        string extension = t.substr(t.find('.')+1);
        m[extension]++;
    }


    for(auto x : m)
        cout << x.first << " " << x.second << '\n';
    

    return 0;
}
728x90