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



Sample input:
BBBAAAABA
BBBBA
4
7 9 2 5
7 9 1 4
1 7 2 5
1 7 2 4


Sample output:
YES
NO
YES
NO

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/arc071/submissions/6325840

from itertools import accumulate

convert = {'A': 1, 'B': 2}
S = tuple(convert[x] for x in input())
T = tuple(convert[x] for x in input())
# maspyさんのやり方

acc_s = (0,) + tuple(accumulate(S))
acc_t = (0,) + tuple(accumulate(T))

ans = []
Q = int(input())
for _ in range(Q):
    sl, sr, tl, tr = map(int, input().split())
    diff = (acc_t[tr] - acc_t[tl - 1]) - (acc_s[sr] - acc_s[sl - 1])
    # 添え字-1は変数再代入しない方が20ms速い
    ans.append('YES' if diff % 3 == 0 else 'NO')

print(*ans, sep='\n')
# 逐次printだと1000ms
# 一括だと500ms

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/arc071/submissions/6325840

from itertools import accumulate

S = tuple(map(lambda x: 1 if x == 'A' else 2, input()))
T = tuple(map(lambda x: 1 if x == 'A' else 2, input()))
# A=1,B=2


acc_s = (0,) + tuple(accumulate(S))
acc_t = (0,) + tuple(accumulate(T))

ans = []
Q = int(input())
for _ in range(Q):
    sl, sr, tl, tr = map(int, input().split())
    diff = (acc_t[tr] - acc_t[tl - 1]) - (acc_s[sr] - acc_s[sl - 1])
    # 添え字-1は変数再代入しない方が20ms速い
    ans.append('YES' if diff % 3 == 0 else 'NO')

print(*ans, sep='\n')
# 逐次printだと1000ms
# 一括だと500ms

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/arc071/submissions/6325840

from itertools import accumulate

*S, = map(lambda x: 1 if x == 'A' else 2, input())
*T, = map(lambda x: 1 if x == 'A' else 2, input())
# A=1,B=2

acc_s = (0,) + tuple(accumulate(S))
acc_t = (0,) + tuple(accumulate(T))

ans = []
Q = int(input())
for _ in range(Q):
    sl, sr, tl, tr = map(int, input().split())
    diff = (acc_t[tr] - acc_t[tl - 1]) - (acc_s[sr] - acc_s[sl - 1])
    ans.append('YES' if diff % 3 == 0 else 'NO')

print(*ans, sep='\n')
# 逐次printだと1000ms
# 一括だと500ms

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/arc071/submissions/6325840

from itertools import accumulate

*S, = map(lambda x: 1 if x == 'A' else 2, input())
*T, = map(lambda x: 1 if x == 'A' else 2, input())
# A=1,B=2

acc_s = (0,) + tuple(accumulate(S))
acc_t = (0,) + tuple(accumulate(T))

ans = []
Q = int(input())
for _ in range(Q):
    sl, sr, tl, tr = map(int, input().split())
    sl -= 1
    tl -= 1
    diff = (acc_t[tr] - acc_t[tl]) - (acc_s[sr] - acc_s[sl])
    ans.append('YES' if diff % 3 == 0 else 'NO')

print(*ans, sep='\n')

Submission status: Accepted

Solution code:
from itertools import accumulate

*S, = map(lambda x: 1 if x == 'A' else 2, input())
*T, = map(lambda x: 1 if x == 'A' else 2, input())
# A=1,B=2

acc_s = (0,) + tuple(accumulate(S))
acc_t = (0,) + tuple(accumulate(T))

Q = int(input())
for _ in range(Q):
    sl, sr, tl, tr = map(int, input().split())
    sl -= 1
    tl -= 1
    diff = (acc_t[tr] - acc_t[tl]) - (acc_s[sr] - acc_s[sl])
    print('YES' if diff % 3 == 0 else 'NO')

Submission status: Accepted

Solution code:
S = input()
T = input()
q = int(input())
abcd = [[int(i) for i in input().split()] for _ in range(q)]

sdp = [(0, 0)]
tdp = [(0, 0)]
for s in S :
    a, b = sdp[-1]
    if s == 'A' :
        sdp.append((a + 1, b))
    else :
        sdp.append((a, b + 1))
for t in T :
    a, b = tdp[-1]
    if t == 'A' :
        tdp.append((a + 1, b))
    else :
        tdp.append((a, b + 1))

for a, b, c, d in abcd :
    sa = sdp[b][0] - sdp[a-1][0]
    sb = sdp[b][1] - sdp[a-1][1]
    ta = tdp[d][0] - tdp[c-1][0]
    tb = tdp[d][1] - tdp[c-1][1]
    
    tb -= ta - sa
    if abs(sb - tb) % 3 == 0 :
        print('YES')
    else :
        print('NO')

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    # 全部 A にした時に、差が3の約数なら良さそう
    S=[1 if(s=='A') else 2 for s in input()]
    T=[1 if(s=='A') else 2 for s in input()]

    # cumsum
    m=len(S)
    n=len(T)
    CS=[0]*(m+1)
    for i in range(m):
        CS[i+1]=CS[i]+S[i]

    CT=[0]*(n+1)
    for i in range(n):
        CT[i+1]=CT[i]+T[i]

    # output
    for _ in range(int(input())):
        a,b,c,d=map(int,input().split())
        a-=1; c-=1
        k=abs((CS[b]-CS[a])-(CT[d]-CT[c]))
        print("YES" if(k%3==0) else "NO")
resolve()

Submission status: Accepted

Solution code:

S = input()
T = input()

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

for i in S:
    if i == "A":
        sa.append(sa[-1] + 1)
        sb.append(sb[-1])
    else:
        sa.append(sa[-1])
        sb.append(sb[-1] + 1)

for i in T:
    if i == "A":
        ta.append(ta[-1] + 1)
        tb.append(tb[-1])
    else:
        ta.append(ta[-1])
        tb.append(tb[-1] + 1)


q = int(input())

for i in range(q):

    a,b,c,d = map(int,input().split())

    one = ((sa[b] - sa[a-1]) - (sb[b] - sb[a-1]))
    two = ((ta[d] - ta[c-1]) - (tb[d] - tb[c-1]))

    if  one % 3 == two % 3:
        print ("YES")
    else:
        print ("NO")

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate
readline = sys.stdin.readline

S = [1 if s == 'A' else 2 for s in readline().strip()]
T = [1 if s == 'A' else 2 for s in readline().strip()]
AS = [0] + list(accumulate(S))
AT = [0] + list(accumulate(T))
Q = int(readline())
Ans = [None]*Q 

for qu in range(Q):
    a, b, c, d = map(int, readline().split())
    if (AS[b]-AS[a-1])%3 != (AT[d]-AT[c-1])%3:
        Ans[qu] = 'NO'    
    else:
        Ans[qu] = 'YES'

print('\n'.join(map(str, Ans)))

Submission status: Accepted

Solution code:
S = [0] + [1 if a == "A" else 2 for a in input()]
T = [0] + [1 if a == "A" else 2 for a in input()]
for i in range(1, len(S)):
    S[i] += S[i-1]
for i in range(1, len(T)):
    T[i] += T[i-1]
for _ in range(int(input())):
    a, b, c, d = map(int, input().split())
    if (S[b] - S[a-1] - T[d] + T[c-1]) % 3 == 0:
        print("YES")
    else:
        print("NO")

Submission status: Accepted

Solution code:
def main():
    def plus(a,b):
        if a=="":
            return b
        elif b=="":
            return a
        elif a=="A":
            if b=="A":
                return "B"
            else:
                return ""
        else:
            if b=="A":
                return ""
            else:
                return "A"
    def minus(a,b):
        if b=="A":
            b="B"
        elif b=="B":
            b="A"
        return plus(a,b)
    dp1=[""]
    dp2=[""]
    s=input()
    t=input()
    q=int(input())
    abcd=[list(map(int,input().split())) for _ in [0]*q]
    for i in s:
        dp1.append(plus(dp1[-1],i))
    for i in t:
        dp2.append(plus(dp2[-1],i))
    for a,b,c,d in abcd:
        a,b,c,d=dp1[a-1],dp1[b],dp2[c-1],dp2[d]
        print(["NO","YES"][minus(b,a)==minus(d,c)])
main()

Submission status: Accepted

Solution code:
#ライブラリインポート
from collections import defaultdict

#入力受け取り
def getlist():
	return list(map(int, input().split()))

#処理内容
def main():
	S = list(input())
	N = len(S)
	s = [0] * (N + 1)
	for i in range(N):
		if S[i] == "A":
			s[i + 1] += s[i] + 2
		else:
			s[i + 1] += s[i] + 1
	T = list(input())
	M = len(T)
	t = [0] * (M + 1)
	for i in range(M):
		if T[i] == "A":
			t[i + 1] += t[i] + 2
		else:
			t[i + 1] += t[i] + 1
	Q = int(input())
	for i in range(Q):
		a, b, c, d = getlist()
		if ((s[b] - s[a - 1]) - (t[d] - t[c - 1])) % 3 == 0:
			print("YES")
		else:
			print("NO")

if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

from bisect import bisect_left, bisect_right

S = list(input().rstrip())
T = list(input().rstrip())
Q = int(input())
Query = [list(map(int, input().split())) for _ in range(Q)]

Sa = []
Sb = []
for i, s in enumerate(S):
    if s == 'A':
        Sa.append(i)
    else:
        Sb.append(i)

Ta = []
Tb = []
for i, t in enumerate(T):
    if t == 'A':
        Ta.append(i)
    else:
        Tb.append(i)

def kind(La, Lb, l, r):
    ca = bisect_right(La, r) - bisect_left(La, l)
    cb = bisect_right(Lb, r) - bisect_left(Lb, l)
    return (ca + cb*2)%3 

for a, b, c, d in Query:
    if kind(Sa, Sb, a-1, b-1) == kind(Ta, Tb, c-1, d-1):
        print('YES')
    else:
        print('NO')

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

S = input()
T = input()
q = int(input())
ls = len(S)
lt = len(T)

cs_s = [0] * (ls+1)
cs_t = [0] * (lt+1)
for i in range(ls):
    tmp = 1 if S[i] == 'A' else -1
    cs_s[i+1] = cs_s[i] + tmp
for i in range(lt):
    tmp = 1 if T[i] == 'A' else -1
    cs_t[i+1] = cs_t[i] + tmp

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

Submission status: Accepted

Solution code:
s = input()
t = input()
q = int(input())

len_s = len(s)
len_t = len(t)

s_counter= [0]*(len_s+1)
s_temp = 0
for i in range(len_s):
	if s[i] == "A":
		s_temp += 1
	s_counter[i+1] = s_temp
	
t_counter= [0]*(len_t+1)
t_temp = 0
for i in range(len_t):
	if t[i] == "A":
		t_temp += 1
	t_counter[i+1] = t_temp

for _ in range(q):
	a, b, c, d = map(int, input().split())
	
	x = s_counter[b]-s_counter[a-1]
	y = t_counter[d]-t_counter[c-1]
	print(['NO', 'YES'][((x+b-a+1)-(y+d-c+1))%3==0])

Submission status: Accepted

Solution code:
S = input()
T = input()
Q = int(input())

N = len(S)
M = len(T)
accSA = [0] * (N + 1)
accSB = [0] * (N + 1)
accTA = [0] * (M + 1)
accTB = [0] * (M + 1)

for i, s in enumerate(S, start=1):
    accSA[i] = accSA[i - 1]
    accSB[i] = accSB[i - 1]
    if s == 'A':
        accSA[i] += 1
    else:
        accSB[i] += 1

for i, t in enumerate(T, start=1):
    accTA[i] = accTA[i - 1]
    accTB[i] = accTB[i - 1]
    if t == 'A':
        accTA[i] += 1
    else:
        accTB[i] += 1

ans = []
for _ in range(Q):
    leftS, rightS, leftT, rightT = map(int, input().split())

    sA = accSA[rightS] - accSA[leftS - 1]
    sB = accSB[rightS] - accSB[leftS - 1]
    tA = accTA[rightT] - accTA[leftT - 1]
    tB = accTB[rightT] - accTB[leftT - 1]

    if (sA + sB * 2) % 3 == (tA + tB * 2) % 3:
        ans.append('YES')
    else:
        ans.append('NO')

print(*ans, sep='\n')

Submission status: Accepted

Solution code:
S = input()
T = input()
Q = int(input())
qs = [tuple(map(int,input().split())) for i in range(Q)]

sas = [0]
sbs = [0]
for c in S:
    a = int(c=='A')
    sas.append(sas[-1] + a)
    sbs.append(sbs[-1] + (a^1))

tas = [0]
tbs = [0]
for c in T:
    a = int(c=='A')
    tas.append(tas[-1] + a)
    tbs.append(tbs[-1] + (a^1))

ans = []
for a,b,c,d in qs:
    sa = sas[b] - sas[a-1]
    sb = sbs[b] - sbs[a-1]
    ta = tas[d] - tas[c-1]
    tb = tbs[d] - tbs[c-1]
    s = (sb-sa)%3
    t = (tb-ta)%3
    ans.append('YES' if s==t else 'NO')
print(*ans, sep='\n')

Submission status: Accepted

Solution code:
s,t,_,*z=open(0)
v,w=[0],[0]
for c in s:v+=v[-1]+(c<'B'or 2),
for c in t:w+=w[-1]+(c<'B'or 2),
for x in z:
  a,b,c,d=map(int,x.split())
  print('YNEOS'[(v[b]-v[a-1])%3!=(w[d]-w[c-1])%3::2])

Submission status: Accepted

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

cumSA = [0] * (len(S) + 1)
cumSB = [0] * (len(S) + 1)
cumTA = [0] * (len(T) + 1)
cumTB = [0] * (len(T) + 1)

for i, s in enumerate(S, start=1):
    cumSA[i] = cumSA[i - 1]
    cumSB[i] = cumSB[i - 1]

    if s == 'A':
        cumSA[i] += 1
    else:
        cumSB[i] += 1

for i, t in enumerate(T, start=1):
    cumTA[i] = cumTA[i - 1]
    cumTB[i] = cumTB[i - 1]

    if t == 'A':
        cumTA[i] += 1
    else:
        cumTB[i] += 1

Q = int(input())
ans = []
for _ in range(Q):
    lS, rS, lT, rT = map(int, input().split())

    sA = cumSA[rS] - cumSA[lS - 1]
    sB = cumSB[rS] - cumSB[lS - 1]
    tA = cumTA[rT] - cumTA[lT - 1]
    tB = cumTB[rT] - cumTB[lT - 1]

    if (sA - sB) % 3 == (tA - tB) % 3:
        ans.append('YES')
    else:
        ans.append('NO')

print(*ans, sep='\n')

Submission status: Accepted

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

# 還元して同じならいいんじゃないか
d = {"A":1, "B":-1}
def make(s):
    ss = [0]
    for i in s:
        if ss[-1] == 0:
            ss.append(d[i])
        else:
            if ss[-1] == d[i]:
                ss.append(ss[-1]*-1)
            else:
                ss.append(0)
    return ss

sl = make(s)
tl = make(t)

def convert(a,b):
    if a == 0:
        return b
    if a == 1:
        c = b-a
        if c == -2:
            c = 1
        return c
    if a == -1:
        c = b-a
        if c == 2:
            c = -1
        return c

q = int(input())
ans = []
for i in range(q):
    a,b,c,d = map(int, input().split())
    sleft = sl[a-1]
    sright = sl[b]
    tleft = tl[c-1]
    tright = tl[d]
    if convert(sleft, sright) == convert(tleft, tright):
        ans.append("YES")
    else:
        ans.append("NO")
print(*ans, sep="\n")

Submission status: Accepted

Solution code:
S = input()
T = input()
q = int(input())

SS = [0]
TT = [0]
for i in S:
    if i=="A":
        SS.append(1+SS[-1])
    else:
        SS.append(2+SS[-1])
for i in T:
    if i=="A":
        TT.append(1+TT[-1])
    else:
        TT.append(2+TT[-1])

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")

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(pow(10, 6))


def main():
    s = input().strip()
    t = input().strip()
    accs, acct = [0], [0]
    for i in range(len(s)):
        if s[i] == "A":
            accs.append(accs[-1]+1)
        else:
            accs.append(accs[-1]+2)
    for i in range(len(t)):
        if t[i] == "A":
            acct.append(acct[-1]+1)
        else:
            acct.append(acct[-1]+2)
    q = int(input())
    for _ in range(q):
        a, b, c, d = map(int, input().split())
        if (accs[b]-accs[a-1])%3 == (acct[d]-acct[c-1])%3:
            print("YES")
        else:
            print("NO")


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    S = list(str(input()))
    T = list(str(input()))
    q = int(input())
    S_cum = [0]
    T_cum = [0]
    for i in S:
        if i == "A":
            S_cum.append((S_cum[-1]+1)%3)
        else:
            S_cum.append((S_cum[-1]+2)%3)
    for i in T:
        if i == "A":
            T_cum.append((T_cum[-1]+1)%3)
        else:
            T_cum.append((T_cum[-1]+2)%3)
    for i in range(q):
        a,b,c,d = map(int,input().split())
        s_ = S_cum[b]-S_cum[a-1]
        t_ = T_cum[d]-T_cum[c-1]
        if s_%3 == t_%3:
            print("YES")
        else:
            print("NO")

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
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" )

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

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

import sys
input = sys.stdin.readline
# 前計算
S_n = [0] * (len(S) + 1)
# S_n[0] = 1 if S[0] == 'A' else 2
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)
# T_n[0] = 1 if T[0] == 'A' else 2
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())
    #output
    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')

Submission status: Accepted

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

# 前計算
S_n = [0] * (len(S) + 1)
# S_n[0] = 1 if S[0] == 'A' else 2
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)
# T_n[0] = 1 if T[0] == 'A' else 2
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())
    #output
    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')

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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")

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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")

Submission status: Accepted

Solution code:
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")

Submission status: Accepted

Solution code:
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")

Submission status: Accepted

Solution code:
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")

Submission status: Accepted

Solution code:
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)
#print(S)
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")

Submission status: Accepted

Solution code:
#!/usr/bin/env python3


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()

Submission status: Accepted

Solution code:
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")

Submission status: Accepted

Solution code:
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")