Problem: Given is a string S. Let T be the concatenation of K copies of S. We can repeatedly perform the following operation: choose a character in T and replace it with a different character. Find the minimum number of operations required to satisfy the following condition: any two adjacent characters in T are different.

Submission attempts (in order):
S = list(input())
K = int(input())
length = []
tl = 1
x = 0
ans = 0
J = True
same = 1
for i in range(len(S)-1):
  if S[i] == S[i+1]:
    same += 1
if same == len(S):
  J = False
S.append('end')
if S[0] != S[len(S)-2] and J:
  for i in range(len(S)):
    if S[i] == S[i+1]:
      tl += 1
    else:
      length.append(tl)
      tl = 1
  for i in range(len(length)):
    x += length[i]//2
  ans += x*K
  print(ans)
elif not J:
  ans = (len(S)-1)*K//2
  print(ans)
else:
  for i in range(len(S)):
    if S[i] == S[i+1]:
      tl += 1
    else:
      length.append(tl)
      tl = 1
  ans += ((length[0]+length[len(length)])*(K-1)//2) + length[0]//2 + length[length]//2
  length[0] = 0
  length[len(length)] = 0
  for i in range(len(length)):
    x += length[i]//2
  ans += x*K
  print(ans)
  


S = list(input())
S.append('end')
K = int(input())
length = []
tl = 1
x = 0
ans = 0
J = True
same = 1
for i in range(len(S)-1):
  if S[i] == S[i+1]:
    same += 1
if same == len(S):
  J = False
if S[0] != S[len(S)-1] and J
  for i in range(len(S)-1):
    if S[i] == S[i+1]:
      tl += 1
    else:
      length.append(tl)
      tl = 1
  for i in range(len(length)):
    x += length[i]//2
  ans += x*K
  print(ans)
elif not J:
  ans = len(S)*K//2
  print(ans)
else:
  for i in range(len(S)-1):
    if S[i] == S[i+1]:
      tl += 1
    else:
      length.append(tl)
      tl = 1
  ans += ((length[0]+length[len(length)])*(K-1)//2) + length[0]//2 + length[length]//2
  length[0] = 0
  length[len(length)] = 0
  for i in range(len(length)):
    x += length[i]//2
  ans += x*K
  print(ans)
  


S = list(input())
R = int(input())
lengths = []
same = False
All = False
odd = False
sl = 1
Sum = 0
if S[0] == S[len(S)-1]:
  same = True
S.append('0')
for i in range(len(S)-1):
  if S[i] == S[i+1]:
    sl += 1
  else:
    lengths.append(sl)
    sl = 1
if len(lengths) == 1:
  All = True
if lengths[0]%2 == 0 and lengths[len(lengths)-1]%2 == 0:
  odd = True
if same:
  if All:
    Sum += (lengths[0]*R)//2
  else:
    temp = lengths[0]
    lengths[0] = 0
    lengths[len(lengths)-1] += temp
    if odd:
      for g in range(len(lengths)):
        Sum += (lengths[j]//2)*R - 1
    else:
      for k in range(len(lengths)):
        Sum += (lengths[k]//2)*R - 1
else:
  for j in range(len(lengths)):
    Sum += (lengths[j]//2)*R
print(Sum)
  
  


S = list(input())
K = int(input())
length = []
tl = 1
x = 0
ans = 0
J = True
same = 1
for i in range(len(S)-1):
  if S[i] == S[i+1]:
    same += 1
if same == len(S):
  J = False
S.append('end')
if S[0] != S[len(S)] and J:
  for i in range(len(S)):
    if S[i] == S[i+1]:
      tl += 1
    else:
      length.append(tl)
      tl = 1
  for i in range(len(length)):
    x += length[i]//2
  ans += x*K
  print(ans)
elif not J:
  ans = (len(S)-1)*K//2
  print(ans)
else:
  for i in range(len(S)):
    if S[i] == S[i+1]:
      tl += 1
    else:
      length.append(tl)
      tl = 1
  ans += ((length[0]+length[len(length)])*(K-1)//2) + length[0]//2 + length[length]//2
  length[0] = 0
  length[len(length)] = 0
  for i in range(len(length)):
    x += length[i]//2
  ans += x*K
  print(ans)
  


S = list(input())
R = int(input())
lengths = []
same = False
All = False
odd = False
sl = 1
Sum = 0
if S[0] == S[len(S)-1]:
  same = True
S.append('0')
for i in range(len(S)-1):
  if S[i] == S[i+1]:
    sl += 1
  else:
    lengths.append(sl)
    sl = 1
if len(lengths) == 1:
  All = True
if lengths[0]%2 == 1 and lengths[len(lengths)-1]%2 == 1:
  odd = True
if same:
  if All:
    Sum += (lengths[0]*R)//2
  else:
    temp = lengths[0]
    lengths[0] = 0
    lengths[len(lengths)-1] += temp
    if odd:
      for l in range(len(lengths)):
        Sum += (lengths[l]//2)*R
      Sum -= 1
    else:
      for g in range(len(lengths)):
        Sum += (lengths[g]//2)*R
else:
  for j in range(len(lengths)):
    Sum += (lengths[j]//2)*R
print(Sum)

Verdicts: Runtime Error

Runtime Error

Wrong Answer

Runtime Error

Accepted