如何将此递归函数转换为动态算法?

发布于 2025-01-25 12:18:15 字数 714 浏览 3 评论 0 原文

我目前正在处理这个问题:

某种字符串处理语言提供了一种原始操作,将字符串分为两部分。由于此操作涉及复制原始字符串,因此无论切割的位置如何,长度为n的字符串需要n个单位。现在,假设您想将一根弦分解为许多碎片。休息的顺序会影响总运行时间。例如,如果您想在位置3和10上切一个20个字符的字符串,那么在位置3下切下的第一个切割将产生20+17 = 37 10 = 30。给出一种动态编程算法,鉴于M剪切的位置在长度为n的字符串中,发现将字符串分解为M + 1件的最低成本。

我设法将问题表示为复发,并提出了以下问题的递归解决方案。

def recursive(M, N):
if len(M) == 0:
    return 0
else:
    c_min = float('inf')
    for c in M:
        lt_cuts = [d for d in M if d < c]
        gt_cuts = [e - c for e in M if e > c]
        c_min = min(c_min, N + recursive(lt_cuts, c) + recursive(gt_cuts, N - c))

return c_min

通常,一旦我发现问题的递归形式,将其调整为动态算法就很容易。这次,我一整天都在面对这个问题,找不到单个工作解决方案。谁能给我一些提示或有用的片段?任何事情都将不胜感激。

I am currently working on this problem:

A certain string-processing language offers a primitive operation which splits a string into two pieces. Since this operation involves copying the original string, it takes n units of time for a string of length n, regardless of the location of the cut. Suppose, now, that you want to break a string into many pieces. The order in which the breaks are made can affect the total running time. For example, if you want to cut a 20-character string at positions 3 and 10, then making the first cut at position 3 incurs a total cost of 20+17=37, while doing position 10 first has a better cost of 20+10=30. Give a dynamic programming algorithm that, given the locations of m cuts in a string of length n, finds the minimum cost of breaking the string into m + 1 pieces.

I managed to represent the problem as a recurrence and came up with the following recursive solution to the problem.

def recursive(M, N):
if len(M) == 0:
    return 0
else:
    c_min = float('inf')
    for c in M:
        lt_cuts = [d for d in M if d < c]
        gt_cuts = [e - c for e in M if e > c]
        c_min = min(c_min, N + recursive(lt_cuts, c) + recursive(gt_cuts, N - c))

return c_min

Normally once I find the recursive form of a problem, adapting it to a dynamic algorithm is pretty easy. This time I've been banging my head against this problem for an entire day and I can't find a single working solution. Can anyone give me some hints or useful snippets? Anything would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花坊 2025-02-01 12:18:15

让我们的切口从1到m,我们将其称为字符串“切割0”的开始,并将字符串的末端“切割M+1”。

现在,对您的递归函数的每个调用都计算成本(i,j),这是切割cut i 和剪切的最低成本代码> j ,带有i&lt; j。

在计算成本(i,j)的计算中,您只会对较小区域进行递归调用。如果您调用成本(a,b),则您将拥有(ba)&lt; (JI)。

因此,您可以通过创建一个矩阵成本来实现动态编程实现,其中 cop> cop [i] [j] = cop(i,j)。如果您按增加区域长度(JI)的顺序填充矩阵,则可以确保您需要计算任何费用[i] [j] 的所有条目,将在您的时间之前完成需要他们。

例如,给定所有剪切位置(包括开始和结束) cuts

# costs for all length-1 regions
for i in range(len(cuts)):
    COST[i][i+1] = 0

# costs for other sizes
for size in range(2,len(cuts)+1):
    for i in range(len(cuts)-size+1):
        j = i+size

        # cost for this region if we start by cutting at i+1
        subcost = COST[i][i+1] + COST[i+1,j]

        # costs for other initial cuts
        for cut in range(i+2,j):
            subcost = min(subcost, COST[i][cut] + COST[cut,j])

        # add the cost of the initial cut
        COST[i][j] = subcost + cuts[j] - cuts[i]
            

Lets number the cuts from 1 to m, and we'll call the start of the string "cut 0" and the end of the string "cut m+1".

Now, each call to your recursive function calculates a cost(i,j), which is the minimum cost to cut up the part of the string between cut i and cut j, with i < j.

In the calculation of cost(i,j), you will only make recursive calls for smaller regions. If you call cost(a,b), then you will have (b-a) < (j-i).

Therefore, you can make a dynamic programming implementation by creating a matrix COST, where COST[i][j] = cost(i,j). If you fill the matrix in order of increasing region length (j-i), then you are guaranteed that all the entries that you need to calculate any COST[i][j] will be done by the time you need them.

For example, given all the cut positions (including the start and end) in cuts:

# costs for all length-1 regions
for i in range(len(cuts)):
    COST[i][i+1] = 0

# costs for other sizes
for size in range(2,len(cuts)+1):
    for i in range(len(cuts)-size+1):
        j = i+size

        # cost for this region if we start by cutting at i+1
        subcost = COST[i][i+1] + COST[i+1,j]

        # costs for other initial cuts
        for cut in range(i+2,j):
            subcost = min(subcost, COST[i][cut] + COST[cut,j])

        # add the cost of the initial cut
        COST[i][j] = subcost + cuts[j] - cuts[i]
            
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文