PLOD

[백준] 파일 정리(20291) 본문

대외 활동 및 IT 지식/알고리즘 문제 풀이 정리

[백준] 파일 정리(20291)

훌룽이 2026. 3. 9. 22:19

🔗 문제 링크

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

✅ 코드

import java.io.*;
import java.util.*;

public class 백준_20291 {
    static int n;
    static HashMap<String,Integer> files;
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        n = Integer.parseInt(st.nextToken());
        files = new HashMap<>();
        while(n --> 0){
            st = new StringTokenizer(br.readLine());
            String[] fileName = st.nextToken().split("\\.");
            files.put(fileName[1], files.getOrDefault(fileName[1],0) + 1);
        }

        ArrayList<String> list = new ArrayList<>(files.keySet());
        Collections.sort(list);

        for(String x : list){
            System.out.println(x + " " + files.get(x));
        }

        br.close();
    }
}

 

💡 배운 점 & 느낀 점

1. String[] fileName = st.nextToken()..split("\\.");  →  "."과 같은 경우 정규식(regex) 기반 이기 때문에 "\\."로 해주어야 함

2. HashMap<Key,Value>() → Key에 get으로 Value 접근 가능

Comments