码迷,mamicode.com
首页 > 其他好文 > 详细

To Buy or Not to Buy - Hard Version

时间:2020-02-09 00:38:57      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:any   The   turn   tween   contains   ++   otto   ext   not   

Eva would like to make a string of beads with her favorite colors so she went to a small shop to buy some beads. There were many colorful strings of beads. However the owner of the shop would only sell the strings in whole pieces. Hence in some cases Eva might have to buy several strings to get all the beads she needs. With a hundred strings in the shop, Eva needs your help to tell her whether or not she can get all the beads she needs with the least number of extra beads she has to pay for.

For the sake of simplicity, let‘s use the characters in the ranges [0-9], [a-z], and [A-Z] to represent the colors. In sample 1, buying the 2nd and the last two strings is the best way since there are only 3 extra beads. In sample 2, buying all the three strings won‘t help since there are three R beads missing.

Input Specification:

Each input file contains one test case. Each case first gives in a line the string that Eva wants. Then a positive integer N (≤) is given in the next line, followed by N lines of strings that belong to the shop. All the strings contain no more than 1000 beads.

Output Specification:

For each test case, print your answer in one line. If the answer is Yes, then also output the least number of extra beads Eva has to buy; or if the answer is No, then also output the number of beads missing from all the strings. There must be exactly 1 space between the answer and the number.

Sample Input 1:

RYg5
8
gY5Ybf
8R5
12346789
gRg8h
5Y37
pRgYgbR52
8Y
8g
 

Sample Output 1:

Yes 3
 

Sample Input 2:

YrRR8RRrY
3
ppRGrrYB225
8ppGrrB25
Zd6KrY
 

Sample Output 2:

No 3
  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include <cstdio>
  3 #include <cstdlib>
  4 #include <cstring>
  5 #include <cctype>
  6 #include <climits>
  7 #include <algorithm>
  8 using namespace std;
  9 const int N = 1010;
 10  
 11 struct bead {
 12     int arr[62], len;
 13     char str[N];
 14     void read(void) {
 15         scanf("%s", str);
 16         return;
 17     }
 18     void convert(void) {
 19         int i;
 20         len = strlen(str);
 21         memset(arr, 0, 62 * sizeof(int));
 22         for (i = 0; str[i] != \0; i++) {
 23             if (isdigit(str[i])) {
 24                 arr[str[i] - 0]++;
 25             }
 26             else if (isupper(str[i])) {
 27                 arr[str[i] - A + 10]++;
 28             }
 29             else {
 30                 arr[str[i] - a + 36]++;
 31             }
 32         }
 33         return;
 34     }
 35 };
 36  
 37 struct record {
 38     int arr[62];
 39     int num;
 40 };
 41  
 42 void DFS(bead arr[], int index, int n, bead &target);
 43 void cmp(bead *arr, bead &target, record &r);
 44 int min_extra, residue, extra;
 45 bool *visit;
 46 int main(void) {
 47     int n, i;
 48     bead *arr, target;
 49     record r;
 50     setvbuf(stdin, new char[1 << 20], _IOFBF, 1 << 20);
 51     target.read();
 52     target.convert();
 53     scanf("%d", &n);
 54     visit = new bool[n];
 55     arr = new bead[n];
 56     for (i = 0; i < n; i++) {
 57         arr[i].read();
 58         arr[i].convert();
 59     }
 60     residue = target.len;
 61     for (i = 0; i < n && residue; i++) {
 62         cmp(arr + i, target, r);
 63     }
 64     if (residue) {
 65         printf("%s %d", "No", residue);
 66     }
 67     else {
 68         min_extra = INT_MAX;
 69         for (i = 0; i < n && min_extra; i++) {
 70             residue = target.len;
 71             extra = 0;
 72             memset(visit, 0, n * sizeof(bool));
 73             target.convert();
 74             DFS(arr, i, n, target);
 75         }
 76         printf("%s %d", "Yes", min_extra);
 77     }
 78     return 0;
 79 }
 80  
 81 void DFS(bead arr[], int index, int n, bead &target) {
 82     int i, j;
 83     record r;
 84     memset(&r, 0, sizeof(record));
 85     cmp(arr + index, target, r);
 86     extra += (arr[index].len - r.num);
 87     visit[index] = true;
 88     if (r.num && extra < min_extra && residue) {
 89         for (i = index + 1; i < n; i++) {
 90             if (!visit[i]) {
 91                 DFS(arr, i, n, target);
 92             }
 93         }
 94     }
 95     if (extra < min_extra && !residue) {
 96         min_extra = extra;
 97     }
 98     visit[index] = false;
 99     for (i = 0; i < 62; i++) {
100         target.arr[i] += r.arr[i];
101     }
102     residue += r.num;
103     extra -= (arr[index].len - r.num);
104     return;
105 }
106  
107 void cmp(bead *arr, bead &target, record &r) {
108     int i;
109     for (i = 0; i < 62 && residue; i++) {
110         if (target.arr[i] && arr->arr[i]) {
111             if (target.arr[i] >= arr->arr[i]) {
112                 target.arr[i] -= arr->arr[i];
113                 residue -= arr->arr[i];
114                 r.arr[i] = arr->arr[i];
115                 r.num += arr->arr[i];
116             }
117             else {
118                 residue -= target.arr[i];
119                 r.arr[i] = target.arr[i];
120                 r.num += target.arr[i];
121                 target.arr[i] = 0;
122             }
123         }
124     }
125     return;
126 }

 

To Buy or Not to Buy - Hard Version

标签:any   The   turn   tween   contains   ++   otto   ext   not   

原文地址:https://www.cnblogs.com/SkystarX/p/12285819.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!