💡Problem Solving/BOJ

[BOJ 22232] 가희와 파일 탐색기 (Java)

gom20 2021. 10. 18. 12:20

#문제

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

 

22232번: 가희와 파일 탐색기

첫 번째 줄에 jo_test 폴더에 있는 파일 개수 N과 가희가 설치한 OS에서 인식하는 파일 확장자의 개수 M이 공백으로 구분되어 주어집니다. 2번째 줄부터 N+1번째 줄까지 FILENAME.EXTENSION 형식의 문자열

www.acmicpc.net

 

#소스코드

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.*;

public class BOJ22232 {

    public static int N, M;
    public static ArrayList<File> files;
    public static HashSet<String> exts;

    public static class File implements Comparable<File>{
        String fullName;
        String name;
        String ext;

        public File(String fullName){
            this.fullName = fullName;
            divideNameAndExt();
        }

        public void divideNameAndExt(){
            String[] arr = fullName.split("\\.");
            this.name = arr[0];
            this.ext = arr[1];
        }

        @Override
        public int compareTo(File f){
            int rs = this.name.compareTo(f.name);
            if(rs == 0){
                int cur = exts.contains(this.ext) ? 0: 1;
                int next = exts.contains(f.ext) ? 0: 1;
                if(cur != next){
                    rs = cur - next;
                } else {
                    rs = this.ext.compareTo(f.ext);
                }
            }
            return rs;
        }
    }

    public static void input() throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());

        files = new ArrayList<File>();
        exts = new HashSet<String>();

        for(int i = 0 ; i < N; i++){
            files.add(new File(br.readLine()));
        }
        for(int i = 0; i < M; i++){
            exts.add(br.readLine());
        }
    }

    public static void main(String[] args) throws Exception {
        input();
        Collections.sort(files);
        StringBuilder sb = new StringBuilder();
        for(File file : files){
            sb.append(file.fullName + "\n");
        }
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        bw.write(sb.toString());
        bw.flush();
    }
}