|
| 1 | +/* |
| 2 | +-WORD WRAP PROBLEM |
| 3 | +-Given an array of size n that contains size of words and maximum width of a line. Arrange the words in such a way that each line does not exceeds the maximum width. |
| 4 | +-Give space after each word except the last one(you can give extra spaces as well) |
| 5 | +-Print 2*L space separated integers, L being the number of lines required to adjust the words with maximum width that shows from which word to word in each line |
| 6 | +
|
| 7 | +-DYNAMIC APPROACH |
| 8 | +-Make all possible lines and store cost of each line in a two dimensional table |
| 9 | +-Optimize the final cost,i.e,calculate the minimum cost arrangement |
| 10 | +-Keep track of from where the optimized cost is coming from |
| 11 | +-Print the optimized cost recursively |
| 12 | +*/ |
| 13 | + |
| 14 | +#include <bits/stdc++.h> |
| 15 | +#define inf INT_MAX |
| 16 | +#define ll long long int |
| 17 | +using namespace std; |
| 18 | +//recursive function to print the required soluton, uses p[] to print the solution. |
| 19 | +ll printwordwrap(ll p[],ll n){ |
| 20 | + //starts traversing from back till it get 1 |
| 21 | + //once 1 is achieved it prints the required value and from where it came from |
| 22 | + ll lineno; |
| 23 | + if(p[n]==1){ |
| 24 | + lineno=1; |
| 25 | + cout<<"Line number "<<lineno<<": From word number "<<p[n]<<" to "<<n<<"\n"; |
| 26 | + } |
| 27 | + else{ |
| 28 | + lineno=printwordwrap(p,p[n]-1)+1; |
| 29 | + cout<<"Line number "<<lineno<<": From word number "<<p[n]<<" to "<<n<<"\n"; |
| 30 | + } |
| 31 | + return lineno; |
| 32 | +} |
| 33 | + |
| 34 | +int main() { |
| 35 | + //no. of words |
| 36 | + ll n; |
| 37 | + cin>>n; |
| 38 | + //length of each word |
| 39 | + ll a[n+1]; |
| 40 | + for(ll i=1;i<=n;i++) |
| 41 | + cin>>a[i]; |
| 42 | + //max width of a line |
| 43 | + ll max; |
| 44 | + cin>>max; |
| 45 | + //space[.][.] contains the number of spaces in a single line |
| 46 | + ll space[n+1][n+1]; |
| 47 | + //cost[.][.] contains the cost of particular line |
| 48 | + ll cost[n+1][n+1]; |
| 49 | + //opcost[.] contains the cost of optimized solution |
| 50 | + ll opcost[n+1]; |
| 51 | + //p[.] for printing the final solution through recursive function printp(...) |
| 52 | + //it keeps track of the path,i.e, from where the optimized cost is coming |
| 53 | + ll p[n+1]; |
| 54 | + //loop for calculating space[.][.] when words are arranged in a line |
| 55 | + for(ll i=1;i<=n;i++) |
| 56 | + { |
| 57 | + space[i][i]=max-a[i]; |
| 58 | + for(ll j=i+1;j<=n;j++) |
| 59 | + { |
| 60 | + space[i][j]=space[i][j-1]-a[j]-1; |
| 61 | + } |
| 62 | + } |
| 63 | + //loop for calculating cost of particular line when words are arranged in asingle line |
| 64 | + for(ll i=1;i<=n;i++) |
| 65 | + { |
| 66 | + for(ll j=i;j<=n;j++) |
| 67 | + { |
| 68 | + if(space[i][j]<0) |
| 69 | + { |
| 70 | + //to avoid the condition when words cannot be arranged in one line |
| 71 | + cost[i][j]=inf; |
| 72 | + } |
| 73 | + else if(j==n && space[i][j]>=0) |
| 74 | + cost[i][j]=0; |
| 75 | + else cost[i][j]=space[i][j]*space[i][j]; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + opcost[0]=0; |
| 80 | + //loop for calculating optimized cost and arrangement of words |
| 81 | + for(ll i=1;i<=n;i++) |
| 82 | + { |
| 83 | + opcost[i]=inf; |
| 84 | + for(ll j=1;j<=i;j++) |
| 85 | + { |
| 86 | + if(opcost[j-1]!=inf && cost[j][i]!=inf && opcost[j-1]+cost[j][i] < opcost[i]) |
| 87 | + { |
| 88 | + opcost[i]=opcost[j-1]+cost[j][i]; |
| 89 | + p[i]=j; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + //calling printwordwrap(...) |
| 94 | + printwordwrap(p,n); |
| 95 | + cout<<"\n"; |
| 96 | + return 0; |
| 97 | +} |
| 98 | + |
| 99 | + |
| 100 | +/* |
| 101 | +
|
| 102 | +TEST CASES |
| 103 | +
|
| 104 | +-Test-Case 1 |
| 105 | +
|
| 106 | +Input: |
| 107 | +18 |
| 108 | +7 2 4 2 10 4 6 2 7 2 1 9 3 2 10 4 2 2 |
| 109 | +20 |
| 110 | +
|
| 111 | +Output: |
| 112 | +Line number 1: From word number 1 to 3 |
| 113 | +Line number 2: From word number 4 to 6 |
| 114 | +Line number 3: From word number 7 to 10 |
| 115 | +Line number 4: From word number 11 to 14 |
| 116 | +Line number 5: From word number 15 to 17 |
| 117 | +Line number 6: From word number 18 to 18 |
| 118 | +
|
| 119 | +
|
| 120 | +-Test-Case 2 |
| 121 | +
|
| 122 | +Input: |
| 123 | +6 |
| 124 | +4 2 2 7 2 4 |
| 125 | +16 |
| 126 | +
|
| 127 | +Output: |
| 128 | +Line number 1: From word number 1 to 3 |
| 129 | +Line number 2: From word number 4 to 6 |
| 130 | +
|
| 131 | +
|
| 132 | +Time Complexity - O(n*n) |
| 133 | +Space Complexity - O(n*n) |
| 134 | +
|
| 135 | +*/ |
| 136 | + |
0 commit comments