【算法导论-动态规划】整齐打印

image
python实现

word = ["optimal", "substructure", "in", "the", "following", "way"]


def Printing_Neatly(l, n, M):
    """
    :param l: 包含每个单词的长度的数组
    :param n: 单词的个数
    :param M:  一行所能容纳的字符个数
    :return:
    """
    Ext = [[0 for i in range(n + 1)] for j in range(n + 1)]
    LCost = [[0 for i in range(n + 1)] for j in range(n + 1)]
    for i in range(1, n + 1):
        Ext[i][i] = M - l[i - 1]
        for j in range(i + 1, n + 1):
            Ext[i][j] = Ext[i][j - 1] - l[j - 1] - 1  # compute extra space
    for i in range(1, n + 1):
        for j in range(i, n + 1):
            if Ext[i][j] < 0:
                LCost[i][j] = sys.maxsize  # this line don’t fit, to set the cost is infinity
            elif Ext[i][j] >= 0 and j == n:
                LCost[i][j] = 0
            else:
                LCost[i][j] = (Ext[i][j]) ^ 3
    C = [0 for i in range(n + 1)]
    # 计算最小代价而且找到最小代价的每行换行处。
    # C[j]意味着从单词1到单词j的最优总代价(代价=立方和)

    # R[]用于打印解决方式
    R = [0 for i in range(n + 1)]
    for j in range(1, n + 1):
        C[j] = sys.maxsize
        for i in range(1, j + 1):
            if C[i - 1] != sys.maxsize and (LCost[i][j] + C[i - 1]) < C[j]:
                C[j] = LCost[i][j] + C[i - 1]
                R[j] = i

    print_lines(R, n)


def print_lines(R, j):
    no = 0
    i = R[j]
    if i == 1:
        no = 1
    else:
        no = print_lines(R, i - 1) + 1
    print("Line number:", no, end=", ")
    for t in range(i, j):
        print("", word[t - 1], end=" ")
    print("")
    return no  # 行号


if __name__ == '__main__':
    n = len(word)
    l = [0 for i in range(n)]
    for i in range(n):
        l[i] = len(word[i])
    Printing_Neatly(l, n, 20)
posted @ 2021-04-15 00:59  Eric小星  阅读(320)  评论(0编辑  收藏  举报