Voice-over: Did not want to do 15-2 question is also a cost of a struggle, it seems that "introduction to the algorithm" in the question is not a vain
Neat printing problem: Consider the problem of printing a piece of paper neatly on a printer. The body of the input is a sequence of n lengths of words consisting of L1, L2 、......、 Ln (measured in number of characters). We want this paragraph to be printed neatly on some lines, with a maximum of M characters per line. The standard of "cleanliness" is as follows: If a row contains a word from I to J (I<j), and only one space is left between the words, the number of extra space characters at the end of the line is M-(j-i)-(Li + ... + Lj), which must be non-negative to allow the row to accommodate the words. We want all rows (except the last row) to be at the end of the number of extra space characters in the cube and the smallest. Please give a dynamic programming algorithm to print an article of n words neatly in the printer. Analyze the execution time and space requirements of the given algorithm.
On the Internet very carefully read a few about this neat print blog, found that really is wrong ! Very deceptive! I will not open the portal, although the idea will be some inspiration but less an accurate version to solve the problem.
The core idea of this problem is still dynamic programming, and the key of dynamic programming is to find the optimal substructure of the whole solution process.
We declare an array to store the dynamic optimal solution as the word length increases op[] (in the following code to solve the problem using vector dynamic allocation in order to achieve a random length), in the process of dynamic programming, the Nth-class optimal substructure We need to calculate is 1) n-1 to level 1 of the optimal solution 2) The cost of a continuous string of I-to-j words under limit length (weight).
The formula is expressed as Op[n]=min{op[k-1]+cost (K,n) ...}////k the value of the loop traversal K from N to 1, the entire calculation process is the process of State transfer
/* When K==n, indicates that the most optimized structure of the preceding n-1 words does not need to be aligned, add directly to the new line */
In order to minimize the time complexity of the algorithm, we can make K traverse from N to small, so that we can detect the current time from K to n words is more expensive than the limit of a line to jump out of the loop.
Run tests multiple times no problem found, please help me find some bugs
#include <string> #include <iostream> #include <vector> #include <cstdlib> #define Max_val 999999using namespace Std;int g_ilast=0;int g_ilimit=max_val;int g_ilen=0;vector<int> G_vecop; Optimal amount for weightvector<string> g_vecword; Storing the wordsvector<int> g_vecindex;int space (int index) {//////////index>=1int total=g_ilimit; for (int i=1;i<=g_ilen;i++) {if (G_vecindex[i]==index) {total-=g_vecword[i-1].size (); total--;}} return total;//////A bug!} int space (int i,int j) {///////1<=i<=j<=lenint total=0;for (int x=i;x<=j;x++) {total+=g_vecword[x-1].size ( );} return g_ilimit-j+i-total;} void optimal () {int icur=1;for (int i=1;i<=g_ilen;i++) {//////descend Order to Save more Timeg_vecop[i]=max_val;int Ispace=space (icur); int isize=g_vecword[i-1].size (); if (ispace>=isize) {////////The space enough to Appendg_ vecindex[i]=icur;g_vecop[i]=g_ilast+ (ispace-isize) * (ispace-isize) * (ispace-isize); else{///////If Noticur++;g_ilast=g_vecOp[i-1];int min=max_val;for (int j=i;j>1;j--) {int s=space (j,i), if (s<0) Break;int temp=g_vecop[j-1]+s*s*s;if ( Min>temp) {min=temp;for (int m=j;m<=i;m++) {g_vecindex[m]=icur;}}} G_vecop[i]=min;g_vecindex[i]=icur;}} cout<< "The cubic minimum of Blank is" <<G_VECOP[G_ILEN]<<ENDL<<ENDL;} int main () {cout<< "Input The total amount of your Word:" <<ends;cin>>g_ilen;/////////////////////// String sztmp;for (int i=0;i<g_ilen;i++) {cin>>sztmp;g_vecword.push_back (sztmp);} cout<< "Input your maximun digits of letters allowed in a line:" <<ends;cin>>g_ ilimit;for (int i=0;i<=g_ilen;i++) {g_vecop.push_back (0); g_vecindex.push_back (0);} Initializing completeoptimal ();//////////////////////outputtingint now=1;for (int i=1;i< =g_ilen;i++) {if (g_vecindex[i]>now) {now++;cout<<endl<<g_vecword[i-1]<< ' <<ends;} else{cout<<g_vecword[i-1]<< ' <<ends;}} Return0;}
Introduction to algorithms study Questions 15-2 neat print