PAT (Top Level) Practise 1004. To Buy or Not to Buy - Hard Version (35)

1004. To Buy or Not to Buy - Hard Version (35)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

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 1:
No 3
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
#define ver(a) a>=97?a-61:(a>=65?a-55:a-48)//0~9 'A'~'Z' 'a'~'z'
const int maxn=62;
int rest=inf,n;
struct node{
	int cnt[maxn];
}want,bead[101],curr;
node add(node a, node b) {
	node c;
	for (int i = 0; i < 62; i++) c.cnt[i] = a.cnt[i] + b.cnt[i];
	return c;
}
bool check(node curr){
	for(int i=0;i<maxn;++i)if(curr.cnt[i]<want.cnt[i])return false;
	return true;
}
int num(node curr){
	int ans=0;
	for(int i=0;i<maxn;++i)ans+=curr.cnt[i]-want.cnt[i];
	return ans;
}
int cnt=0;
void dfs(node curr,int id){
	++cnt;
	if(cnt>1000)return;	//凑出来的强行剪枝。。 
	if(rest<=num(curr))return;
	if(check(curr)){
		rest=num(curr);
		return;
	}
	for(int i=id;i<n;++i)dfs(add(curr,bead[i]),i+1);
}
int main(){
	ios::sync_with_stdio(false); 
	string str;
	cin>>str;
	for(int i=0;i<str.length();++i)want.cnt[ver(str[i])]++;
	cin>>n;
	for(int i=0;i<n;++i){
		cin>>str;
		for(int j=0;j<str.length();++j)bead[i].cnt[ver(str[j])]++;
	}
	int other=0;
	for(int i=0;i<maxn;++i)
		if(want.cnt[i]){
			int sum=0;
			for(int j=0;j<n;++j){
				sum+=bead[j].cnt[i];
				if(sum>=want.cnt[i])break;
			}
			if(sum<want.cnt[i])other+=want.cnt[i]-sum;
		}
	if(other)cout << "No " << other;//珠子不够的情况 
	else{
		dfs(curr,0);
		cout << "Yes " << rest;
	} 
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值