Problem:


Score : 300 points

Problem Statement

You are given a string S of length N and another string T of length M.
These strings consist of lowercase English letters.

A string X is called a good string when the following conditions are all met:

Let L be the length of X. L is divisible by both N and M.

Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.

Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.

Determine if there exists a good string. If it exists, find the length of the shortest such string.

Constraints

1 \leq N,M \leq 10^5

S and T consist of lowercase English letters.

|S|=N

|T|=M

Input

Input is given from Standard Input in the following format:

N M
S
T

Output

If a good string does not exist, print -1; if it exists, print the length of the shortest such string.

Sample Input 1

3 2
acp
ae

Sample Output 1

6

For example, the string accept is a good string.
There is no good string shorter than this, so the answer is 6.

Sample Input 2

6 3
abcdef
abc

Sample Output 2

-1

Sample Input 3

15 9
dnsusrayukuaiia
dujrunuma

Sample Output 3

45



Sample input:
3 2
acp
ae


Sample output:
6

Submission status: Wrong Answer

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

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

def lcm(a, b):
    return int(a * b / gcd (a, b))

N, M = [int(i) for i in input().split()]
S = input()
T = input()

if N > M:
    a, b = N, M
    A, B = S, T
else:
    a, b = M, N
    A, B = T, S

if a % b == 0:
    for i in range(b):
        if A[int(i * a / b)] == B[i]:
            continue
        else:
            ans = -1
            break
    ans = lcm(a, b)
            
else:
    if A[0] == B[0]:
        ans = lcm(a, b)
    else:
        ans = -1        
        
print(ans)

Submission status: Wrong Answer

Solution code:
from fractions import *

def lcm(a,b): 
    return int(abs(a * b) / gcd(a,b) if a and b else 0)

def getDivisors(a,b):
    divisors = []
    for i in range(2,min(a,b)+1):
        if a % i == 0 and b % i == 0:
            divisors.append(i)
    return divisors

N,M = map(int,input().split())
a = input()
b = input()

if gcd(N,M) == 1 and a[0] == b[0]:
    print(lcm(N,M))
elif N != M:
    divisors = getDivisors(N,M)
    if a[0] != b[0]:
        print(-1)
        quit()

    for i in divisors:
        if a[N//i] != b[M//i]:
            print(-1)
            quit()
    print(lcm(N,M))
else:
    if a == b: print(N)
    else: print(-1)

Submission status: Wrong Answer

Solution code:
def gcd(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        return int(a)
    r = a % b
    return gcd(b, r)

def lcm(a,b):
  return int(a*b/gcd(a,b))
 
N,M = map(int,input().split())
S = input()
T = input()
L = lcm(N,M)
flag = True
min_len = min(N,M)
s_index = int(int(L/M)/gcd(int(L/M),int(L/N)))
t_index = int(int(L/N)/gcd(int(L/M),int(L/N)))

for i in range(min_len):
  if i * s_index < N-1 and i*t_index < M - 1:
    if S[i*s_index] == T[i*t_index]:
      continue
    else:
      flag = False
      break
  else:
    break
    
if flag:  
  print(L)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
s = input()
t = input()

nsky = n
msky = m
while not nsky % msky == 0:
    tmp = msky
    msky = nsky % msky
    nsky = tmp
skys = msky

idx_n = 1
idx_m = 1
okans = -1

while n * idx_n < 1000000 and m * idx_m < 1000000 and okans < 0:
    if n * idx_n == m * idx_m:
        idx_2 = 0
        
        while (n/skys)*idx_2 < n:
            if s[int(n/skys)*idx_2] == t[int(m/skys)*idx_2]:
                if (n/skys)*(idx_2+1) == n:
                    okans = n * idx_n
                idx_2 = idx_2 + 1
            else:
                idx_2 = n
        idx_m = idx_m + 1
        idx_n = idx_n + 1
    elif n * idx_n > m * idx_m:
        idx_m = idx_m + 1
    elif n * idx_n < m * idx_m:
        idx_n = idx_n + 1

print(okans)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
s = input()
t = input()

# 最大公約数
def gcd(a, b):
    while b != 0:
        a, b = b, a % b
    return a
# x,yの最小公倍数lcm(x, y)を求める
def lcm(a, b):
    return a * b // gcd(a, b)
x = gcd(m, n)
a = int(m/x)
b = lcm(a, int((n/m)*a))
i = 0
flag = True
while b*i <= min(m-1, n-1):
    if s[int((b*i)/a)] == t[int((b*i)*m/(n*a))]:
        flag = True
        i += 1
    else:
        flag = False
        break
if flag == True:
    print(lcm(n, m))
elif flag == False:
    print(-1)

Submission status: Wrong Answer

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

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

def lcm(a, b):
    return int(a * b / gcd (a, b))

N, M = [int(i) for i in input().split()]
S = input()
T = input()

if N > M:
    a, b = N, M
    A, B = S, T
else:
    a, b = M, N
    A, B = T, S

if a % b == 0:
    for i in range(b):
        if A[int(i * a / b)] == B[i]:
            continue
        else:
            ans = -1
            break
    ans = (a - 1) / a * lcm(a, b) + 1
            
else:
    if A[0] == B[0]:
        ans = lcm(a, b)
    else:
        ans = -1        
        
print(ans)

Submission status: Wrong Answer

Solution code:

def main():
    buf = input()
    buflist = buf.split()
    N = int(buflist[0])
    M = int(buflist[1])
    buf = input()
    S = buf
    buf = input()
    T = buf
    L = lcm(N, M)
    # main part
    i = 0
    j = 0
    while i < N and j < M:
        if i == j:
            if not S[int(i / (L / N))] == T[int(j / (L / M))]:
                print(-1) # faliure
                return
            else:
                i += L / N
                j += L / M
        elif i < j:
            i += L / N
        elif i > j:
            j += L / M
    print(L) # success
    return

# 最大公約数 & 最小公倍数
def gcd(a, b):
    while b > 0:
        a, b = b, a % b
    return int(a)

def lcm(a, b):
    return int(a * b / gcd(a, b))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
s = input()
t = input()

# 最大公約数
def gcd(a, b):
    while b != 0:
        a, b = b, a % b
    return a
# x,yの最小公倍数lcm(x, y)を求める
def lcm(a, b):
    return a * b // gcd(a, b)
x = gcd(m, n)
a = int(m/x)
b = lcm(a, int((n/m)*a))
i = 0
flag = False
while b*i <= min(m-1, n-1):
    if s[int((b*i)/a)] == t[int((b*i)*m/(n*a))]:
        flag = True
        i += 1
    else:
        flag = False
        break
if flag == True:
    print(lcm(n, m))
elif flag == False:
    print(-1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
def getLCM(a,b):
    gcd = 1
    if a > b:
        gcd = getGcd(a,b)
        return int(a*b/gcd)
    else:
        gcd = getGcd(b,a)
        return int(b*a/gcd)

def getGcd(a,b):
    while a%b != 0:
        temp = b
        b = a%b
        a = temp
    return b

def main():
    N,M = map(int,input().split())
    S = str(input())
    T = str(input())
    L = getLCM(N,M)
    list_N = set([])
    list_M = set([])
    for i in range(0,N):
        list_N.add(i*L//N+1)
    for i in range(0,M):
        list_M.add(i*L//M+1)

    same_index = set([])
    for i in list_N:
        if i in list_M:
            same_index.add(i)

    for i in same_index:
        if S[i-1:i] != T[i-1:i]:
            print(-1)
            exit()

    print(L)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def gcd(a, b):
  while b > 0:
    a, b = b, a%b
  return a

N,M = map(int,input().split(" "))
S = input()
T = input()
N = len(S)
M = len(T)
x = gcd(N,M)
y = int(N*M/x)
Answer = int(N*M/x)

for i in range(int(Answer/y)):
    if S[int(i*N/x)] == T[int(i*M/x)]:
        continue
    else:
        print(-1)
        exit()
print(Answer)

Submission status: Wrong Answer

Solution code:
import fractions

nm = input().split()
n = int(nm[0])
m = int(nm[1])

s = input()
t = input()

lcm = n * m / fractions.gcd(n, m)

n_ind = []
for i in range(n):
    n_ind.append((lcm / n) * (i))

ind_list = []
for i in range(m):
    if n_ind.count((lcm / m) * (i)) == 1:
        ind_list.append((lcm / m) * (i))

for i in range(len(ind_list)):
    if s[int(i / (lcm / n))] != t[int(i / (lcm / m))]:
        print(-1)
        break
print(int(lcm))

Submission status: Wrong Answer

Solution code:
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

def solve(N, M, S, T):
    L = N * M // gcd (N, M)
    X = ["" for _ in range(L)]
    for n in range(N):
        ni = int(L/N * n)
        X[ni] = S[n]

    for m in range(M):
        mi = int(L/M * m) # + 1
        if X[mi] != "":
            if X[mi] != T[m]:
                return "-1"
        else:
            X[mi] = T[m]
    for i in range(L):
        if X[i] == "":
            X[i] = "a"
    return "".join(X)


if __name__ == "__main__":
    l = input()
    N, M = int(l.split()[0]),int(l.split()[1])
    S = input()
    T = input()
    print(solve(N, M, S, T))

Submission status: Wrong Answer

Solution code:

n, m = map(int, input().split())
s = input()
t = input()

nsky = n
msky = m
while not nsky % msky == 0:
    tmp = msky
    msky = nsky % msky
    nsky = tmp
skys = msky

idx_n = 1
idx_m = 1
okans = -1

while n * idx_n < 100000000 and m * idx_m < 100000000 and okans < 0:
    if n * idx_n == m * idx_m:
        idx_2 = 0
        
        while (n/skys)*idx_2 < n:
            if s[int(n/skys)*idx_2] == t[int(m/skys)*idx_2]:
                if (n/skys)*(idx_2+1) == n:
                    okans = n * idx_n
                idx_2 = idx_2 + 1
            else:
                idx_2 = n
        idx_m = idx_m + 1
        idx_n = idx_n + 1
    elif n * idx_n > m * idx_m:
        idx_m = idx_m + 1
    elif n * idx_n < m * idx_m:
        idx_n = idx_n + 1

print(okans)

Submission status: Wrong Answer

Solution code:
def gcd(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        return int(a)
    r = a % b
    return gcd(b, r)

def lcm(a,b):
  return int(a*b/gcd(a,b))

N,M = map(int,input().split())
S = input()
T = input()

#X = ["" for i in range(lcm(N,M))]
#L = len(X)
L = lcm(N,M)
flag = True
min_len = min(N,M)

for i in range(min_len):
  if i * int(L/M)/gcd(int(L/M),int(L/N)) < N-1 and i*int(L/N)/gcd(int(L/M),int(L/N)) < M - 1:
    if S[i*int(L/M)] == T[i*int(L/N)]:
      continue
    else:
      flag = False
      break
  else:
    break
    
if flag:  
  print(L)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
s = input()
t = input()

nsky = n
msky = m
while not nsky % msky == 0:
    tmp = msky
    msky = nsky % msky
    nsky = tmp
skys = msky

idx_n = 1
idx_m = 1
okans = -1

while n * idx_n < 10000 and m * idx_m < 10000 and okans < 0:
    if n * idx_n == m * idx_m:
        idx_2 = 0
        
        while (n/skys)*idx_2 < n:
            if s[int(n/skys)*idx_2] == t[int(m/skys)*idx_2]:
                if (n/skys)*(idx_2+1) == n:
                    okans = n * idx_n
                idx_2 = idx_2 + 1
            else:
                idx_2 = n
        idx_m = idx_m + 1
        idx_n = idx_n + 1
    elif n * idx_n > m * idx_m:
        idx_m = idx_m + 1
    elif n * idx_n < m * idx_m:
        idx_n = idx_n + 1

print(okans)

Submission status: Wrong Answer

Solution code:
import sys
# 最大公約数
def gcd(a, b):
    while b > 0:
        a, b = b, a % b
    return a

# 最小公倍数
def lcm(a, b):
    return a * b / gcd(a, b)

A = list(map(int, input().split()))
N = A[0]
M = A[1]
S = input()
T = input()
#N = 9999
#M = 9999
#S = 'acp'
#T = 'ae'

if N * M > 1000000000:
    print(-1)
    sys.exit()

S_list = list(S)
T_list = list(T)

# Xの長さ
L = int((lcm(N, M)))

Xn = []
for i in range(N * -1 + 1, 1):
    tmp = (N - 1 + i) * L / N + 1
    Xn.append(int(tmp))

Xm = []
for i in range(M * -1 + 1, 1):
    tmp = (M - 1 + i) * L / M + 1
    Xm.append(int(tmp))

#print(Xn)
#print(Xm)



double_list = list(set(Xn) & set(Xm))
double_list.sort()
#print('double_list', double_list)

Xn_list = []
for i in double_list:
    Xn_list.append(Xn.index(i) + 1)

#print(Xn_list)

Xm_list = []
for i in double_list:
    Xm_list.append(Xm.index(i) + 1)

#print(Xm_list)






flag = False
for xn, xm in zip(Xn_list, Xm_list):
    #print(S_list[xn - 1])
    #print(T_list[xm - 1])

    if S_list[xn - 1] == T_list[xm - 1]:
        #print("同じだ！")
        pass
    else:
        #print("違うだ！")
        flag = True
        break

if flag:
    print(-1)
else:
    print(L)

Submission status: Wrong Answer

Solution code:
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

def lcm(x, y):
    return (x * y) // gcd(x, y)

N, M = map(int, input().split())
S = input()
T = input()

lcm_num = lcm(N,M)
lN = lcm_num // N
lM = lcm_num // M
gcd_num = gcd(N,M)

for i in range(0,lcm_num,gcd_num):
    if i == 0 or (i%(lN) == 0 and i%(lM) == 0):
        if S[i//lN] == T[i//lM]:
            continue
        else:
            print(-1)
            exit()

print(lcm_num)

Submission status: Wrong Answer

Solution code:
import sys
# 最大公約数
def gcd(a, b):
    while b > 0:
        a, b = b, a % b
    return a

# 最小公倍数
def lcm(a, b):
    return a * b / gcd(a, b)

A = list(map(int, input().split()))
N = A[0]
M = A[1]
S = input()
T = input()
#N = 9999
#M = 9999
#S = 'acp'
#T = 'ae'

if N * M > 100000000:
    print(-1)
    sys.exit()

S_list = list(S)
T_list = list(T)

# Xの長さ
L = int((lcm(N, M)))

Xn = []
for i in range(N * -1 + 1, 1):
    tmp = (N - 1 + i) * L / N + 1
    Xn.append(int(tmp))

Xm = []
for i in range(M * -1 + 1, 1):
    tmp = (M - 1 + i) * L / M + 1
    Xm.append(int(tmp))

#print(Xn)
#print(Xm)



double_list = list(set(Xn) & set(Xm))
double_list.sort()
#print('double_list', double_list)

Xn_list = []
for i in double_list:
    Xn_list.append(Xn.index(i) + 1)

#print(Xn_list)

Xm_list = []
for i in double_list:
    Xm_list.append(Xm.index(i) + 1)

#print(Xm_list)






flag = False
for xn, xm in zip(Xn_list, Xm_list):
    #print(S_list[xn - 1])
    #print(T_list[xm - 1])

    if S_list[xn - 1] == T_list[xm - 1]:
        #print("同じだ！")
        pass
    else:
        #print("違うだ！")
        flag = True
        break

if flag:
    print(-1)
else:
    print(L)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
S = str(input())
T = str(input())


def get_gcd(a, b):
    if a < b:
        a, b = b, a
    if a % b == 0:
        return b
    else:
        return get_gcd(b, a % b)


def get_lcm(a, b, gcd):
    lcm = a * b // gcd
    return lcm


start = get_lcm(N, M, get_gcd(N, M))
ans = 0
for i in range(100):
    L = (i+1) * start
    flg = 0

    s_ind = set(j * (L / N) + 1 for j in range(N))
    t_ind = set(k * (L / M) + 1 for k in range(M))
    for num in (s_ind & t_ind):
        n = (num - 1) * (N / L)
        m = (num - 1) * (M / L)
        if S[int(n)] != T[int(m)]:
            flg = 1
            break
    if flg == 0:
        ans = L
        break

if ans == 0:
    print('-1')
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
S = str(input())
T = str(input())


def get_gcd(a, b):
    if a < b:
        a, b = b, a
    if a % b == 0:
        return b
    else:
        return get_gcd(b, a % b)


def get_lcm(a, b, gcd):
    lcm = a * b // gcd
    return lcm


start = get_lcm(N, M, get_gcd(N, M))
ans = 0
for i in range(1000):
    L = (i+1) * start
    flg = 0

    s_ind = set(j * (L / N) + 1 for j in range(N))
    t_ind = set(k * (L / M) + 1 for k in range(M))
    for num in (s_ind & t_ind):
        n = (num - 1) * (N / L)
        m = (num - 1) * (M / L)
        if S[int(n)] != T[int(m)]:
            flg = 1
            break
    if flg == 0:
        ans = L
        break

if ans == 0:
    print('-1')
else:
    print(ans)

Submission status: Wrong Answer

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

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

def lcm(a, b):
    return int(a * b / gcd (a, b))

N, M = [int(i) for i in input().split()]
S = input()
T = input()

if N > M:
    a, b = N, M
    A, B = S, T
else:
    a, b = M, N
    A, B = T, S

if a % b == 0:
    for i in range(a):
        if A[int(i * a / b)] == B[i]:
            continue
        else:
            ans = -1
            break
    ans = (a - 1) / a * lcm(a, b) + 1
            
else:
    if A[0] == B[0]:
        ans = lcm(a, b)
    else:
        ans = -1        
        
print(ans)

Submission status: Wrong Answer

Solution code:
# coding:utf-8

n, m = [int(i) for i in input().split()]
s = input()
t = input()

if not s[0] == t[0]:
	print(-1)
	exit()

nl = n
while nl < 10 ** 5:
	if nl % m:
		nl += n
		continue

	nb = True
	nl_x = [""] * nl
	for i in range(n):
		nl_x[int(i * nl / n)] = s[i]
	for i in range(m):
		if nl_x[int(i * nl / m)] == "" or nl_x[int(i * nl / m)] == t[i]:
			nl_x[int(i * nl / m)] = t[i]
		else:
			nb = False
			break
	else:
		if nb:
			print(nl)
			exit()
	nl += n
print(-1)

Submission status: Wrong Answer

Solution code:
import sys
# 最大公約数
def gcd(a, b):
    while b > 0:
        a, b = b, a % b
    return a

# 最小公倍数
def lcm(a, b):
    return a * b / gcd(a, b)

A = list(map(int, input().split()))
N = A[0]
M = A[1]
S = input()
T = input()
#N = 9999
#M = 9999
#S = 'acp'
#T = 'ae'

if N > 9999 and M > 9999:
    print(-1)
    sys.exit()

S_list = list(S)
T_list = list(T)

# Xの長さ
L = int((lcm(N, M)))

Xn = []
for i in range(N * -1 + 1, 1):
    tmp = (N - 1 + i) * L / N + 1
    Xn.append(int(tmp))

Xm = []
for i in range(M * -1 + 1, 1):
    tmp = (M - 1 + i) * L / M + 1
    Xm.append(int(tmp))

#print(Xn)
#print(Xm)



double_list = list(set(Xn) & set(Xm))
double_list.sort()
#print('double_list', double_list)

Xn_list = []
for i in double_list:
    Xn_list.append(Xn.index(i) + 1)

#print(Xn_list)

Xm_list = []
for i in double_list:
    Xm_list.append(Xm.index(i) + 1)

#print(Xm_list)






flag = False
for xn, xm in zip(Xn_list, Xm_list):
    #print(S_list[xn - 1])
    #print(T_list[xm - 1])

    if S_list[xn - 1] == T_list[xm - 1]:
        #print("同じだ！")
        pass
    else:
        #print("違うだ！")
        flag = True
        break

if flag:
    print(-1)
else:
    print(L)

Submission status: Wrong Answer

Solution code:
N,M=map(int, raw_input().split() )
S=raw_input()
T=raw_input()

if M>N:
	N,M=M,N
	S,T=T,S

if S[0]!=T[0]:
	print -1
	quit()

def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

def lcm(a, b):
	return a * b // gcd (a, b)


LCM=lcm(N,M)

if gcd(N,M)==1:
	print N*M
else:
	n=N/gcd(N,M)
	m=M/gcd(N,M)
	if n!=1 and m!=1:
		print LCM
	else:
		S_tmp=""
		for i in range(M):
			S_tmp+=S[i* max(n,m) ]
		if S_tmp==T:
			print LCM
		else:
			print -1

Submission status: Wrong Answer

Solution code:
from fractions import gcd
def main():
    n, m = list(map(int, input().split(" ")))
    s = input()
    t = input()

    if n < m:
        n, m = m, n
        s, t = t, s

    def lcm(x, y):
        return ((x * y)//gcd(x, y))

    for i in range(100000):
        if i == 0:
            continue
        if i % n != 0 or i % m != 0:
            continue

        s_spliters = list(range(0, i, i // n))
        t_spliters = list(range(0, i, i // m))

        flag = True
        count = 0
        for t_spliter in t_spliters:
            if t_spliter in s_spliters:
                index = s_spliters.index(t_spliter)
                if s[index] != t[count]:
                    flag = False
                    break
            count += 1

        if flag:
            print(i)
            return
    print(-1)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import fractions
def lcm(x, y):
    return (int)((x * y) / fractions.gcd(x, y))

N, M = list(map(int, input().split()))
S = list(input())
T = list(input())
flag = True

L = lcm(N, M)

if S[0] != T[0] or N == M:
  flag = False

if N % M == 0 or M % N == 0:
  for i in range(min(N, M)):
    #print(i, S[int(L*i/M)], T[int(L*i/N)])
    if S[int(L*i/M)] != T[int(L*i/N)]:
      flag = False
      break

if flag:
  print(L)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

def lcm(a, b):
    return int(a * b / gcd (a, b))

N, M = [int(i) for i in input().split()]
S = input()
T = input()

if N > M:
    a, b = N, M
    A, B = S, T
else:
    a, b = M, N
    A, B = T, S

if a % b == 0:
    i = 0
    while i < a:
        if A[int(i * a / b)] == B[i]:
            continue
        else:
            ans = -1
            break
        i += 1 
    ans = (a - 1) / a * lcm(a, b) + 1
            
else:
    if A[0] == B[0]:
        ans = lcm(a, b)
    else:
        ans = -1        
        
print(ans)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
def lcm(x,y):
    return (x*y)//gcd(x,y)
n,m=map(int,input().split())
s=input()
t=input()
d=lcm(n,m)
p=d//n
q=d//m
h=lcm(p,q)
l=d//h
for i in range(0,l-1):
    g=i*h
    if s[g//p]!=t[g//q]:
        print(-1)
        break
else:
    print(d)

Submission status: Wrong Answer

Solution code:
N,M=map(int, raw_input().split() )
S=raw_input()
T=raw_input()

if M>N: N,M=M,N

if S[0]!=T[0]:
	print -1
	quit()

def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

def lcm(a, b):
	return a * b // gcd (a, b)


LCM=lcm(N,M)

if gcd(N,M)==1:
	print N*M
else:
	n=N/gcd(N,M)
	m=M/gcd(N,M)
	if n!=1 and m!=1:
		print LCM
	else:
		S_tmp=""
		for i in range(M):
			S_tmp+=S[i* max(n,m) ]
		if S_tmp==T:
			print LCM
		else:
			print -1

Submission status: Wrong Answer

Solution code:
import fractions
N,M = map(int,input().split())
S = input()
T = input()
if N>=M:
  if N%M!=0:
    x = fractions.gcd(N,M)
    a = []
    b = []
    for i in range(int(x)):
      a.append(S[int(i*N/x)])
    for i in range(int(x)):
      b.append(T[int(i*M/x)])
    if a==b:
      print (int(N*M/x))
    else:
      print (-1)
  else:
    x = int(N/M)
    a = []
    b = []
    for i in range(M):
      a.append(S[i*x])
    for i in range(M):
      b.append(T[i])
    print (a)
    print (b)
    if a==b:
      print (N)
    else:
      print (-1)
else:
  if M%N!=0:
    x = fractions.gcd(N,M)
    a = []
    b = []
    for i in range(int(x)):
      a.append(S[int(i*N/x)])
    for i in range(int(x)):
      b.append(T[int(i*M/x)])
    if a==b:
      print (int(N*M/x))
    else:
      print (-1)
  else:
    x = int(M/N)
    a = []
    b = []
    for i in range(N):
      a.append(T[i*x])
    for i in range(N):
      b.append(S[i])
    print (a)
    print (b)
    if a==b:
      print (M)
    else:
      print (-1)

Submission status: Wrong Answer

Solution code:
# A
n,m = map(int, input().split())
s = str(input())
t = str(input())

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
def lcm(a,b):
    return a*b/gcd(a,b)
l=int(lcm(n,m))
si = ti= 0
ss = tt = 1
for i in range(l):
    print(i)
    if ss<i:
        ss=int(ss+l/n)
        si+=1
    if tt<i:
        tt=int(tt+l/m)
        ti+=1
    if ss<=l and tt<=l and i<=ss and i<=tt and ss==tt:
        if s[si]!=t[ti]:
            print('-1')
            break
else:
    print(l)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
S = input()
T = input()

def gcd(a, b):
    if a < b:
        return gcd(b, a)
    else:
        return a if b == 0 else gcd(b, a % b)

def lcm(a, b):
    return a * b // gcd(b, a%b)
L = lcm(N, M)

r = ["*" for i in range(L)]
f = False

s_step = L // N
t_step = L // M
sL = lcm(s_step, t_step)
s = 0
t = 0

for i in range(0, L, sL):
    if i == 0:
        if S[i] == T[i]:
            continue
        else:
            f = True
            break
    else:
        s = i % s_step -1
        t = i % t_step -1
        if s > N-1:
            break
        if t > M -1:
            break
        if S[s] == T[t]:
            continue
        else:
            f = True
            break


if f:
    print(-1)
else:
    print(L)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split(" "))
S = input()
T = input()

L = N * M
n, m = N, M
GCD = 0
if n < m:
    n, m = m, n
r = n % m
while r != 0:
    n, m, r = m, r, n%m
GCD = m
LCM = L//m

if GCD == 1 and S[0] == T[0]:
    print(L)
elif S[::N//GCD] == T[::M//GCD]:
    print(LCM)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
a,b=map(int,input().split(" "))
s=list(input())
t=list(input())
i=1
i1=0
if (a%b!=0)&(b%a!=0):
    if s[0]==t[0]:
        while True:
            i+=1
            i1=i
            if (i%a==0)&(i%b==0):
                break
    print(i1)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().strip().split())
S = input().strip()
T = input().strip()

if N % M == 0 or M % N == 0:
  if len(S) > len(T):
    maxS, minS = S, T
    if all(s == t for s, t in zip(maxS[1:-1:len(maxS)//len(minS)], minS)):
      print(len(maxS))
    else:
      print(-1)
  elif len(T) > len(S):
    maxS, minS = T, S
    if all(s == t for s, t in zip(maxS[1:-1:len(maxS)//len(minS)], minS)):
      print(len(maxS))
    else:
      print(-1)
  else:
    if S == T:
      print(len(S))
    else:
      print(-1)
else:
  c = None
  a, b = max(N, M), min(N, M)
  while True:
    if a % b == 0:
      c = b
      break
    else:
      c = a % b
      a = b
      b = c
  print(max(N, M) * int(min(N, M) / c))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
S = input()
T = input()


def gcd(a, b):
    if a < b:
        return gcd(b, a)
    else:
        return a if b == 0 else gcd(b, a % b)


def lcm(a, b):
    return a * b // gcd(b, a%b)

L = lcm(N, M)

r = ["*" for i in range(L)]
f = False

s_step = L // N
t_step = L // M
sL = lcm(s_step, t_step)
s = 0
t = 0

for i in range(0, L, sL):
    if i == 0:
        if S[i] == T[i]:
            continue
        else:
            f = True
            break
    else:
        s = i % s_step -1
        t = i % t_step -1
        if s > N-1:
            continue
        if t > M -1:
            continue
        if S[s] == T[t]:
            continue
        else:
            f = True
            break


if f:
    print(-1)
else:
    print(L)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())

S = input()
T = input()

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

x = N * M // gcd(N, M)

if x != N and x != M:
    print(x)
else:
    if N == M:
        if S == T:
            print(x)
            exit()
    elif N > M:
        for index, i in enumerate(S):
            if index % int(N // M) == 0:
                if T[int(index / int(N // M))] != i:
                    break
        else:
            print(x*2)
            exit()
    else:
        for index, i in enumerate(T):
            if index % int(M // N) == 0:
                if S[int(index / int(M // N))] != i:
                    break
        else:
            print(x*2)
            exit()

    print(-1)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
S = input()
T = input()

def gcd(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        return a
    r = a % b
    return gcd(b, r)

def lcm(a,b):
  return int(a*b/gcd(a,b))

X = ["" for i in range(lcm(N,M))]
L = len(X)
flag = True

min_len = min(N,M)

for i in range(min_len):
  if i * int(L/M) < N-1 and i*int(L/N) < M - 1:
    if S[i*int(L/M)] == T[i*int(L/N)]:
      continue
    else:
      flag = False
      break
  else:
    break
    
if flag:  
  print(L)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
S=input()
T=input()

def gcd(a,b):
    if a<b:a,b=b,a
    c=a%b
    while c>0:
        a,b=b,c
        c=a%b
    return b

def enstr(A,g):
    m=''
    for i in range(len(A)//g):
        m+=A[g*i]
    return m
    
if S[0]==T[0]:
    gcd=gcd(N,M)
    if N<M:
        N,M=M,N
        S,T=T,S
    if gcd==M:
        U=enstr(S,N//M)
        if U==T:
            print(N)
        else:
            print(-1)
    else:
        gcm=N*M//gcd
        print(gcm)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import sys

def gcd(n,m):
    if m==0:
        return n
    else:
        return gcd(m,n%m)

n,m=list(map(int,input().split()))
s=input()
t=input()

x=gcd(n,m)

if x==n:
    for i in range(0,m+1,n):
        if t[i]!=s[i//n]:
            print(-1)
            sys.exit()
    print(n*m//x)
elif x==m:
    for i in range(0,n+1,m):
        if s[i]!=t[i//m]:
            print(-1)
            sys.exit()
    print(n*m//x)
else:
    if s[0]!=t[0]:
        print(-1)
        sys.exit()
    print(n*m//x)

Submission status: Wrong Answer

Solution code:
# coding:utf-8

n, m = [int(i) for i in input().split()]
s = input()
t = input()

if not s[0] == t[0]:
	print(-1)
	exit()

nl = max(n, m)
while nl < 10 ** 6:
	if nl % n or nl % m:
		nl += 1
		continue
	if not max(((n - 1) * nl / n) + 1, ((m - 1) * nl / m) + 1) <= nl:
		nl += 1
		continue

	nb = True
	nl_x = [""] * nl
	for i in range(1, n):
		nl_x[int((i - 1) * nl / n) + 1] = s[i - 1]
	for j in range(1, m):
		if nl_x[int((j - 1) * nl / m) + 1] == "" or nl_x[int((j - 1) * nl / m) + 1] == t[j - 1]:
			nl_x[int((j - 1) * nl / m) + 1] = t[j - 1]
		else:
			nb = False
			break
	else:
		if nb:
			print(nl)
			exit()
	nl += 1
print(-1)

Submission status: Wrong Answer

Solution code:
import fractions
N, M = map(int,input().split())
S = [x for x in input()]
T = [x for x in input()]

if S[0] != T[0]:
    print(-1)
    exit()

if N >= M:
    h, l, hl, ll = N, M, S, T
else:
    h, l, hl, ll = M, N, T, S

if h % l == 0:
    if h == l:
        if S == T:
            print(N * 2)
            exit()
    else:
        temp = h // l
        for i in range(1, l):
            if hl[temp * i] != ll[i]:
                print(-1)
                exit()

        print(N * 2)
        exit()
else:
    print((h * l) // fractions.gcd(h, l))

Submission status: Wrong Answer

Solution code:
def gcd(m,n) :
    x = max(m,n)
    y = min(m,n)
    if x%y == 0 :
        return y
    else :
        while x%y != 0 :
            z = x%y
            x = y
            y = z
        else :
            return z

def lcm(m,n) :
    return int(m*n/gcd(m,n))

N,M = map(int,input().split())
S = list(input())
T = list(input())
L = lcm(N,M)
A = []
B = []
flag = 0
counter = 0
for i in range(N) :
    A.append(i*L//N+1)
for j in range(M) :
    b = j*L//M+1
    if j % M == 0 :
        index = N*j//M
        if S[index] == T[j] :
            counter += 1
        else :
            flag = 1
    else :
        counter += 1
if flag == 1 :
    print(-1)
else :
    if counter == M :
        print(L)

Submission status: Wrong Answer

Solution code:
import fractions
def lcm(x, y):
    return (int)((x * y) / fractions.gcd(x, y))

N, M = list(map(int, input().split()))
S = list(input())
T = list(input())
flag = True

L = lcm(N, M)

if S[0] != T[0] or N == M:
  flag = False

if N % M == 0 or M % N == 0:
  for i in range(int(min(N, M))-1):
    print(S[int(L*i/N)], T[int(L*i/M)], i)
    if S[int(L*i/N)] != T[int(L*i/M)]:
      flag = False
      break

if flag:
  print(L)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
import sys

def gcd(n,m):
    if m==0:
        return n
    else:
        return gcd(m,n%m)

n,m=list(map(int,input().split()))
s=input()
t=input()

x=gcd(n,m)

if x==n:
    for i in range(0,m-n,n):
        if t[i]!=s[i//n]:
            print(-1)
            sys.exit()
    print(n*m//x)
elif x==m:
    for i in range(0,n-m,m):
        if s[i]!=t[i//m]:
            print(-1)
            sys.exit()
    print(n*m//x)
else:
    if s[0]!=t[0]:
        print(-1)
        sys.exit()
    print(n*m//x)