A remote weather station streams each hour's sensor readings as one long line of letters and digits, but a firmware bug on the transmitter drops every space character before the line leaves the device, leaving one unbroken run of characters. A ground-station engineer has already figured out, by cross-referencing the station's field-width table, the exact character positions (0-indexed offsets into the original, space-free string) at which a single space must be reinserted to recover the readable log line. You are given the space-free string and the strictly increasing list of positions. For each position p in the list, a single space must appear immediately before the character that was at index p in the original string (all positions refer to offsets in the original string, never to positions in the growing output). Reconstruct and print the readable line.
A single line (or equivalently, whitespace-separated tokens) containing:
n — the length of the original strings — the original string, exactly n characters, containing only lowercase English letters and digits (no whitespace)m — the number of positions where a space must be insertedm integers p_1 < p_2 < ... < p_m, strictly increasing, each satisfying 1 <= p_i <= n-1Print a single line: the string s with a space inserted immediately before index p_i for every i, for the original indices (spaces do not shift subsequent position references).
1 <= n <= 3000000 <= m <= n-11 <= p_i <= n-1, strictly increasings consists only of lowercase English letters and digitsExample 1
Input
10 helloworld 1 5
Expected
hello world
Explanation
s = "helloworld" (n=10) and the single position is 5. Splitting the original string right before index 5 gives "hello" (indices 0-4) and "world" (indices 5-9), so inserting one space there produces "hello world".
Example 2
Input
16 thequickbrownfox 3 3 8 13
Expected
the quick brown fox
Explanation
s = "thequickbrownfox" (n=16) with positions 3, 8, 13. The segments between consecutive positions (and before the first / after the last) are s[0:3]="the", s[3:8]="quick", s[8:13]="brown", s[13:16]="fox". Joining them with a single space at each requested position gives "the quick brown fox".
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →