Submission

Status:

[PPPPPPPPPPPPPPPPPP]

Subtask/Task Score:

{100/100}

Score: 100

User: mantaggez

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

Language: cpp

Time: 0.002 second

Submitted On: 2026-03-20 14:08:15

#include <bits/stdc++.h>

using namespace std;

const int nx = 205;

int n, m;
vector<string> str(nx);

int main()
{
    cin.tie(NULL)->sync_with_stdio(false);
    cin >> n >> m;
    for(int i=1;i<=n;i++) cin >> str[i];

    int idx = 1, prev = 1;
    while(idx <= n) {
        int len = 0, space = 0;
        while(idx <= n) {
            len += (str[idx].size());
            idx++;
            space++;
            if(len + str[idx].size() + space > m) {
                break;
            }
        }

        int extra_space = m - len;
        int gap = idx - prev - 1;
        int tab = (gap <= 0 ? 0 : extra_space / gap);
        int off = extra_space - tab * gap;
        for(int i=prev;i<idx;i++) {
            cout << str[i];
            if(idx > n) {
                if(i != idx - 1) cout << ' ';
                continue;
            }

            if(gap == 0) {
                for(int j=len;j<m;j++)
                    cout << ' ';
                continue;
            }
            if(off) cout << ' ', off--;
            for(int j=1;j<=tab;j++) {
                cout << ' ';
            }
        }
        cout << '\n';

        // cout << len << ' ' << prev << ' ' << idx << ' ' << space << '\n'; 
        prev = idx;
    }

    return 0;
}