zoukankan      html  css  js  c++  java
  • [Coding Made Simple] Text Justification

    Given a sequence of words, and a limit on the number of characters that can be put in one line(line width).

    Put line breaks in the given sequence such that the cost of padding spaces is minimal.

    The cost of padding spaces is defined as the sum of the square of padding spaces of each line.

    Algorithm:

    1. Fill a 2D cost array cost[i][j]. cost[i][j] is the cost of putting strs[i]....strs[j] on one line, if they can't be put in the same line, Integer.MAX_VALUE is used to indicate that.

     When computing this cost, we need to add each string's length from strs[i] to strs[j], this operation takes O(n) time. To get a O(1) time, prefix sum technique is used.

    2. minCost[i] is the min cost possible from strs[0] to strs[i]; the final answer is minCost[n - 1].

     lineBreakIdx[i] is the last string's index on the previous line. 

    State: 

    1. If strs[0] to strs[i] can be put in the same line.

      minCost[i] = cost[0][i]; 

      lineBreakIdx[i] = i;

    2. If strs[0] to strs[i] can not be put in the same line, then we need to find a split point that gives a minimal cost.

         minCost[i] = min { minCost[j] + cost[j + 1][i] }, for j is from 0 to i - 1 and strs[j + 1] to strs[i] can be put in the same line.

         j is the split index. strs[0] to strs[j] are put on one line; strs[j + 1] to strs[i] are put on anothre line.

    Why dynamic programming?

    To compute minCost[i], we need to compute the min costs of its subproblems. To compute minCost[i + 1], we possbily need to compute minCost[i], this is a clear overlapping subproblems pattern.

     1 import java.util.ArrayList;
     2 
     3 public class TextJustification {
     4     private ArrayList<String> brokeSen;
     5     public int getMinCost(String[] strs, int maxWidth) {
     6         brokeSen = new ArrayList<String>();
     7         if(strs == null || strs.length == 0) {
     8             return 0;
     9         }
    10         int[][] cost = new int[strs.length][strs.length];
    11         //minCost[i]: the min cost of padding spaces for strs[0....i];
    12         int[] minCost = new int[strs.length];
    13         int[] lineBreakIdx = new int[strs.length];
    14         int[] prefixSum = new int[strs.length + 1];
    15         prefixSum[0] = 0;
    16         for(int i = 1; i <= strs.length; i++) {
    17             prefixSum[i] = prefixSum[i - 1] + strs[i - 1].length();
    18         }
    19         for(int i = 0; i < strs.length; i++) {
    20             for(int j = i; j < strs.length; j++) {
    21                 int len = prefixSum[j + 1] - prefixSum[i] + j - i;
    22                 if(len <= maxWidth) {
    23                     cost[i][j] = (maxWidth - len) * (maxWidth - len); 
    24                 }
    25                 else {
    26                     cost[i][j] = Integer.MAX_VALUE;
    27                 }
    28             }
    29         }
    30         for(int i = 0; i < strs.length; i++) {
    31             minCost[i] = cost[0][i];
    32             if(minCost[i] < Integer.MAX_VALUE) {
    33                 lineBreakIdx[i] = i;
    34             }
    35             else {
    36                 for(int j = 0; j < i; j++) {
    37                     if(cost[j + 1][i] < Integer.MAX_VALUE && minCost[j] + cost[j + 1][i] < minCost[i]) {
    38                         minCost[i] = minCost[j] + cost[j + 1][i];
    39                         lineBreakIdx[i] = j;
    40                     }
    41                 }                
    42             }            
    43         }
    44         int endIdx = strs.length - 1;
    45         StringBuilder sb;
    46         while(lineBreakIdx[endIdx] != endIdx) {
    47             sb = new StringBuilder();
    48             for(int i = lineBreakIdx[endIdx] + 1; i < endIdx; i++) {
    49                 sb.append(strs[i] + " ");               
    50             }
    51             sb.append(strs[endIdx]);
    52                brokeSen.add(sb.toString()); 
    53             endIdx = lineBreakIdx[endIdx];
    54         }
    55         sb = new StringBuilder();
    56         for(int i = 0; i < endIdx; i++) {
    57             sb.append(strs[i] + " ");
    58         }
    59         sb.append(strs[endIdx]);
    60         brokeSen.add(sb.toString());
    61         return minCost[strs.length - 1];
    62     }
    63     public static void main(String[] args) {
    64         String[] strs1 = {"tushar", "roy", "likes", "to", "code"};
    65         String[] strs2 = {"a", "b", "c", "d", "e"};
    66         TextJustification test = new TextJustification();
    67         System.out.println(test.getMinCost(strs1, 10));
    68         for(int i = 0; i < test.brokeSen.size(); i++) {
    69             System.out.println(test.brokeSen.get(i));
    70         }
    71         System.out.println(test.getMinCost(strs2, 10));
    72         for(int i = 0; i < test.brokeSen.size(); i++) {
    73             System.out.println(test.brokeSen.get(i));
    74         }
    75     }
    76 }

    Related Problems

    [LeetCode 68] Text Justification 

  • 相关阅读:
    uva 10099(最大生成树+搜索)
    Codeforces Round #218 (Div. 2) 解题报告
    CodeChef December Challenge 2013 解题报告
    Codeforces Round #217 (Div. 2) 解题报告
    uva 1423 (拓扑排序)
    UESTC 1307 windy数(数位DP)
    Codeforces Round #216 (Div. 2) 解题报告
    Codeforces Round #215 (Div. 2) 解题报告
    uva 10047(BFS)
    uva 10369(最小生成树)
  • 原文地址:https://www.cnblogs.com/lz87/p/7288837.html
Copyright © 2011-2022 走看看