백준 - 단어 수학

greenTea·2023년 8월 3일
0
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        int[] alpahbet = new int[26];

        ArrayList<String> list = new ArrayList<>();

        for (int i = 0; i < N; i++) {
            list.add(br.readLine());
        }

        for (int i = 0; i < N; i++) {
            String str = list.get(i);
            for (int j = 0; j < str.length(); j++) {
                alpahbet[str.charAt(j) - 'A']+= (int) Math.pow(10, str.length()-1-j);
            }
        }

        Arrays.sort(alpahbet);

        int ans = 0;
        int alpha = 25;
        int num = 9;

        while (alpha >= 0) {
            int cur = alpahbet[alpha];
            if (cur == 0)
                break;
            ans += cur * num--;
            alpha--;
        }
        System.out.println(ans);
    }
}

🤔알파벳별로 가중치를 더해서 푸는 문제입니다.
알파벳을 입력받을 경우 예를 들어서 ABCD가 들어올 경우 A = 1000 , B =100 , C = 10, D= 1로 가중치를 부여합니다. 다음 해당 값을 알파벳 배열에 더해줍니다.
위 과정을 반복한 후 가장 높은 가중치를 가진 값을 9부터 시작하여 시작합니다.

😭가장 큰 값을 가진 값부터 시작하는 것은 이해했지만 그 이후의 과정이 감이 잡히지 않아 아래 자료를 참고하여 풀었습니다.
(이런 생각을 할 수 있다는 것이 놀랍네요...)

참고자료 : 티스토리 - 산넘어 산 개발일지
출처 : 백준 - 단어 수학

profile
greenTea입니다.

0개의 댓글