Submission
Status:
[PPPPPPPPPPPPPPPPPP]
Subtask/Task Score:
{100/100}
Score: 100
User: erng
Problemset: การจัดแนวข้อความ
Language: cpp
Time: 0.002 second
Submitted On: 2026-03-10 20:30:07
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<string> word(205), w;
int main()
{
cin>>n>>m;
for (int i=0; i<n; i++) cin>>word[i];
int cnt=0;
for (int i=0; i<n; i++)
{
if (cnt==0) cnt+=word[i].size();
else cnt+=word[i].size()+1;
if (cnt>m)
{
if (w.size()==1)
{
cout<<w[0];
cnt=word[i].size();
w.clear();
w.push_back(word[i]);
}
else
{
int charcnt=0;
for (auto c : w)
{
charcnt+=c.size()+1;
}
charcnt-=1;
int c=m-charcnt;
int cc=c%(w.size()-1);
cnt=0;
for (auto j : w)
{
cout<<j;
if (cnt!=w.size()-1)
{
for (int p=0; p<c/(w.size()-1)+(cnt<cc)+1; p++) cout<<" ";
}
cnt++;
}
cnt=word[i].size();
w.clear();
w.push_back(word[i]);
}
cout<<'\n';
}
else w.push_back(word[i]);
}
if (!w.empty())
{
for (int i=0; i<w.size(); i++)
{
cout<<w[i];
if (!w.size()-1) cout<<" ";
}
}
}