Character Blocking (Codeforces 1840E)
Tags: Implement, hashing

You are given two strings of equal length $$$s_1$$$ and $$$s_2$$$, consisting of lowercase Latin letters, and an integer $$$t$$$.You need to answer $$$q$$$ queries, numbered from $$$1$$$ to $$$q$$$. The $$$i$$$-th query comes in the $$$i$$$-th second of time. Each query is one of three types:   block the characters at position $$$pos$$$ (indexed from $$$1$$$) in both strings for $$$t$$$ seconds;  swap two unblocked characters;  determine if the two strings are equal at the time of the query, ignoring blocked characters. Note that in queries of the second type, the characters being swapped can be from the same string or from $$$s_1$$$ and $$$s_2$$$.

Input: The first line of the input contains a single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) — the number of test cases.Then follow the descriptions of the test cases.The first line of each test case contains a string $$$s_1$$$ consisting of lowercase Latin letters (length no more than $$$2 \cdot 10^5$$$).The second line of each test case contains a string $$$s_2$$$ consisting of lowercase Latin letters (length no more than $$$2 \cdot 10^5$$$).The strings have equal length.The third line of each test case contains two integers $$$t$$$ and $$$q$$$ ($$$1 \le t, q \le 2 \cdot 10^5$$$). The number $$$t$$$ indicates the number of seconds for which a character is blocked. The number $$$q$$$ corresponds to the number of queries.Each of the next $$$q$$$ lines of each test case contains a single query. Each query is one of three types:   "$$$1\ \ \ pos$$$" — block the characters at position $$$pos$$$ in both strings for $$$t$$$ seconds;  "$$$2\ \ \ 1/\;\!2\ \ \ pos_1\ \ \ 1/\;\!2\ \ \ pos_2$$$" — swap two unblocked characters. The second number in the query indicates the number of the string from which the first character for the swap is taken. The third number in the query indicates the position in that string of that character. The fourth number in the query indicates the number of the string from which the second character for the swap is taken. The fifth number in the query indicates the position in that string of that character;  "$$$3$$$" — determine if the two strings are equal at the time of the query, ignoring blocked characters. For queries of the first type, it is guaranteed that at the time of the query, the characters at position $$$pos$$$ are not blocked.For queries of the second type, it is guaranteed that the characters being swapped are not blocked.All values of $$$pos, pos_1, pos_2$$$ are in the range from $$$1$$$ to the length of the strings.The sum of the values of $$$q$$$ over all test cases, as well as the total length of the strings $$$s_1$$$, does not exceed $$$2 \cdot 10^5$$$.

Output: For each query of the third type, output "YES" if the two strings $$$s_1$$$ and $$$s_2$$$ are equal at the time of the query, ignoring blocked characters, and "NO" otherwise.You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes" and "YES" will be accepted as a positive answer.

Note: Let's look at the strings $$$s_1$$$ and $$$s_2$$$ after each of the $$$q$$$ queries. Blocked characters will be denoted in red.First example input:($$$codeforces$$$, $$$codeblocks$$$) $$$\rightarrow$$$ ($$$codeforces$$$, $$$codeblocks$$$) $$$\rightarrow$$$ ($$$code\color{red}{f}orces$$$, $$$code\color{red}{b}locks$$$) $$$\rightarrow$$$ ($$$code\color{red}{fo}rces$$$, $$$code\color{red}{bl}ocks$$$) $$$\rightarrow$$$ ($$$code\color{red}{for}ces$$$, $$$code\color{red}{blo}cks$$$) $$$\rightarrow$$$ ($$$code\color{red}{for}c\color{red}{e}s$$$, $$$code\color{red}{blo}c\color{red}{k}s$$$) $$$\rightarrow$$$ ($$$code\color{red}{for}c\color{red}{e}s$$$, $$$code\color{red}{blo}c\color{red}{k}s$$$) $$$\rightarrow$$$ ($$$codef\color{red}{or}c\color{red}{e}s$$$, $$$codeb\color{red}{lo}c\color{red}{k}s$$$)Second example input:($$$cool$$$, $$$club$$$) $$$\rightarrow$$$ ($$$cuol$$$, $$$clob$$$) $$$\rightarrow$$$ ($$$cuol$$$, $$$cbol$$$) $$$\rightarrow$$$ ($$$c\color{red}{u}ol$$$, $$$c\color{red}{b}ol$$$) $$$\rightarrow$$$ ($$$c\color{red}{u}ol$$$, $$$c\color{red}{b}ol$$$) $$$\rightarrow$$$ ($$$cuol$$$, $$$cbol$$$)

Sample Inputs:
2
codeforces
codeblocks
5 7
3
1 5
1 6
1 7
1 9
3
3
cool
club
2 5
2 1 2 2 3
2 2 2 2 4
1 2
3
3

Sample Outputs:
NO
YES
NO
YES
NO

Solution (Python):
from collections import deque
from sys import stdin
for _ in range(int(stdin.readline())):
    s = [list(stdin.readline().rstrip()), list(stdin.readline().rstrip())]
    (t, q) = map(int, stdin.readline().split())
    D = set((i for (i, (a, b)) in enumerate(zip(s[0], s[1])) if a != b))
    U = deque()
    for i in range(q):
        if U and U[0][0] == i:
            (_, pos) = U.popleft()
            if s[0][pos] != s[1][pos]:
                D.add(pos)
        query = stdin.readline()[:-1]
        if query[0] == '1':
            pos = int(query[2:]) - 1
            D.discard(pos)
            U.append((i + t, pos))
        elif query[0] == '2':
            (s1, pos1, s2, pos2) = (int(j) - 1 for j in query[2:].split())
            D.discard(pos1)
            D.discard(pos2)
            (s[s1][pos1], s[s2][pos2]) = (s[s2][pos2], s[s1][pos1])
            if s[0][pos1] != s[1][pos1]:
                D.add(pos1)
            if s[0][pos2] != s[1][pos2]:
                D.add(pos2)
        else:
            print('NO' if D else 'YES')

Solution (Python):
from collections import deque
from sys import stdin
for _ in range(int(stdin.readline())):
    s = [list(stdin.readline().rstrip()), list(stdin.readline().rstrip())]
    (t, q) = map(int, stdin.readline().split())
    D = set((i for (i, (a, b)) in enumerate(zip(s[0], s[1])) if a != b))
    U = deque()
    for i in range(q):
        if U and U[0][0] == i:
            (_, pos) = U.popleft()
            if s[0][pos] != s[1][pos]:
                D.add(pos)
        query = stdin.readline()[:-1]
        if query[0] == '1':
            pos = int(query[2:]) - 1
            D.discard(pos)
            U.append((i + t, pos))
        elif query[0] == '2':
            (s1, pos1, s2, pos2) = (int(j) - 1 for j in query[2:].split())
            D.discard(pos1)
            D.discard(pos2)
            (s[s1][pos1], s[s2][pos2]) = (s[s2][pos2], s[s1][pos1])
            if s[0][pos1] != s[1][pos1]:
                D.add(pos1)
            if s[0][pos2] != s[1][pos2]:
                D.add(pos2)
        else:
            print('NO' if D else 'YES')

Solution (Python):
import sys
input = lambda : sys.stdin.readline().strip()
T = int(input())
for _ in range(T):
    s1 = input()
    s2 = input()
    s1 = [x for x in s1]
    s2 = [x for x in s2]
    (t, q) = map(int, input().split())
    n = 0
    m = len(s1)
    a = []
    for i in range(len(s1)):
        if s1[i] == s2[i]:
            n += 1
    i = 0
    for j in range(q):
        if len(a) > 0 and i < len(a):
            if a[i][1] == j:
                m += 1
                if s1[a[i][0]] == s2[a[i][0]]:
                    n += 1
                i += 1
        x = list(map(int, input().split()))
        if x[0] == 1:
            k = x[1] - 1
            m -= 1
            if s1[k] == s2[k]:
                n -= 1
            a.append([k, j + t])
        elif x[0] == 2:
            if x[1] != x[3]:
                if x[1] == 1:
                    if x[2] != x[4]:
                        if s1[x[2] - 1] == s2[x[2] - 1]:
                            n -= 1
                        if s2[x[4] - 1] == s1[x[4] - 1]:
                            n -= 1
                    elif s1[x[2] - 1] == s2[x[2] - 1]:
                        n -= 1
                    (s1[x[2] - 1], s2[x[4] - 1]) = (s2[x[4] - 1], s1[x[2] - 1])
                    if x[2] != x[4]:
                        if s1[x[2] - 1] == s2[x[2] - 1]:
                            n += 1
                        if s2[x[4] - 1] == s1[x[4] - 1]:
                            n += 1
                    elif s1[x[2] - 1] == s2[x[2] - 1]:
                        n += 1
                else:
                    if x[2] != x[4]:
                        if s2[x[2] - 1] == s1[x[2] - 1]:
                            n -= 1
                        if s1[x[4] - 1] == s2[x[4] - 1]:
                            n -= 1
                    elif s2[x[2] - 1] == s1[x[2] - 1]:
                        n -= 1
                    (s2[x[2] - 1], s1[x[4] - 1]) = (s1[x[4] - 1], s2[x[2] - 1])
                    if x[2] != x[4]:
                        if s2[x[2] - 1] == s1[x[2] - 1]:
                            n += 1
                        if s1[x[4] - 1] == s2[x[4] - 1]:
                            n += 1
                    elif s2[x[2] - 1] == s1[x[2] - 1]:
                        n += 1
            elif x[1] == 1:
                if x[2] != x[4]:
                    if s1[x[2] - 1] == s2[x[2] - 1]:
                        n -= 1
                    if s2[x[4] - 1] == s1[x[4] - 1]:
                        n -= 1
                elif s1[x[2] - 1] == s2[x[2] - 1]:
                    n -= 1
                (s1[x[2] - 1], s1[x[4] - 1]) = (s1[x[4] - 1], s1[x[2] - 1])
                if x[2] != x[4]:
                    if s1[x[2] - 1] == s2[x[2] - 1]:
                        n += 1
                    if s2[x[4] - 1] == s1[x[4] - 1]:
                        n += 1
                elif s1[x[2] - 1] == s2[x[2] - 1]:
                    n += 1
            else:
                if x[2] != x[4]:
                    if s2[x[2] - 1] == s1[x[2] - 1]:
                        n -= 1
                    if s2[x[4] - 1] == s1[x[4] - 1]:
                        n -= 1
                elif s2[x[2] - 1] == s1[x[2] - 1]:
                    n -= 1
                (s2[x[2] - 1], s2[x[4] - 1]) = (s2[x[4] - 1], s2[x[2] - 1])
                if x[2] != x[4]:
                    if s2[x[2] - 1] == s1[x[2] - 1]:
                        n += 1
                    if s2[x[4] - 1] == s1[x[4] - 1]:
                        n += 1
                elif s2[x[2] - 1] == s1[x[2] - 1]:
                    n += 1
        elif n == m:
            print('YES')
        else:
            print('NO')