cswork / OA打开练习工作台

ConfluentOA #4

Tail N Lines

简单可在线判题

考点(AI 标注)原地数组单调栈与堆

Given a string array lines (each element is one line, without a newline character) and a non-negative integer n, return the last n lines in their original order. For n=0, return an empty array; when n exceeds the array length, return all lines.

输入格式

The first line contains lineCount n. Then, for each line string, first give its UTF-8 byte length L, then on the next line give exactly L raw bytes as the content, immediately followed by an LF separator. A string may be empty and may contain spaces, tabs, and non-ASCII UTF-8 characters, but not LF. 0≤lineCount≤5000, 0≤n≤5000, and the total length of all payload is ≤30000 bytes. This length-prefixed protocol preserves empty lines and in-line whitespace losslessly.

输出格式

Output the result with the same length-prefixed protocol: first output the number of returned lines, then for each line output its UTF-8 byte length and raw content; for an empty array, output only 0.

样例 1

输入:

5 2
1
a
1
b
1
c
1
d
1
e

输出:

2
1
d
1
e

样例 2

输入:

3 5
1
a
1
b
1
c

输出:

3
1
a
1
b
1
c

样例说明

The source semantics are clear; this site supplements the I/O encoding with UTF-8 byte-length prefixes so that empty strings, spaces, and tabs are not lost in token-based input.