1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

#include <vector>
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

vector<string> fullJustify(vector<string> &words, int L) {
    if (L == 0 || words.size() == 0) {
        return words;
    }
    
    size_t start = 0;   // start index in words that fit in line
    size_t wordsCount = 1;  // count of words that fit in line
    size_t wordsLength = words[0].length(); // totatl length of words excluds spaces that fit in line
    vector<string> result;
    
    size_t size = words.size();
    for (size_t i = 1; i < size; ++i) {
        size_t len = words[i].length();
        if (wordsLength + wordsCount + len <= L) {
            wordsLength += words[i].length();
            wordsCount++;
            continue;
        }
        
        
        // adjust words that fit in one line
        result.push_back("");
        string &str = result.back();
        float spacesRemain = L - wordsLength;
        for (int w = 0; w < wordsCount-1; ++w) {
            str += words[start+w];
            // calcuate spaces for words, wordsCount-w-1 is gaps that in remain words
            size_t spacesForWord = ceil(spacesRemain/(wordsCount-w-1));
            spacesRemain -= spacesForWord;
            str += string(spacesForWord, ' ');
        }
        // append last words to current line
        str += words[start+wordsCount-1] + string(spacesRemain, ' ');

        wordsCount = 1;
        wordsLength = len;
        start = i;
    }
    
    //adjust words that remain for last line
    result.push_back("");
    string &str = result.back();
    float spacesRemain = L - wordsLength;
    for (int w = 0; w < wordsCount-1; ++w) {
        str += words[start+w];
        //last line only need 1 space for each word to make left adjustment
        size_t spacesForWord = 1; 
        spacesRemain -= spacesForWord;
        str += string(spacesForWord, ' ');
    }
    
    str += words[start+wordsCount-1] + string(spacesRemain, ' ');
    
    return result;
}

ostream &operator<<(ostream &out, const vector<string> &v) {
    
    for (int i = 0; i < v.size(); ++i) {
        cout<< v[i] << endl;
    }
    
    cout << endl;
    return out;
}

int main(int argc, char const *argv[]) {
    vector<string> words;
    words.push_back("This");
    words.push_back("is");
    words.push_back("an");
    words.push_back("example");
    words.push_back("of");
    words.push_back("text");
    words.push_back("justification.");
    
    cout << fullJustify(words, 16);
    
    words.clear();
    words.push_back("a");
    words.push_back("b");
    words.push_back("c");
    words.push_back("d");
    words.push_back("e");
    cout << fullJustify(words, 3);

    return 0;
}
View Program Text


Test Status