50058. Word Selection
難度:4/5
Second Try: 2/5 Used time: 11:40
Using bit operations can increase speed and decrease using array.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
int n;
int cost[32];
int arr[32][26] = {0};
int min_cost = INT_MAX;
int get_str(int num){
char s[64];
scanf("%s %d", s, &cost[num]);
int s_len = strlen(s);
for(int i = 0; i < s_len; i++) arr[num][s[i] - 'a'] = 1;
}
void dfs(int now_idx, int now_cost, int now_have[26]){
if(now_cost >= min_cost) return;
if(now_idx == n){
for(int i = 0; i < 26; i++) if(now_have[i] == 0) return;
min_cost = now_cost;
}
int tmp[26];
for(int i = 0; i < 26; i++) tmp[i] = now_have[i];
// choose
for(int i = 0; i < 26; i++) if(arr[now_idx][i] == 1) tmp[i] = 1;
dfs(now_idx + 1, now_cost + cost[now_idx], tmp);
// not choose
dfs(now_idx + 1, now_cost, now_have);
}
int main(){
scanf("%d", &n);
for(int i = 0; i < n; i++) get_str(i);
int now_have[26] = {0};
dfs(0, 0, now_have);
printf("%d\n", min_cost);
return 0;
}