Problem p03231 (python)

Fixed submission:
#66 A - Two Abbreviations
import math
N,M = map(int,input().split())
S = input()
T = input()

GCD = math.gcd(N,M)
L = N*M//GCD

dist_N = L//N
dist_M = L//M

# 同じ位置に書く文字は最大公約数 + 1 だけ存在する
for i in range(GCD):
    # S の s 文字目と T の t 文字目が同じ位置に来る
    s = dist_M * i
    t = dist_N * i
    if S[s] != T[t]:
        ans = -1
        break
else:
    ans = L
print(ans)

Buggy submission (verdict: Runtime Error):
#66 A - Two Abbreviations
import math
N,M = map(int,input().split())
S = input()
T = input()

L = N*M//math.gcd(N,M)

dist_N = L//N
dist_M = L//M
lis = [0]*L

i = 0
for n in range(0,L,dist_N):
    lis[n] = S[i]
    i += 1
    print(lis)
    
j = 0
for m in range(0,L,dist_M):
    if lis[m] == 0:
        j += 1
    else:
        if lis[m] == T[j]:
            j += 1
        else:
            ans = -1
            break
    print(lis)
else:
    ans = L
print(ans)

Buggy submission (verdict: Runtime Error):
#66 A - Two Abbreviations
import math
N,M = map(int,input().split())
S = input()
T = input()

L = N*M//math.gcd(N,M)

dist_N = L//N
dist_M = L//M
lis = [0]*L
i = 0
for n in range(0,L,dist_N):
    if i == N:
        break
    lis[n] = S[i]
    i+=1
j = 0
for m in range(0,L,dist_M):
    if j == M:
        break
    if lis[m] == 0:
        lis[m] = T[j]
        j += 1
    else:
        if lis[m] == T[j]:
            j += 1
        else:
            L = -1
            break
ans = L
print(ans)

Buggy submission (verdict: Runtime Error):
#66 A - Two Abbreviations
import math
N,M = map(int,input().split())
S = input()
T = input()

L = N*M//math.gcd(N,M)

dist_N = L//N
dist_M = L//M
lis = [0]*L
i = 0
for n in range(0,L,dist_N):
    if i == len(S):
        break
    lis[n] = S[i]
    i+=1
j = 0
for m in range(0,L,dist_M):
    if j == len(T):
        break
    if lis[m] == 0:
        lis[m] = T[j]
        j += 1
    else:
        if lis[m] == T[j]:
            j += 1
        else:
            L = -1
            break
ans = L
print(ans)

Buggy submission (verdict: Runtime Error):
#66 A - Two Abbreviations
import math
N,M = map(int,input().split())
S = input()
T = input()

L = N*M//math.gcd(N,M)

dist_N = L//N
dist_M = L//M
lis = [0]*(L)
i = 0
for n in range(0,L,dist_N):
    lis[n] = S[i]
    i+=1
j = 0
for m in range(0,L,dist_M):
    if lis[m] == 0:
        lis[m] = T[j]
        j += 1
    else:
        if lis[m] == T[j]:
            j += 1
            continue
        else:
            ans = -1
            break
else:
    ans = L
print(ans)

Buggy submission (verdict: Runtime Error):
#66 A - Two Abbreviations
import math
N,M = map(int,input().split())
S = input()
T = input()

L = N*M//math.gcd(N,M)

dist_N = L//N
dist_M = L//M
lis = [0]*(L+1)
i = 0
for n in range(0,L,dist_N):
    lis[n] = S[i]
    i+=1
j = 0
for m in range(0,L,dist_M):
    if lis[m] == 0:
        lis[m] = T[j]
        j += 1
    else:
        if lis[m] == T[j]:
            j += 1
            continue
        else:
            ans = -1
            break
else:
    ans = L
print(ans)

Buggy submission (verdict: Runtime Error):
#66 A - Two Abbreviations
import math
N,M = map(int,input().split())
S = input()
T = input()

L = N*M//math.gcd(N,M)

dist_N = L//N
dist_M = L//M
lis = [0]*L
i = 0
for n in range(0,L,dist_N):
    lis[n] = S[i]
    i+=1
j = 0
for m in range(0,L,dist_M):
    if lis[m] == 0:
        lis[m] = T[j]
        j += 1
    else:
        if lis[m] == T[j]:
            j += 1
            continue
        else:
            ans = -1
            break
else:
    ans = L
print(ans)