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):
import re

s = input()
k = int(input())

frag = False

def nchars(s, n):
    assert n > 0
    reg = re.compile("(.)\\1{%d,}" % (n - 1))
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]
        
s_ = list(nchars(s,1))

for i in s_[0]:
  s1 = i
for i in s_[-1]:
  s2 = i

if s1 == s2:
  frag = True

l = [len(i) for i in s_]

c = len(l[0])//2

c1 = 0
c2 = 0

for i in range(len(l)-1):
  if i != len(l)-1:
    c1 += l[i+1]//2
  else:
    if frag:
      c1 += l[i] // 2
    else:
      c2 = (l[0]+l[i])//2
  
if not frag:
  print(c1*k)
else:
  print(c1)

import re
 
s = input()
k = int(input())
 
frag = False
 
def nchars(s, n):
    assert n > 0
    reg = re.compile("(.)\\1{%d,}" % (n - 1))
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]
        
s_ = list(nchars(s,1))
 
for i in s_[0]:
  s1 = i
for i in s_[-1]:
  s2 = i
 
if s1 == s2:
  frag = True
 
l = [len(i) for i in s_]
 
c = l[0]//2
 
c1 = 0
c2 = 0
 
for i in range(len(l)-1):
  if i != len(l)-1:
    c1 += l[i+1]//2
  else:
    if frag:
      c1 += l[i] // 2
    else:
      c2 = (l[0]+l[i])//2
  
if not frag:
  print(c1*k)
else:
  print(c1)

import re
 
s = input()
k = int(input())
 
frag = False
 
def nchars(s, n):
    assert n > 0
    reg = re.compile("(.)\\1{%d,}" % (n - 1))
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]
        
s_ = list(nchars(s,1))
 
for i in s_[0]:
  s1 = i
for i in s_[-1]:
  s2 = i
 
if s1 == s2:
  frag = True
 
l = [len(i) for i in s_]
 
c = l[0]//2
 
c1 = 0
c2 = 0
 
for i in range(len(l)):
  if i != len(l)-1:
    c1 += l[i]//2
  else:
    if not frag:
      c1 += l[i] // 2
    else:
      c2 = (l[0]+l[i])//2
  
if not frag:
  print(c1*k)
else:
  print(c + (c1-c)*k + c2*(k-1))

import re
 
s = input()
k = int(input())
 
frag = False
 
def nchars(s, n):
    assert n > 0
    reg = re.compile("(.)\\1{%d,}" % (n - 1))
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]
        
s_ = list(nchars(s,1))
 
for i in s_[0]:
  s1 = i
for i in s_[-1]:
  s2 = i
 
if s1 == s2:
  frag = True
 
l = [len(i) for i in s_]
 
c = l[0]//2
 
c1 = 0
c2 = 0
 
for i in range(len(l)):
  if i != len(l)-1:
    c1 += l[i+1]//2
  else:
    if frag:
      c1 += l[i] // 2
    else:
      c2 = (l[0]+l[i])//2
  
if not frag:
  print(c1*k)
else:
  print(c + (c1-c)*k + c2*(k-1))

import re
 
s = input()
k = int(input())
 
frag = False
 
def nchars(s, n):
    assert n > 0
    reg = re.compile("(.)\\1{%d,}" % (n - 1))
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]
        
s_ = list(nchars(s,1))
 
for i in s_[0]:
  s1 = i
for i in s_[-1]:
  s2 = i
 
if s1 == s2:
  frag = True
 
l = [len(i) for i in s_]
 
c = l[0]//2
 
c1 = 0
c2 = 0
c3 = 0

for i in range(len(l)):
  if i != len(l)-1:
    c1 += l[i]//2
  else:
    if not frag:
      c1 += l[i] // 2
    else:
      c2 = (l[0]+l[i])//2
      c3 = l[i]//2

if len(l) == 1:
  print((l[0]*k)//2)

elif not frag:
  print(c1*k)
else:
  print(c + (c1-c)*k + c2*(k-1) + c3)

Verdicts: Runtime Error

Wrong Answer

Wrong Answer

Wrong Answer

Accepted