算法分治和动态规划模板

分而治之

分治代码模板

def divide_conquer(problem,param1,param2,....):
    #recursion terminator
    if problem is None:
        print_result
        return
    #process data
    data = prepare_data(problem)
    subproblems = splite_problem(problem,data)

    #conquer subproblems
    subresult1 = self.divide_conquer(subproblems[0],p1.p2,...)
    subresult2 = self.divide_conquer(subproblems[1],p1.p2,...)
    subresult3 = self.divide_conquer(subproblems[2],p1.p2,...)
    ...

    #process and generate the final result
    result = process_result(subresult1,subresult2,subresult3,..)

    #revert the current level status    
    


DP 顺推模板
function DP():
    dp=[][]# 二维情况

    for i=0.. M{
        for j=0..N{
            dp[i][j] = _Function(dp[i'][j'])        
        }
    }
    
    return dp[M][N]



 

你可能感兴趣的:(数据结构与算法)