πŸ“– [λ°±μ€€μ•Œκ³ λ¦¬μ¦˜ 풀이] Q.1339 단어 μˆ˜ν•™ 풀이

πŸ“– 문제

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

λ―Όμ‹μ΄λŠ” μˆ˜ν•™ν•™μ›μ—μ„œ 단어 μˆ˜ν•™ 문제λ₯Ό ν‘ΈλŠ” μˆ™μ œλ₯Ό λ°›μ•˜λ‹€.

단어 μˆ˜ν•™ λ¬Έμ œλŠ” N개의 λ‹¨μ–΄λ‘œ 이루어져 있으며, 각 λ‹¨μ–΄λŠ” μ•ŒνŒŒλ²³ λŒ€λ¬Έμžλ‘œλ§Œ 이루어져 μžˆλ‹€.

μ΄λ•Œ, 각 μ•ŒνŒŒλ²³ λŒ€λ¬Έμžλ₯Ό 0λΆ€ν„° 9κΉŒμ§€μ˜ 숫자 쀑 ν•˜λ‚˜λ‘œ λ°”κΏ”μ„œ N개의 수λ₯Ό ν•©ν•˜λŠ” λ¬Έμ œμ΄λ‹€.

같은 μ•ŒνŒŒλ²³μ€ 같은 숫자둜 λ°”κΏ”μ•Ό ν•˜λ©°, 두 개 μ΄μƒμ˜ μ•ŒνŒŒλ²³μ΄ 같은 숫자둜 λ°”λ€Œμ–΄μ§€λ©΄ μ•ˆ λœλ‹€.

예λ₯Ό λ“€μ–΄, GCF + ACDEBλ₯Ό κ³„μ‚°ν•œλ‹€κ³  ν•  λ•Œ, A = 9, B = 4, C = 8, D = 6, E = 5, F = 3, G = 7둜 κ²°μ •ν•œλ‹€λ©΄,

두 수의 합은 99437이 λ˜μ–΄μ„œ μ΅œλŒ€κ°€ 될 것이닀.

N개의 단어가 μ£Όμ–΄μ‘Œμ„ λ•Œ, κ·Έ 수의 합을 μ΅œλŒ€λ‘œ λ§Œλ“œλŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜μ‹œμ˜€.

πŸ” 접근법

단어 μˆ˜ν•™ λ¬Έμ œμž…λ‹ˆλ‹€.

각 μ•ŒνŒŒλ²³λ§ˆλ‹€ μ€‘λ³΅λ˜μ§€ μ•Šκ²Œ 1~9 의 μˆ«μžκ°€ μ£Όμ–΄μ Έμ•Όν•˜κ³ 

λ‹¨μ–΄λ“€μ˜ 합이 μ΅œλŒ€κ°€ λ˜λ„λ‘ ν•΄μ•Όν•©λ‹ˆλ‹€.

각 μ•ŒνŒŒλ²³μ΄ μžˆλŠ” 자릿수의 값을 더 ν•΄

κ°€μž₯ ν°κ°’μ˜ μ•ŒνŒŒλ²³λΆ€ν„°

큰 수λ₯Ό λŒ€μž…ν•΄μ£Όλ©΄ ν’€ μˆ˜κ°€ μžˆμŠ΅λ‹ˆλ‹€.

ex) ABA
A= 10^2+10^0
B= 10^1
= AλŠ” 101 이고 BλŠ” 10 으둜
A λŠ” 9 B λŠ” 8을 λŒ€μž…ν•΄μ£Όλ©΄ μ΅œλŒ€κ°’μ΄ λ‚˜μ˜΅λ‹ˆλ‹€.

ex) ABACB , BDAAC
A = 10^4 + 10^2 + 10^2 + 10^1   = 10210 =>8 λŒ€μž…
B = 10^3 + 10^0 + 10^4          = 11001 =>9 λŒ€μž…
C = 10^1 + 10^0                 = 11    =>6 λŒ€μž…
D = 10^3                        = 1000  =>7 λŒ€μž…
89869 + 97866 = 187,755 κ°€
μ΅œλŒ€κ°’μž…λ‹ˆλ‹€.

μ•ŒνŒŒλ²³μ΄ μœ„μΉ˜ν•œ 각 자리의 수λ₯Ό map을 μ΄μš©ν•΄

λ„£μ–΄μ£Όμ—ˆκ³ 

map의 value

각 μ•ŒνŒŒλ²³λ§ˆλ‹€μ˜ 합을 κΈ°μ€€μœΌλ‘œ μ •λ ¬ν•˜μ—¬

큰 κ°’λΆ€ν„° 숫자λ₯Ό λŒ€μž…ν•΄μ£Όμ—ˆμŠ΅λ‹ˆλ‹€.

πŸ’» μ½”λ“œ


package problem.greedy;

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

public class Main_1339 {
    static int N;
    static String words[];

    static class ValueComparator implements Comparator<Character> {

        Map<Character, Integer> base;

        public ValueComparator(Map<Character, Integer> base) {
            this.base = base;
        }

        public int compare(Character a, Character b) {
            if (base.get(a) >= base.get(b)) { //λ°˜λŒ€λ‘œ ν•˜λ©΄ μ˜€λ¦„μ°¨μˆœ <=
                return -1;
            } else {
                return 1;
            } // returning 0 would merge keys
        }
    }


    public static void main(String[] args) throws IOException {
        Map<Character,Integer> map = new HashMap<>();
        ValueComparator valueComparator = new ValueComparator(map);
        TreeMap<Character,Integer> sortedMap = new TreeMap<>(valueComparator);
        input();
        for(int i=0; i<N; i++){
            String word = words[i];
            for(int j=0; j<word.length(); j++){
                char c = word.charAt(j);
                int num = (int) Math.pow(10 ,word.length()-j-1);
                if(map.containsKey(c) ){
                    int temp = map.get(c);
                    map.put(c,num+temp);
                }
                else{
                    map.put(c,num);
                }
            }
        }
        sortedMap.putAll(map);
        int answer = 0;
        int num = 9;
        for (Map.Entry<Character,Integer> entry : sortedMap.entrySet()) {
            System.out.println(entry.getKey()+" : "+map.get(entry.getKey()));
            int value = map.get(entry.getKey());
            answer += (value*num--);
        }
        System.out.println(answer);
    }

    public static void input() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        N = Integer.parseInt(br.readLine());
        words = new String[N];
        for(int i=0; i<N; i++){
            words[i] = br.readLine();
        }
        br.close();
    }

}


Written on November 26, 2020