PAT「1004 To Buy or Not to Buy  Hard Version (35分)」
1. 题目
题目链接：PAT「1004 To Buy or Not to Buy  Hard Version (35分)」 。
Description
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 [09]
, [az]
, and [AZ]
to represent the colors. In sample $1$, buying the $2$nd 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$ ($\leq 100$) 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:
1 

Sample Output 1:
1 

Sample Input 2:
1 

Sample Output 2:
1 

2. 题解
分析
初一看题，以为是 dp，然后发现状态太多了，根本无法 dp，左思右想没有好办法，只能暴力 dfs + 剪枝了（如果有其他更好的解法，希望大佬告知 😃 感谢 ），由于 PAT 上的数据太弱了，这都能过！剪枝主要考虑两个：
 如果所有珠子串都用上仍然无法满足要求，则直接退出 bfs
 如果 bfs 当前发现多余的珠子比原来的答案还多，则直接剪掉当前分支；如果当前刚好满足要求且多余的珠子数比原来的答案少，则更新当前答案
代码
1 

本博客所有文章除特别声明外，均采用 CC BYSA 4.0 协议 ，转载请注明出处！