【问题标题】:Recursive Word Wrap Algorithm递归自动换行算法
【发布时间】:2011-03-06 22:39:16
【问题描述】:

我正在研究一种包含三个部分的算法。第一种是递归方法,它将单词以最小的代价包装到特定的长度。第二种是一种算法,它是递归方法的动态实现。最后一个是问题的贪心算法。我已经编写了贪婪的代码,但我在递归解决方案上苦苦挣扎。我不太确定我的递归方法到底在哪里遇到了问题,但我知道它应该类似于 Knuth-Plass 算法。递归算法应该具有阶乘运行时间,并且更多地用于帮助动态解决方案。如果有人有 Knuth-Plass 实现的链接或者可以在我的代码中发现一些巨大的东西,我们将不胜感激。

递归算法:

public static ArrayList<String> recursive(ArrayList<String> input, int size) {
    if(input.size() <= 1)
        return input;
    ArrayList<String> temp1 = input;
    ArrayList<String> temp2 = input;
    for(int i = 0; i < input.size(); i++) {
        if(input.size() - 1 >= size)
            break;
        else {
            for(int j = 0; j < input.size(); j++) {
                temp1.set(j, temp1.get(j) + " " + temp1.get(j + 1));
                temp1.remove(j + 1);
                if(totalPenalty(blankChars(temp1, size)) < totalPenalty(blankChars(temp2, size))) {
                    input = recursive(temp1, size);
                } else {
                    input = recursive(temp2, size);
                }
            }
        }
    }
    return input;
}

totalPenalty() 和 blankChars 返回每行末尾的罚款金额。

编辑:我仍然没有看到任何直接的解决方案。任何帮助将不胜感激。

【问题讨论】:

  • 你为什么明确希望它是递归的?如果可以避免,递归是低效的编程。您最终可能会耗尽堆栈空间。
  • 这是为了让我得到一个动态编程解决方案。基本上我正在尝试递归(糟糕的 big-O,但有助于转换为动态)>动态(更快且始终正确)>贪婪(并不总是正确)。如果这有意义的话。
  • @Chris Dennett:更准确地说,这种递归效率低下,因为:a)它是 Java b)无论如何都没有可以消除的尾调用。然而,即使在这种情况下,如果我们能保证输入相当小,递归也可以帮助我们更清楚地表达算法。
  • @hoha:谢谢你的澄清。
  • 我必须承认我无法理解它应该如何工作。既然你不想使用 Knuth 的方法,你可以用伪代码或算法描述来更新问题文本吗?

标签: java algorithm recursion word-wrap


【解决方案1】:

这看起来像 Java,而在 Java 中没有隐式的复制构造函数。

ArrayList&lt;String&gt; temp1 = input; 不会创建另一个具有相同内容的对象,而是对同一对象的引用。

您需要将第 4 行和第 5 行更改为:

ArrayList<String> temp1 = new ArrayList<String>(input);
ArrayList<String> temp2 = new ArrayList<String>(input);

我没有查找任何其他错误,因此请尝试此操作,如果您还有其他问题,请更新问题。

关于 Knuth-Pass 破断算法;您可以在 http://oedipus.sourceforge.net/texlib/ 找到 Python 实现。我还没有仔细看它,但描述似乎就是你要找的。​​p>

【讨论】:

  • 感谢您的快速响应。以上似乎并没有解决问题。我认为问题出在我的逻辑上。该链接没有那么有用,因为没有实际的实现,而只是框架。我不是在寻找确切的 Knuth-Plass 实现,但在进行研究时,我发现 Knuth-Plass 算法最接近我想要实现的目标。
  • 那里肯定有一个实现。下载链接在底部。
【解决方案2】:

我希望以下代码能够运行。在这里,我还添加了最后一行的成本。尽管文字处理器大部分时间都使用贪婪算法,并且它们忽略了最后一行的成本。让我知道您是否清楚。

import java.lang.Math;

public int RCS(int[] l , int n , int m , int index) {

    // first base condition - if index gets beyond the array 'l' , then return 0;
    if (index > n - 1) return 0;

    // second base condition - if index is the last word i.e there is only one word left in the
    // array to be inserted in the line then return the cost if added in that line.
    if (index == n - 1) return (m - l[n - 1]) * (m - l[n - 1]) * (m - l[n - 1]);

    // make a global cost variable to be returned
    int cost = Integer.MAX_VALUE;

    // Here , we try to select words from the array and apply RCS on the rest of the array.
    // From index to last element , we iteratvely select first , or first two and so on.
    for (int i = index ; i < n ; i++) {
        int current_space_sum = 0 ;
        // we add the length of the selected word. We have selected words in array from index to i.
        for (int k = index ; k <= i ; k++) {
            current_space_sum = current_space_sum + l[k] ;
        }
        // Adding the space between the words choses. If 2 words are chosen , there is one space and so on
        current_space_sum = current_space_sum + i - index;
        // If the length of the chosen words is greater than the line can accept , no need of looking beyond.
        if (current_space_sum > m) break;
        // Iteratively find the minimum cost
        cost =  Math.min(cost , (m - current_space_sum) * (m - current_space_sum) * (m - current_space_sum) + RCS(l , n , m , i + 1));
    }
    return cost;
}



public static void main(String[] args) {
    WordWrap w = new WordWrap();
    int[] l = {3, 2 , 2 , 5};
    int n = l.length;
    int m = 6;
    int result = w.RCS(l , n , m , 0);

    System.out.println(result);
}

【讨论】:

    猜你喜欢
    • 2014-07-20
    • 1970-01-01
    • 2019-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-25
    • 2013-02-14
    相关资源
    最近更新 更多