Today is the judgment day. The world is ending and all man will pay for their guilt and sin. Now the Almighty God has a long string, and some people’s name. He will take some letters from the string and use them to spell out those people’s name. What is the maximum number of the names that can be spelled, with each name be spelled at most once?
Input
The first line of input contains a number T, indicating the number of test cases. (T≤30) For each test case, the first line contains a string s, which is the string the God has. The following line contains a number n, the number of people’s names. Then n lines follow, each with a string indicating the names. There will be no more than 10 names in each test case, the length of string s will not exceed 100,000, and the length of each name will not exceed 100,000. All the strings contains lowercase letters from a
to z
only.
Output
For each case, output Case #i:
first. (i is the number of the test case, from 1 to T). Then output a single number, as the maximum number of the people whose name can be spelled.
Sample input and output
Sample Input | Sample Output |
---|---|
2 abc 3 aa bb cc aabbc 3 abc aa bb |
Case #1: 0 Case #2: 2 |
Source
The 11th UESTC Programming Contest Final
解题报告:
先扫出每个名字要多少个字符,之后暴力枚举拼的方式并更新ans
#include <iostream> #include <cstring> #include <algorithm> using namespace std; int letter[26+5]; int cost[10+3][26+5]; int ans; char buffer[100000 + 50]; int main(int argc , char * argv[]) { int Case,T=1; scanf("%d%*c",&Case); while(Case--) { memset(letter,0,sizeof(letter)); memset(cost,0,sizeof(cost)); int len,n; gets(buffer);len = strlen(buffer); for(int i = 0 ; i < len ; ++ i ) letter[buffer[i]-‘a‘]++; scanf("%d%*c",&n); for(int i = 0 ; i < n ; ++ i) { gets(buffer);len = strlen(buffer); for(int j = 0 ; j < len ; ++ j ) cost[i][buffer[j]-‘a‘]++; } int allcost[26 + 5]; ans = 0; for(int i = 1 ; i < (1<<n) ; ++ i) { memset(allcost,0,sizeof(allcost)); int newans = 0; for(int j = 0 ; j < n ; ++ j) if ( ( i >> j ) & 1) { newans++; for(int k = 0 ; k < 26 ; ++ k) allcost[k] += cost[j][k]; } int flag = 1; for(int j = 0 ; j < 26 ; ++ j) if (allcost[j] > letter[j]) { flag = 0; break; } if (flag) ans = max(ans,newans); } printf("Case #%d: %d\n",T++,ans); } return 0; }