ZhenlongDai/ACPR huggingface dataset:
Problem: p02962
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    MOD = 2**61-1
    SI = lambda : sys.stdin.readline().rstrip()

    s = SI()
    t = SI()
    s = s * 2
    lens = len(s)
    lent = len(t)

    def cx(x):
        return ord(x) - ord('a') + 1

    hash = 0
    for x in t:
        hash = (hash * 26 + cx(x)) % MOD

    cnt = 0
    f = False
    h = 0
    last_i = 0
    ans = 0
    for i in range(lens):
        if i >= lent:
            h -= cx(s[i-lent]) * pow(26,lent-1,MOD)
        h = (h * 26 + cx(s[i])) % MOD
        if h == hash:
            cnt += 1
            ans = max(ans,cnt)
            last_i = i
        else:
            if i - last_i >= lent and i < lent:
                cnt = 0
                f = True
    if f or ans == 0:
        print(ans)
    else:
        print(-1)

if __name__ == '__main__':
    main()

Failing test case input:
aa`bdca
aa

Expected output:
1

Actual output:
-1


Fixed submission (verdict: Accepted):
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    MOD = 2**61-1
    SI = lambda : sys.stdin.readline().rstrip()

    s = SI()
    t = SI()
    lens = len(s)
    lent = len(t)

    def cx(x):
        return ord(x) - ord('a') + 1

    hsh = 0
    for x in t:
        hsh = (hsh * 26 + cx(x)) % MOD

    n = (lens+lent) * 2 + 1
    dp = [0] * n

    h = 0
    ans = 0
    for i in range(n):
        if i >= lent:
            h -= cx(s[(i - lent)%lens]) * pow(26,lent-1,MOD)
        h = (h * 26 + cx(s[i % lens])) % MOD
        if h == hsh:
            if i < lent:
                dp[i] = 1
            else:
                dp[i] = dp[i-lent] + 1
            ans = max(ans,dp[i])

    if n - ans * lent < lent * 2:
        print(-1)
    else:
        print(ans)

if __name__ == '__main__':
    main()
Problem: p02962
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
s = input()
t = input()


def pattern(inputv):
    if not inputv:
        return inputv

    nxt = [0]*len(inputv)
    for i in range(1, len(nxt)):
        k = nxt[i - 1]
        while True:
            if inputv[i] == inputv[k]:
                nxt[i] = k + 1
                break
            elif k == 0:
                nxt[i] = 0
                break
            else:
                k = nxt[k - 1]

    smallPieceLen = len(inputv) - nxt[-1]
    if len(inputv) % smallPieceLen != 0:
        return inputv

    return inputv[0:smallPieceLen]

if pattern(s) == pattern(t):
    print(-1)
    exit()

s = s + s
cnt = 0
ans = []

j = 0
i = 0
li = []
flag = True
while i< len(s):
    
    if s[i] == t[j]:
        flag = True
        if j == len(t) - 1:
            cnt += 1
            j = 0
            i += 1
        else:
            j += 1
            i += 1
    else:
        if cnt != 0:
            ans.append(cnt)
        cnt = 0
        j = 0
        if flag:
            i += 1
            flag = False
        else:
            flag = True
            continue
if cnt != 0:
    ans.append(cnt)
    cnt = 0

ans.append(0)
print(max(ans))

Failing test case input:
acdaabb
ab

Expected output:
1

Actual output:
0


Fixed submission (verdict: Accepted):
s = input()
t = input()

def z_algorithm(s):
    a = [0] * len(s)
    i = 1
    j = 0
    a[0] = len(s)
    while i < len(s):
        while i + j < len(s) and s[j] == s[i+j]:
            j += 1
        a[i] = j
        if j == 0:
            i += 1
            continue
        k = 1
        while i + k < len(s) and k + a[k] < j:
            a[i+k] = a[k]
            k += 1
        i += k
        j -= k
    return a

def solve(i, li):
    ans = 0
    while True:
        if i < 0 or len(li) <= i or visited[i]:
            break
        if li[i] < len(t):
            visited[i] = True
            break
        if li[i] >= len(t):
            visited[i] = True
            ans += 1
            i += len(t)
    return ans


new_s = s
while True:
    if len(new_s) > len(t):
        s = new_s
        break
    new_s += new_s

s = s*3
li = z_algorithm(t + s)[len(t):]
visited = [False] * len(li)
ans1 = 0
for i in range(len(li)):
    ans1 = max(ans1, solve(i, li))

s += s
li = z_algorithm(t + s)[len(t):]
visited = [False] * len(li)
ans2 = 0
for i in range(len(li)):
    ans2 = max(ans2, solve(i, li))

if ans1 == ans2:
    print(ans1)
else:
    print(-1)
Problem: p02962
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    MOD = 2**61-1
    SI = lambda : sys.stdin.readline().rstrip()

    s = SI()
    t = SI()
    s = s * 2
    lens = len(s)
    lent = len(t)

    def cx(x):
        return ord(x) - ord('a') + 1

    hash = 0
    for x in t:
        hash = (hash * 26 + cx(x)) % MOD

    cnt = 0
    f = False
    h = 0
    last_i = 0
    ans = 0
    for i in range(lens):
        if i >= lent:
            h -= cx(s[i-lent]) * pow(26,lent-1,MOD)
        h = (h * 26 + cx(s[i])) % MOD
        if h == hash:
            cnt += 1
            ans = max(ans,cnt)
            last_i = i
        else:
            if i - last_i >= lent and last_i > 0:
                cnt = 0
                f = True
    if f or ans == 0:
        print(ans)
    else:
        print(-1)

if __name__ == '__main__':
    main()

Failing test case input:
aa`bdca
aa

Expected output:
1

Actual output:
2


Fixed submission (verdict: Accepted):
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    MOD = 2**61-1
    SI = lambda : sys.stdin.readline().rstrip()

    s = SI()
    t = SI()
    lens = len(s)
    lent = len(t)

    def cx(x):
        return ord(x) - ord('a') + 1

    hsh = 0
    for x in t:
        hsh = (hsh * 26 + cx(x)) % MOD

    n = (lens+lent) * 2 + 1
    dp = [0] * n

    h = 0
    ans = 0
    for i in range(n):
        if i >= lent:
            h -= cx(s[(i - lent)%lens]) * pow(26,lent-1,MOD)
        h = (h * 26 + cx(s[i % lens])) % MOD
        if h == hsh:
            if i < lent:
                dp[i] = 1
            else:
                dp[i] = dp[i-lent] + 1
            ans = max(ans,dp[i])

    if n - ans * lent < lent * 2:
        print(-1)
    else:
        print(ans)

if __name__ == '__main__':
    main()
Problem: p02962
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

S = input().rstrip()
T = input().rstrip()


LS = len(S)
LT = len(T)
n = (LT + (-LT) % LS) 
S *= (n+1)
S = S[:LS+LT]

MOD = 10 ** 9 + 7
base = 1234
base_inv = pow(base,MOD-2,MOD) 
power = [1] * (LS+LT)
power_inv = [1] * (LS+LT)
for n in range(1,LS+LT):
    power[n] = power[n-1] * base % MOD
    power_inv[n] = power_inv[n-1] * base_inv % MOD

def to_rolling_hash(S):
    ret = [0] * len(S)
    prev = 0
    for i,s in enumerate(S):
        x = (prev + power[i] * ord(s)) % MOD
        ret[i] = x
        prev = x
    return ret

S_hash = to_rolling_hash(S) + [0]
T_hash = to_rolling_hash(T)[-1] 

can_start = [(S_hash[i + LT - 1] - S_hash[i - 1]) * power_inv[i] % MOD == T_hash for i in range(LS)]


Failing test case input:
`c`
aa`ac

Expected output:
0

Actual output:

Fixed submission (verdict: Accepted):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

S = input().rstrip()
T = input().rstrip()

S,T


LS = len(S)
LT = len(T)
n = (LT + (-LT) % LS) 
S *= (n+1)
S = S[:LS+LT]

MOD = 10 ** 16 + 61
base = 12345
base_inv = pow(base,MOD-2,MOD) 
power = [1] * (LS+LT)
power_inv = [1] * (LS+LT)
for n in range(1,LS+LT):
    power[n] = power[n-1] * base % MOD
    power_inv[n] = power_inv[n-1] * base_inv % MOD

def to_rolling_hash(S):
    ret = [0] * len(S)
    for i,s in enumerate(S):
        ret[i] = (ret[i-1] + power[i] * ord(s)) % MOD
    return ret

S_hash = to_rolling_hash(S) + [0]
T_hash = to_rolling_hash(T)[-1] 

can_start = [(S_hash[i + LT - 1] - S_hash[i - 1]) * power_inv[i] % MOD == T_hash for i in range(LS)]

INF = 10 ** 18
visited = [False] * LS
dp = [None] * LS

arr = [INF] * LS

q = [(i,0) for i,bl in enumerate(can_start) if not bl]

while q:
    qq = []
    for x,d in q:
        if arr[x] == INF:
            arr[x] = d
            qq.append(((x-LT)%LS, d+1))
    q = qq

answer = max(arr)

if answer >= INF:
    answer = -1

print(answer)
Problem: p02962
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
s = input()
t = input()

def z_algorithm(s):
    a = [0] * len(s)
    i = 1
    j = 0
    a[0] = len(s)
    while i < len(s):
        while i + j < len(s) and s[j] == s[i+j]:
            j += 1
        a[i] = j
        if j == 0:
            i += 1
            continue
        k = 1
        while i + k < len(s) and k + a[k] < j:
            a[i+k] = a[k]
            k += 1
        i += k
        j -= k
    return a

def solve(i, li):
    ans = 0
    while True:
        if i < 0 or len(li) <= i or visited[i]:
            break
        if li[i] < len(t):
            visited[i] = True
            break
        if li[i] >= len(t):
            visited[i] = True
            ans += 1
            i += len(t)
    return ans


new_s = ""
while True:
    new_s += s
    if len(new_s) > len(t):
        s = new_s
        break

s = s*3
li = z_algorithm(t + s)[len(t):]
visited = [False] * len(li)
ans1 = 0
for i in range(len(li)):
    ans1 = max(ans1, solve(i, li))

print(ans1)

Failing test case input:
aa
aaaaaaa

Expected output:
-1

Actual output:
3


Fixed submission (verdict: Accepted):
s = input()
t = input()

def z_algorithm(s):
    a = [0] * len(s)
    i = 1
    j = 0
    a[0] = len(s)
    while i < len(s):
        while i + j < len(s) and s[j] == s[i+j]:
            j += 1
        a[i] = j
        if j == 0:
            i += 1
            continue
        k = 1
        while i + k < len(s) and k + a[k] < j:
            a[i+k] = a[k]
            k += 1
        i += k
        j -= k
    return a

def solve(i, li):
    ans = 0
    while True:
        if i < 0 or len(li) <= i or visited[i]:
            break
        if li[i] < len(t):
            visited[i] = True
            break
        if li[i] >= len(t):
            visited[i] = True
            ans += 1
            i += len(t)
    return ans


new_s = s
while True:
    if len(new_s) > len(t):
        s = new_s
        break
    new_s += new_s

s = s*3
li = z_algorithm(t + s)[len(t):]
visited = [False] * len(li)
ans1 = 0
for i in range(len(li)):
    ans1 = max(ans1, solve(i, li))

s += s
li = z_algorithm(t + s)[len(t):]
visited = [False] * len(li)
ans2 = 0
for i in range(len(li)):
    ans2 = max(ans2, solve(i, li))

if ans1 == ans2:
    print(ans1)
else:
    print(-1)
Problem: p02746
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
import sys
input=sys.stdin.readline

def dist(a,b,c,d,P3):
    for p in P3:
        da,ma=divmod(a,p)
        dc,mc=divmod(c,p)
        if da!=dc:
            return abs(a-c)+abs(b-d)
        if da!=1:
            a,c=ma,mc
            continue
        db,mb=divmod(b,p)
        dd,md=divmod(d,p)
        if db==dd:
            a,c=ma,mc
            continue
        return min(ma+mc+2,2*p-(ma+mc))+abs(b-d)
    return abs(b-d)

def main():
    q=int(input())
    P3=[pow(3,i) for i in reversed(range(30))]
    for _ in range(q):
        a,b,c,d=map(lambda x: int(x)-1,input().split())
        print(max(dist(a,b,c,d,P3),dist(b,a,d,c,P3)))

if __name__=='__main__':
    main()

Failing test case input:
2
3 4 6 4
12 3 2 4

Expected output:
3
11


Actual output:
5
11


Fixed submission (verdict: Accepted):
import sys
input=sys.stdin.readline

def dist(a,b,c,d,P3):
    for p in P3:
        da,ma=divmod(a,p)
        dc,mc=divmod(c,p)
        if da!=dc:
            return abs(a-c)+abs(b-d)
        if da!=1:
            a,c=ma,mc
            continue
        db,mb=divmod(b,p)
        dd,md=divmod(d,p)
        if abs(db-dd)<2:
            a,c=ma,mc
            continue
        return min(ma+mc+2,2*p-(ma+mc))+abs(b-d)
    return abs(a-c)+abs(b-d)

def main():
    q=int(input())
    P3=[pow(3,i) for i in reversed(range(30))]
    for _ in range(q):
        a,b,c,d=map(lambda x: int(x)-1,input().split())
        print(max(dist(a,b,c,d,P3),dist(b,a,d,c,P3)))

if __name__=='__main__':
    main()
Problem: p02746
Buggy submission (Python (3.4.3), verdict: Wrong Answer):

def check(lv,aa,bb,cc,dd,xy):
  a,b,c,d=aa,bb,cc,dd
  if a>c:a,c=c,a
  if b>d:b,d=d,b
  mi=3**(lv-1)
  ma=2*3**(lv-1)+1
  if xy=="x":
    return (a<=mi and ma<=c and mi<b<=d<ma)
  else:
    return (b<=mi and ma<=d and mi<a<=c<ma)
def solve(a,b,c,d):
  mx=my=0
  for i in range(1,31):
    if check(i,a,b,c,d,"x"):mx=i
    if check(i,a,b,c,d,"y"):my=i
  if mx<my:
    ans=abs(a-c)
    mi=3**(my-1)
    ma=2*3**(my-1)+1
    ans+=min(abs(mi-b)+abs(mi-d),abs(ma-b)+abs(ma-d))
    return ans
  if mx>my:
    ans=abs(b-d)
    mi=3**(mx-1)
    ma=2*3**(mx-1)+1
    ans+=min(abs(mi-a)+abs(mi-c),abs(ma-a)+abs(ma-c))
    return ans
  if mx==my==0:return abs(a-c)+abs(b-d)
  ans=1/0
q=int(input())
for _ in range(q):
  a,b,c,d=map(int,input().split())
  print(solve(a,b,c,d))


Failing test case input:
2
3 3 6 0
8 5 2 5

Expected output:
6
10


Actual output:
6
6


Fixed submission (verdict: Accepted):
def solve(aa,bb,cc,dd):
  a,b,c,d=aa,bb,cc,dd
  mx=my=0
  for i in range(1,31):
    if a==c and a%3==2 and abs(b-d)>=2:mx=i
    if b==d and b%3==2 and abs(a-c)>=2:my=i
    a=0--a
    b=0--b
    c=0--c
    d=0--d
  a,b,c,d=aa,bb,cc,dd
  if mx==my==0:return abs(a-c)+abs(b-d)
  if mx<my:
    ans=abs(a-c)
    mi=3**(my-1)
    ma=2*3**(my-1)+1
    bd=(3**my)*((0--b
    b-=bd
    d-=bd
    ans+=min(abs(mi-b)+abs(mi-d),abs(ma-b)+abs(ma-d))
    return ans
  if mx>my:
    ans=abs(b-d)
    mi=3**(mx-1)
    ma=2*3**(mx-1)+1
    ac=(3**mx)*((0--a
    a-=ac
    c-=ac
    ans+=min(abs(mi-a)+abs(mi-c),abs(ma-a)+abs(ma-c))
    return ans
  if mx==my==0:return abs(a-c)+abs(b-d)
  ans=1/0
q=int(input())
for _ in range(q):
  a,b,c,d=map(int,input().split())
  print(solve(a,b,c,d))
Problem: p02746
Buggy submission (Python (3.4.3), verdict: Wrong Answer):

def check(lv,aa,bb,cc,dd,xy):
  a,b,c,d=aa,bb,cc,dd
  if a>c:a,c=c,a
  if b>d:b,d=d,b
  mi=3**(lv-1)
  ma=2*3**(lv-1)+1
  if xy=="x":
    return (a<=mi and ma<=c and mi<b<=d<=ma)
  else:
    return (b<=mi and ma<=d and mi<a<=c<=ma)
def solve(a,b,c,d):
  mx=my=0
  for i in range(1,31):
    if check(i,a,b,c,d,"x"):mx=i
    if check(i,a,b,c,d,"y"):my=i
  if mx<my:
    ans=abs(a-c)
    mi=3**(my-1)
    ma=2*3**(my-1)+1
    ans+=min(abs(mi-b)+abs(mi-d),abs(ma-b)+abs(ma-d))
    return ans
  if mx>my:
    ans=abs(b-d)
    mi=3**(mx-1)
    ma=2*3**(mx-1)+1
    ans+=min(abs(mi-a)+abs(mi-c),abs(ma-a)+abs(ma-c))
    return ans
  if mx==my==0:return abs(a-c)+abs(b-d)
  ans=1/0
q=int(input())
for _ in range(q):
  a,b,c,d=map(int,input().split())
  print(solve(a,b,c,d))


Failing test case input:
2
3 0 2 0
12 2 2 2

Expected output:
1
12


Actual output:
1
10


Fixed submission (verdict: Accepted):
def solve(aa,bb,cc,dd):
  a,b,c,d=aa,bb,cc,dd
  mx=my=0
  for i in range(1,31):
    if a==c and a%3==2 and abs(b-d)>=2:mx=i
    if b==d and b%3==2 and abs(a-c)>=2:my=i
    a=0--a
    b=0--b
    c=0--c
    d=0--d
  a,b,c,d=aa,bb,cc,dd
  if mx==my==0:return abs(a-c)+abs(b-d)
  if mx<my:
    ans=abs(a-c)
    mi=3**(my-1)
    ma=2*3**(my-1)+1
    bd=(3**my)*((0--b
    b-=bd
    d-=bd
    ans+=min(abs(mi-b)+abs(mi-d),abs(ma-b)+abs(ma-d))
    return ans
  if mx>my:
    ans=abs(b-d)
    mi=3**(mx-1)
    ma=2*3**(mx-1)+1
    ac=(3**mx)*((0--a
    a-=ac
    c-=ac
    ans+=min(abs(mi-a)+abs(mi-c),abs(ma-a)+abs(ma-c))
    return ans
  if mx==my==0:return abs(a-c)+abs(b-d)
  ans=1/0
q=int(input())
for _ in range(q):
  a,b,c,d=map(int,input().split())
  print(solve(a,b,c,d))
Problem: p02746
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
import sys
input=sys.stdin.readline

def dist(a,b,c,d,P3):
    for p in P3:
        da,ma=divmod(a,p)
        dc,mc=divmod(c,p)
        if da!=dc:
            return abs(a-c)+abs(b-d)
        if da!=1:
            a,c=ma,mc
            continue
        db,mb=divmod(b,p)
        dd,md=divmod(d,p)
        if db==dd:
            a,c=ma,mc
            continue
        return min(ma+mc+2,2*p-(ma+mc))+abs(b-d)
    return abs(a-c)+abs(b-d)

def main():
    q=int(input())
    P3=[pow(3,i) for i in reversed(range(30))]
    for _ in range(q):
        a,b,c,d=map(lambda x: int(x)-1,input().split())
        print(max(dist(a,b,c,d,P3),dist(b,a,d,c,P3)))

if __name__=='__main__':
    main()

Failing test case input:
2
3 4 6 4
8 3 2 5

Expected output:
3
8


Actual output:
5
8


Fixed submission (verdict: Accepted):
import sys
input=sys.stdin.readline

def dist(a,b,c,d,P3):
    for p in P3:
        da,ma=divmod(a,p)
        dc,mc=divmod(c,p)
        if da!=dc:
            return abs(a-c)+abs(b-d)
        if da!=1:
            a,c=ma,mc
            continue
        db,mb=divmod(b,p)
        dd,md=divmod(d,p)
        if abs(db-dd)<2:
            a,c=ma,mc
            continue
        return min(ma+mc+2,2*p-(ma+mc))+abs(b-d)
    return abs(a-c)+abs(b-d)

def main():
    q=int(input())
    P3=[pow(3,i) for i in reversed(range(30))]
    for _ in range(q):
        a,b,c,d=map(lambda x: int(x)-1,input().split())
        print(max(dist(a,b,c,d,P3),dist(b,a,d,c,P3)))

if __name__=='__main__':
    main()
Problem: p02746
Buggy submission (Python (3.4.3), verdict: Wrong Answer):

def check(lv,aa,bb,cc,dd,xy):
  a,b,c,d=aa,bb,cc,dd
  if a>c:a,c=c,a
  if b>d:b,d=d,b
  mi=3**(lv-1)
  ma=2*3**(lv-1)+1
  if xy=="y":
    return (a<=mi and ma<=c and mi<b<=d<ma)
  else:
    return (b<=mi and ma<=d and mi<a<=c<ma)
def solve(a,b,c,d):
  mx=my=0
  for i in range(1,31):
    if check(i,a,b,c,d,"x"):mx=i
    if check(i,a,b,c,d,"y"):my=i
  if mx<my:
    ans=abs(a-c)+1
    mi=3**(my-1)
    ma=2*3**(my-1)+1
    ans+=min(abs(mi-b)+abs(mi-d),abs(ma-b)+abs(ma-d))
    return ans
  if mx>my:
    ans=abs(b-d)+1
    mi=3**(mx-1)
    ma=2*3**(mx-1)+1
    ans+=min(abs(mi-a)+abs(mi-c),abs(ma-a)+abs(ma-c))
    return ans
  if mx==my==0:return abs(a-c)+abs(b-d)
  ans=1/0
q=int(input())
for _ in range(q):
  a,b,c,d=map(int,input().split())
  print(solve(a,b,c,d))


Failing test case input:
2
3 8 5 0
8 5 2 5

Expected output:
10
10


Actual output:
10
11


Fixed submission (verdict: Accepted):
def solve(aa,bb,cc,dd):
  a,b,c,d=aa,bb,cc,dd
  mx=my=0
  for i in range(1,31):
    if a==c and a%3==2 and abs(b-d)>=2:mx=i
    if b==d and b%3==2 and abs(a-c)>=2:my=i
    a=0--a
    b=0--b
    c=0--c
    d=0--d
  a,b,c,d=aa,bb,cc,dd
  if mx==my==0:return abs(a-c)+abs(b-d)
  if mx<my:
    ans=abs(a-c)
    mi=3**(my-1)
    ma=2*3**(my-1)+1
    bd=(3**my)*((0--b
    b-=bd
    d-=bd
    ans+=min(abs(mi-b)+abs(mi-d),abs(ma-b)+abs(ma-d))
    return ans
  if mx>my:
    ans=abs(b-d)
    mi=3**(mx-1)
    ma=2*3**(mx-1)+1
    ac=(3**mx)*((0--a
    a-=ac
    c-=ac
    ans+=min(abs(mi-a)+abs(mi-c),abs(ma-a)+abs(ma-c))
    return ans
  if mx==my==0:return abs(a-c)+abs(b-d)
  ans=1/0
q=int(input())
for _ in range(q):
  a,b,c,d=map(int,input().split())
  print(solve(a,b,c,d))
Problem: p02746
Buggy submission (Python (3.4.3), verdict: Wrong Answer):

def check(lv,aa,bb,cc,dd,xy):
  a,b,c,d=aa,bb,cc,dd
  if a>c:a,c=c,a
  if b>d:b,d=d,b
  mi=3**(lv-1)
  ma=2*3**(lv-1)+1
  aa=0--a
  bb=0--b
  cc=0--c
  dd=0--d
  if xy=="y":
    if bb==dd and bb%3==2:return False
    return (a<=mi and ma<=c and mi<b<=d<ma)
  else:
    if aa==cc and cc%3==2:return False
    return (b<=mi and ma<=d and mi<a<=c<ma)
def solve(a,b,c,d):
  mx=my=0
  for i in range(1,31):
    if check(i,a,b,c,d,"x"):mx=i
    if check(i,a,b,c,d,"y"):my=i
  if mx<my:
    ans=abs(a-c)
    mi=3**(my-1)
    ma=2*3**(my-1)+1
    ans+=min(abs(mi-b)+abs(mi-d),abs(ma-b)+abs(ma-d))
    return ans
  if mx>my:
    ans=abs(b-d)
    mi=3**(mx-1)
    ma=2*3**(mx-1)+1
    ans+=min(abs(mi-a)+abs(mi-c),abs(ma-a)+abs(ma-c))
    return ans
  if mx==my==0:return abs(a-c)+abs(b-d)
  ans=1/0
q=int(input())
for _ in range(q):
  a,b,c,d=map(int,input().split())
  print(solve(a,b,c,d))


Failing test case input:
2
3 3 7 0
8 5 2 5

Expected output:
7
10


Actual output:
7
6


Fixed submission (verdict: Accepted):
def solve(aa,bb,cc,dd):
  a,b,c,d=aa,bb,cc,dd
  mx=my=0
  for i in range(1,31):
    if a==c and a%3==2 and abs(b-d)>=2:mx=i
    if b==d and b%3==2 and abs(a-c)>=2:my=i
    a=0--a
    b=0--b
    c=0--c
    d=0--d
  a,b,c,d=aa,bb,cc,dd
  if mx==my==0:return abs(a-c)+abs(b-d)
  if mx<my:
    ans=abs(a-c)
    mi=3**(my-1)
    ma=2*3**(my-1)+1
    bd=(3**my)*((0--b
    b-=bd
    d-=bd
    ans+=min(abs(mi-b)+abs(mi-d),abs(ma-b)+abs(ma-d))
    return ans
  if mx>my:
    ans=abs(b-d)
    mi=3**(mx-1)
    ma=2*3**(mx-1)+1
    ac=(3**mx)*((0--a
    a-=ac
    c-=ac
    ans+=min(abs(mi-a)+abs(mi-c),abs(ma-a)+abs(ma-c))
    return ans
  if mx==my==0:return abs(a-c)+abs(b-d)
  ans=1/0
q=int(input())
for _ in range(q):
  a,b,c,d=map(int,input().split())
  print(solve(a,b,c,d))
Problem: p02746
Buggy submission (Python (3.4.3), verdict: Wrong Answer):

import math
Q = int(input())
A = [list(map(int, input().split())) for _ in range(Q)]



def measure(A):
    a,b,c,d = A
    if abs(a-c) < abs(b-d):
        s1, s2 = min([a,c]), max([a,c])
        l1, l2 = min([b,d]), max([b,d])
    else:
        l1, l2 = min([a,c]), max([a,c])
        s1, s2 = min([b,d]), max([b,d])
    min_K = int(math.log(s2-s1+1, 3) + 1)
    max_K = int(math.log(l2-l1+1, 3) + 1)
    for k in list(range(min_K, max_K+1))[::-1]:
        v = 3 ** k
        _l = 3 ** (k-1) + 1
        _h = (3 ** (k-1)) * 2
        c1 = s1
        c2 = (s1 % v <= _h) & (_l <= s1 % v)
        c3 = (s2 % v <= _h) & (_l <= s2 % v)
        if c1 & c2 & c3:
            val1 = (s1 % v - _l + 1) + (s2 % v - _l + 1) + (l2 - l1)
            val2 = (_h - s1 % v + 1) + (_h - s2 % v + 1) + (l2 - l1)
            print(min([val1, val2]))
            return None
    print(l2-l1+s2-s1)

if __name__ == '__main__':
    for val in A:
        measure(val)

Failing test case input:
2
3 2 6 4
13 4 1 5

Expected output:
5
15


Actual output:
5
13


Fixed submission (verdict: Accepted):

import math
Q = int(input())
A = [list(map(int, input().split())) for _ in range(Q)]



def measure(A):
    a,b,c,d = A
    if abs(a-c) < abs(b-d):
        s1, s2 = min([a,c]), max([a,c])
        l1, l2 = min([b,d]), max([b,d])
    else:
        l1, l2 = min([a,c]), max([a,c])
        s1, s2 = min([b,d]), max([b,d])
    min_K = int(math.log(s2-s1+1, 3) + 1)
    max_K = int(math.log(l2-l1+1, 3) + 1)
    for k in list(range(min_K, max_K+1))[::-1]:
        v = 3 ** k
        _l = 3 ** (k-1) + 1
        _h = (3 ** (k-1)) * 2
        c1 = s1
        c2 = (s1 % v <= _h) & (_l <= s1 % v)
        c3 = (s2 % v <= _h) & (_l <= s2 % v)
        c4 = True
        if (l1 
            if (l1 % v < _l) & (l2 % v > _h):
                c4 = True
            else:c4=False
        if ((l1 
            if ((l2 % v < _l) & (l1 % v > _h)):
                c4 = False
        if c1 & c2 & c3 & c4:
            val1 = (s1 % v - _l + 1) + (s2 % v - _l + 1) + (l2 - l1)
            val2 = (_h - s1 % v + 1) + (_h - s2 % v + 1) + (l2 - l1)
            print(min([val1, val2]))
            return None
    print(l2-l1+s2-s1)

if __name__ == '__main__':
    for val in A:
        measure(val)
Problem: p02746
Buggy submission (Python (3.4.3), verdict: Wrong Answer):

def check(lv,aa,bb,cc,dd,xy):
  a,b,c,d=aa,bb,cc,dd
  if a>c:a,c=c,a
  if b>d:b,d=d,b
  mi=3**(lv-1)
  ma=2*3**(lv-1)+1
  if xy=="y":
    return (a<=mi and ma<=c and mi<b<=d<ma)
  else:
    return (b<=mi and ma<=d and mi<a<=c<ma)
def solve(a,b,c,d):
  mx=my=0
  for i in range(1,31):
    if check(i,a,b,c,d,"x"):mx=i
    if check(i,a,b,c,d,"y"):my=i
  if mx<my:
    ans=abs(a-c)
    mi=3**(my-1)
    ma=2*3**(my-1)+1
    ans+=min(abs(mi-b)+abs(mi-d),abs(ma-b)+abs(ma-d))
    return ans
  if mx>my:
    ans=abs(b-d)
    mi=3**(mx-1)
    ma=2*3**(mx-1)+1
    ans+=min(abs(mi-a)+abs(mi-c),abs(ma-a)+abs(ma-c))
    return ans
  if mx==my==0:return abs(a-c)+abs(b-d)
  ans=1/0
q=int(input())
for _ in range(q):
  a,b,c,d=map(int,input().split())
  print(solve(a,b,c,d))


Failing test case input:
2
2 8 2 4
13 3 2 5

Expected output:
6
13


Actual output:
4
13


Fixed submission (verdict: Accepted):
def solve(aa,bb,cc,dd):
  a,b,c,d=aa,bb,cc,dd
  mx=my=0
  for i in range(1,31):
    if a==c and a%3==2 and abs(b-d)>=2:mx=i
    if b==d and b%3==2 and abs(a-c)>=2:my=i
    a=0--a
    b=0--b
    c=0--c
    d=0--d
  a,b,c,d=aa,bb,cc,dd
  if mx==my==0:return abs(a-c)+abs(b-d)
  if mx<my:
    ans=abs(a-c)
    mi=3**(my-1)
    ma=2*3**(my-1)+1
    bd=(3**my)*((0--b
    b-=bd
    d-=bd
    ans+=min(abs(mi-b)+abs(mi-d),abs(ma-b)+abs(ma-d))
    return ans
  if mx>my:
    ans=abs(b-d)
    mi=3**(mx-1)
    ma=2*3**(mx-1)+1
    ac=(3**mx)*((0--a
    a-=ac
    c-=ac
    ans+=min(abs(mi-a)+abs(mi-c),abs(ma-a)+abs(ma-c))
    return ans
  if mx==my==0:return abs(a-c)+abs(b-d)
  ans=1/0
q=int(input())
for _ in range(q):
  a,b,c,d=map(int,input().split())
  print(solve(a,b,c,d))
Problem: p02746
Buggy submission (Python (3.4.3), verdict: Wrong Answer):

def solve(a,b,c,d):
  x1,y1,x2,y2=a,b,c,d
  mx=my=0
  if x1==x2 and x1%3==2:mx=1
  if y1==y2 and y1%3==2:my=1
  for i in range(29):
    x1=0--x1
    y1=0--y1
    x2=0--x2
    y2=0--y2
    if x1==x2 and x1%3==2:mx=i+2
    if y1==y2 and y1%3==2:my=i+2
  ans=abs(a-c)+abs(b-d)
  if mx<my:
    mi=3**(my-1)
    ma=2*3**(my-1)+1
    ans+=min(abs(mi-b)+abs(mi-d),abs(ma-b)+abs(ma-d))
    return ans
  if mx>my:
    mi=3**(mx-1)
    ma=2*3**(mx-1)+1
    ans+=min(abs(mi-a)+abs(mi-c),abs(ma-a)+abs(ma-c))
    return ans
  if mx==my==0:return ans
  ans=1/0
q=int(input())
for _ in range(q):
  a,b,c,d=map(int,input().split())
  print(solve(a,b,c,d))


Failing test case input:
2
1 0 7 0
4 2 0 2

Expected output:
6
6


Actual output:
6
137260754729766


Fixed submission (verdict: Accepted):
def solve(aa,bb,cc,dd):
  a,b,c,d=aa,bb,cc,dd
  mx=my=0
  for i in range(1,31):
    if a==c and a%3==2 and abs(b-d)>=2:mx=i
    if b==d and b%3==2 and abs(a-c)>=2:my=i
    a=0--a
    b=0--b
    c=0--c
    d=0--d
  a,b,c,d=aa,bb,cc,dd
  if mx==my==0:return abs(a-c)+abs(b-d)
  if mx<my:
    ans=abs(a-c)
    mi=3**(my-1)
    ma=2*3**(my-1)+1
    bd=(3**my)*((0--b
    b-=bd
    d-=bd
    ans+=min(abs(mi-b)+abs(mi-d),abs(ma-b)+abs(ma-d))
    return ans
  if mx>my:
    ans=abs(b-d)
    mi=3**(mx-1)
    ma=2*3**(mx-1)+1
    ac=(3**mx)*((0--a
    a-=ac
    c-=ac
    ans+=min(abs(mi-a)+abs(mi-c),abs(ma-a)+abs(ma-c))
    return ans
  if mx==my==0:return abs(a-c)+abs(b-d)
  ans=1/0
q=int(input())
for _ in range(q):
  a,b,c,d=map(int,input().split())
  print(solve(a,b,c,d))
Problem: p02746
Buggy submission (Python (3.4.3), verdict: Wrong Answer):

def solve(a,b,c,d):
  x1,y1,x2,y2=a,b,c,d
  mx=my=0
  if x1==x2 and x1%3==2:mx=1
  if y1==y2 and y1%3==2:my=1
  for i in range(29):
    x1=0--x1
    y1=0--y1
    x2=0--x2
    y2=0--y2
    if x1==x2 and x1%3==2:mx=i+2
    if y1==y2 and y1%3==2:my=i+2
  if mx<my:
    ans=abs(a-c)
    mi=3**(my-1)
    ma=2*3**(my-1)+1
    ans+=min(abs(mi-b)+abs(mi-d),abs(ma-b)+abs(ma-d))
    return ans
  if mx>my:
    ans=abs(b-d)
    mi=3**(mx-1)
    ma=2*3**(mx-1)+1
    ans+=min(abs(mi-a)+abs(mi-c),abs(ma-a)+abs(ma-c))
    return ans
  if mx==my==0:return abs(a-c)+abs(b-d)
  ans=1/0
q=int(input())
for _ in range(q):
  a,b,c,d=map(int,input().split())
  print(solve(a,b,c,d))


Failing test case input:
2
0 4 2 0
12 2 2 2

Expected output:
6
12


Actual output:
6
137260754729772


Fixed submission (verdict: Accepted):
def solve(aa,bb,cc,dd):
  a,b,c,d=aa,bb,cc,dd
  mx=my=0
  for i in range(1,31):
    if a==c and a%3==2 and abs(b-d)>=2:mx=i
    if b==d and b%3==2 and abs(a-c)>=2:my=i
    a=0--a
    b=0--b
    c=0--c
    d=0--d
  a,b,c,d=aa,bb,cc,dd
  if mx==my==0:return abs(a-c)+abs(b-d)
  if mx<my:
    ans=abs(a-c)
    mi=3**(my-1)
    ma=2*3**(my-1)+1
    bd=(3**my)*((0--b
    b-=bd
    d-=bd
    ans+=min(abs(mi-b)+abs(mi-d),abs(ma-b)+abs(ma-d))
    return ans
  if mx>my:
    ans=abs(b-d)
    mi=3**(mx-1)
    ma=2*3**(mx-1)+1
    ac=(3**mx)*((0--a
    a-=ac
    c-=ac
    ans+=min(abs(mi-a)+abs(mi-c),abs(ma-a)+abs(ma-c))
    return ans
  if mx==my==0:return abs(a-c)+abs(b-d)
  ans=1/0
q=int(input())
for _ in range(q):
  a,b,c,d=map(int,input().split())
  print(solve(a,b,c,d))
Problem: p03054
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
h,w,n=[int(j) for j in input().split()]
R,C=[int(j) for j in input().split()]
s=input()
t=input()


x=C
l,r=1,w
for i in range(n)[::-1]:
    if t[i]=="R":
        l-=1
    elif t[i]=="L":
        r+=1

    if s[i]=="R":
        r-=1
    elif s[i]=="L":
        l+=1
    if not l<=x<=r:
        print("NO")
        exit()
    



x=R
l,r=1,h
for i in range(n)[::-1]:
    if t[i]=="U":
        l-=1
    elif t[i]=="D":
        r+=1
    if s[i]=="U":
        r-=1
    elif s[i]=="D":
        l+=1
    if not l<=x<=r:
        print("NO")
        exit()


print("YES")

Failing test case input:
4 4 5
2 2
UDRRR
LLDTD

Expected output:
YES


Actual output:
NO


Fixed submission (verdict: Accepted):
h,w,n=[int(j) for j in input().split()]
R,C=[int(j) for j in input().split()]
s=input()
t=input()


x=C
l,r=1,w
for i in range(n)[::-1]:
    if t[i]=="R":
        l-=1
    elif t[i]=="L":
        r+=1
    if l==0:
        l=1
    if r>w:
        r=w
    if s[i]=="R":
        r-=1
    elif s[i]=="L":
        l+=1
    if  l>r:
        print("NO")
        exit()
if not l<=x<=r:
    print("NO")
    exit()

x=h+1-R
l,r=1,h
for i in range(n)[::-1]:
    if t[i]=="U":
        l-=1
    elif t[i]=="D":
        r+=1
    if l==0:
        l=1
    if r>h:
        r=h
    if s[i]=="U":
        r-=1
    elif s[i]=="D":
        l+=1
    if l>r:
        print("NO")
        exit()
if not l<=x<=r:
    print("NO")
    exit()
print("YES")

Problem: p03054
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()
l = 0
r = W+1
u = 0
d = H+1
for s,t in zip(S[::-1],T[::-1]):
    if s == 'L' and t != 'R':
        l += 1
    if s == 'R' and t != 'L':
        r -= 1
    if s == 'U' and t != 'D':
        u += 1
    if s == 'D' and t != 'U':
        d -= 1
    if l == r-1 or u == d-1:
        break
if l < sc < r and u < sr < d:
    print('YES')
else:
    print('NO')

Failing test case input:
4 4 5
2 2
UDRRR
LLDTD

Expected output:
YES


Actual output:
NO


Fixed submission (verdict: Accepted):
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()
l = 0
r = W+1
u = 0
d = H+1
flag = True
for s,t in zip(S[::-1],T[::-1]):
    if l+1==r or u+1==d:
        flag=False
        break
    if t == 'L':
        r = min(W+1, r+1)
    if t == 'R':
        l = max(0, l-1)
    if t == 'U':
        d = min(H+1, d+1)
    if t == 'D':
        u = max(0, u-1)
    if s == 'L':
        l += 1
    if s == 'R':
        r -= 1
    if s == 'U':
        u += 1
    if s == 'D':
        d -= 1
if not(l < sc < r and u < sr < d):
    flag = False
if flag:
    print('YES')
else:
    print('NO')
Problem: p03054
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()

left = 1
right = W
up = 1
down = H

for i in reversed(range(N)):
    if i != N - 1:
        if T[i] == "U":
            if down != H:
                down -= 1
        elif T[i] == "D":
            if up != 1:
                up += 1
        elif T[i] == "L":
            if right != W:
                right += 1
        else:
            if left != 1:
                left -= 1
    if S[i] == "U":
        up -= 1
    elif S[i] == "D":
        down += 1
    elif S[i] == "L":
        left += 1
    else:
        right -= 1
    if left == right or up == down:
        print("NO")
        exit()
if left <= sr <= right and up <= sc <= down:
    print("YES")
else:
    print("NO")


Failing test case input:
2 5 3
2 4
RLR
LUD

Expected output:
YES


Actual output:
NO


Fixed submission (verdict: Accepted):
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()

left = 1
right = W
up = 1
down = H

for i in reversed(range(N)):
    if T[i] == "U":
        down = min(H, down+1)
    elif T[i] == "D":
        up = max(1, up-1)
    elif T[i] == "L":
        right = min(W, right+1)
    else:
        left = max(1, left-1)

    if S[i] == "U":
        up += 1
    elif S[i] == "D":
        down -= 1
    elif S[i] == "L":
        left += 1
    else:
        right -= 1

    

    if left > right or up > down:
        print("NO")
        exit()



if left <= sc <= right and up <= sr <= down:
    print("YES")
else:
    print("NO")
Problem: p03054
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
def main():
    H, W, N = map(int, input().split())
    sr, sc = map(int, input().split())
    S = input()
    T = input()
    S = S[::-1]
    T = T[::-1]
    L, R, U, D = 1, W, 1, H
    f = False
    if S[0] == "L":
        L += 1
    elif S[0] == "R":
        R -= 1
    elif S[0] == "U":
        U += 1
    else:
        D -= 1
    if R < L or D < U:
        f = True
    for i in range(1, N):
        if (S[i] == "L" and T[i] == "R") or (S[i] == "R" and T[i] == "L") or (S[i] == "U" and T[i] == "D") or (S[i] == "D" and T[i] == "U"):
            continue
        if S[i] == "L":
            L += 1
        elif S[i] == "R":
            R -= 1
        elif S[i] == "U":
            U += 1
        else:
            D -= 1
        if R < L or D < U:
            f = True
    if sr < U or D < sr or sc < L or R < sc:
        f = True
    if f:
        print("NO")
    else:
        print("YES")

if __name__ == "__main__":
    main()


Failing test case input:
4 4 5
2 2
UDRRR
LLDTD

Expected output:
YES


Actual output:
NO


Fixed submission (verdict: Accepted):
def main():
    H, W, N = map(int, input().split())
    sr, sc = map(int, input().split())
    S = input()
    T = input()
    f = True
    pL, pR, pU, pD = sc, sc, sr, sr
    for i in range(N):
        if S[i] == "L":
            pL -= 1
        elif S[i] == "R":
            pR += 1
        elif S[i] == "U":
            pU -= 1
        else:
            pD += 1
        if pL <= 0 or W < pR or pU <= 0 or H < pD:
            f = False
        if T[i] == "L":
            if 1 < pR:
                pR -= 1
        elif T[i] == "R":
            if pL < W:
                pL += 1
        elif T[i] == "U":
            if 1 < pD:
                pD -= 1
        else:
            if pU < H:
                pU += 1
    if f:
        print("YES")
    else:
        print("NO")

if __name__ == "__main__":
    main()
Problem: p03054
Buggy submission (Python (3.4.3), verdict: Wrong Answer):



def main():
  L, R, U, D = 0, 1, 2, 3
  h, w, n = map(int, input().split())
  sr, sc = map(int, input().split())
  ls = input().strip()[::-1]
  lt = input().strip()[::-1]
  r = [0, w-1, 0, h-1]
  
  def f(d, x):
    if d=='L':
      r[L] = (min(max(0, r[L]-x), w-1) )
    if d=='R':
      r[R] = (min(max(0, r[R]+x), w-1) )
    if d=='U':
      r[U] = (min(max(0, r[U]-x), h-1) )
    if d=='D':
      r[D] = (min(max(0, r[D]+x), h-1) )
      
  def check():
    if r[L] >= r[R]:
      return False
    if r[U] >= r[D]:
      return False
    return True
  
  f(ls[0], 1)
  for s, t in zip(ls[1:], lt[1:]):
    f(s, 1)
    f(t, -1)
    if check():
      print('NO')
      return
  print('YES')
    
    
    

if __name__=='__main__':
  main()



Failing test case input:
2 3 3
3 1
RQL
DUL

Expected output:
NO


Actual output:
YES


Fixed submission (verdict: Accepted):



def main():
  L, R, U, D = 0, 1, 2, 3
  h, w, n = map(int, input().split())
  sr, sc = map(int, input().split())
  sr, sc = sr-1, sc-1
  ls = input().strip()[::-1]
  lt = input().strip()[::-1]
  r = [0, w-1, 0, h-1]
  
  def f(d):
    if d=='L':
      r[L] = (min(max(-1, r[L]+1), w))
    if d=='R':
      r[R] = (min(max(-1, r[R]-1), w))
    if d=='U':
      r[U] = (min(max(-1, r[U]+1), h))
    if d=='D':
      r[D] = (min(max(-1, r[D]-1), h))
      
  def g(d):
    if d=='L':
      r[R] = (min(max(0, r[R]+1), w-1))
    if d=='R':
      r[L] = (min(max(0, r[L]-1), w-1))
    if d=='U':
      r[D] = (min(max(0, r[D]+1), h-1))
    if d=='D':
      r[U] = (min(max(0, r[U]-1), h-1))
      
  def check():
    if r[L] > r[R] or r[L] < 0 or r[R] > w-1:
      return True
    if r[U] > r[D] or r[U] < 0 or r[D] > h-1:
      return True
    return False
  
  for s, t in zip(ls, lt):
    g(t)
    f(s)
    if check():
      print('NO')
      return
  if r[L] <= sc <= r[R] and r[U] <= sr <= r[D]:
    print('YES')
  else:
    print('NO')
    
if __name__=='__main__':
  main()
Problem: p03054
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
H, W, N = map(int, input().split())
Sr, Sc = map(int, input().split())
S = input()
T = input()


l = 1
r = W
u = 1
d = H
for s, t in zip(S[::-1], T[::-1]):

    if t=="L":
        r = min(r+1, W)
    elif t=="R":
        l = max(l-1, 1)
    elif t=="U":
        d = min(d+1, W)
    else:
        u = max(u-1, 1)

    if s=="L":
        l+=1
    elif s=="R":
        r-=1
    elif s=="U":
        u+=1
    else:
        d-=1

    if l > r or u > d:
        print("NO")
        exit()
    
if d<=Sr<=u and l<=Sc<=r:
    print("YES")
else:
    print("NO")


Failing test case input:
2 3 3
2 2
RRL
LUD

Expected output:
YES

Actual output:
NO


Fixed submission (verdict: Accepted):
H, W, N = map(int, input().split())
Sr, Sc = map(int, input().split())
S = input()
T = input()


l = 1
r = W
u = 1
d = H

for s, t in zip(S[::-1], T[::-1]):

    if t=="L":
        r = min(r+1, W)
    elif t=="R":
        l = max(l-1, 1)
    elif t=="U":
        d = min(d+1, H)
    else:
        u = max(u-1, 1)

    
    if s=="L":
        l+=1
    elif s=="R":
        r-=1
    elif s=="U":
        u+=1
    else:
        d-=1

    if l > r or u > d:
        print("NO")
        exit()
    
if u<=Sr<=d and l<=Sc<=r:
    print("YES")
else:
    print("NO")

Problem: p03054
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
H,W,N = map(int, input().split())
st = [int(i) for i in input().split()]
S = input()
T = input()


def simu(dim1):
    now = [st[0],st[1]]

    if dim1 == 'U':
        dim2,L,step,dim = 'D',H,-1,0
    if dim1 == 'D':
        dim2,L,step,dim = 'U',H,1,0
    if dim1 == 'R':
        dim2,L,step,dim = 'L',W,1,1
    if dim1 == 'L':
        dim2,L,step,dim = 'R',W,-1,1

    for i in range(N):
        if S[i] == dim1:
            now[dim] -= step
        if now[dim] < 1 or now[dim] >= L+1:
            return True
        if T[i] == dim2:
            if step == -1:
                if now[dim] < L:
                    now[dim] += step
            else:
                if now[dim] > 0:
                    now[dim] += step
        
D = ['U','D','R','L']
for d in D:
    if simu(d):
        print('YES')
        exit()
print('NO')

Failing test case input:
1 1 4
0 1
UDRRR
LLDTC

Expected output:
NO


Actual output:
YES


Fixed submission (verdict: Accepted):
H,W,N = map(int, input().split())
st = [int(i) for i in input().split()]
S = input()
T = input()


def simu(dim1):
    now = [st[0],st[1]]

    if dim1 == 'U':
        dim2,L,step,dim = 'D',H,1,0
    if dim1 == 'D':
        dim2,L,step,dim = 'U',H,-1,0
    if dim1 == 'L':
        dim2,L,step,dim = 'R',W,1,1
    if dim1 == 'R':
        dim2,L,step,dim = 'L',W,-1,1

    for i in range(N):
        if S[i] == dim1:
            now[dim] -= step
        if now[dim] < 1 or now[dim] >= L+1:
            return True
        if T[i] == dim2:
            if step == 1:
                if now[dim] < L:
                    now[dim] += step
            else:
                if now[dim] > 1:
                    now[dim] += step
    return False
        
Dim = ['U','D','R','L']
for d in Dim:
    if simu(d):
        print('NO')
        exit()
print('YES')
Problem: p03054
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()

left = 1
right = W
up = 1
down = H

for i in reversed(range(N)):
    if i != N - 1:
        if T[i] == "U":
            if down != H:
                down -= 1
        elif T[i] == "D":
            if up != 1:
                up += 1
        elif T[i] == "L":
            if right != W:
                right += 1
        else:
            if left != 1:
                left -= 1
    if S[i] == "U":
        up -= 1
    elif S[i] == "D":
        down += 1
    elif S[i] == "L":
        left += 1
    else:
        right -= 1
    if left == right or up == down:
        print("NO")
        exit()

if left <= sr <= right and down <= sc <= up:
    print("YES")
else:
    print("NO")


Failing test case input:
2 5 3
2 4
RLR
LUD

Expected output:
YES


Actual output:
NO


Fixed submission (verdict: Accepted):
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()

left = 1
right = W
up = 1
down = H

for i in reversed(range(N)):
    if T[i] == "U":
        down = min(H, down+1)
    elif T[i] == "D":
        up = max(1, up-1)
    elif T[i] == "L":
        right = min(W, right+1)
    else:
        left = max(1, left-1)

    if S[i] == "U":
        up += 1
    elif S[i] == "D":
        down -= 1
    elif S[i] == "L":
        left += 1
    else:
        right -= 1

    

    if left > right or up > down:
        print("NO")
        exit()



if left <= sc <= right and up <= sr <= down:
    print("YES")
else:
    print("NO")

Problem: p03054
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
H,W,N=map(int,input().split())
sr,sc=map(int,input().split())
S=input()
T=input()

lu=H-sr
ld=sr-1
ll=W-sc
lr=sc-1

for i in range(N):
  x=S[i]
  y=T[i]

  if x=='U':
    lu-=1
  elif x=='D':
    ld-=1
  elif x=='L':
    ll-=1
  elif x=='R':
    lr-=1

  

  if lu<0 or ld<0 or ll<0 or lr<0:
    print('NO')
    exit()

  if y=='U':
    ld+=1
  elif y=='D':
    lu+=1
  elif y=='L':
    lr+=1
  elif y=='R':
    ll+=1

  

print('YES')

Failing test case input:
4 3 5
2 2
UDRRR
LLDTD

Expected output:
NO


Actual output:
YES


Fixed submission (verdict: Accepted):
H,W,N=map(int,input().split())
sr,sc=map(int,input().split())
S=input()
T=input()

ld=H-sr
lu=sr-1
lr=W-sc
ll=sc-1

for i in range(N):
  x=S[i]
  y=T[i]

  if x=='U':
    lu-=1
  elif x=='D':
    ld-=1
  elif x=='L':
    ll-=1
  elif x=='R':
    lr-=1

  

  if lu<0 or ld<0 or ll<0 or lr<0:
    print('NO')
    exit()

  if y=='U' and ld<H-1:
    ld+=1
  elif y=='D' and lu<H-1:
    lu+=1
  elif y=='L' and lr<W-1:
    lr+=1
  elif y=='R' and ll<W-1:
    ll+=1

  

print('YES')
Problem: p03054
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()

left = 0
right = W
up = 0
down = H

for i in reversed(range(N)):
    if i != N - 1:
        if T[i] == "U":
            down = min(H, down+1)
        elif T[i] == "D":
            up = max(0, up-1)
        elif T[i] == "L":
            right = min(W, right+1)
        else:
            left = max(0, left-1)
    if S[i] == "U":
        up += 1
    elif S[i] == "D":
        down -= 1
    elif S[i] == "L":
        left += 1
    else:
        right -= 1
    if left == right or up == down:
        print("NO")
        exit()


if left <= sr <= right and up <= sc <= down:
    print("YES")
else:
    print("NO")


Failing test case input:
2 6 3
4 0
RLR
LUD

Expected output:
NO


Actual output:
YES


Fixed submission (verdict: Accepted):
H, W, N = map(int, input().split())
sr, sc = map(int, input().split())
S = input()
T = input()

left = 1
right = W
up = 1
down = H

for i in reversed(range(N)):
    if T[i] == "U":
        down = min(H, down+1)
    elif T[i] == "D":
        up = max(1, up-1)
    elif T[i] == "L":
        right = min(W, right+1)
    else:
        left = max(1, left-1)

    if S[i] == "U":
        up += 1
    elif S[i] == "D":
        down -= 1
    elif S[i] == "L":
        left += 1
    else:
        right -= 1

    

    if left > right or up > down:
        print("NO")
        exit()



if left <= sc <= right and up <= sr <= down:
    print("YES")
else:
    print("NO")
Problem: p03054
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
h, w, n = map(int, input().split())
sr, sc = map(int, input().split())
S = input()[::-1]
T = input()[::-1]

r_bound = w
l_bound = 1
d_bound = h
u_bound = 1

remain = True

if S[0] =='R':
    r_bound -= 1
if S[0] =='L':
    l_bound += 1
if S[0] =='D':
    d_bound -= 1
if S[0] =='U':
    u_bound += 1

for s, t in zip(S[1:], T[1:]):
    if s =='R' and t !='L':
        r_bound -= 1
    if s =='L' and t !='R':
        l_bound += 1
    if s =='D' and t !='U':
        d_bound -= 1
    if s =='U' and t !='D':
        u_bound += 1
    if (r_bound < l_bound) or (d_bound < u_bound):
        remain = False
        break
if (sc < l_bound or sc > r_bound) or (sr < u_bound or sr > d_bound):
    remain = False

if remain:
    print('YES')
else:
    print('NO')


Failing test case input:
4 4 5
2 2
UDRRR
LLDTD

Expected output:
YES


Actual output:
NO


Fixed submission (verdict: Accepted):
h, w, n = map(int, input().split())
sr, sc = map(int, input().split())
S = input()[::-1]
T = input()[::-1]

r_bound = w
l_bound = 1
d_bound = h
u_bound = 1

remain = True

for s, t in zip(S, T):
    if t =='R' and l_bound > 1:
        l_bound -= 1
    elif t =='L' and r_bound < w:
        r_bound += 1
    elif t =='D' and u_bound > 1:
        u_bound -= 1
    elif t =='U' and d_bound < h:
        d_bound += 1
    if s =='R':
        r_bound -= 1
    elif s =='L':
        l_bound += 1
    elif s =='D':
        d_bound -= 1
    elif s =='U':
        u_bound += 1
    if (r_bound < l_bound) or (d_bound < u_bound):
        remain = False
        break
if (sc < l_bound or sc > r_bound) or (sr < u_bound or sr > d_bound):
    remain = False

if remain:
    print('YES')
else:
    print('NO')
Problem: p03054
Buggy submission (Python (3.4.3), verdict: Wrong Answer):



def main():
  L, R, U, D = 0, 1, 2, 3
  h, w, n = map(int, input().split())
  sr, sc = map(int, input().split())
  ls = input().strip()[::-1]
  lt = input().strip()[::-1]
  r = [0, w-1, 0, h-1]
  
  def f(d, x):
    if d=='L':
      r[L] = (min(max(0, r[L]-x), w-1) )
    if d=='R':
      r[R] = (min(max(0, r[R]+x), w-1) )
    if d=='U':
      r[U] = (min(max(0, r[U]-x), h-1) )
    if d=='D':
      r[D] = (min(max(0, r[D]+x), h-1) )
      
  def check():
    if r[L] >= r[R]:
      return False
    if r[U] >= r[D]:
      return False
    return True
  
  f(ls[0], 1)
  for s, t in zip(ls[1:], lt[1:]):
    f(t, -1)
    f(s, 1)
    if check():
      print('NO')
      return
  print('YES')

if __name__=='__main__':
  main()



Failing test case input:
1 1 4
-1 4
UDRRR
DTDLL

Expected output:
NO


Actual output:
YES


Fixed submission (verdict: Accepted):



def main():
  L, R, U, D = 0, 1, 2, 3
  h, w, n = map(int, input().split())
  sr, sc = map(int, input().split())
  sr, sc = sr-1, sc-1
  ls = input().strip()[::-1]
  lt = input().strip()[::-1]
  r = [0, w-1, 0, h-1]
  
  def f(d):
    if d=='L':
      r[L] = (min(max(-1, r[L]+1), w))
    if d=='R':
      r[R] = (min(max(-1, r[R]-1), w))
    if d=='U':
      r[U] = (min(max(-1, r[U]+1), h))
    if d=='D':
      r[D] = (min(max(-1, r[D]-1), h))
      
  def g(d):
    if d=='L':
      r[R] = (min(max(0, r[R]+1), w-1))
    if d=='R':
      r[L] = (min(max(0, r[L]-1), w-1))
    if d=='U':
      r[D] = (min(max(0, r[D]+1), h-1))
    if d=='D':
      r[U] = (min(max(0, r[U]-1), h-1))
      
  def check():
    if r[L] > r[R] or r[L] < 0 or r[R] > w-1:
      return True
    if r[U] > r[D] or r[U] < 0 or r[D] > h-1:
      return True
    return False
  
  for s, t in zip(ls, lt):
    g(t)
    f(s)
    if check():
      print('NO')
      return
  if r[L] <= sc <= r[R] and r[U] <= sr <= r[D]:
    print('YES')
  else:
    print('NO')
    
if __name__=='__main__':
  main()