백준 1339번 단어수학 Java(자바) 풀이

최태순·2022년 3월 16일
0

알고리즘

목록 보기
3/4
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class BOJ_G4_1339_단어수학 {
	
	static BufferedReader br;
	static int N;
	static String str;
	static int[] alpha;
	static int l, num, answer;

	public static void main(String[] args) throws NumberFormatException, IOException {
		
		// System.setIn(new FileInputStream("./input.txt"));
		br = new BufferedReader(new InputStreamReader(System.in));
		
		N = Integer.parseInt(br.readLine());
		alpha = new int[26];
		
		for(int i = 0; i < N; i++) {
			str = br.readLine();
			// l += str.length();
			for(int j = 0; j < str.length(); j++) {
             	// 자릿수만큼 10의 거듭제곱 더해준다
				alpha[str.charAt(j) - 'A'] += (Math.pow(10, str.length() - j - 1));
			}
		}
		
		Arrays.sort(alpha);
		
		// System.out.println(Arrays.toString(alpha));
		
		num = 9;
		
		for(int i = 25; i >= 0; i--) {
			if(alpha[i] == 0) {
				continue;
			}
			answer += (alpha[i] * num); // 큰 애부터 9 8 7 6... 순서대로 붙여준다
			num--;
		}
		
		System.out.println(answer);
		
	}

}
profile
'개'으른 '게'발자

0개의 댓글