#K91047. Substring Shift Transformation
Substring Shift Transformation
Substring Shift Transformation
You are given two strings s
and t
of equal length consisting only of lowercase English letters. You may perform the following operation an arbitrary number of times:
- Choose any contiguous substring of
s
and shift every character in that substring to its next character in the alphabet. The shift is cyclic, so that 'z' becomes 'a'.
Your goal is to transform string s
into string t
using a sequence of operations. In order to save space, you are allowed to compress consecutive identical operations. In other words, if the optimal sequence would apply an operation on the same substring several times in a row, you can represent that as a single operation along with the number of times it is performed.
In this problem, it turns out that an optimal strategy is to repeatedly shift the entire string. More formally, for a given test case, define the shift required at position i as $$ d_i = (t_i - s_i) \mod 26. $$ Although these values might not be identical for all positions, you are allowed to choose the shift value d that appears most frequently, and then perform the operation on the entire string that many times, which will correctly transform all characters that require that shift. (It is guaranteed that the test cases are chosen such that this strategy yields the expected answer.)
Your task is to compute the most common shift value (i.e. the maximum frequency shift among all positions) and output that value as the minimum number of operations, along with the operation details: the substring to operate on, which is always the entire string (from index 1 to index n
).
inputFormat
The input is read from standard input (stdin) and has the following format:
q n₁ s₁ t₁ n₂ s₂ t₂ ⋮ n_q s_q t_q
Here:
q
is an integer representing the number of test cases.- For each test case,
n
is the length of the strings, ands
andt
are two strings of lengthn
consisting only of lowercase letters.
outputFormat
For each test case, output the result in the following format on standard output (stdout):
M l r
Where:
M
is the minimum number of operations required (the most frequent shift value as computed by taking the shifts for each position, i.e. the value of d that maximizes the count of indices with that difference).l
andr
(with 1 ≤ l ≤ r ≤ n) denote the starting and ending indices (inclusive) of the substring on which each operation is performed. In this problem, you should always output1 n
(i.e. the entire string).
The output for different test cases should be printed sequentially.
## sample5
3
abc
def
4
abcd
wxyz
5
hello
world
1
a
z
2
aa
cc
3
1 3
22
1 4
15
1 5
25
1 1
2
1 2
</p>