주어진 단어들에서 각 문자에 숫자를 할당하여 최대 합을 구하는 문제이다.

alpha 배열에 저장한다.alpha 배열을 내림차순으로 정렬한다.import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
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[] alpha = new int[26];
for(int i = 0; i < N; i++) {
String str = br.readLine();
for(int j = 0; j < str.length(); j++) {
alpha[str.charAt(j) - 'A'] += (int)Math.pow(10, str.length() - j - 1);
}
}
Arrays.sort(alpha);
int sum = 0;
int idx = 25;
int num = 9;
while(alpha[idx] > 0) {
sum += alpha[idx] * num;
idx--;
num--;
}
System.out.println(sum);
}
}
max 변수에 저장하여 출력한다.import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Main {
static int N;
static int max = Integer.MIN_VALUE;
static List<Character> list;
static String[] word;
static int[] value;
static boolean[] visited;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
list = new ArrayList<>();
word = new String[N];
for(int i = 0; i < N; i++) {
word[i] = br.readLine();
for(int j = 0; j < word[i].length(); j++) {
if(!list.contains(word[i].charAt(j))) list.add(word[i].charAt(j));
}
}
value = new int[list.size()]; // 각 문자에 할당된 숫자 저장하는 배열
visited = new boolean[10]; // 0 ~ 9 숫자 사용 여부
permutation(0);
System.out.println(max);
}
private static void permutation(int cnt) {
if(cnt == list.size()) {
int sum = 0;
for(int i = 0; i < N; i++) {
int num = 0;
for(int j = 0; j < word[i].length(); j++) {
num *= 10;
num += value[list.indexOf(word[i].charAt(j))];
}
sum += num;
}
max = Math.max(max, sum);
return;
}
for(int i = 0; i <= 9; i++) {
if(visited[i]) continue;
visited[i] = true;
value[cnt] = i;
permutation(cnt + 1);
visited[i] = false;
value[cnt] = 0;
}
}
}