코딩블로그
백준 1339 골드 4 - 단어 수학 본문
728x90
https://www.acmicpc.net/problem/1339
1339번: 단어 수학
첫째 줄에 단어의 개수 N(1 ≤ N ≤ 10)이 주어진다. 둘째 줄부터 N개의 줄에 단어가 한 줄에 하나씩 주어진다. 단어는 알파벳 대문자로만 이루어져있다. 모든 단어에 포함되어 있는 알파벳은 최대
www.acmicpc.net
import java.util.*;
import java.lang.*;
import java.io.*;
// The main method must be in a class named "Main".
class Main {
public static int N,K;
public static int[] arr = new int[26];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//단어 개수
N = Integer.parseInt(br.readLine());
int max = 9;
//초기화
for (int i = 0; i < N; i++) {
String str = br.readLine();
for (int j = 0; j < str.length(); j++) {
char c = str.charAt(j);
arr[c-'A'] += (int)Math.pow(10, str.length() - 1 - j);
}
}
Arrays.sort(arr);
int turn = 25;
int ans = 0;
while (arr[turn] != 0) {
ans += arr[turn] * max;
turn--;
max--;
}
System.out.println(ans);
}
}
배운 것
arr[c-'A'] += (int)Math.pow(10, str.length() - 1 - j)
가져온 문자 c의 ASCII 값에서 'A'의 ASCII 값을 빼어서 arr 배열의 인덱스로 사용한다. 그리고 Math.pow는 해당 인덱스에 문자열의 길이에서 현재 위치 j를 뺀 값에 10의 거듭제곱을 곱한 값을 더한다.
이는 해당 문자가 등장한 위치에 따라 가중치를 부여하는 부분이다
놓친 것
알파벳 전체 배열로 그냥 int[26]으로 해도 되는데 처음부터 막 HashMap쓸까? 이런 접근을 해서 풀 수 가 없었다
728x90