问题描述:
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 8gSample Output 1:
Yes 3Sample Input 2:
YrRR8RRrY 3 ppRGrrYB225 8ppGrrB25 Zd6KrYSample Output 1:
No 3
这一题和Safe Fruit那一题思路基本一致,dfs+剪枝 算法就可以了,而且,在需要 数组或vector元素 作为局部变量时,最好用 局部备份+全局变量 的方式,否则会很费时间。。。
AC代码:
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
#include<bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; int k,mu; vector<pair<string,int> > v0; vector<int> ul; vector<int> v(62,0); inline int c2i(char a) { if(a<='9'&&a>='0') return a-'0'; else if(a<='z'&&a>='a') return a-'a'+10; else if(a<='Z'&&a>='A') return a-'A'+36; else return -1; } inline void dfs(int n,int lack,int useless) { if(useless>=mu) return; else if(n<v0.size()&&lack) { int clack=lack,cuseless=useless; vector<int> cv=v; for(auto i:v0[n].first) { int ci=c2i(i); if(v[ci]) { v[ci]--; lack--; } else useless++; } dfs(n+1,lack,useless+v0[n].second); v=cv; dfs(n+1,clack,cuseless); } else if(!lack) mu=min(useless,mu); } int main() { // freopen("data.txt","r",stdin); int is,lack=0; mu=999999999; for(;;) { is=c2i(getchar()); if(is>-1) { v[is]++; lack++; } else break; } vector<int> vv(v); int llack=lack; scanf("%d",&k); getchar(); for(;k--;) { string s=""; int useless=0; for(;;) { char c=getchar(); is=c2i(c); if(is>-1) { if(v[is]) { s=s+c; if(vv[is]) { vv[is]--; llack--; } } else useless++; } else break; } if(!s.empty()) v0.emplace_back(make_pair(s,useless)); } if(llack) printf("No %d",llack); else { ul.resize(v0.size(),0); dfs(0,lack,0); printf("Yes %d",mu); } return 0; } |