Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.58 KB | None | 0 0
  1. static void solveWordWrap (int []l, int n,  
  2.                                         int M)
  3.     {
  4.          
  5.         // For simplicity, 1 extra space
  6.         // is used in all below arrays
  7.      
  8.         // extras[i][j] will have number of
  9.         // extra spaces if words from i
  10.         // to j are put in a single line
  11.         int [,]extras = new int[n+1,n+1];
  12.      
  13.         // lc[i][j] will have cost of a line  
  14.         // which has words from i to j
  15.         int [,]lc = new int[n+1,n+1];
  16.      
  17.         // c[i] will have total cost of
  18.         // optimal arrangement of words  
  19.         // from 1 to i
  20.         int []c = new int[n+1];
  21.      
  22.         // p[] is used to print the solution.
  23.         int []p = new int[n+1];
  24.      
  25.         // calculate extra spaces in a single  
  26.         // line. The value extra[i][j] indicates
  27.         // extra spaces if words from word number
  28.         // i to j are placed in a single line
  29.         for (int i = 1; i <= n; i++)
  30.         {
  31.             extras[i,i] = M - l[i-1];
  32.              
  33.             for (int j = i+1; j <= n; j++)
  34.                 extras[i,j] = extras[i,j-1]
  35.                                  - l[j-1] - 1;
  36.         }
  37.          
  38.         // Calculate line cost corresponding to  
  39.         // the above calculated extra spaces. The
  40.         // value lc[i][j] indicates cost of  
  41.         // putting words from word number i to  
  42.         // j in a single line
  43.         for (int i = 1; i <= n; i++)
  44.         {
  45.             for (int j = i; j <= n; j++)
  46.             {
  47.                 if (extras[i,j] < 0)
  48.                     lc[i,j] = MAX;
  49.                 else if (j == n &&  
  50.                               extras[i,j] >= 0)
  51.                     lc[i,j] = 0;
  52.                 else
  53.                     lc[i,j] = extras[i,j]
  54.                                  * extras[i,j];
  55.             }
  56.         }
  57.          
  58.         // Calculate minimum cost and find  
  59.         // minimum cost arrangement. The value
  60.         // c[j] indicates optimized cost to
  61.         // arrange words from word number
  62.         // 1 to j.
  63.         c[0] = 0;
  64.         for (int j = 1; j <= n; j++)
  65.         {
  66.             c[j] = MAX;
  67.             for (int i = 1; i <= j; i++)
  68.             {
  69.                 if (c[i-1] != MAX && lc[i,j]  
  70.                     != MAX && (c[i-1] + lc[i,j]  
  71.                                        < c[j]))
  72.                 {
  73.                     c[j] = c[i-1] + lc[i,j];
  74.                     p[j] = i;
  75.                 }
  76.             }
  77.         }
  78.      
  79.         printSolution(p, n);
  80.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement