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 

  • 相关阅读:
    互斥锁
    信号量、互斥体和自旋锁
    【设计模式】template method(模板方法)-- 类行为型模式5.10
    C/C++中的auto关键词
    【设计模式】observer(观察者)-- 对象行为型模式5.7
    UML图
    MapReduce阅读
    shell提取文件后缀名,并判断其是否为特定字符串
    shell编程--遍历目录下的文件
    Linux shell字符串截取与拼接
  • 原文地址:https://www.cnblogs.com/lz87/p/7288837.html
Copyright © 2011-2022 走看看