#K1076. Reverse Segments in a String

    ID: 23318 Type: Default 1000ms 256MiB

Reverse Segments in a String

Reverse Segments in a String

Given a string S and an integer N, split the string into segments of length N (the last segment may be shorter) and reverse each segment independently.

For example, if S = abcdefghij and N = 5, then the segments are abcde and fghij, and after reversing each segment, the output will be edcbajihgf.

You are required to read the input from the standard input and produce the output to the standard output.

The segmentation can be formally described as follows: if \[ S = s_1 s_2 \ldots s_n, \] then for each segment \(S_i = s_{(i-1)N+1} s_{(i-1)N+2} \ldots s_{\min(iN, n)}\), the reversed segment is \(S_i^R\). The final answer is the concatenation of all reversed segments.

inputFormat

The first line of input contains a single integer T, representing the number of test cases. Each of the following T lines contains a string S and an integer N separated by whitespace.

Example:

4
abcdefghij 5
abcdefghij 2
abcdefghi 3
hello 3

outputFormat

For each test case, output the resulting string after reversing every segment of length N on a separate line.

Example:

edcbajihgf
badcfehgji
cbafedihg
lehol
## sample
4
abcdefghij 5
abcdefghij 2
abcdefghi 3
hello 3
edcbajihgf

badcfehgji cbafedihg lehol

</p>