Submission
Status:
[PPPPPPPPPPPPPPPPPP]
Score: 100
User: Nightingale
Problemset: การจัดแนวข้อความ
Language: cpp
Time: 0.003 second
Submitted On: 2025-03-26 18:26:41
#include <bits/stdc++.h>
using namespace std;
int a;
int b;
int sum = 0;
int sum2 = 0;
int row = 0;
int word = 0;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> a >> b;
string accept[a];
vector<vector<string>> regret(505);
for(int i=0;i<a;i++){
cin >> accept[i];
//cout << accept[i] << ' ' << accept[i].size() << '\n';
}
for(int i=0;i<a;i++){
if(sum+accept[i].size()<=b){
sum = sum+accept[i].size();
//cout << accept[i] << ' ';
sum++;
sum2 = sum2+accept[i].size();
word++;
}else{
//cout << '\n';
sum--;
//cout << b << ' ' << sum2 << '\n';
int space = b-sum2;
int placement = 0;
vector<int> spacebar(word,0);
while(space>0){
if(placement==word-1) placement = 0;
spacebar[placement]++;
placement++;
space--;
}
placement = 0;
for(int j=i-word;j<i;j++){
regret[row].push_back(accept[j]);
if(placement>=word-1) placement = word-1;
for(int k=0;k<spacebar[placement];k++){
regret[row].push_back(" ");
}
placement++;
}
sum = 0;
word = 0;
sum2 = 0;
row++;
i--;
}
}
for(int j=a-word;j<a;j++){
regret[row].push_back(accept[j]);
regret[row].push_back(" ");
}
row++;
for(int i=0;i<row;i++){
for(int j=0;j<regret[i].size();j++){
cout << regret[i][j];
}
cout << '\n';
}
}