Submission
Status:
[PPPPPPPPPPPPPPPPPP]
Subtask/Task Score:
{100/100}
Score: 100
User: colo39
Problemset: การจัดแนวข้อความ
Language: cpp
Time: 0.003 second
Submitted On: 2026-11-22 17:58:40
#include <bits/stdc++.h>
using namespace std;
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n,m;
cin >> n >> m;
vector<vector<string>> st;
int cnt = 0;
int cnt_w = 0;
vector<string> v;
map<int,int> sz;
int idx = 1;
for (int i = 1;i <= n;i++) {
string s;
cin >> s;
cnt_w++;
if (cnt + s.size() > m - (cnt_w - 1)) {
st.push_back(v);
cnt_w = 1;
sz[idx] = cnt;
idx++;
v.clear();
v.push_back(s);
cnt = s.size();
if (i == n) {
sz[idx] = cnt;
st.push_back(v);
}
} else {
cnt += s.size();
v.push_back(s);
if (i == n) {
sz[idx] = cnt;
st.push_back(v);
}
}
}
map<int,int> mp;
idx = 1;
for (auto &x : st) {
if (idx == st.size()) {
for (auto &s : x) {
cout << s;
cout << " ";
}return 0;
}
int rem = (m-sz[idx]) % max((int)x.size() - 1,1);
for (int i = 0;i < x.size();i++) {
mp[i] = (m-sz[idx]) / max((int)x.size() - 1,1);
}
int ii = 0;
while (rem > 0) {
mp[ii]++;
rem--;
ii++;
}
for (int i = 0;i < x.size();i++) {
cout << x[i];
for (int j = 1;j <= mp[i];j++) cout << " ";
}
idx++;
cout << "\n";
}
return 0;
}