-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0068_Text_Justification.cpp
More file actions
77 lines (61 loc) · 2.21 KB
/
Copy path0068_Text_Justification.cpp
File metadata and controls
77 lines (61 loc) · 2.21 KB
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
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class Solution {
public:
vector<string> fullJustify(vector<string>& words, int maxWidth) {
vector<string> output;
int i, j, k, len, n = words.size(), skip, more;
for(i = 0; i < n;){
len = words[i].size();
for(j = i + 1; j < n; j++){
len += 1 + words[j].size();
if(len > maxWidth) break;
}
if(j < n) len -= words[j].size() + j - i;
else len -= j - i - 1;
if(j - i - 1 > 0){
// left more space
if(j < n){
skip = (maxWidth - len) / (j - i - 1);
more = (maxWidth - len) % (j - i - 1);
}
// last line one space between words
else{
skip = 1;
more = 0;
}
}
cout << "i=" << i << " j=" << j << " skip=" << skip << " more=" << more << endl;
string s = words[i];
for(k = i + 1; k < j; k++){
string tmp(skip, ' ');
if(k - i <= more) tmp += ' ';
s += tmp + words[k];
}
// pad 0 if not enough to maxWidth
if(s.size() < maxWidth){
string tmp(maxWidth - s.size(), ' ');
s += tmp;
}
output.push_back(s);
i = j;
}
return output;
}
};
int main(){
Solution solve;
//vector<string> input = {"This", "is", "an", "example", "of", "text", "justification."};
//vector<string> input = {"What","must","be","acknowledgment","shall","be"};
vector<string> input = {"Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is","everything","else","we","do"};
int maxWidth = 20;
cout << "Input: " << endl;
for(int i = 0; i < input.size(); i++) cout << input[i] << " ";
cout << endl;
vector<string> output = solve.fullJustify(input, maxWidth);
cout << "Output: " << endl;
for(int i = 0; i < output.size(); i++) cout << output[i] << endl;
return 0;
}