- backtracking(int idx, int sum, int n)
1. 탐색할 문자열의 인덱스를 의미하는 idx 파라미터와 비용의 합을 관리할 sum,
base condition을 위한 n까지 파라미터로 갖는다.
- backtracking(idx+1, sum + list.get(idx).cost, n)
1. idx번째의 문자열을 선택하는 경우로, 그 비용을 더해서 넘겨준다
2. 재귀식 실행 전에 list.get(idx).name.length만큼 순회하여,
선택 문자 배열인 counts 배열에 현재 선택된 인덱스의 문자열의 문자 알파벳을
인덱스로하여 개수를 증가시켜준다
- backtracking(idx+1, sum, n)
1. idx번째의 문자열을 선택하지 않는 경우로, 비용합산 없이 그대로 넘겨준다
2. 앞서 증가시킨 알파벳 개수를 선택하지 않는 경우이므로, 이번에는 감소시켜준다
- if(idx == n) {...}
1. idx가 n이되면 모든 문자열을 탐색한 것이므로 종료시켜준다
2. 검증 메소드를 통과하는 경우 ans와 sum을 비교하여 더작은 값은 ans에 넣어준다
import java.io.*;
import java.util.*;
class Pair{
int cost;
String name;
public Pair(int cost, String name) {
this.cost = cost;
this.name = name;
}
}
public class Main {
static String word;
static List<Pair> list;
static int[] alpha = new int[26];
static int[] counts = new int[26];
static int ans = Integer.MAX_VALUE;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
word = br.readLine();
for (int i = 0; i < word.length(); i++) {
alpha[word.charAt(i) - 'A']++;
}
int n = Integer.parseInt(br.readLine());
list = new ArrayList<>();
for (int i = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
String b = st.nextToken();
list.add(new Pair(a, b));
}
backtracking(0,0, n);
if(ans == Integer.MAX_VALUE){
bw.write("-1");
}else{
bw.write(ans+"");
}
br.close();
bw.close();
}
private static void backtracking(int idx, int sum, int n) {
if(idx == n){
if (chk()) {
ans = Math.min(ans, sum);
}
return;
}
for (int i = 0; i < list.get(idx).name.length(); i++) {
counts[list.get(idx).name.charAt(i) - 'A']++;
}
backtracking(idx+1, sum + list.get(idx).cost, n);
for (int i = 0; i < list.get(idx).name.length(); i++) {
counts[list.get(idx).name.charAt(i) - 'A']--;
}
backtracking(idx+1, sum, n);
}
private static boolean chk() {
for (int i = 0; i < 26; i++) {
if(alpha[i] > counts[i]){
return false;
}
}
return true;
}
}