1004 To Buy or Not to Buy - Hard Version (35 分)(C++)

PAT顶级题解目录​​​​​​​

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

题目大意:Eva要买一串珠子,商店有已经串好的n串,只能一串一串的买,现在请问Eva是否能把她想要的颜色都买走,能的话请问他最少要多买几个珠子,如果不能的话,请问还缺多少珠子。

解题思路:DFS递归

注意:最后两个测试点容易超时,所以我在DFS递归200次之后就停止递归(其实有点讨巧~),本题的数据不够严谨,如果出现超过200次迭代,且答案为“Yes”,本题代码就需要改动了。

代码:

#include <bits/stdc++.h>
using namespace std;
vector<int> need(62,0);
int graph[105][62]={0};
int n, res=0x3f3f3f3f, ans = 0x3f3f3f3f, needcnt, num = 0, k = 0;
bool flag = false;
int char2index(char ch){
    if(isdigit(ch))
        return ch - '0';
    else if(ch >= 'a' && ch <= 'z')
        return ch -'a' + 10;
    else
        return ch - 'A' + 36;
}
void dfs(int index){
	k += 1;
    if(needcnt == 0){
        ans = min(num, ans);
        flag = true;
        return;
    }
    if(!flag)
        res = min(res, needcnt); 
    if(index == n || k >200)
        return;
    vector<int> temp(62, 0);
    int tempcnt = 0, tempsum = 0;
    for(int i = 0; i < 62; ++ i){
        if(graph[index][i]){
            int x = min(need[i], graph[index][i]);
            tempsum += graph[index][i];
            tempcnt += x;
            need[i] -= x;
            temp[i] = x;
        }
    }
    if(tempcnt != 0){
        needcnt -= tempcnt;
        num += tempsum-tempcnt;
        dfs(index+1);
        for(int i = 0; i < 62; ++ i)
            need[i] += temp[i];
        needcnt += tempcnt;
        num -= tempsum-tempcnt;
    }
    dfs(index+1);
}
int main(){
    string s;
    cin >> s >> n;
    for(char c : s)
        ++ need[char2index(c)]; 
    needcnt = s.length();
    for(int i = 0; i < n; ++ i){
        cin >> s;
        for(char c: s)
            ++ graph[i][char2index(c)];
    }
    dfs(0);
    printf("%s %d",flag ? "Yes" : "No", flag ? ans : res);
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章