-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_justification.h
144 lines (126 loc) · 3.24 KB
/
text_justification.h
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#pragma once
#include <string>
#include <vector>
#include <stdint.h>
using namespace std;
class text_justifier_t
{
public:
static vector<string> exec(vector<string> & words, int32_t max_width)
{
vector<char> str_builder;
str_builder.reserve(2 * max_width);
vector<string> ret;
if (max_width == 0)
{
if (words.size() == 1 && words[0].length() == 0)
ret.push_back("");
return ret;
}
for (int32_t i = 0; i < words.size();)
{
int32_t len = 0;
int32_t cur = i;
while (cur < words.size() && len < max_width)
len += words[cur++].length();
if (len > max_width)
{
--cur;
len -= words[cur].length();
}
while (len + cur - i - 1 > max_width)
{
--cur;
len -= words[cur].length();
}
int32_t spaces = cur - i - 1;
str_builder.resize(max_width + 1);
if (len == 0)
{
for (int32_t c = 0; c < max_width; ++c)
str_builder[c] = ' ';
}
else if (spaces == 0)
{
std::copy(words[i].begin(), words[i].end(), str_builder.begin());
for (int32_t c = words[i].length(); c < max_width; ++c)
str_builder[c] = ' ';
}
else if (cur == words.size())
{
auto ite = str_builder.begin();
std::copy(words[i].begin(), words[i].end(), ite);
ite += words[i].length();
int32_t j = i + 1;
for (; j < cur; ++j)
{
*(ite++) = ' ';
std::copy(words[j].begin(), words[j].end(), ite);
ite += words[j].length();
}
while (ite < str_builder.end())
{
*(ite++) = ' ';
}
}
else if (spaces == 1)
{
int32_t blanks = max_width - len;
std::copy(words[i].begin(), words[i].end(), str_builder.begin());
int32_t blank_beg = words[i].length(), blank_end = blank_beg + blanks;
for (int32_t c = blank_beg; c < blank_end; ++c)
str_builder[c] = ' ';
std::copy(words[i + 1].begin(), words[i + 1].end(), str_builder.begin() + blank_end);
}
else
{
int32_t blanks = max_width - len;
int32_t blank_len = 0, rh_blanks = 0, lh_blanks = 0;
if (blanks % spaces != 0)
{
double avg_blanks = blanks / (double)spaces;
blank_len = avg_blanks;
rh_blanks = (blank_len + 1) * spaces - blanks;
lh_blanks = spaces - rh_blanks;
}
else
{
blank_len = blanks / spaces - 1;
lh_blanks = spaces;
rh_blanks = 0;
}
auto ite = str_builder.begin();
std::copy(words[i].begin(), words[i].end(), ite);
ite += words[i].length();
int32_t j = i + 1;
for (; j < cur - rh_blanks; ++j)
{
for (int32_t c = 0; c <= blank_len; ++c)
*(ite++) = ' ';
std::copy(words[j].begin(), words[j].end(), ite);
ite += words[j].length();
}
for (; j < cur; ++j)
{
for (int32_t c = 0; c < blank_len; ++c)
*(ite++) = ' ';
std::copy(words[j].begin(), words[j].end(), ite);
ite += words[j].length();
}
}
str_builder[max_width] = '\0';
ret.push_back(&str_builder[0]);
str_builder.clear();
i = cur;
}
return ret;
}
};
#if 0
class Solution {
public:
vector<string> fullJustify(vector<string>& words, int maxWidth) {
return text_justifier_t::exec(words, maxWidth);
}
};
#endif