cswork / OAOpen practice workspace

ConfluentOA #4

Tail N Lines

EasyJudge-ready

Topics (AI-labelled)In-place ArraysStacks & Heaps

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.

Input

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

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.

Example 1

Input:

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

Output:

2
1
d
1
e

Example 2

Input:

3 5
1
a
1
b
1
c

Output:

3
1
a
1
b
1
c

Example notes

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.

Editorial

Loading solution…