Game with Reversing (Codeforces 1834C)
Tags: Math, games

Alice and Bob are playing a game. They have two strings $$$S$$$ and $$$T$$$ of the same length $$$n$$$ consisting of lowercase latin letters. Players take turns alternately, with Alice going first.On her turn, Alice chooses an integer $$$i$$$ from $$$1$$$ to $$$n$$$, one of the strings $$$S$$$ or $$$T$$$, and any lowercase latin letter $$$c$$$, and replaces the $$$i$$$-th symbol in the chosen string with the character $$$c$$$.On his turn, Bob chooses one of the strings $$$S$$$ or $$$T$$$, and reverses it. More formally, Bob makes the replacement $$$S := \operatorname{rev}(S)$$$ or $$$T := \operatorname{rev}(T)$$$, where $$$\operatorname{rev}(P) = P_n P_{n-1} \ldots P_1$$$.The game lasts until the strings $$$S$$$ and $$$T$$$ are equal. As soon as the strings become equal, the game ends instantly.Define the duration of the game as the total number of moves made by both players during the game. For example, if Alice made $$$2$$$ moves in total, and Bob made $$$1$$$ move, then the duration of this game is $$$3$$$.Alice's goal is to minimize the duration of the game, and Bob's goal is to maximize the duration of the game.What will be the duration of the game, if both players play optimally? It can be shown that the game will end in a finite number of turns.

Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of the strings $$$S$$$ and $$$T$$$.The second line of each test case contains a string $$$S$$$ of length $$$n$$$ consisting of lowercase latin letters.The third line of each test case contains a string $$$T$$$ of length $$$n$$$ consisting of lowercase latin letters.It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

Output: For each test case, output a single number on a separate line — the duration of the described game, if both players play optimally.

Note: In the first test case, in her turn, Alice can replace the third symbol of the string $$$S$$$ with x. After that, both strings will become equal to "abxde" and the game will end after one move. Since Alice's goal is to finish the game in as few moves as possible, this move will be one of her optimal first moves, and the final answer will be $$$1$$$.In the second test case, in her turn, Alice can replace the fifth symbol of the string $$$T$$$ with h. After this move, $$$S =$$$ "hello", $$$T =$$$ "olleh". Then Bob makes his turn. In his turn, he must reverse one of the strings. If Bob chooses the string $$$S$$$, then after his turn both strings will be equal to "olleh", and if he chooses the string $$$T$$$, then after his turn both strings will be equal to "hello". Thus, after the presented first move of Alice, the game will definitely end in $$$2$$$ moves. It can be shown that there is no strategy for Alice to finish the game in less than $$$2$$$ moves, with both players playing optimally. The final answer is $$$2$$$.In the third test case, in her first move, Alice can replace the second symbol of the string $$$S$$$ with c. After this move, $$$S =$$$ "ac", $$$T =$$$ "cd". Then Bob makes his turn. If Bob reverses the string $$$S$$$, then after his turn $$$S =$$$ "ca", $$$T =$$$ "cd". Then it is easy to see that in this case Alice can definitely finish the game on the $$$3$$$-rd move, by replacing the second symbol of the string $$$T$$$ with a, after which both strings will become equal to "ca". If Bob reverses the string $$$T$$$, then after his turn $$$S =$$$ "ac", $$$T =$$$ "dc". In this case, Alice can also definitely finish the game on the $$$3$$$rd move, by replacing the first symbol of the string $$$S$$$ with d, after which both strings will become equal to "dc". Thus, Alice can definitely finish the game in $$$3$$$ moves regardless of Bob's moves. It can be shown that the game cannot end in less than $$$3$$$ moves, with both players playing optimally.In the fifth test case, the strings $$$S$$$ and $$$T$$$ are equal, so the game will end without starting, in $$$0$$$ moves.

Sample Inputs:
7
5
abcde
abxde
5
hello
olleo
2
ab
cd
7
aaaaaaa
abbbbba
1
q
q
6
yoyoyo
oyoyoy
8
abcdefgh
hguedfbh

Sample Outputs:
1
2
3
9
0
2
6

Solution (Python):
import sys, threading, bisect, heapq, math
from functools import cmp_to_key as ctk
from collections import defaultdict, deque
readline = sys.stdin.readline
read = lambda : [int(x) for x in readline().split()]
alloc = lambda dfv, *s: len(s) != 1 and [alloc(dfv, *s[1:]) for _ in range(int(s[0]))] or [dfv] * int(s[0])
bet = lambda a, b: a <= b and range(a, b + 1) or range(a, b - 1, -1)
show = lambda arr: print(' '.join(map(str, arr)))
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    for _wzl_ in range(int(input())):
        (n,) = read()
        s = [readline().split()[0] for i in range(2)]
        x = y = 0
        for (a, b, c) in zip(s[0], s[1], s[0][::-1]):
            x += a != b
            y += b != c
        print(min(x * 2 - x % 2, 2 * y - (y + 1) % 2 if y else 2))
main()

Solution (Python):
t = int(input())
l1 = []
for i in range(t):
    c1 = 0
    c2 = 0
    n = int(input())
    a = input()
    b = input()
    b1 = b[::-1]
    for j in range(n):
        if a[j] != b[j]:
            c1 += 1
        if a[j] != b1[j]:
            c2 += 1
    d1 = c1 // 2 * 2 + c1
    d2 = (c2 + 1) // 2 * 2 - 1 + c2
    if c2 == 0:
        d2 = 2
    ans = min(d1, d2)
    l1 += [ans]
print(*l1, sep='\n')

Solution (Python):
import sys
input = lambda : sys.stdin.buffer.readline().decode('utf-8').rstrip('\r\n')
from array import array
inp = lambda : int(input())
mi = lambda x=int: map(x, input().split())
arr = lambda d='i', x=int: array(d, mi(x))
li = lambda x=int: list(mi(x))
lb = lambda : list(map(int, input()))
ls = lambda : list(input())
bi = lambda n: bin(n).replace('0b', '')
yn = ['No', 'Yes']
YN = ['NO', 'YES']
YY = 'YES'
NN = 'NO'
yy = 'Yes'
nn = 'No'
inf = sys.maxsize
mod = 998244353
alp = 'abcdefghijklmnopqrstuvwxyz'
from itertools import accumulate as cml, permutations
from collections import Counter as ctr, deque as dq, defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from math import ceil, gcd, log, sqrt
import heapq as hq

def main(kase):
    n = inp()
    a = input()
    b = input()
    mv1 = mv2 = 0
    for (i, x) in enumerate(a):
        if x != b[i]:
            mv1 += 1
        if x != b[n - 1 - i]:
            mv2 += 1
    if mv1:
        mv1 = 2 * mv1 - 1 if mv1 % 2 else 2 * mv1
    else:
        print(0)
        return
    if mv2:
        mv2 = 2 * mv2 if mv2 % 2 else 2 * mv2 - 1
    else:
        print(min(2, mv1))
        return
    print(min(mv1, mv2))
    pass
if __name__ == '__main__':
    test_Cases = 1
    test_Cases = inp()
    for i in range(test_Cases):
        main(i)