Submission

Status:

[PPPPPPPPPPPPPPPPPP]

Subtask/Task Score:

{100/100}

Score: 100

User: august

Problemset: การจัดแนวข้อความ

Language: cpp

Time: 0.003 second

Submitted On: 2026-03-18 20:40:40

#include <bits/stdc++.h>
using namespace std;


int main() {
    cin.tie(0)->sync_with_stdio(0);
    int n,m;
    cin>> n>> m;

    queue<string> s;
    for (int i=0; i<n; i++) {
        string x;
        cin>> x;
        s.push(x);
    }

    queue<string> tem;
    int strs = 0;

    while (!s.empty() || !tem.empty()) {
        string ns;
        if (!s.empty()) ns = s.front();
        

        if (!s.empty() && strs+ns.size() <= m-tem.size()) {
            strs+=ns.size();
            tem.push(ns);
            s.pop();
        }
        else {
            int between = (m-strs);
            int add=0;
            if (tem.size() >1) {
                between/=(tem.size()-1);
                add=(m-strs)%(tem.size()-1);
            }
           // cout<< between<< ' '<< add<< '\n';
            while (!tem.empty()) {
                string now = tem.front();
              
                tem.pop();
                cout<< now;
                if (s.empty()) cout<< ' ';
                else if (!tem.empty()) {
                    for (int i=0; i<between+(add > 0); i++) {
                        cout<< ' ';
                    }
                    add--;
                }
            }
            cout<< '\n';
            strs = 0;

        }
    }
}