Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Text Justification
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
/* Text Justification Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified. You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ' ' when necessary so that each line has exactly L characters. Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right. For the last line of text, it should be left justified and no extra space is inserted between words. */ #include <iostream> #include <vector> #include <string> using namespace std; string getSpaces(int n){ string s = ""; for(int i=0; i<n;i++) s += " "; return s; } string getLine(vector<string>& words, int start, int end, int letterCount, int maxWidth){ string res = words[start]; int spaces = maxWidth - letterCount; if(start == end){ // don't forget this corner case res += getSpaces(spaces); return res; } int numOfSpace = spaces/(end-start); int extraOne = spaces%(end-start); string space0 = getSpaces(numOfSpace); string space1 = space0 + " "; for(int i= 0; i< end-start; i++){ res = res + (i < extraOne? space1: space0) + words[start + 1 + i]; } return res; } vector<string> fullJustify(vector<string>& words, int maxWidth) { int N = words.size(); int i = 0, j = 0; // for word counting int counter = 0; // letter counting vector<string> res; while(i<N && j<N){ int len = words[j].length(); counter += len; if(counter + j - i > maxWidth){ counter -= len; res.push_back(getLine(words, i, j-1, counter, maxWidth)); i = j; counter = 0; }else{ j++; } } // check the last session if(counter){ string last = words[i]; for(int x=i+1; x < j; x++) last = last + " " + words[x]; last = last + getSpaces(maxWidth - last.size()); res.push_back(last); } return res; } int main(){ vector<string> words = {"This", "is", "an", "example", "of", "text", "justification."}; vector<string> lines = fullJustify(words, 16); string s = ""; for(int i=0; i<16; i++) s += char(i%10+'0'); cout << "=>" << s << "<=" << endl; for(auto x: lines) cout << "=>" << x << "<=" << endl; return 0; }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 0.63 sec, absolute running time: 0.07 sec, cpu time: 0.01 sec, memory peak: 3 Mb, absolute service time: 0,7 sec
edit mode
|
history
|
discussion
=>0123456789012345<= =>This is an<= =>example of text<= =>justification. <=