-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathAC_simulation_n.cpp
69 lines (61 loc) · 1.75 KB
/
AC_simulation_n.cpp
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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_simulation_n.cpp
* Create Date: 2015-03-04 10:57:18
* Descripton: Simulation
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
class Solution {
private:
string getOneLine(vector<string> &words, int start, int end, int rest, int L) {
string line = words[start];
rest += (end - start);
int even_space = rest + 1, num_of_more = 0;
if (start != end) {
even_space = rest / (end - start);
num_of_more = rest - even_space * (end - start);
}
for (int i = start + 1; i <= end; ++i) {
if (i - start <= num_of_more)
line.append(even_space + 1, ' ');
else
line.append(even_space, ' ');
line += words[i];
}
if (line.size() < L)
line.append(L - line.size(), ' ');
return line;
}
public:
vector<string> fullJustify(vector<string> &words, int L) {
vector<string> ret;
int sz = words.size();
int start_pos = 0;
int cur_len = 0;
for (int i = 0; i < sz; ++i) {
if (cur_len + words[i].length() > L) {
ret.push_back(getOneLine(words, start_pos, i - 1, L - cur_len + 1, L));
start_pos = i;
cur_len = 0;
}
cur_len += words[i].length();
cur_len += 1;
}
ret.push_back(getOneLine(words, start_pos, sz - 1, 0, L));
return ret;
}
};
int main() {
int n, l;
cin >> n >> l;
vector<string> w(n);
for (auto &i : w)
cin >> i;
Solution s;
vector<string> ans = s.fullJustify(w, l);
for (auto &i : ans)
cout << i << endl;
return 0;
}