Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Input has spaces inside lines, and even empty lines matter. So yeah, cin >> s is a trap; use getline.
You cannot print the frame until you know the longest line. Store all lines first, then compute . The inside width of the box should be exactly .
For a line of length , the total padding you need is . If is even, split it evenly: put spaces on each side.
The only annoying case is odd . Then one side gets spaces and the other gets spaces. These odd cases must alternate between being closer to the left and closer to the right edge, starting with left.
Keep a boolean toggle that you change only when is odd. For the first odd line, use for the next odd line, swap them. Then print * + left spaces + line + right spaces + *, and put a border of length on top and bottom.
This is implementation, not black magic. But there are two easy ways to punt this into WA:
Since lines may contain spaces and may even be empty, getline is mandatory. If you use cin >>, congratulations, you just deleted half the input.
Also, you cannot print on the fly, because the frame width depends on the longest line. So first store every line, then compute
The inside width of the framed text must be exactly :
So the top and bottom borders are simply strings of length made of *.
For a line , let be the number of spaces we still need to add.
We want left padding and right padding such that
Then life is easy: Perfectly centered. No drama.
Now perfect centering is impossible, because one side must get the extra space. The statement says these lines should be moved alternately closer to the left and right edge, starting with left.
That means:
So for odd , we just keep a boolean toggle:
The trap is stupid but deadly: toggle only for odd . Even-difference lines are already perfectly centered, so they should not affect the alternation.
getline into a vector.* repeated times.bool closerLeft = true.closerLeft, add the extra space to the right;closerLeft.* + left spaces + line + right spaces + *.Empty lines are handled automatically: they are just strings of length . No special case needed. Nice when a problem lets you be lazy for the right reasons.
Let .
So the output matches the statement, and nothing sketchy is happening behind the curtain.
Let be the number of lines and the maximum line length.
With the given limits, this is absurdly safe.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
vector<string> lines;
string s;
while (getline(cin, s)) lines.push_back(s);
if (lines.empty()) return 0;
int w = 0;
for (const string& t : lines) w = max(w, (int)t.size());
string border(w + 2, '*');
cout << border << '\n';
bool closerLeft = true;
for (const string& t : lines) {
int d = w - (int)t.size();
int left = d / 2, right = d / 2;
if (d % 2) {
// First odd line should be closer to the left edge,
// meaning fewer spaces on the left and more on the right.
if (closerLeft) right++;
else left++;
closerLeft = !closerLeft;
}
cout << '*' << string(left, ' ') << t << string(right, ' ') << "*\n";
}
cout << border << '\n';
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
vector<string> lines;
string s;
while (getline(cin, s)) lines.push_back(s);
if (lines.empty()) return 0;
int w = 0;
for (const string& t : lines) w = max(w, (int)t.size());
string border(w + 2, '*');
cout << border << '\n';
bool closerLeft = true;
for (const string& t : lines) {
int d = w - (int)t.size();
int left = d / 2, right = d / 2;
if (d % 2) {
// First odd line should be closer to the left edge,
// meaning fewer spaces on the left and more on the right.
if (closerLeft) right++;
else left++;
closerLeft = !closerLeft;
}
cout << '*' << string(left, ' ') << t << string(right, ' ') << "*\n";
}
cout << border << '\n';
}