B - 手記の縦読み / Vertical Reading of a Memoir Editorial by admin Editorial for printing
gpt-6-astra-mediumOverview
This problem asks you to extract the \(K\)-th character from the left of each row, from top to bottom, and output them.
Analysis
The secret message is obtained simply by reading the characters in the specified column from top to bottom. Therefore, a straightforward approach of extracting \(1\) character from each row is sufficient, and no optimization is required.
For example, consider the case where \(H = 3\), \(W = 4\), \(K = 2\), and the note is as follows:
abcd
efgh
ijkl
The \(2\)-nd character from the left in each row is b, f, and j from top to bottom, so the answer is bfj.
One point to note is that while the problem statement uses \(1\)-based indexing for the columns, string indices in C++ are \(0\)-based. Therefore, the \(K\)-th character from the left is accessed with index \(K-1\). Using index \(K\) would lead to accessing the wrong character or causing an out-of-bounds access.
Algorithm
- Read \(H, W, K\).
- Store the \(H\) strings in an array
g. - For each \(i = 0, 1, \ldots, H-1\) in this order, output
g[i][K-1]. - Finally, output a newline.
g[i][K-1] is the character at the \((i+1)\)-th row from the top and the \(K\)-th column from the left. By outputting these in increasing order of row indices, we obtain the secret message read from top to bottom in the specified column.
Complexity
- Time complexity: \(O(HW)\)
It takes \(O(HW)\) to read the entire note and \(O(H)\) to output the specified column. - Space complexity: \(O(HW)\)
It stores the note of \(H\) rows, each with \(W\) characters.
Implementation Notes
The provided code is in C++. It uses
vector<string>to store each row as a string.The row indices range from
0toh - 1, and the index of the column to read isk - 1.Since the answer must be output as a single string, do not insert spaces or newlines between characters. Print a newline only after outputting all characters.
Source Code
// 問題文から独立に実装: 二次元配列に格納してから列を走査
#include <iostream>
#include <string>
#include <vector>
int main() {
int h, w, k; std::cin >> h >> w >> k;
std::vector<std::string> g(h);
for (auto& s : g) std::cin >> s;
for (int i = 0; i < h; ++i) std::cout << g[i][k - 1];
std::cout << std::endl;
}
This editorial was generated by gpt-6-astra-medium.
posted:
last update: