Official
B - Vertical Writing Editorial by en_translator
First, let us ignore the second condition, that \(T_i\) does not end with *, before constructing \(T_i\).
Initialize \(T_1,T_2,\dots,T_M\) with a length-\(N\) string consisting of *.
Then, for each \(i\) and \(j\), assign the \(j\)-th character of \(S_i\) to the \((N-i+1)\)-th character of \(T_j\).
Now, all but the second condition are satisfied.
To satisfy the second condition, for each \(T_i\), repeatedly remove the last character while it ends with *.
Sample code (Python)
N = int(input())
S = [input() for _ in range(N)]
M = max(map(len, S))
T = [["*"] * N for _ in range(M)]
for i in range(N):
for j in range(len(S[i])):
T[j][N - i - 1] = S[i][j]
for i in range(M):
while T[i][-1] == "*":
T[i].pop()
print("".join(T[i]))
Sample code (C++)
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<string> S(N);
for (auto& s : S) cin >> s;
int M = 0;
for (auto& s : S) M = max(M, (int)s.size());
vector<string> T(M, string(N, '*'));
for (int i = 0; i < N; ++i) {
for (int j = 0; j < (int)S[i].size(); ++j) {
T[j][N - i - 1] = S[i][j];
}
}
for (int i = 0; i < M; ++i) {
while (T[i].back() == '*') {
T[i].pop_back();
}
cout << T[i] << endl;
}
}
posted:
last update: