Problem:
 Score : 600 points    Problem Statement Let us consider the following operations on a string consisting of A and B :   Select a character in a string. If it is A , replace it with BB . If it is B , replace with AA .  Select a substring that is equal to either AAA or BBB , and delete it from the string.   For example, if the first operation is performed on ABA and the first character is selected, the string becomes BBBA .
If the second operation is performed on BBBAAAA and the fourth through sixth characters are selected, the string becomes BBBA .  These operations can be performed any number of times, in any order.  You are given two string S and T , and q queries a_i, b_i, c_i, d_i .
For each query, determine whether S_{a_i} S_{{a_i}+1} ... S_{b_i} , a substring of S , can be made into T_{c_i} T_{{c_i}+1} ... T_{d_i} , a substring of T .      Constraints  1 \leq |S|, |T| \leq 10^5  S and T consist of letters A and B .  1 \leq q \leq 10^5  1 \leq a_i \leq b_i \leq |S|  1 \leq c_i \leq d_i \leq |T|         Input Input is given from Standard Input in the following format:  S  T  q  a_1  b_1  c_1  d_1  ...  a_q  b_q  c_q  d_q       Output Print q lines. The i -th line should contain the response to the i -th query. If S_{a_i} S_{{a_i}+1} ... S_{b_i} can be made into T_{c_i} T_{{c_i}+1} ... T_{d_i} , print YES . Otherwise, print NO .        Sample Input 1 BBBAAAABA
BBBBA
4
7 9 2 5
7 9 1 4
1 7 2 5
1 7 2 4      Sample Output 1 YES
NO
YES
NO  The first query asks whether the string ABA can be made into BBBA .
As explained in the problem statement, it can be done by the first operation.  The second query asks whether ABA can be made into BBBB , and the fourth query asks whether BBBAAAA can be made into BBB .
Neither is possible.  The third query asks whether the string BBBAAAA can be made into BBBA .
As explained in the problem statement, it can be done by the second operation.       Sample Input 2 AAAAABBBBAAABBBBAAAA
BBBBAAABBBBBBAAAAABB
10
2 15 2 13
2 13 6 16
1 13 2 20
4 20 3 20
1 18 9 19
2 14 1 11
3 20 3 15
6 16 1 17
4 18 8 20
7 20 3 14      Sample Output 2 YES
YES
YES
YES
YES
YES
NO
NO
NO
NO

Solution (Python):
S = input()
T = input()
LS = [[0, 0] for _ in range(len(S) + 1)]
LT = [[0, 0] for _ in range(len(S) + 1)]
for i, s in enumerate(S):
  LS[i + 1] = list(LS[i])
  if s == 'A': LS[i + 1][0] += 1
  if s == 'B': LS[i + 1][1] += 1
for i, t in enumerate(T):
  LT[i + 1] = list(LT[i])
  if t == 'A': LT[i + 1][0] += 1
  if t == 'B': LT[i + 1][1] += 1
q = int(input())
for _ in range(q):
  a, b, c, d = map(int, input().split())
  sa = (LS[b][0] - LS[a - 1][0]) % 3
  sb = (LS[b][1] - LS[a - 1][1]) % 3
  ta = (LT[d][0] - LT[c - 1][0]) % 3
  tb = (LT[d][1] - LT[c - 1][1]) % 3
  print('YES' if (sa - sb) % 3 == (ta - tb) % 3 else 'NO')

Solution (Python):
import os
import sys

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

S = list(sys.stdin.readline().rstrip())
T = list(sys.stdin.readline().rstrip())
Q = int(sys.stdin.readline())
A, B, C, D = list(zip(*[map(int, sys.stdin.readline().split()) for _ in range(Q)]))
A = np.array(A, dtype=int)
B = np.array(B, dtype=int)
C = np.array(C, dtype=int)
D = np.array(D, dtype=int)

S = np.array(['.'] + S)
T = np.array(['.'] + T)

SA = (S == 'A').cumsum()
SB = (S == 'B').cumsum()
TA = (T == 'A').cumsum()
TB = (T == 'B').cumsum()

sa = SA[B] - SA[A - 1]
sb = SB[B] - SB[A - 1]
ta = TA[D] - TA[C - 1]
tb = TB[D] - TB[C - 1]
ans = np.full(Q, 'NO', dtype='U3')
ans[(sa - sb) % 3 == (ta - tb) % 3] = 'YES'
print(*ans, sep='\n')

Solution (Python):
import bisect
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

S = list(sys.stdin.readline().rstrip())
T = list(sys.stdin.readline().rstrip())
Q = int(sys.stdin.readline())
ABCD = [list(map(int, sys.stdin.readline().split())) for _ in range(Q)]

S = np.array(['.'] + S + ['.'])
T = np.array(['.'] + T + ['.'])

SA = (S == 'A').cumsum()
SB = (S == 'B').cumsum()
TA = (T == 'A').cumsum()
TB = (T == 'B').cumsum()
ans = []
for a, b, c, d in ABCD:
    sa = SA[b] - SA[a - 1]
    sb = SB[b] - SB[a - 1]
    ta = TA[d] - TA[c - 1]
    tb = TB[d] - TB[c - 1]
    ans.append((sa - sb) % 3 == (ta - tb) % 3)

for a in ans:
    if a:
        print('YES')
    else:
        print('NO')

Solution (Python):
import itertools
S = list(itertools.accumulate([0] + [1 if s == 'A' else 0 for s in input()]))
T = list(itertools.accumulate([0] + [1 if t == 'A' else 0 for t in input()]))
Q = int(input())
ABCD = [[int(_) for _ in input().split()] for _ in range(Q)]
for a, b, c, d in ABCD:
    x = ((b - a + 1) + (S[b] - S[a - 1]))
    y = ((d - c + 1) + (T[d] - T[c - 1]))
    print('NO') if (x - y) % 3 else print('YES')

Solution (Python):
S = input()
T = input()
Q = int(input())
ABCD = [list(map(int,input().split())) for _ in [0]*Q]

Si = [0]*(len(S)+1)
for i,s in enumerate(S,1):
    if s=="A": Si[i] = Si[i-1] + 1
    else: Si[i] = Si[i-1] + 2
    Si[i] %= 3
Ti = [0]*(len(T)+1)
for i,t in enumerate(T,1):
    if t=="A": Ti[i] = Ti[i-1] + 1
    else: Ti[i] = Ti[i-1] + 2
    Ti[i] %= 3
for a,b,c,d in ABCD:
    print("YES" if ((Si[b]-Si[a-1])%3 == (Ti[d]-Ti[c-1])%3) else "NO" )

Solution (Python):
import sys
input=sys.stdin.readline
S,T=input().strip(),input().strip()
Acc_S,Acc_T=[0],[0]
for s in S:
    if s=='A':
        Acc_S.append(Acc_S[-1]+1)
    else:
        Acc_S.append(Acc_S[-1]+2)
for t in T:
    if t=='A':
        Acc_T.append(Acc_T[-1]+1)
    else:
        Acc_T.append(Acc_T[-1]+2)
q=int(input())
for _ in range(q):
    a,b,c,d=map(int,input().split())
    print('YES' if (Acc_S[b]-Acc_S[a-1])%3==(Acc_T[d]-Acc_T[c-1])%3 else 'NO')

Solution (Python):
S = list(input())
T = list(input())

import sys
input = sys.stdin.readline

S_n = [0] * (len(S) + 1)

for i in range(1, len(S) + 1):
    if S[i - 1] == 'A':
        S_n[i] = S_n[i - 1] + 1
    else:
        S_n[i] = S_n[i - 1] + 2

T_n = [0] * (len(T) + 1)

for i in range(1, len(T) + 1):
    if T[i - 1] == 'A':
        T_n[i] = T_n[i - 1] + 1
    else:
        T_n[i] = T_n[i - 1] + 2

q = int(input())
for _ in range(q):
    a, b, c, d = map(int, input().split())
    
    S_A = S_n[b] - S_n[a - 1]
    T_A = T_n[d] - T_n[c - 1]
    delta = abs(S_A - T_A)
    if delta % 3 == 0:
        print ('YES')
    else:
        print ('NO')

Solution (Python):
S = list(input())
T = list(input())


S_n = [0] * (len(S) + 1)

for i in range(1, len(S) + 1):
    if S[i - 1] == 'A':
        S_n[i] = S_n[i - 1] + 1
    else:
        S_n[i] = S_n[i - 1] + 2

T_n = [0] * (len(T) + 1)

for i in range(1, len(T) + 1):
    if T[i - 1] == 'A':
        T_n[i] = T_n[i - 1] + 1
    else:
        T_n[i] = T_n[i - 1] + 2

q = int(input())
for _ in range(q):
    a, b, c, d = map(int, input().split())
    
    S_A = S_n[b] - S_n[a - 1]
    T_A = T_n[d] - T_n[c - 1]
    delta = abs(S_A - T_A)
    if delta % 3 == 0:
        print ('YES')
    else:
        print ('NO')

Solution (Python):
S = list(input())
T = list(input())


S_n = [0] * (len(S) + 1)

for i in range(1, len(S) + 1):
    if S[i - 1] == 'A':
        S_n[i] = S_n[i - 1] + 1
    else:
        S_n[i] = S_n[i - 1] + 2

T_n = [0] * (len(T) + 1)

for i in range(1, len(T) + 1):
    if T[i - 1] == 'A':
        T_n[i] = T_n[i - 1] + 1
    else:
        T_n[i] = T_n[i - 1] + 2

q = int(input())
for _ in range(q):
    a, b, c, d = map(int, input().split())
    
    S_A = S_n[b] - S_n[a - 1]
    T_A = T_n[d] - T_n[c - 1]
    delta = abs(S_A - T_A)
    if delta % 3 == 0:
        print ('YES')
    else:
        print ('NO')

Solution (Python):
S = input()
T = input()
S_a = [0]*(len(S)+1)
S_b = [0]*(len(S)+1)
T_a = [0]*(len(T)+1)
T_b = [0]*(len(T)+1)
for i, s in enumerate(S):
    if s == 'A':
        S_a[i+1] = S_a[i]+1
        S_b[i+1] = S_b[i]
    else:
        S_a[i+1] = S_a[i]
        S_b[i+1] = S_b[i]+1
        
for i, T in enumerate(T):
    if T == 'A':
        T_a[i+1] = T_a[i]+1
        T_b[i+1] = T_b[i]
    else:
        T_a[i+1] = T_a[i]
        T_b[i+1] = T_b[i]+1
q = int(input())
for _ in range(q):
    a, b, c, d = map(int, input().split())
    s_a = S_a[b]-S_a[a-1]
    s_b = S_b[b]-S_b[a-1]
    t_a = T_a[d]-T_a[c-1]
    t_b = T_b[d]-T_b[c-1]
    if ((t_a-t_b)%3) == ((s_a-s_b)%3):
        print('YES')
    else:
        print('NO')

Solution (Python):
s = input()
t = input()
ss = [0]
tt = [0]

for i in range(len(s)):
    if s[i]=='B':
        ss.append(ss[-1]+2)
    else:
        ss.append(ss[-1]+1)

for i in range(len(t)):
    if t[i]=='B':
        tt.append(tt[-1]+2)
    else:
        tt.append(tt[-1]+1)

q = int(input())

for i in range(q):
    a,b,c,d = map(int, input().split())
    if (ss[b]-ss[a-1])%3==(tt[d]-tt[c-1])%3:
        print('YES')
    else:
        print('NO')

Solution (Python):
import sys
input = sys.stdin.readline


def main():
    S = input()
    T = input()
    scnt = [0] * (len(S)+1)
    for i in range(len(S)):
        scnt[i+1] = scnt[i] + (1 if S[i] == "A" else 2)
    tcnt = [0] * (len(T)+1)
    for i in range(len(T)):
        tcnt[i+1] = tcnt[i] + (1 if T[i] == "A" else 2)
    q = int(input())
    for _ in range(q):
        a, b, c, d = map(int, input().split())
        sn = scnt[b] - scnt[a-1]
        tn = tcnt[d] - tcnt[c-1]
        if sn % 3 == tn % 3:
            print("YES")
        else:
            print("NO")


if __name__ == "__main__":
    main()

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


def cumsum_list(inlist):
    l = len(inlist)
    out_cumsum_list = [ None for i in range(l+1) ]
    out_cumsum_list[0] = (0,0)

    for i in range(l):
        if inlist[i] == "A":
            out_cumsum_list[i+1] = ( out_cumsum_list[i][0] + 1, out_cumsum_list[i][1] )
        else:
            out_cumsum_list[i+1] = ( out_cumsum_list[i][0], out_cumsum_list[i][1] + 1 )
    return out_cumsum_list

cumsum_list_s = cumsum_list(s)
cumsum_list_t = cumsum_list(t)

n = int(input())

for i in range(n):
    a, b, c, d = [ int(v) for v in input().split() ]
    sa = cumsum_list_s[b][0] - cumsum_list_s[a-1][0]
    sb = cumsum_list_s[b][1] - cumsum_list_s[a-1][1]
    ta = cumsum_list_t[d][0] - cumsum_list_t[c-1][0]
    tb = cumsum_list_t[d][1] - cumsum_list_t[c-1][1]

    total_s, total_t = sa + 2 * sb, ta + 2 * tb

    if total_s % 3 == total_t % 3:
        print("YES")
    else:
        print("NO")

Solution (Python):
import sys
input = sys.stdin.readline
import itertools

"""
A -> BB -> AAAA -> A とできるので、操作は可逆。
全部 A, AA, AAA のどれかにできる
A の個数 + 2 * (B の個数) mod 3が等しいことと同値
"""

convert = {'A':1, 'B':2}
S = [convert[x] for x in input().rstrip()]
T = [convert[x] for x in input().rstrip()]

Scum = [0] + list(itertools.accumulate(S))
Tcum = [0] + list(itertools.accumulate(T))

Q = int(input())
answer = []
for _ in range(Q):
    a,b,c,d = map(int,input().split())
    x = Scum[b] - Scum[a-1]
    y = Tcum[d] - Tcum[c-1]
    answer.append('YES' if x % 3 == y % 3 else 'NO')

print('\n'.join(answer))

Solution (Python):
s = input()
t = input()
ssum = [0 for _ in range(len(s)+1)]
for i in range(len(s)):
    ssum[i+1] = ssum[i] + (s[i] == 'A')
tsum = [0 for _ in range(len(t)+1)]
for i in range(len(t)):
    tsum[i+1] = tsum[i] + (t[i] == 'A')
q = int(input())
for _ in range(q):
    a, b, c, d = map(int, input().split())
    cnts = (b-a+1)*2 - (ssum[b] - ssum[a-1])
    cntt = (d-c+1)*2 - (tsum[d] - tsum[c-1])
    print("YES" if cnts % 3 == cntt % 3 else "NO")

Solution (Python):
s = input().rstrip()
t = input().rstrip()
s_anum = [0] * len(s)
t_anum = [0] * len(s)
val = 0
for i, ch in enumerate(s):
    if ch == "A":
        val += 1
    elif ch == "B":
        val += 2
    s_anum[i] = val % 3
s_anum = [0] + s_anum
val = 0
for i, ch in enumerate(t):
    if ch == "A":
        val += 1
    elif ch == "B":
        val += 2
    t_anum[i] = val % 3
t_anum =  [0] + t_anum

q = int(input())
for i in range(q):
    a, b, c, d = [int(item) for item in input().split()]
    if (s_anum[b] - s_anum[a-1] + 3) % 3 == (t_anum[d] - t_anum[c-1] + 3) % 3:
        print("YES")
    else:
        print("NO")

Solution (Python):
s=input()
t=input()
S,T=[0]*(len(s)+1),[0]*(len(t)+1)
for i in range(len(s)):
  if s[i]=="A":
    S[i+1]=S[i]+1
  else:
    S[i+1]=S[i]-1
for i in range(len(t)):
  if t[i]=="A":
    T[i+1]=T[i]+1
  else:
    T[i+1]=T[i]-1
q=int(input())
for i in range(q):
  a,b,c,d=map(int,input().split())
  ss=S[b]-S[a-1]
  tt=T[d]-T[c-1]
  if (ss-tt)%3==0:
    print("YES")
  else:
    print("NO")

Solution (Python):
s=input()
t=input()
S,T=[0]*(len(s)+1),[0]*(len(t)+1)
for i in range(len(s)):
  if s[i]=="A":
    S[i+1]=S[i]+1
  else:
    S[i+1]=S[i]-1
for i in range(len(t)):
  if t[i]=="A":
    T[i+1]=T[i]+1
  else:
    T[i+1]=T[i]-1
q=int(input())
for i in range(q):
  a,b,c,d=map(int,input().split())
  ss=S[b]-S[a-1]
  tt=T[d]-T[c-1]
  if (ss-tt)%3==0:
    print("YES")
  else:
    print("NO")

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

S=[0]
for i in range(len(s)):
    if s[i]=="A":
        S.append(S[i]+1)
    else:
        S.append(S[i]+2)

T=[0]
for i in range(len(t)):
    if t[i]=="A":
        T.append(T[i]+1)
    else:
        T.append(T[i]+2)
Q=int(input())

for i in range(Q):
    a,b,c,d=map(int,input().split())
    if ((S[b]-S[a-1])%3)==((T[d]-T[c-1])%3):
        print("YES")
    else:
        print("NO")

Solution (Python):



def solve(s, t, q):

    cnt_s = [0] * (len(s) + 1)
    cnt_t = [0] * (len(t) + 1)

    r = 0
    for i in range(len(s)):
        cnt_s[i] = r
        if s[i] == 'A':
            r += 1
    cnt_s[-1] = r

    r = 0
    for i in range(len(t)):
        cnt_t[i] = r
        if t[i] == 'A':
            r += 1
    cnt_t[-1] = r

    ans = []
    for _ in range(q):
        a, b, c, d = input().split()
        a = int(a) - 1
        b = int(b)
        c = int(c) - 1
        d = int(d)
        cs = cnt_s[b] - cnt_s[a]
        ms = (2 * (b - a) - cs) % 3
        ct = cnt_t[d] - cnt_t[c]
        mt = (2 * (d - c) - ct) % 3
        ans.append(ms == mt)

    for r in ans:
        print('YES' if r else 'NO')


def main():
    s = input()
    t = input()
    q = input()
    q = int(q)

    solve(s, t, q)


if __name__ == '__main__':
    main()

Solution (Python):
s = input()
t = input()
q =int(input())
info = [list(map(int,input().split())) for i in range(q)]
MOD = 3
memo = {"A":1,"B":2}

s_ruiseki = [0]*(len(s)+1)
for i in range(1,len(s)+1):
  s_ruiseki[i] = (s_ruiseki[i-1]+memo[s[i-1]]) % MOD

t_ruiseki = [0]*(len(t)+1)
for i in range(1,len(t)+1):
  t_ruiseki[i] = (t_ruiseki[i-1]+memo[t[i-1]]) % MOD

for i in range(q):
  ans1 =  (2*s_ruiseki[info[i][0]-1] + s_ruiseki[info[i][1]])%MOD
  ans2 =  (2*t_ruiseki[info[i][2]-1] + t_ruiseki[info[i][3]])%MOD
  print("YES" if ans1 == ans2 else "NO")

Solution (Python):
s = input()
t = input()
q =int(input())
info = [list(map(int,input().split())) for i in range(q)]

s_ruiseki = "X"
for i in range(len(s)):
  if s_ruiseki[-1] == "X":
    s_ruiseki = s_ruiseki+s[i]
  elif s_ruiseki[-1] != s[i]:
    s_ruiseki = s_ruiseki+"X"
  elif s_ruiseki[-1] == s[i] =="A":
    s_ruiseki = s_ruiseki+"B"
  else:
    s_ruiseki = s_ruiseki+"A"    

t_ruiseki = "X"
for i in range(len(t)):
  if t_ruiseki[-1] == "X":
    t_ruiseki = t_ruiseki+t[i]
  elif t_ruiseki[-1] != t[i]:
    t_ruiseki = t_ruiseki+"X"
  elif t_ruiseki[-1] == t[i] =="A":
    t_ruiseki = t_ruiseki+"B"
  else:
    t_ruiseki = t_ruiseki+"A"    

for i in range(q):
  if s_ruiseki[info[i][0]-1]  == "X":
    ans1 = s_ruiseki[info[i][1]]
  elif s_ruiseki[info[i][0]-1]  == "A":
    if s_ruiseki[info[i][1]] == "X":
      ans1 = "B"
    elif s_ruiseki[info[i][1]] == "B":
      ans1 = "A"
    elif s_ruiseki[info[i][1]] == "A":
      ans1 = "X"
  else:
    if s_ruiseki[info[i][1]] == "X":
      ans1 = "A"
    elif s_ruiseki[info[i][1]] == "B":
      ans1 = "X"
    elif s_ruiseki[info[i][1]] == "A":
      ans1 = "B"

  if t_ruiseki[info[i][2]-1]  == "X":
    ans2 = t_ruiseki[info[i][3]]
  elif t_ruiseki[info[i][2]-1]  == "A":
    if t_ruiseki[info[i][3]] == "X":
      ans2 = "B"
    elif t_ruiseki[info[i][3]] == "B":
      ans2 = "A"
    elif t_ruiseki[info[i][3]] == "A":
      ans2 = "X"  
  else:
    if t_ruiseki[info[i][3]] == "X":
      ans2 = "A"
    elif t_ruiseki[info[i][3]] == "B":
      ans2 = "X"
    elif t_ruiseki[info[i][3]] == "A":
      ans2 = "B"  
      
  if ans1 == ans2:
    print("YES")
  else:
    print("NO")

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007


def A():
    x = S()
    s = 0
    ans = len(x)
    for i in x:
        if i == "S":
            s += 1
        else:
            if s > 0:
                s -= 1
                ans -= 2
    print(ans)
    return


def B():
    n = I()
    a = LI()
    f = [0 for i in range(n+1)]
    for i in range(n):
        f[a[i]] = i
    l = [i for i in range(n+1)]
    r = [i for i in range(n+1)]
    ans = 0
    for i in range(1,n+1)[::-1]:
        j = f[i]
        ans += (j-l[j]+1)*(r[j]-j+1)*i
        l[r[j]+1] = l[j]
        r[l[j]-1] = r[j]
    print(ans)
    return


def C():
    n,x = LI()
    if x == 1 or x == 2*n-1:
        print("No")
        quit()
    else:
        ans = [i+1 for i in range(2*n-1)]
        k = x-n
        if k < 0:
            ans = ans[2*n-1+k:]+ans[:2*n-1+k]
        if k > 0:
            ans = ans[k:]+ans[:k]
        print("Yes")
        for i in ans:
            print(i)
    return


def D():
    s = S()
    t = S()
    n = len(s)
    m = len(t)
    fs = [1 if s[i] == "A" else 2 for i in range(n)]
    ft = [1 if t[i] == "A" else 2 for i in range(m)]
    for i in range(n-1):
        fs[i+1] += fs[i]
        fs[i+1] %= mod
    for i in range(m-1):
        ft[i+1] += ft[i]
        ft[i+1] %= mod
    fs.insert(0,0)
    ft.insert(0,0)
    q = I()
    for i in range(q):
        a,b,c,d = LI()
        a -= 1
        c -= 1
        if (fs[b]-fs[a])%3 == (ft[d]-ft[c])%3:
            print("YES")
        else:
            print("NO")
    return


def E():
    return


def F():
    return


def G():
    return


def H():
    return


if __name__ == "__main__":
    D()

Solution (Python):
S = input()
T = input()

q = int(input())

SA = [0]
SB = [0]
TA = [0]
TB = [0]

sa=0
sb=0
ta=0
tb=0

for i in range(len(S)):
    if S[i]=="A":
        sa+=1
    elif S[i]=="B":
        sb+=1
    SA.append(sa)
    SB.append(sb)
    

for i in range(len(T)):
    if T[i]=="A":
        ta+=1
    elif T[i]=="B":
        tb+=1
    TA.append(ta)
    TB.append(tb)

for i in range(q):
    a,b,c,d = list(map(int,input().split()))
    sa_num=SA[b]-SA[a-1]
    sb_num=SB[b]-SB[a-1]
    ta_num=TA[d]-TA[c-1]
    tb_num=TB[d]-TB[c-1]
    if (sa_num+2*sb_num)%3==(ta_num+2*tb_num)%3:
        print("YES")
    else:
        print("NO")

Solution (Python):
s = input()
t = input()
ls = len(s)
lt = len(t)
sl = [0]*(ls+1)
tl = [0]*(lt+1)
for i in range(ls):
  if s[i] == "A":
    sl[i+1] = sl[i] + 1
  else:
    sl[i+1] = sl[i] + 2
for i in range(lt):
  if t[i] == "A":
    tl[i+1] = tl[i] + 1
  else:
    tl[i+1] = tl[i] + 2
for _ in range(int(input())):
  a,b,c,d = map(int, input().split())
  if (sl[b]-tl[d]-sl[a-1]+tl[c-1])%3 == 0:
    print("YES")
  else:
    print("NO")

Solution (Python):
import sys
F = sys.stdin
S = F.readline().replace("A", "1").replace("B", "2").strip("\n")
T = F.readline().replace("A", "1").replace("B", "2").strip("\n")

sSum = [0] * (len(S)+1)
tSum = [0] * (len(T)+1)
for i in range(len(S)):
    sSum[i+1] = sSum[i] + int(S[i])
for i in range(len(T)):
    tSum[i+1] = tSum[i] + int(T[i])

Ans = ""
Q = int(F.readline().strip("\n"))
for _ in range(Q):
    a, b, c, d = map(int, F.readline().strip("\n").split(" "))
    if (sSum[b] - sSum[a-1]) % 3 == (tSum[d] - tSum[c-1]) % 3: Ans += "YES\n"
    else: Ans += "NO\n"
print(Ans)

Solution (Python):
import sys
input = sys.stdin.readline
s = list(input())
t = list(input())

snum = [(0,0)] 
for i in range(len(s)):
    if s[i] == 'A':
        snum.append((snum[-1][0]+1,snum[-1][1]))
    else:
        snum.append((snum[-1][0],snum[-1][1]+1))

tnum = [(0,0)]
for i in range(len(t)):
    if t[i] == 'A':
        tnum.append((tnum[-1][0]+1,tnum[-1][1]))
    else:
        tnum.append((tnum[-1][0],tnum[-1][1]+1))

q = int(input())
for i in range(q):
    a,b,c,d = map(int,input().split())
    sa = snum[b][0]-snum[a-1][0]
    sb = snum[b][1]-snum[a-1][1]
    ta = tnum[d][0]-tnum[c-1][0]
    tb = tnum[d][1]-tnum[c-1][1]
    
    
    b1 = sb+sa*2
    b2 = tb+ta*2
    if abs(b2-b1)%3==0:
        print('YES')
        continue
    
    
    print('NO')

Solution (Python):
import sys
input = sys.stdin.readline
s, t, q = list(input()), list(input()), int(input())
ssum, tsum = [0], [0]
for i in range(len(s)):
    if s[i]=="A": ssum.append(ssum[-1]+1)
    else: ssum.append(ssum[-1]+2)
for i in range(len(t)):
    if t[i]=="A": tsum.append(tsum[-1]+1)
    else: tsum.append(tsum[-1]+2)
for i in range(q):
    a,b,c,d = map(int,input().split())
    sa = (ssum[b]-ssum[a-1])-(tsum[d]-tsum[c-1])
    if abs(sa)%3==0:print("YES")
    else: print("NO")

Solution (Python):
s, t, q = list(input()), list(input()), int(input())
ssum, tsum = [0], [0]
for i in range(len(s)):
    if s[i]=="A": ssum.append(ssum[-1]+1)
    else: ssum.append(ssum[-1]+2)
for i in range(len(t)):
    if t[i]=="A": tsum.append(tsum[-1]+1)
    else: tsum.append(tsum[-1]+2)
for i in range(q):
    a,b,c,d = map(int,input().split())
    sa = (ssum[b]-ssum[a-1])-(tsum[d]-tsum[c-1])
    if abs(sa)%3==0:print("YES")
    else: print("NO")

Solution (Python):
s, t, q = list(input()), list(input()), int(input())
ssum, tsum = [0], [0]
for i in range(len(s)):
    if s[i]=="A": ssum.append(ssum[-1]+1)
    else: ssum.append(ssum[-1]+2)
for i in range(len(t)):
    if t[i]=="A": tsum.append(tsum[-1]+1)
    else: tsum.append(tsum[-1]+2)
for i in range(q):
    a,b,c,d = map(int,input().split())
    sa = (ssum[b]-ssum[a-1])-(tsum[d]-tsum[c-1])
    if abs(sa)%3==0:print("YES")
    else: print("NO")

Solution (Python):
import sys
input = sys.stdin.readline
s = list(input())
t = list(input())

snum = [(0,0)] 
for i in range(len(s)):
    if s[i] == 'A':
        snum.append((snum[-1][0]+1,snum[-1][1]))
    else:
        snum.append((snum[-1][0],snum[-1][1]+1))

tnum = [(0,0)]
for i in range(len(t)):
    if t[i] == 'A':
        tnum.append((tnum[-1][0]+1,tnum[-1][1]))
    else:
        tnum.append((tnum[-1][0],tnum[-1][1]+1))

q = int(input())
for i in range(q):
    a,b,c,d = map(int,input().split())
    sa = snum[b][0]-snum[a-1][0]
    sb = snum[b][1]-snum[a-1][1]
    ta = tnum[d][0]-tnum[c-1][0]
    tb = tnum[d][1]-tnum[c-1][1]
    
    
    b1 = sb+sa*2
    b2 = tb+ta*2
    if abs(b2-b1)%3==0:
        print('YES')
        continue
    
    
    print('NO')

Solution (Python):
import sys
input = sys.stdin.readline
s = list(input())
t = list(input())

snum = [(0,0)] 
for i in range(len(s)):
    if s[i] == 'A':
        snum.append((snum[-1][0]+1,snum[-1][1]))
    else:
        snum.append((snum[-1][0],snum[-1][1]+1))

tnum = [(0,0)]
for i in range(len(t)):
    if t[i] == 'A':
        tnum.append((tnum[-1][0]+1,tnum[-1][1]))
    else:
        tnum.append((tnum[-1][0],tnum[-1][1]+1))

q = int(input())
for i in range(q):
    a,b,c,d = map(int,input().split())
    sa = snum[b][0]-snum[a-1][0]
    sb = snum[b][1]-snum[a-1][1]
    ta = tnum[d][0]-tnum[c-1][0]
    tb = tnum[d][1]-tnum[c-1][1]
    
    
    b1 = sb+sa*2
    b2 = tb+ta*2
    if abs(b2-b1)%3==0:
        print('YES')
        continue
    
    
    a1 = sa+sb*2
    a2 = ta+tb*2
    if abs(a2-a1)%3==0:
        print('YES')
        continue
    
    print('NO')

Solution (Python):
import sys
input = sys.stdin.readline
s = list(input())
t = list(input())

snum = [(0,0)] 
for i in range(len(s)):
    if s[i] == 'A':
        snum.append((snum[-1][0]+1,snum[-1][1]))
    else:
        snum.append((snum[-1][0],snum[-1][1]+1))

tnum = [(0,0)]
for i in range(len(t)):
    if t[i] == 'A':
        tnum.append((tnum[-1][0]+1,tnum[-1][1]))
    else:
        tnum.append((tnum[-1][0],tnum[-1][1]+1))

q = int(input())
for i in range(q):
    a,b,c,d = map(int,input().split())
    sa = snum[b][0]-snum[a-1][0]
    sb = snum[b][1]-snum[a-1][1]
    ta = tnum[d][0]-tnum[c-1][0]
    tb = tnum[d][1]-tnum[c-1][1]
    
    
    b1 = sb+sa*2
    b1 += max(tb-b1,0)//3*3+3
    a1 = (b1-tb)*2
    if abs(a1-ta)%3==0:
        print('YES')
        continue
    
    
    a2 = sa+sb*2
    a2 += max(ta-a2,0)//3*3+3
    b2 = (a2-ta)*2
    if abs(b2-tb)%3==0:
        print('YES')
        continue
    
    print('NO')

Solution (Python):
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

s = ns()
t = ns()
q = ni()

sa = [0]
sb = [0]
ta = [0]
tb = [0]

for si in s:
    if si == "A":
        sa.append(sa[-1] + 1)
        sb.append(sb[-1])
        
    else:
        sa.append(sa[-1])
        sb.append(sb[-1] + 1)
        
for ti in t:
    if ti == "A":
        ta.append(ta[-1] + 1)
        tb.append(tb[-1])
        
    else:
        ta.append(ta[-1])
        tb.append(tb[-1] + 1)
        
for _ in range(q):
    a,b,c,d = li()
    
    sanum = sa[b] - sa[a-1]
    sbnum = sb[b] - sb[a-1]
    
    tanum = ta[d] - ta[c-1]
    tbnum = tb[d] - tb[c-1]
    
    sanum += 2*sbnum
    tanum += 2*tbnum
    
    if sanum%3 == tanum%3:
        print('YES')
    else:
        print('NO')

Solution (Python):
def getN():
    return int(input())

def getMN():
    a = input().split()
    b = [int(i) for i in a]
    return b[0],b[1]

def getlist():
    a = input().split()
    b = [int(i) for i in a]
    return b

astr = input()
bstr = input()
n = getN()
queries = []
for i in range(n):
    queries.append(getlist())
s1 = 1 if astr[0] == "A" else 2
t1 = 1 if bstr[0] == "A" else 2
s_acc = [0]
t_acc = [0]
tmp = 0
for s in astr:
    if s == "A":
        s_acc.append(s_acc[-1] + 1)
    else:
        s_acc.append(s_acc[-1] + 2)

for s in bstr:
    if s == "A":
        t_acc.append(t_acc[-1] + 1)
    else:
        t_acc.append(t_acc[-1] + 2)


for q in queries:
    x,y,z,w = q

    if (s_acc[y] - s_acc[x-1]) % 3 == (t_acc[w] - t_acc[z-1])%3:
        print("YES")
    else:
        print("NO")

Solution (Python):
S = input()
T = input()
N = len(S)
M = len(T)
Ss = [0]*(N+1)
Ts = [0]*(M+1)
for i in range(N):
  if S[i] == "A":
    temp = 1
  else:
    temp = -1
  Ss[i+1] = Ss[i] + temp
for i in range(M):
  if T[i] == "A":
    temp = 1
  else:
    temp = -1
  Ts[i+1] = Ts[i] + temp
Q = int(input())
for i in range(Q):
  a, b, c, d = (int(i) for i in input().split())
  if (Ss[b] - Ss[a-1] - Ts[d] + Ts[c-1])%3==0:
    print("YES")
  else:
    print("NO")

Solution (Python):
S=input()
T=input()
q=int(input())
table=[]
for i in range(q):
    a,b,c,d=map(int,input().split())
    a,b,c,d=a-1,b-1,c-1,d-1
    table.append((a,b,c,d))
N=len(S)
M=len(T)
A=[[0]*(N+1) for i in range(2)]
B=[[0]*(N+1) for i in range(2)]
for i in range(1,N+1):
    if S[i-1]=='A':
        A[0][i]+=A[0][i-1]+1
        A[1][i]+=A[1][i-1]
    else:
        A[0][i]+=A[0][i-1]
        A[1][i]+=A[1][i-1]+1
for i in range(1,M+1):
    if T[i-1]=='A':
        B[0][i]+=B[0][i-1]+1
        B[1][i]+=B[1][i-1]
    else:
        B[0][i]+=B[0][i-1]
        B[1][i]+=B[1][i-1]+1


for i in range(q):
    a,b,c,d = table[i]
    
    
    x=A[0][b+1]-A[0][a]
    y=A[1][b+1]-A[1][a]
    z=B[0][d+1]-B[0][c]
    w=B[1][d+1]-B[1][c]
    
    if (x+2*y)%3==(z+2*w)%3:
        print('YES')
    else:
        print('NO')

Solution (Python):
s = input()
t = input()
q = int(input())
xa = [0]*len(s)+[0]
xb = [0]*len(s)+[0]
ya = [0]*len(t)+[0]
yb = [0]*len(t)+[0]
for i in range(len(s)):
  if s[i] == "A":
    xa[i+1] = xa[i] + 1
    xb[i+1] = xb[i]
  else:
    xa[i+1] = xa[i] 
    xb[i+1] = xb[i] + 1
for i in range(len(t)):
  if t[i] == "A":
    ya[i+1] = ya[i] + 1
    yb[i+1] = yb[i]
  else:
    ya[i+1] = ya[i] 
    yb[i+1] = yb[i] + 1
for i in range(q):
  a,b,c,d = map(int,input().split())
  sa = (xa[b] -xa[a-1]) %3
  sb = (xb[b] -xb[a-1]) %3
  ta = (ya[d] -ya[c-1]) %3
  tb = (yb[d] -yb[c-1]) %3
  if [(sa-1)%3,(sb+2)%3] == [ta,tb] or [(sa+2)%3,(sb-1)%3] == [ta,tb] or [sa,sb] == [ta,tb] or [(sa-2)%3,(sb+4)%3] == [ta,tb] or [(sa+4)%3,(sb-2)%3] == [ta,tb]:
    print("YES")
  else:
    print("NO")

Solution (Python):
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]

S=input()
T=input()
s=[0 for i in range(len(S))]
for i in range(len(s)):
 if S[i]=="B":
    s[i]=2
 else:
    s[i]=1

t=[0 for i in range(len(T))]
for i in range(len(t)):
 if T[i]=="B":
    t[i]=2
 else:
    t[i]=1
for i in range(len(s)-1):
   s[i+1]+=s[i]
for i in range(len(t)-1):
   t[i+1]+=t[i]
q=i1()
s=[0]+s
t=[0]+t
for i in range(q):
 [a,b,c,d]=i2()
 if (s[b]-s[a-1])%3==(t[d]-t[c-1])%3:
    print("YES")
 else:
    print("NO")

Solution (Python):
s=input()
t=input()
S,T=[0],[0]
for i in range(len(s)):
  if s[i]=="A":
    S.append((S[-1]+1)%3)
  elif s[i]=="B":
    S.append((S[-1]+2)%3)
for i in range(len(t)):
  if t[i]=="A":
    T.append((T[-1]+1)%3)
  elif t[i]=="B":
    T.append((T[-1]+2)%3)
    
Q=int(input())
for q in range(Q):
  a,b,c,d=map(int,input().split())
  if (S[b]-S[a-1])%3==(T[d]-T[c-1])%3:
    print("YES")
  else:
    print("NO")

Solution (Python):
def inpl(): return [int(i) for i in input().split()]

S = input()
T = input()
typeS = [0 for _ in range(1+len(S))]
typeT = [0 for _ in range(1+len(T))]
typeS[0] = typeT[0] = 0 
for i in range(len(S)):
    ctr = 2
    if S[i] == 'A':
        ctr = 1
    typeS[i+1] = (typeS[i] + ctr)%3
for i in range(len(T)):
    ctr = 2
    if T[i] == 'A':
        ctr = 1
    typeT[i+1] = (typeT[i] + ctr)%3
Q = int(input())
for _ in range(Q):
    a, b, c, d = inpl()
    if (typeS[b] - typeS[a-1])%3 == (typeT[d] -typeT[c-1])%3:
        print('YES')
    else:
        print('NO')

Solution (Python):
def inpl(): return [int(i) for i in input().split()]

S = input()
T = input()
typeS = [0 for _ in range(1+len(S))]
typeT = [0 for _ in range(1+len(T))]
typeS[0] = typeT[0] = 0 
for i in range(len(S)):
    ctr = 2
    if S[i] == 'A':
        ctr = 1
    typeS[i+1] = (typeS[i] + ctr)%3
for i in range(len(T)):
    ctr = 2
    if T[i] == 'A':
        ctr = 1
    typeT[i+1] = (typeT[i] + ctr)%3
Q = int(input())
for _ in range(Q):
    a, b, c, d = inpl()
    if (typeS[b] - typeS[a-1])%3 == (typeT[d] -typeT[c-1])%3:
        print('YES')
    else:
        print('NO')