#K78927. Transforming Strings Using Limited Operations
Transforming Strings Using Limited Operations
Transforming Strings Using Limited Operations
You are given k test cases. In each test case, there are two strings s and t of length n. Your task is to transform s into t using a series of at most 20000 operations. There are two types of operations:
- Operation 1 (Swap): Denoted by
1 i
. Conceptually, this operation swaps the character at position i with another character later in the string that equals the desired character at position i. (Though in your solution you may choose to ignore swaps if a direct replacement is simpler.) - Operation 2 (Replace): Denoted by
2 i c
, which means to replace the character at position i with character c.
The operations are executed sequentially. You do not need to minimize the number of operations as long as the total does not exceed 20000. In particular, a straightforward approach is to directly replace every character where s and t differ.
Formally, for each test case, if we denote the positions by i=1,2,...,n, you need to output the total count of operations m followed by m lines describing each operation. All indices are 1-indexed. Any necessary formulas should be represented in \( \LaTeX \) format. For instance, the condition on the number of operations can be written as \( m \leq 20000 \).
inputFormat
The input begins with an integer k representing the number of test cases. Each test case is described as follows:
- An integer n — the length of the strings.
- A string s — the initial string.
- A string t — the target string.
All strings consist of lowercase English letters. It is guaranteed that \( |s| = |t| = n \).
outputFormat
For each test case, output the answer in the following format:
- The first line contains an integer m, the number of operations performed.
- The following m lines each describe an operation. Each operation is either in the format
1 i
(swap operation) or2 i c
(replacement operation), where i is the 1-indexed position and c is a lowercase letter.
If no operation is needed, simply output a single line with 0.
## sample2
5
abcde
badce
4
alex
bale
4
2 1 b
2 2 a
2 3 d
2 4 c
4
2 1 b
2 2 a
2 3 l
2 4 e
</p>