Submission
Status:
[PP-SSSSSSSSSSSSSSS]
Subtask/Task Score:
{0/100}
Score: 0
User: navysrimuang
Problemset: การจัดแนวข้อความ
Language: cpp
Time: 0.002 second
Submitted On: 2026-03-14 23:17:54
#include<bits/stdc++.h>
using namespace std;
vector<string> v(200);
vector<int> space(200,-1);
vector<int> l(200,1);
int main(){
cin.tie(0)->sync_with_stdio(0);
int n,m;
cin >> n >> m;
int left = m;
int j = 0;
for(int i = 0;i<n;i++){
string w;
cin >> w;
if(w.size()+1 <= left){
left -= (w.size()+1);
w += " ";
v[j] += w;
}else{
for(int k = 0;k<left;k++) v[j] += " ";
l[j] = left+1;
left = m;
j++;
w += " ";
v[j] += w;
left -= (w.size());
}
space[j]++;
}
for(int i = 0;i<j;i++){
if(!space[i]) continue;
for(int j = 0;j<l[i];j++) v[i].pop_back();
int per = l[i]/space[i];
int extra = l[i]%space[i];
string s = v[i];
string t;
bool used = 0;
for(char c : s){
t += c;
if(c == ' '){
for(int k = 0;k<per;k++) t += " ";
if(!used){
for(int k = 0;k<extra;k++) t += " ";
used = 1;
}
}
}
v[i] = t;
}
for(int i = 0;i<=j;i++){
cout << v[i] << "\n";
}
return 0;
}
// foo.bar.koo.zaa....