BOJ 20291 - 파일정리 (by cpp)

rivermt·2023년 4월 18일
0

BOJ

목록 보기
3/18

문제

https://www.acmicpc.net/problem/20291
파일명들이 n개만큼 주어지는데 각각의 파일명 들은 파일명.확장자 의 형태를 가진다. 이때, 파일을 확장자 별로 정리하여 몇 개씩 있는지 출력해야 되며 확장자들을 사전 순으로 정렬해서 출력해야 한다.

풀이

각 확장자 별 몇 개씩 있는지 알아야 하고 (key, value) 사전 순으로 정렬해야한다 라는 점에서 map 자료구조를 떠올렸다. 입력받은 문자열들에서 확장자들을 뽑아내야 하기 때문에 저번에 포스팅한 sstream을 활용해봤다. (https://velog.io/@rivermt/c-%EB%AC%B8%EC%9E%90%EC%97%B4-%EC%9E%90%EB%A5%B4%EA%B8%B0-with.-stringstream)
확장자를 추출하고 확장자를 key 값으로 하고 그 확장자가 나올 때마다 카운팅 해주면 해결된다.

CODE

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <sstream>
#include <deque>
#include <regex>
using namespace std;
#define ll long long
#define pi pair<int,int>
#define ppi pair<int,pair<int,int>>
#define pll pair<ll,ll>
#define FOR(a,n) for(int i=a;i<n;++i)
#define FastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define vvi vector<vector<int>>
#define vi vector<int>
#define vpi vector<pair<int,int>>
#define endl '\n'
#define INF 1e9
#define MOD 1000000000
#define all(v) v.begin(), v.end()
#define compress(v) v.erase(unique(all(v)),v.end());

int n;
vector<string> files;
map<string, int> mp;

int main(){
    FastIO
    cin >> n;
    files.resize(n);
    for(int i = 0; i < n; i++) cin >> files[i];
    
    for(string file: files) {
        string name;
        string extender;
        stringstream ss(file);
        while(getline(ss, name, '.') && ss >> extender) mp[extender]++;
    }
    
    for(auto&[a,b] : mp) cout << a << ' ' << b << endl;

    return 0;
}
profile
화이팅!!

0개의 댓글

Powered by GraphCDN, the GraphQL CDN