我想开发一种递归自动换行算法,它采用指定的字符串和换行长度(一行上的最大字符数)来返回输入长度的换行输出。我不希望它分崩离析。例如,长度为20的This is the first paragraph that you need to input
返回为:
This is the first
paragraph that you
need to input
我已经实现了动态编程(自下而上)解决方案,但我想知道是否可以使用递归(自顶向下)编写算法来执行此操作?如果可以的话,我也想记住它。请不要给我任何可运行的代码......我只是想知道想法/伪代码。
答案 0 :(得分:2)
下面的伪代码应该可以工作。 (如果我犯了错误,我确定我们会得到评论!)
function Wrap(the_text,line_len)
if length(the_text) > line_len then
text_bit = the first few words of the_text, keeping their length shorter than line_len
remove text_bit from the beginning of the_text
return text_bit + linefeed + Wrap(the_text, line_len)
else
return the_text
end if
end function
答案 1 :(得分:0)
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);
}
答案 2 :(得分:0)
以下代码将帮助您获得该问题的最佳成本。
#include<bits/stdc++.h>
using namespace std;
// method to get the optimal cost
int findOptimalCost(int *arr, int s, int e,int lineLength,map<pair<int,int>,int>dp) {
if(s>=e) // return 0 for the last line because we are not calculating the last line space
return 0;
if(dp.find({s,e}) != dp.end()) { // return cost if we already calculate
return dp[{s,e}];
}
int minCost = INT_MAX;
for(int i=s;i<=e;i++) {
int sum = 0,space=i-s;
for(int j =s; j<=i; j++)
sum += arr[j]; // add the word length
sum += space; // add the space for words (if 2 word then we will count 1 space )
int cost;
if(sum<=lineLength)
cost = (lineLength-sum)*(lineLength-sum) + findOptimalCost(arr,s+1+space,e,lineLength,dp); // calculate the cost for perticular line and call for rest line
if(minCost > cost) {
minCost = cost; // update the minCost variable if the latest cost is less then the previous calculated cost
}
}
return dp[{s,e}] = minCost; // store the minimum cost for particular line and return
}
int main()
{
//code
int len = 4; // total word in the list
int arr[] = {3,2,2,5}; // let us assume the length of word
int lineLength = 6; // size of max line length
map<pair<int,int>,int> dp;
cout<<findOptimalCost(arr,0,len-1,lineLength,dp)<<endl;
return 0;
}