Submission
Status:
[PPPPPPPPPPPPPPPPPP]
Subtask/Task Score:
{100/100}
Score: 100
User: faofao
Problemset: การจัดแนวข้อความ
Language: cpp
Time: 0.003 second
Submitted On: 2026-03-13 20:41:48
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
queue<string> q;
for(int i = 0; i < n; i++){
string s;
cin >> s;
q.push(s);
}
while(!q.empty()){
queue<string> v;
int word_len = 0;
while(!q.empty() && word_len + q.front().length() + v.size() <= m){
v.push(q.front());
word_len += q.front().length();
q.pop();
}
int words = v.size();
int gap = words - 1;
int space = m - word_len;
if(q.empty()){
while(!v.empty()){
cout << v.front();
v.pop();
if(!v.empty()){
cout << " ";
space--;
}
}
while(space--) cout << " ";
}
else if(words == 1){
cout << v.front();
v.pop();
while(space--) cout << " ";
}
else{
int base = space / gap;
int add = space % gap;
while(!v.empty()){
cout << v.front();
v.pop();
if(v.empty()) break;
for(int i = 0; i < base; i++) cout << " ";
if(add > 0){
cout << " ";
add--;
}
}
}
cout << "\n";
}
}