Java 递归换行算法

Java 递归换行算法,java,algorithm,recursion,word-wrap,Java,Algorithm,Recursion,Word Wrap,我正在研究一个由三部分组成的算法。第一种是递归方法,它将以最小的代价将单词包装到特定的长度。第二种算法是递归方法的动态实现。最后一个是问题的贪婪算法。我已经有贪婪的一个编码,但我正在努力递归解决方案。我不太确定我的递归方法到底在哪里遇到了问题,但我知道它应该类似于Knuth-Plass算法。递归算法应该有一个阶乘的运行时间,并且更多地用于帮助动态解。如果任何人有一个Knuth Plass实现的链接,或者可以在我的代码中发现一些巨大的东西,任何帮助都将不胜感激 递归算法: public stati

我正在研究一个由三部分组成的算法。第一种是递归方法,它将以最小的代价将单词包装到特定的长度。第二种算法是递归方法的动态实现。最后一个是问题的贪婪算法。我已经有贪婪的一个编码,但我正在努力递归解决方案。我不太确定我的递归方法到底在哪里遇到了问题,但我知道它应该类似于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;
}
publicstaticarraylistrecursive(arraylistinput,int-size){
if(input.size()=size)
打破
否则{
对于(int j=0;j
total罚金()和blankchar返回每行末尾的罚金金额


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

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


ArrayList temp1=输入我希望下面的代码能够运行。在这里,我还添加了最后一行的成本。尽管文字处理器大部分时间使用贪婪算法,并且忽略了最后一行的成本。如果你清楚这一点,请告诉我

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);
}
import java.lang.Math;
公共整数RCS(整数[]l,整数n,整数m,整数索引){
//第一个基本条件-如果索引超出数组“l”,则返回0;
如果(索引>n-1)返回0;
//第二个基本条件-如果索引是最后一个单词,即索引中只剩下一个单词
//要插入到行中的数组,如果在该行中添加,则返回成本。
if(index==n-1)返回(m-l[n-1])*(m-l[n-1])*(m-l[n-1]);
//生成要返回的全局成本变量
int cost=Integer.MAX_值;
//这里,我们尝试从数组中选择单词,并将RCS应用于数组的其余部分。
//从索引到最后一个元素,我们迭代地选择第一个,或前两个,依此类推。
for(int i=索引;i
为什么明确希望它是递归的?如果可以避免,递归编程效率很低。你最终会耗尽堆栈空间。这是为了让我得到一个动态规划的解决方案。基本上,我正在尝试递归(糟糕的大O,但有助于转换为动态)>动态(更快且总是正确)>贪婪(并不总是正确的)。克里斯·丹尼特:说这样的递归效率低会更准确一些,因为:a)它是Java的b)没有可以消除的尾部调用。然而,即使在这样的情况下,如果我们能保证输入相当小,递归也可以帮助我们更清楚地表达算法。@hoha:谢谢你的澄清。我必须承认,我不明白这是怎么回事。既然你不想使用Knuth的方法,你能用伪代码或算法描述更新问题文本吗?谢谢你的快速回复。上述问题似乎没有解决。我认为问题出在我的逻辑上。链接没有那么大帮助,因为没有实际的实现,只有框架。我不是在寻找一个精确的Knuth-Plass实现,但在进行研究时,我发现Knuth-Plass算法是最接近我试图实现的算法。那里肯定有一个实现。下载链接在底部。
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);
}