Submission
Status:
[TSSSSSSSSSSSSSSSSS]
Subtask/Task Score:
{0/100}
Score: 0
User: faofao
Problemset: การจัดแนวข้อความ
Language: cpp
Time: 1.078 second
Submitted On: 2026-03-13 20:36:15
#include <bits/stdc++.h>
using namespace std;
#define ll long long
queue<string> q ;
int main(){
ios::sync_with_stdio(0),cin.tie(0) ;
int n,m ; cin >> n >> m ;
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 ;
int base = 0 , add = 0;
if(gap>0){
base = space/gap ;
add = space%gap ;
}
if(q.empty()){
while(!v.empty()){
cout << v.front();
space -= v.front().length() ;
v.pop() ;
if(!v.empty()){
cout << " " ;
space--;
}
}
while(space--) cout << " " ;
}
else if(words==1){
cout << v.front();
v.pop();
while(space--) cout << " ";
}
else{
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" ;
}
}