#K89267. Reverse Substrings
Reverse Substrings
Reverse Substrings
You are given a string s
and an integer k
. Your task is to partition the string into contiguous substrings each of length k
and then reverse each of these substrings. If the last substring is shorter than k
, simply reverse it as well.
For example, if s = "abcdefghi"
and k = 3
, the string can be divided into "abc", "def", "ghi", which when reversed individually become "cba", "fed", "ihg". Combining them together gives cbafedihg
.
The relation can be formulated as:
\( \text{result} = \bigoplus_{i=0}^{n-1} \text{reverse}(s[i\cdot k : i\cdot k+k]) \)
where \(\oplus\) denotes string concatenation.
inputFormat
The input is given via stdin
and consists of two lines:
- The first line contains the string
s
. - The second line contains the integer
k
.
outputFormat
Output the transformed string in a single line via stdout
.
abcdefghi
3
cbafedihg