아무코딩

[백준 1339] 단어 수학 본문

알고리즘/백준

[백준 1339] 단어 수학

동 코 2020. 6. 8. 18:31

문제

민식이는 수학학원에서 단어 수학 문제를 푸는 숙제를 받았다.

단어 수학 문제는 N개의 단어로 이루어져 있으며, 각 단어는 알파벳 대문자로만 이루어져 있다. 이때, 각 알파벳 대문자를 0부터 9까지의 숫자 중 하나로 바꿔서 N개의 수를 합하는 문제이다. 같은 알파벳은 같은 숫자로 바꿔야 하며, 두 개 이상의 알파벳이 같은 숫자로 바뀌어지면 안 된다.

예를 들어, GCF + ACDEB를 계산한다고 할 때, A = 9, B = 4, C = 8, D = 6, E = 5, F = 3, G = 7로 결정한다면, 두 수의 합은 99437이 되어서 최대가 될 것이다.

N개의 단어가 주어졌을 때, 그 수의 합을 최대로 만드는 프로그램을 작성하시오.

입력

첫째 줄에 단어의 개수 N(1 ≤ N ≤ 10)이 주어진다. 둘째 줄부터 N개의 줄에 단어가 한 줄에 하나씩 주어진다. 단어는 알파벳 대문자로만 이루어져있다. 모든 단어에 포함되어 있는 알파벳은 최대 10개이고, 수의 최대 길이는 8이다. 서로 다른 문자는 서로 다른 숫자를 나타낸다.

출력

첫째 줄에 주어진 단어의 합의 최댓값을 출력한다.

문제 풀이

ABC 의 경우  : 100A + 10B + C 임을 고려해서

각자리수를 계속 알파벳 별로 더해준다.

 

arrayList 에는 계속 더해주고 map 에는 알파벳별 index를 저장해서 빠르게 맵핑 시켜준다.

 

그렇게 다 더한후

 

sort를 진행한다. 인스턴스를 소팅하기위해서 

Comparable<Alphabet> 인터페이스의 compareTo 메서드를 오버라이딩한다.

@Override
        public int compareTo(Alphabet o) {
            return o.coefficient - coefficient;
        }

sort 진행후 큰값부터 차례로 9부터 내림차순으로 곱한뒤 모두 더한값을 반환한다.

 

 

소스코드

더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
 
public class Baekjoon1339 {
 
    static Map<Character, Integer> indexMap;
    static List<Alphabet> alphabetList;
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static int N;
    static int cntAlphabet;
    public static void main(String[] args) throws IOException {
        inputData();
        Collections.sort(alphabetList);
        int num=9;
        int result = 0;
        for(int i=0;i<alphabetList.size();i++){
            Alphabet a = alphabetList.get(i);
            //System.out.println(a.alphabet+" : "+a.coefficient);
            result += (a.coefficient*num);
            num--;
        }
        System.out.println(result);
    }
 
    public static void inputData() throws IOException {
        N = Integer.parseInt(br.readLine());
        alphabetList = new ArrayList<Alphabet>();
        indexMap = new HashMap<>();
        cntAlphabet = 0;
        for(int i=0;i<N;i++) {
            String input = br.readLine();
            addCoefficient(input);
        }
 
 
    }
 
    private static void addCoefficient(String input) {
        int one =1;
        for(int i=input.length()-1;i>=0;i--){
            char c = input.charAt(i);
            if(indexMap.containsKey(c)){
                int idx = indexMap.get(c);
                int temp = alphabetList.get(idx).getCoefficient();
                temp +=one;
                alphabetList.get(idx).setCoefficient(temp);
            }
            else{
                alphabetList.add(new Alphabet(c,one));
                indexMap.put(c,alphabetList.size()-1);
            }
            one*=10;
        }
    }
 
    static class Alphabet implements Comparable<Alphabet>{
        char alphabet;
        int coefficient;
 
        public Alphabet(char alphabet, int coefficient) {
            this.alphabet = alphabet;
            this.coefficient = coefficient;
        }
 
        public void setCoefficient(int coefficient) {
            this.coefficient = coefficient;
        }
 
        public int getCoefficient() {
            return coefficient;
        }
 
        @Override
        public int compareTo(Alphabet o) {
            return o.coefficient - coefficient;
        }
    }
}
 
cs
 
 

문제 링크 : https://www.acmicpc.net/problem/1339

 

1339번: 단어 수학

첫째 줄에 단어의 개수 N(1 ≤ N ≤ 10)이 주어진다. 둘째 줄부터 N개의 줄에 단어가 한 줄에 하나씩 주어진다. 단어는 알파벳 대문자로만 이루어져있다. 모든 단어에 포함되어 있는 알파벳은 최대

www.acmicpc.net

 

'알고리즘 > 백준' 카테고리의 다른 글

[백준 12865] 평범한 배낭  (0) 2020.06.06
[백준 4915] 친구 네트워크  (0) 2020.06.05
[백준 1938] 통나무 옮기기  (0) 2020.06.05
[백준 7432] 디스크 트리(java)  (0) 2020.06.04
[백준 11967] 불켜기(c++)  (0) 2020.05.30
Comments