Character Swap (Hard Version) (Codeforces 1243B2)
Tags: Others, strings

This problem is different from the easy version. In this version Ujan makes at most $$$2n$$$ swaps. In addition, $$$k \le 1000, n \le 50$$$ and it is necessary to print swaps themselves. You can hack this problem if you solve it. But you can hack the previous problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings $$$s$$$ and $$$t$$$ of length $$$n$$$ consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation at most $$$2n$$$ times: he takes two positions $$$i$$$ and $$$j$$$ ($$$1 \le i,j \le n$$$, the values $$$i$$$ and $$$j$$$ can be equal or different), and swaps the characters $$$s_i$$$ and $$$t_j$$$.Ujan's goal is to make the strings $$$s$$$ and $$$t$$$ equal. He does not need to minimize the number of performed operations: any sequence of operations of length $$$2n$$$ or shorter is suitable.

Input: The first line contains a single integer $$$k$$$ ($$$1 \leq k \leq 1000$$$), the number of test cases.For each of the test cases, the first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 50$$$), the length of the strings $$$s$$$ and $$$t$$$. Each of the next two lines contains the strings $$$s$$$ and $$$t$$$, each having length exactly $$$n$$$. The strings consist only of lowercase English letters. It is guaranteed that strings are different.

Output: For each test case, output "Yes" if Ujan can make the two strings equal with at most $$$2n$$$ operations and "No" otherwise. You can print each letter in any case (upper or lower).In the case of "Yes" print $$$m$$$ ($$$1 \le m \le 2n$$$) on the next line, where $$$m$$$ is the number of swap operations to make the strings equal. Then print $$$m$$$ lines, each line should contain two integers $$$i, j$$$ ($$$1 \le i, j \le n$$$) meaning that Ujan swaps $$$s_i$$$ and $$$t_j$$$ during the corresponding operation. You do not need to minimize the number of operations. Any sequence of length not more than $$$2n$$$ is suitable.


Sample Inputs:
4
5
souse
houhe
3
cat
dog
2
aa
az
3
abc
bca

Sample Outputs:
Yes
1
1 4
No
No
Yes
3
1 2
3 1
2 3

Solution (Python):
for _ in range(int(input())):
    n = int(input())
    a = list(input())
    b = list(input())
    freqs = set()
    for ch in a + b:
        if ch in freqs:
            freqs.remove(ch)
        else:
            freqs.add(ch)
    if len(freqs) > 0:
        print('no')
        continue
    ans = []
    for atual in range(n):
        seg = atual
        if a[atual] == b[atual]:
            continue
        for outro in range(atual + 1, n):
            if a[atual] == a[outro]:
                ans.append([outro, atual])
                (a[outro], b[atual]) = (b[atual], a[outro])
                break
            if a[atual] == b[outro]:
                ans.append([outro, outro])
                (a[outro], b[outro]) = (b[outro], a[outro])
                ans.append([outro, atual])
                (a[outro], b[atual]) = (b[atual], a[outro])
                break
    print('yes')
    print(len(ans))
    for line in ans:
        print(line[0] + 1, line[1] + 1)

Solution (Python):
t = int(input())

def equal(s, t):
    s = [x for x in s]
    t = [x for x in t]
    ans = []
    for i in range(len(s)):
        for j in range(i + 1, len(s)):
            if s[i] == s[j]:
                (s[j], t[i]) = (t[i], s[j])
                ans.append([j + 1, i + 1])
                break
            if s[i] == t[j]:
                (s[j], t[j]) = (t[j], s[j])
                ans += [[j + 1, j + 1]]
                (s[j], t[i]) = (t[i], s[j])
                ans += [[j + 1, i + 1]]
                break
    l = len(ans)
    ans = '\n'.join(['%s %s' % (x[0], x[1]) for x in ans])
    if s == t:
        return 'YES\n%s\n%s' % (l, ans)
    else:
        return 'NO'
for i in range(t):
    l = input()
    print(equal(input(), input()))

Solution (Python):
def ss(s, t):
    n = len(s)
    ans = []
    d = {}
    for i in s:
        if i in d:
            d[i] += 1
        else:
            d[i] = 1
    for i in t:
        if i in d:
            d[i] += 1
        else:
            d[i] = 1
    for i in d:
        if d[i] % 2 == 1:
            return [-1]
    for i in range(n):
        if s[i] == t[i]:
            continue
        for j in range(i + 1, n):
            if s[i] == s[j]:
                ans.append((j + 1, i + 1))
                (t[i], s[j]) = (s[j], t[i])
                break
            elif s[i] == t[j]:
                ans.append((j + 1, j + 1))
                (s[j], t[j]) = (t[j], s[j])
                ans.append((j + 1, i + 1))
                (t[i], s[j]) = (s[j], t[i])
                break
    return ans
for _ in range(int(input())):
    n = int(input())
    s = list(input())
    t = list(input())
    ans = ss(s, t)
    if ans == [-1]:
        print('NO')
    else:
        print('YES')
        print(len(ans))
        for (a, b) in ans:
            print(a, b)