This Java program Implements Word Wrap Problem.A 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 lines are printed neatly. Assume that the length of each word is smaller than the line width.
Here is the source code of the Java Program to Implement Word Wrap Problem.The Java program is successfully compiled and run on a Linux system. The program output is also shown below.
public class WordWrapProblem { private static final int INFINITY = Integer.MAX_VALUE; void solveWordWrap(int l[], int n, int M) { int extras[][] = new int[n + 1][n + 1]; int lineCost[][] = new int[n + 1][n + 1]; int cost[] = new int[n + 1]; int printSol[] = new int[n + 1]; int i, j; for (i = 1; i <= n; i++) { extras[i][i] = M - l[i - 1]; for (j = i + 1; j <= n; j++) { extras[i][j] = extras[i][j - 1] - l[j - 1] - 1; } } for (i = 1; i <= n; i++) { for (j = i; j <= n; j++) { if (extras[i][j] < 0) { lineCost[i][j] = INFINITY; } else if (j == n && extras[i][j] >= 0) { lineCost[i][j] = 0; } else lineCost[i][j] = extras[i][j] * extras[i][j]; } } cost[0] = 0; for (j = 1; j <= n; j++) { cost[j] = INFINITY; for (i = 1; i <= j; i++) { if (cost[i - 1] != INFINITY && lineCost[i][j] != INFINITY && (cost[i - 1] + lineCost[i][j] < cost[j])) { cost[j] = cost[i - 1] + lineCost[i][j]; printSol[j] = i; } } } printSolution(printSol, n); } private int printSolution(int p[], int n) { int k; if (p[n] == 1) { k = 1; } else { k = printSolution(p, p[n] - 1) + 1; } System.out.println("Line number " + k + " From word no " + p[n] + " to " + n); return k; } public static void main(String...arg) { int l[] = {3,2,2,5}; int n = 4; int M = 6; WordWrapProblem wordWrapProblem = new WordWrapProblem(); wordWrapProblem.solveWordWrap(l, n, M); } }
$ javac WordWrapProblem.java $ java WordWrapProblem Line number 1 From word no 1 to 1 Line number 2 From word no 2 to 3 Line number 3 From word no 4 to 4
Related posts:
Jackson Ignore Properties on Marshalling
Java 8 Stream API Analogies in Kotlin
Hướng dẫn Java Design Pattern – Decorator
Java Program to Implement Unrolled Linked List
Java Program to Implement Regular Falsi Algorithm
Java Program to implement Circular Buffer
Spring Boot - Cloud Configuration Server
Implementing a Binary Tree in Java
Java Program to Find All Pairs Shortest Path
Tạo ứng dụng Java RESTful Client với thư viện OkHttp
Converting between an Array and a List in Java
Hashtable trong java
Lớp Collectors trong Java 8
The XOR Operator in Java
Introduction to the Java NIO Selector
Java Program to Use Above Below Primitive to Test Whether Two Lines Intersect
Java Program to Implement Expression Tree
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
The Registration API becomes RESTful
ETL with Spring Cloud Data Flow
Inject Parameters into JUnit Jupiter Unit Tests
Guide to Guava Multimap
Java Program to Implement ArrayList API
How to Kill a Java Thread
Guide to Java 8’s Collectors
Java Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
Java Program to Perform Complex Number Multiplication
Getting Started with GraphQL and Spring Boot
Java Program to Implement WeakHashMap API
Giới thiệu Java Service Provider Interface (SPI) – Tạo các ứng dụng Java dễ mở rộng
Java Program to Implement Circular Doubly Linked List
Retrieve User Information in Spring Security