Median String (Codeforces 1144E)
Tags: Math, bitmasks

You are given two strings $$$s$$$ and $$$t$$$, both consisting of exactly $$$k$$$ lowercase Latin letters, $$$s$$$ is lexicographically less than $$$t$$$.Let's consider list of all strings consisting of exactly $$$k$$$ lowercase Latin letters, lexicographically not less than $$$s$$$ and not greater than $$$t$$$ (including $$$s$$$ and $$$t$$$) in lexicographical order. For example, for $$$k=2$$$, $$$s=$$$"az" and $$$t=$$$"bf" the list will be ["az", "ba", "bb", "bc", "bd", "be", "bf"].Your task is to print the median (the middle element) of this list. For the example above this will be "bc".It is guaranteed that there is an odd number of strings lexicographically not less than $$$s$$$ and not greater than $$$t$$$.

Input: The first line of the input contains one integer $$$k$$$ ($$$1 \le k \le 2 \cdot 10^5$$$) — the length of strings.The second line of the input contains one string $$$s$$$ consisting of exactly $$$k$$$ lowercase Latin letters.The third line of the input contains one string $$$t$$$ consisting of exactly $$$k$$$ lowercase Latin letters.It is guaranteed that $$$s$$$ is lexicographically less than $$$t$$$.It is guaranteed that there is an odd number of strings lexicographically not less than $$$s$$$ and not greater than $$$t$$$.

Output: Print one string consisting exactly of $$$k$$$ lowercase Latin letters — the median (the middle element) of list of strings of length $$$k$$$ lexicographically not less than $$$s$$$ and not greater than $$$t$$$.


Sample Inputs:
2
az
bf

5
afogk
asdji

6
nijfvj
tvqhwp

Sample Outputs:
bc

alvuw

qoztvz

Solution (Python):
from sys import stdin, stdout
char_to_int = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
int_to_char = {0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f', 6: 'g', 7: 'h', 8: 'i', 9: 'j', 10: 'k', 11: 'l', 12: 'm', 13: 'n', 14: 'o', 15: 'p', 16: 'q', 17: 'r', 18: 's', 19: 't', 20: 'u', 21: 'v', 22: 'w', 23: 'x', 24: 'y', 25: 'z'}
N = 26
n = int(stdin.buffer.readline())
string_1 = [char_to_int[char] for char in stdin.readline().rstrip('\r\n')]
string_2 = [char_to_int[char] for char in stdin.readline().rstrip('\r\n')]
current = string_1[n - 1] + string_2[n - 1]
answer = [''] * n
for index in range(n - 1, 0, -1):
    current += (string_1[index - 1] + string_2[index - 1]) * N
    answer[index] = int_to_char[(current >> 1) % N]
    current //= N
answer[0] = int_to_char[(current >> 1) % N]
stdout.write(f"{''.join(answer)}")

Solution (Python):
a = int(input())
b = [ord(i) - 97 for i in input()[::-1]]
c = [ord(i) - 97 for i in input()[::-1]]
it = b[0] + c[0]
s = []
for i in range(1, a):
    it += 26 * (b[i] + c[i])
    s.append(chr(it // 2 % 26 + 97))
    it //= 26
s.append(chr(it // 2 % 26 + 97))
s = ''.join(s[::-1])
print(s)

Solution (Python):
a = int(input())
b = [ord(i) - 97 for i in input()[::-1]]
c = [ord(i) - 97 for i in input()[::-1]]
it = b[0] + c[0]
s = []
for i in range(1, a):
    it += 26 * (b[i] + c[i])
    s.append(chr(it // 2 % 26 + 97))
    it //= 26
s.append(chr(it // 2 % 26 + 97))
s = ''.join(s[::-1])
print(s)