Algorithm
백준 1339번: 단어 수학
qlyh8
2018. 9. 23. 22:50
문제 출처: https://www.acmicpc.net/problem/1339
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 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; public class Main { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int N = Integer.parseInt(reader.readLine()); String[] inputArr = new String[N]; for(int i=0 ; i<N ; i++) inputArr[i] = reader.readLine(); ArrayList<Character> alphabet = new ArrayList<>(); ArrayList<Integer> number = new ArrayList<>(); for(int i=0 ; i<N ; i++){ String str = inputArr[i]; for(int j=0 ; j<str.length() ; j++){ if(!alphabet.contains(str.charAt(j))){ alphabet.add(str.charAt(j)); number.add((int)Math.pow(10, str.length()-j-1)); } else{ int index = alphabet.indexOf(str.charAt(j)); number.set(index, number.get(index) + (int)Math.pow(10, str.length()-j-1)); } } } int result = 0; int n = 9; while(!number.isEmpty()){ int max = 0; int index = 0; for(int i=0 ; i<number.size() ; i++){ if(max < number.get(i)){ max = number.get(i); index = i; } } result += max * n--; number.remove(index); } System.out.println(result); } } | cs |