Problem p03081 (python)

Fixed submission:
def main():
    N, Q = map(int, input().split())
    s = input()
    l = []
    for _ in range(Q):
        t, d = input().split()
        l.append((t, d))
    lmax = N + 1
    lmin = 0
    rmax = N + 1
    rmin = 0
    # find most left R
    t = N
    for i in range(Q):
        if s[t-1] == l[i][0]:
            if l[i][1] == 'L':
                t -= 1
            else:
                t += 1
        if t == N+1:
            break
        if t == 0:
            print('0')
            return 0
    else:
        rmin = N + 1
    while rmax != rmin:
        m = t = (rmax + rmin) // 2
        for i in range(Q):
            if s[t-1] == l[i][0]:
                if l[i][1] == 'L':
                    t -= 1
                else:
                    t += 1
            if t == N+1:
                rmax = m
                break
            if t == 0:
                rmin = m + 1
                break
        else:
            rmin = m + 1
    t = 1
    for i in range(Q):
        if s[t-1] == l[i][0]:
            if l[i][1] == 'L':
                t -= 1
            else:
                t += 1
        if t == 0:
            break
        if t == N + 1:
            print('0')
            return 0
    else:
        lmax = 0
 
    # find most right L
    while lmax != lmin:
        m = t = (lmax + lmin + 1) // 2
        for i in range(Q):
            if s[t-1] == l[i][0]:
                if l[i][1] == 'L':
                    t -= 1
                else:
                    t += 1
            if t == 0:
                lmin = m
                break
            if t == N + 1:
                lmax = m - 1
                break
        else:
            lmax = m - 1
    print(rmax-lmax-1)
main()

Buggy submission (verdict: Runtime Error):
def main():
    N, Q = map(int, input().split())
    s = input()
    l = []
    for _ in range(Q):
        t, d = input().split()
        l.append((t, d))
    lmax = N + 1
    lmin = 0
    rmax = N + 1
    rmin = 0
    # find most left R
    t = N
    for i in range(Q):
        if s[t-1] == l[i][0]:
            if l[i][1] == 'L':
                t -= 1
            else:
                t += 1
        if t == N+1:
            break
    else:
        rmin = N + 1
    while rmax != rmin:
        m = t = (rmax + rmin) // 2
        for i in range(Q):
            if s[t-1] == l[i][0]:
                if l[i][1] == 'L':
                    t -= 1
                else:
                    t += 1
            if t == N+1:
                rmax = m
                break
        else:
            rmin = m + 1
    t = 1
    for i in range(Q):
        if s[t-1] == l[i][0]:
            if l[i][1] == 'L':
                t -= 1
            else:
                t += 1
        if t == 0:
            break
    else:
        lmax = 0
 
    # find most right L
    while lmax != lmin:
        m = t = (lmax + lmin + 1) // 2
        for i in range(Q):
            if s[t-1] == l[i][0]:
                if l[i][1] == 'L':
                    t -= 1
                else:
                    t += 1
            if t == 0:
                lmin = m
                break
        else:
            lmax = m - 1
    print(rmax-lmax-1)
main()

Buggy submission (verdict: Runtime Error):
def main():
    N, Q = map(int, input().split())
    s = input()
    l = []
    for _ in range(Q):
        t, d = input().split()
        l.append((t, d))
    lmax = N + 1
    lmin = 0
    rmax = N + 1
    rmin = 0
    # find most left R
    while rmax != rmin:
        m = t = (rmax + rmin) // 2
        if t == 0 or t == N + 1:
            break
        for i in range(Q):
            if s[t-1] == l[i][0]:
                if l[i][1] == 'L':
                    t -= 1
                else:
                    t += 1
            if t == N+1:
                rmax = m
                break
        else:
            rmin = m + 1
    # find most right L
    while lmax != lmin:
        m = t = (lmax + lmin + 1) // 2
        if t == 0 or t == N + 1:
            break
        for i in range(Q):
            if s[t-1] == l[i][0]:
                if l[i][1] == 'L':
                    t -= 1
                else:
                    t += 1
            if t == 0:
                lmin = m
                break
        else:
            lmax = m - 1
    print(rmax-lmax-1)
main()

Buggy submission (verdict: Runtime Error):
def main():
    N, Q = map(int, input().split())
    s = input()
    l = []
    for _ in range(Q):
        t, d = input().split()
        l.append((t, d))
    lmax = N + 1
    lmin = 0
    rmax = N + 1
    rmin = 0
    # find most left R
    while rmax != rmin:
        m = t = (rmax + rmin) // 2
        for i in range(Q):
            if s[t-1] == l[i][0]:
                if l[i][1] == 'L':
                    t -= 1
                else:
                    t += 1
            if t == N+1:
                rmax = m
                break
        else:
            rmin = m + 1
    # find most right L
    while lmax != lmin:
        m = t = (lmax + lmin + 1) // 2
        for i in range(Q):
            if s[t-1] == l[i][0]:
                if l[i][1] == 'L':
                    t -= 1
                else:
                    t += 1
            if t == 0:
                lmin = m
                break
        else:
            lmax = m - 1
    print(rmax-lmax-1)
main()

Buggy submission (verdict: Runtime Error):
def main():
    N, Q = map(int, input().split())
    s = input()
    l = []
    for _ in range(Q):
        t, d = input().split()
        l.append((t, d))
    lmax = N
    lmin = 1
    rmax = N
    rmin = 1
    # find most left R
    while rmax != rmin:
        m = t = (rmax + rmin) // 2
        for i in range(Q):
            if s[t-1] == l[i][0]:
                if l[i][1] == 'L':
                    t -= 1
                else:
                    t += 1
            if t == N+1:
                rmax = m
                break
        else:
            rmin = m + 1
    # find most right L
    while lmax != lmin:
        m = t = (lmax + lmin + 1) // 2
        for i in range(Q):
            if s[t-1] == l[i][0]:
                if l[i][1] == 'L':
                    t -= 1
                else:
                    t += 1
            if t == 0:
                lmin = m
                break
        else:
            lmax = m - 1
    if rmax == N:
        print(rmax-lmax)
    else:
        print(rmax-lmax-1)
main()