(蓝桥杯)邮票--模拟组合的子集dfs勉勉强强过了。。

本文介绍了如何使用深度优先搜索(DFS)解决组合问题,包括不允许重复和允许重复数字的组合。首先讲解了如何通过DFS求解无重复数字的组合,接着讨论了当数字可以重复使用时的组合总和问题。最后,将这些方法应用于邮票问题,即在给定面值和数量限制下,找出所有可能的邮票组合来达到特定总和,这里利用一个布尔数组记录组合的可能性。虽然DFS在大规模问题上效率较低,但在这个问题规模较小的情况下仍可适用。
摘要由CSDN通过智能技术生成

题目

在这里插入图片描述

拿到这道题,可能很多人,会想到可不可以用多重背包的模板,毕竟也是给定次数,然而完全不能,千万别走偏,这个给定次数是所有面值共有的,而不是每个面值单独设置次数。

一、你是如何用dfs递归求组合的?

递归求组合数,这个方法就是dfs回溯。当达到目的就返回之前的路径重新选择。

具体某题
组合
具体代码

思考留给你们自己,我就不画蛇添足了


class Solution {
   
    
public:
    vector<vector<int>> combine(int n, int k) {
   
        dfs(temp,n,k,0);
        return res;
    }

private:
    vector<int>temp;
    vector<vector<int>>res;
void  dfs(vector<int>&temp,int n,int k,int x){
   
        if(n-x+temp.size()<k)return;//对本已无法进行配对的情况进行优化
        if(temp.size()==k){
   
            res.push_back(temp);
            return
DESCRIPTION: 1.Analyze Problem A : sorted stamps array A={ai} ai: one stamp element in array n: array size, that is number of elements in array r: desired value of stamps F(n,r):expected result, minimum number of stamps for a given value r from n size array. S: selected stamps array S={si} 2.Choose Algorithm a.Greedy algorithm seems to be a good choice, try to solve it in O(n), i try divide array into subarry B={bi}, r should larger than every elemnt in B that is r>bi and suppose bk is the smallest element in B, so that r= bk%r, f(i,r)=(bk/r), F(n,r)=∑f(i,r). The main idea is to choose the last element who larger than desired value each time. However,it can not give us optimal solution in some condition, like A={8,5,4,1}, if r=10, this algoritm will give a solution F(n,r)=3, S={8,1,1},but the optimal solution should be F(n,r)=2, S={5,5}. b.Full search so the straight forwards algorithm is to search for every solution in A for desired value directly.However, it will always take O(n!) to go through every combination. c.Dynamic programming, at last, I decide to choose dynamic programming. analyze optimal structure, suppose in A={ai}, for a specific stamp ak,there will be two cases it is choosen so that f(i,r)=1+f(i,r-ak) , 1<=i<=k, r>=ak it is not valid so that f(i,r)=f(i-1,r) 3.Design Dynamic programming optimal structure: Compute-opt(r)= 1 + Compute-opt(r-ai) value: Compute-opt(r) = ∞ (r < 0) Compute-opt(r) = 0 (r = 0) Compute-opt(r) = 1+{Compute-opt(r-ai)} ( 1=<i<=n, r>ai>0 ) Complexity :O(nr) Memory cost:O(n+r) Compute in a bottom-up style to recursive every desired value and array. store value of Compute-opt in memory for future use, so that we can easily get value from those memory in future recursive call, and avoid compute again, that is if the array is not change, you can easily fetch result any desired value j (j < r, r is the value using for compute ). 2.For User totally, I design a small command line for this machine list below 1.Manual Operation 2.Self Auto Testing 3.Check Results q.Quit Manual Operation: when select this machine will turn to be manual mode, ask person to input stamps and desired value; Self Auto Testing:when select this machine will turn to be auto mode, show the test case already design in code after that machine will quit automatically. Check Results: only be visiable in Manual Operation, people can check desired value for the array input before, the desired value should be no more than first time input. Quit, clean all the memory and quit system.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值