본문 바로가기
Problem Solving/Baekjoon

[백준] 15323 ZigZag - Data Structure / Java

by graycode 2023. 12. 31.

 문제 링크

 

15323번: ZigZag

Zig and Zag are playing a word game. Zig says one letter, and Zag says a word that starts with that letter. However, the word needs to be from the allowed word list and such that Zag already said it the least amount of times. If the word choice is ambiguou

www.acmicpc.net

 

 풀이 코드

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringBuilder sb = new StringBuilder();

        StringTokenizer st = new StringTokenizer(br.readLine());
        int k = Integer.parseInt(st.nextToken()), n = Integer.parseInt(st.nextToken());

        Map<Character, PriorityQueue<Pair>> map = new HashMap<>();
        while (k-- > 0) {
            String s = br.readLine();
            map.computeIfAbsent(s.charAt(0), pq -> new PriorityQueue<>()).add(new Pair(s));
        }

        while (n-- > 0) {
            char c = br.readLine().charAt(0);
            Pair p = map.get(c).poll();

            assert p != null;
            sb.append(p.s).append("\n");

            p.count();
            map.get(c).add(p);
        }

        bw.write(sb.toString());
        bw.flush();
    }

    private static class Pair implements Comparable<Pair> {
        String s;
        int cnt;

        public Pair(String s) {
            this.s = s;
            this.cnt = 0;
        }

        public void count() {
            this.cnt++;
        }

        @Override
        public int compareTo(Pair o) {
            return this.cnt == o.cnt ? this.s.compareTo(o.s) : this.cnt - o.cnt;
        }
    }

}

댓글