把阿里内推时做的那题做了一便,该写的坑还是得填。。
// 自己的写的,不一定对,但是能过给的样例。
// 主要思路就是 深度搜索,终止条件就是 不能再加组合时,和best对比。

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int bom[9][10];
int product[10];
int cnt[10];
int res_count[10];
int res_sum = 0;
int zero_count = 0;
int n,m;
char c ;

void isbest(){
    int tmp_count = 0;
    int sum = 0;
    for(int i =0; i <m;i++){
        if(product[i] == 0){
            tmp_count ++;
        }
        sum += product[i];
    }
    if(tmp_count>zero_count ||(tmp_count == zero_count&&sum< res_sum)){
        zero_count = tmp_count;
        for(int i =0; i < m;i++){
            res_count[i] =     cnt[i];
        }
        res_sum = sum;
    }
}
// m 为组合数量
void dfs(int m){
    for(int i =0; i < m;i++){
        if(product[i] <0)
            return;
    }
    isbest();
    for(int i = 0; i < m;i++){
        cnt[i]++;
        // 使用组合i
        for(int j = 0;j < m;j++){
            product[j] -= bom[i][j];
        }
        dfs(m);
        // 回滚组合i
        for(int j = 0;j < m;j++){
            product[j] += bom[i][j];
        }
        cnt[i]--;
    }
}
int main(){

    // m 为组合数, n为商品数目
    cin >>n>>c>>m;
    // 读取商品数量
    for(int i = 0 ; i < n;i++){
        if(i <n-1){
            cin >> product[i];
            cin >> c;
        }else if(i == n-1) {
            cin >> product[i];
        }
    }
    cin.ignore();
    //读取组合
    string s;
    stringstream str;
    for(int i = 0; i < m;i++){
        getline(cin,s,'\n');
        int pos = s.find(",");
        int start = 0;
        int n_copy = 0;
        while(n_copy<n+1){
            if(n_copy >0&n_copy < n){
                str << s.substr(start,pos-start) <<endl;
                 str>>bom[i][n_copy-1];
                 str.str("");
            }else if(n_copy == n){
                str << s.substr(start,s.size()-start)<<endl;
                 str>>bom[i][n_copy-1];
                 str.str("");
            }
            start = pos+1;
            pos = s.find(",",start);
            n_copy++;
        }
    }
    dfs(m);

    for(int i =0; i<m;i++)
    {
        if(res_count[i] != 0)
            cout << "bom" <<i+1 <<"*"<< res_count[i]<<endl;
    }

    return 0;
}