1004 To Buy or Not to Buy - Hard Version (35分)
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 (≤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:
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
思想:本题理论复杂度很高,但可以用DFS+剪枝通过
思路见代码里的注释。
#include <bits/stdc++.h>
using namespace std;
string BeadString[127];
int num[127];
int n;
int Max = 0x3f3f3f3f;
string alpha = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
int beads[127][127];//统计每串珠子的个数
void dfs(int now,int cost,int remain){
if(cost>=Max){//可行性剪枝
return;
}
if(remain==0){
Max = min(Max,cost);
return;
}
for(int i=now+1;i<=n;++i){
vector<int> v;//用于记录哪些数字变化了
v.resize(127);
int cnt = 0;
int tmp = remain;
for(int j=0;j<BeadString[i].size();++j){
int pos = BeadString[i][j];
int have = beads[i][pos];
int need = beads[0][pos];
if(have>=need){
cnt += have-need;
remain -= need;
beads[0][pos] = 0;
v[pos] = need;
}
else{
beads[0][pos] -= have;
remain -= have;
v[pos] = have;
}
}
dfs(i,cost+cnt,remain);
for(int j=0;j<BeadString[i].size();++j){
int pos = BeadString[i][j];
beads[0][pos] += v[pos];
}
remain = tmp;
}
}
int main(){
//Input部分
cin>>BeadString[0];
cin>>n;
for(int i=1;i<=n;++i){
cin>>BeadString[i];
}
//预处理部分
for(int i=0;i<=n;++i){
for(int j=0;j<BeadString[i].size();++j){
int pos = BeadString[i][j];
beads[i][pos]++;//统计某一串珠子的个数
if(i) num[pos]++;//统计所有珠子的个数
}
if(i){//把每串珠子去重,方便后期遍历
sort(BeadString[i].begin(),BeadString[i].end());
BeadString[i] = BeadString[i].substr(0,unique(BeadString[i].begin(),BeadString[i].end())-BeadString[i].begin());
}
}
int failed = 0;
for(int i=0;i<alpha.size();++i){
int have = num[(int)(alpha[i])];
int need = beads[0][(int)(alpha[i])];
if(have<need){
failed+=need-have;
}
}
if(failed){
cout<<"No"<<" "<<failed<<endl;
}
else{
dfs(0,0,BeadString[0].size());
cout<<"Yes"<<" "<<Max<<endl;
}
return 0;
}