gpt4 book ai didi

string - 最小破弦成本的动态规划

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:38:00 25 4
gpt4 key购买 nike

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.

这道题来自《算法》chapter6 6.9.

既然这个问题没有答案,我就是这么想的。

定义OPT(i,j,n)作为断弦的最低成本,i对于起始索引,j对于 String 和 n 的结束索引我可以使用的剩余切割次数。

这是我得到的:

OPT(i,j,n) = min {OPT(i,k,w) + OPT(k+1,j,n-w) + j-i} for i<=k<j and 0<=w<=n

对不对?请帮忙,谢谢!

最佳答案

我觉得你的递归关系可以变得更好。这是我想出的,将 cost(i,j) 定义为将字符串从索引 i 切割到 j 的成本。然后,

cost(i,j) = min {length of substring + cost(i,k) + cost(k,j) where i < k < j}

关于string - 最小破弦成本的动态规划,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19666102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com