Problem:


Score : 300 points

Problem Statement

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.

Constraints

1 \leq |S| \leq 100

S consists of lowercase English letters.

1 \leq K \leq 10^9

K is an integer.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the minimum number of operations required.

Sample Input 1

issii
2

Sample Output 1

4

T is issiiissii.  For example, we can rewrite it into ispiqisyhi, and now any two adjacent characters are different.

Sample Input 2

qq
81

Sample Output 2

81

Sample Input 3

cooooooooonteeeeeeeeeest
999993333

Sample Output 3

8999939997



Sample input:
issii
2


Sample output:
4

Submission status: Wrong Answer

Solution code:
def main():
    S = input()
    K = int(input())
    ans = 0

    if len(set(S)) is 1:
        print(len(S) * K // 2)
        return

    i = 0
    while i < len(S) - 1:
        if S[i] is S[i + 1]:
            ans += 1
            i += 1
        i += 1

    if ans is 0 and S[0] is S[-1]:
        print(K - 1)
    elif S[0] is S[-1]:
        tmp_str = S[::-1]
        cnt = 0
        i = 0
        while tmp_str[i] is tmp_str[0]:
            cnt += 1
            i += 1
        if cnt % 2 is not 0:
            print((ans + 1) * K)
        else:
            print(ans * K)
    else:
        print(ans * K)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import math
S = list(input())
K = int(input())
i = 0

for x in range(len(S)):
    if x>0:
        if S[x-1] == S[x]:
            i += 1
          
if S[0] == S[len(S)-1]:
  i += 1
                    
i = math.ceil(i/2)
i *= K
  
print(i)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())

if len(S) == 1:
    print(K//2)
    exit()
L = []
LS = []
ans = 0
streak = 1
s = ''

for i in range(len(S)-1):
    if i == 0:
        s = S[0]
        streak = 1
    
    if s == '':
        if S[i+1:i+2] == S[i:i+1]:
            s = S[i:i+1]
            streak += 1
    else:
        if S[i+1:i+2] == s:
            streak += 1
        else:
            L.append(streak)
            LS.append(s)
            s = ''
            streak = 1
    
    if i == len(S)-2:
        L.append(streak)
        LS.append(S[-1])
        # if streak == len(S):
        #     ans = K*(len(S)//2)
        #     print(ans)
        #     exit()

# print(LS)
# print(L)
if streak == len(S):
    ans = (len(S)*K)//2
    # print(len(S))
    print(ans)
    exit()

if LS[0] != LS[-1]:
    for l in L:
        if l >=2:
            ans += l//2
    ans *= K
elif LS[0] == LS[-1]:
    for i in range(1,len(L)-1):
        ans += L[i]//2
    ans = K*ans + L[0]//2 + ((K-1)*(L[0]+L[-1]))//2 + L[-1]//2
print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())

res = 0
cn = 0
for i in range(1, len(S)):
    if S[i-1] == S[i] :
        res += 1          
        if i != len(S)-1 and S[i] == S[i+1]:
            cn += 1

cnt = cn // 2
if cn % 2 == 1:
    cnt += 1
n = res - cnt

if len(S) > 1 and S[0] == S[-1] and S[0] != S[1]  and S[-2] != S[-1]:
    ans = n*K + K - 1
elif len(S) == 1:
    if K%2 == 1:
        ans = K//2 + 1
    else:
        ans = K//2
else:
    ans = n*K

print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
 
ans = 0
length = len(s)
 
if length == 1:
  ans = k // 2
elif length == 2:
  if s[0] == s[1]:
    ans = k
else:
  sep = 0
  i = 0
  last = 0
  firstchar = s[0]
  is_one_char = True
  while i < length-1:
    if firstchar != s[i] or firstchar != s[i+1]:
      is_one_char = False
    
    if s[i] == s[i+1]:
      sep += 1
      last = i
      i += 1
    i += 1
  
  if is_one_char:
    ans = (length // 2) * k
    if length % 2 != 0:
      ans += k // 2
  else:
    ans = sep * k
    if last != length-1:
      if s[length-1] != s[length-2] or (s[length-1] == s[length-2] and s[length-1] == s[length-3]):
        if s[0] == s[length-1] and s[0] != s[1]:
          ans += k-1
 
print(ans)

Submission status: Wrong Answer

Solution code:
S = list(input())
K = int(input())

def len_checker(S,r):
    count = 1
    if r == 0:
        icon = S[0]
        for j in range(1,len(S)):
            if S[j] == icon:
                count += 1
            else:
                break
    elif r == 1:
        icon = S[len(S)-1]
        for j in range(1,len(S)):
            jj = len(S) - j -1
            if S[jj] == icon:
                count += 1
            else:
                break
#     print(S,r,count)
    return(count)

def s_change(S):
    s_henkan = 0
    L = len(S) -1
#     print(S,S[0],S[L])
    if S[0] == S[L]:
        cf = len_checker(S,0)
        rf = len_checker(S,1)
#         print("cf:",cf,"rf:",rf)
        if cf % 2 == 1 and rf % 2 == 0:
            pass
        else:
            S[0] = '2'
            s_henkan += 1
#     print("mit:",S)
    for i in range(1,len(S)):
#         print(S[i])
        if S[i] == S[i-1]:
#             print(S[i-1],S[i])
            S[i] = '1'
            s_henkan += 1
    return(S,s_henkan)

        
henkan = 0
    
if len(S) == 1:
    henkan = K // 2
elif len(S) == 2:
    if S[0] == S[1]:
        henkan = K
    else:
        henkan = 0
else:
    SSSS = s_change(S)
#     print("SSSS: ",SSSS)
    henS = SSSS[0]
    s_henkan = SSSS[1]
    
    if henS[0] == henS[len(henS)-1]:
        henkan = s_henkan * K + (K-1)
    else:
        henkan = s_henkan * K

print(henkan)

Submission status: Wrong Answer

Solution code:
tmp = input()
S = list(tmp.strip())
K = int(input())
#S = S * K
lens = len(S)


#print(S)
count = 0
for i in range(1, lens):
    if S[i-1] == S[i]:
        count += 1
        S[i] ='X'
if S[0] == S[lens-1] == S[lens-2] or S[0] == S[1] == S[lens-1]:
    count = (count-1)*K + (K-1)
if S[0] == S[lens-1]:
    count = count * K + (K-1)
else:
    count = count * K

print(count)

Submission status: Wrong Answer

Solution code:
lsorg=list(input())
n=int(input())
mode='normal'

dounat=False
while True:
    if len(lsorg)==1:
        print(n//2)
        break
    else:
        ls=lsorg[:]+lsorg[:]
        ls.append(ls[0])
        count=0
        for i in range(len(ls)-1):
            a,b=ls[i],ls[i+1]
            if a==b:
                if i == len(ls)-2:
                    ls[i]='!'
                    dounat=True
                else:
                    ls[i+1]='?'
                count+=1
        ans = count * (n//2)
        if n%2==1:
            extra_count=0
            for i in range(len(lsorg)-1):
                a,b=lsorg[i],lsorg[i+1]
                if a==b:
                    lsorg[i+1]='?'
                    extra_count+=1
            ans += extra_count
        else:
            if dounat==True:
                ans -= 1
        print(ans)
        break

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
def main():
    input = sys.stdin.readline
    S = input().strip()
    K = input()
    ans = np.zeros(1, dtype="int64")
    len_S = len(S)
    if len_S != 1:
        char_S = []
        cnts_S = []
        #char_S.append(S[0])
        cnt = 0
        for i in range(len_S - 1):
            #print(i+1)
            #print(len_S-1)
            #print(char_S)
            #print(cnts_S)
            if cnt == 0:
                if S[i] == S[i+1]:
                    cnt += 1
                    if i + 1 == len_S - 1:
                        char_S.append(S[i])
                        cnts_S.append(cnt)
                else:
                    char_S.append(S[i])
                    cnts_S.append(cnt)
                    cnt = 0
                    if i + 1 == len_S - 1:
                        char_S.append(S[i+1])
                        cnts_S.append(cnt)
            elif cnt == 1:
                char_S.append(S[i])
                cnts_S.append(cnt)
                cnt = 0
                if i + 1 == len_S - 1:
                    char_S.append(S[i+1])
                    cnts_S.append(cnt)

        cnts_repeat = np.count_nonzero(cnts_S)

        ans[0] = np.int64(cnts_repeat)
        ans[0] *= np.int64(K)
        #print(cnts_repeat)
        if S[-1] == S[0]:
            if cnts_S[0] + cnts_S[-1] == 0:
                ans[0] += np.int64(K) - np.int64(1)
            elif cnts_S[0] + cnts_S[-1] == 1:
                if S.count(S[0]) == len_S:
                    ans[0] += np.int64(K) - np.int64(1)
                elif S[0] == S[1] == S[2] == S[-1]:
                    ans[0] += np.int64(K) - np.int64(1)
                elif S[0] == S[-1] == S[-2] == S[-3]:
                    ans[0] += np.int64(K) - np.int64(1)
            else:
                pass
    else:
        if np.int64(K) % 2 == 0:
            ans[0] = np.int64(K) // 2
        else:
            ans[0] = (np.int64(K) - 1) // 2
    print(ans[0])
    #print(cnts_S)
    #print(char_S)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
s = input()
length = len(s)
k = int(input())
if(len(s)==1):
    print(int(k/2))
    exit()
start = 0
end = 0
t = 0
last = 0
count = 0
ans = 0
b4 = s[0]
for c in s:
    if(b4!=c):
        if(last==0):
            start=t
            t=1
            last = 1
        else:
            ans+=int(t/2)
            t=1
    else:
        t+=1
    count+=1
    b4=c
end = t

print((int(start/2) + int(end/2) + ans)*k)

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
ans=0
cnt=1
add=False
lead=1
if len(s)==1:
  print(k//2)
  exit()
for i in range(1,len(s)):
  if s[i-1] == s[i]:
    lead+=1
  else:
    break
for i in range(1,len(s)):
  if s[i-1] == s[i]:
    cnt+=1
  else:
    ans+=cnt//2
    cnt=1
  if s[0] == s[-1] and lead%2!=0 and cnt%2==1 and i == (len(s)-1):
    ans+=cnt//2
    add=True
  elif i == (len(s)-1):
    ans+=cnt//2
if add:
  print((ans*k)+1*(k-1))
else:
  print(ans*k)

Submission status: Wrong Answer

Solution code:
import math

s = input() * 2
k = int(input())
count = 0
i = 0
while i <= len(s):
    tmp = 1
    for j in range(i + 1, len(s)):
        if s[i] == s[j]:
            tmp += 1
        else:
            break
    i += tmp
    tmp //= 2
    count += tmp

print(math.ceil(count * k / 2))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys
import random
import itertools
import math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt


def LI(): return list(map(int, input().split()))


def LF(): return list(map(float, input().split()))


def LI_(): return list(map(lambda x: int(x)-1, input().split()))


def II(): return int(input())


def IF(): return float(input())


def LS(): return list(map(list, input().split()))


def S(): return list(input().rstrip())


def IR(n): return [II() for _ in range(n)]


def LIR(n): return [LI() for _ in range(n)]


def FR(n): return [IF() for _ in range(n)]


def LFR(n): return [LI() for _ in range(n)]


def LIR_(n): return [LI_() for _ in range(n)]


def SR(n): return [S() for _ in range(n)]


def LSR(n): return [LS() for _ in range(n)]


def gcd(x, y):
    if y == 0:
        return x
    return gcd(y, x % y)


def my_index_multi(l, x):
    return [i for i, _x in enumerate(l) if _x == x]


mod = 1000000007
inf = float('INF')


def A():
    S = input()

    K = II()

    if len(S) == 2:
        print(K//2)
        return

    i = 0
    ans = 0
    A = 1
    p = 0
    z=0
    while(A > 0):
        count = 0
        while(S[i] == S[i+1]):
            if i == 0 or S[i+2] == '\n':
                p = 1
            count += 1
            i += 1
        if p == 1 and count % 2 == 0:
            z = 2

        if(S[i] != S[i+1]):
            i += 1
        if count == len(S)-2:
            print(((len(S)-1)*K)//2)
            return

        if count > 0:
            ans += (count+1)//2

        if S[-1] == S[i]:
            A = -1
        p=0
    if K >= 2 and S[-2] == S[0] and S[-3] != S[-2] and S[0] != S[1]:
        ans += 1
        print((ans*K)-1)
    elif K >= 2 and S[-2] == S[0] and S[-3] == S[-2] and S[0] == S[1]:
        print(ans*K)
    elif K >= 2 and S[-2] == S[0] and z == 2:
        ans += 1
        print((ans*K)-1)
    else:
        print(ans*K)

    return

# B


def B():
    N, K = map(int, input().split())
    h = LI()
    count = 0

    for i in h:
        if i >= K:
            count += 1
        else:
            continue
    print(count)
    return

# C


def C():
    N = II()
    A = LI()
    J = [0] * N
    for i, n in enumerate(A):
        J[n-1] = i+1
    for s in J:
        print(s, end=" ")

    return

# D


def D():
    A, B = map(int, input().split())
    g = gcd(A, B)
    # go = g
    # myset = set([1])
    sq = int(sqrt(g)) + 2
    ans = 1

    for i in range(2, sq):
        if g % i == 0:
            ans += 1
        while g % i == 0:
            g /= i

            # myset.add(i)
        if g == 1:
            break
        if i == sq - 1:
            # myset.add(go)
            ans += 1

    print(ans)

    return

# E


def E():
    N, M = LI()
    bit_max = 1 << N
    dp = [[inf for i in range(bit_max)] for j in range(M+1)]
    dp[0][0] = 0

    for i in range(M):
        a, b = LI()

        c = LI()
        bit = 0

        for d in c:
            bit |= 1 << (d-1)

        for w in range(bit_max):

            t = w | bit
            cost = dp[i][w] + a

            dp[i+1][t] = min(dp[i+1][t], cost)

            dp[i+1][w] = min(dp[i][w], dp[i+1][w])

    print(-1) if dp[-1][-1] == inf else print(dp[-1][-1])

    return

# F


def F():
    N, M = LI()

    value = [0] * M
    power = [0] * M
    for i in range(M):
        a, b = LI()
        value[i] = a
        c = LI()
        L = ['0'] * N
        for k in c:
            L[k - 1] = '1'
        bint = ''.join(L)
        power[i] = int(bint, 2)
    bint = ''.join(list(['1'] * N))
    dp = [inf] * (int(bint, 2)+1)
    t = 0
    cost = 0

    dp[0] = 0
    for s in range(int(bint, 2) + 1):
        for i in range(M):
            t = s | power[i]
            cost = dp[s] + value[i]
            dp[t] = min(dp[t], cost)
    print(-1) if(dp[-1] == inf) else print(dp[-1])

    return

    # you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")


def solution(S):
    # write your code in Python 3.6
    import math

    vowels = "AIUEO"
    count = 0
    consonants = 0
    newC = []
    newV = []

    for i in S[:-1]:
        if i in vowels:
            count += 1
            newV.append(i)
        else:
            consonants += 1
            newC.append(i)

    if consonants < count or consonants - 2 > count:
        return 0

    a = math.factorial(len(set(newC))) * math.factorial(len(set(newV)))
    return a

    pass


# Solve
if __name__ == '__main__':
    A()

Submission status: Wrong Answer

Solution code:
def count_changes(s, k, prev=None):
    count = 0
    for _ in range(k):
        for cur in s:
            if cur == prev:
                count += 1
                prev = None
            else:
                prev = cur
    return count, prev


if __name__ == '__main__':
    s = input()
    k = int(input())
    if k <= 10 ** 8:
        print(count_changes(s, k)[0])
    else:
        base_count, prev = count_changes(s, 3)
        d, rest = divmod(k, 3)
        res = base_count * d
        if rest:
            res += count_changes(s, rest, prev)[0]
        elif s[0] == prev:
            res += (d - 1)
        print(res)

Submission status: Wrong Answer

Solution code:
import sys
import os

S = input()
K = int(input())
if len(S) == 1:
    print(0)
    sys.exit()
cnt0 = 1
cntL = 1
cnt = 1
ans = 0
i = 0
while i < len(S)-1:
    for j in range(i+1,len(S)):
        if S[i] == S[j]:
            cnt +=1
        else:
            if i == 0:
                cnt0 = cnt
            i = j
            break
    if cnt > 1:
        ans += cnt//2
        cnt = 1
        i = j

for i in range(len(S)-1,0,-1):
    if S[i] == S[i-1]:
        cntL +=1
    else:
        break
ans = K*ans
if S[0] == S[-1] and cnt0%2 != 0 and cntL %2 != 0:
    ans += K-1
if cntL == len(S):
    if (K*cntL)%2==0:
        print((K*cntL)//2)
    else:
        print((K*cntL-1)//2)
    sys.exit()
print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
code_list = 'abcdefghijklmnopqrstuvwxyz'

def c1(s):#len(s)>1
  count1=0
  S = list(s)
  l_s=len(S)
  for i in range(1,l_s):
    #print(S,count)
    if S[i]==S[i-1] and i<l_s-1:
      for j in code_list:
        if j != S[i+1] and j !=S[i-1]:
          S[i]=j
          count1+=1
          break
    elif S[i]==S[i-1]:
      for j in code_list:
        if j != S[i-1] and j !=S[0]:
          S[i]=j
          count1+=1
          break
  return count1,S[-1]

def c2(s,last):#k>2 and len(s)>1
  S=list(s)
  count2=0
  l_s=len(S)
  for i in range(0,l_s):
    #print(S,count)
    if i==0 and S[0]==last:
      for j in code_list:
        if j != S[i+1] and j !=last:
          S[i]=j
          count2+=1
          break
    elif S[i]==S[i-1] and i<l_s-1:
      for j in code_list:
        if j != S[i+1] and j !=S[i-1]:
          S[i]=j
          count2+=1
          break
    elif S[i]==S[i-1]:
      for j in code_list:
        if j != S[i-1] and j !=S[0]:
          S[i]=j
          count2+=1
          break
  return count2

def c3(s,last):#k>1 len(s)>1
  S=list(s)
  count3=0
  l_s=len(S)
  for i in range(0,l_s):
    #print(S,count)
    if i==0:
      if S[0]==last:
        for j in code_list:
          if j != S[i+1] and j !=last:
            S[i]=j
            count3+=1
            break
    elif S[i]==S[i-1] and i<l_s-1:
      for j in code_list:
        if j != S[i+1] and j !=S[i-1]:
          S[i]=j
          count3+=1
          break
    elif S[i]==S[i-1]:
      for j in code_list:
        if j != S[i-1]:
          S[i]=j
          count3+=1
          break
  return count3

if len(s)==1:
  print(k//2)
elif k==1:
  c,_=c1(s)
  print(c)
elif k==2:
  count1,last=c1(s)
  count3=c3(s,last)
  print(count1+count3)
else:
  count1,last=c1(s)
  count2=c2(s,last)
  count3=c3(s,last)
  #print(count1,count2,count3)
  print(count1+count2*(k-2)+count3)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())


ds = S + S + S[0]
ss = S
dlength = len(ds)
slength = len(ss)
chkd, chks = K // 2, K % 2

def chkerss(idx):
    i = 1
    while idx + i < slength and ss[idx] == ss[idx + i]:
        i += 1

    return i // 2, idx + i

def chkerds(idx):
    i = 1
    while idx + i < dlength and ds[idx] == ds[idx + i]:
        i += 1

    return i // 2, idx + i

c1, idx1 = 0, 0
while idx1 < dlength:
    a, idx1 = chkerds(idx1)
    c1 += a

c2, idx2 = 0, 0
if chks != 0:
    while idx2 < slength:
        a, idx2 = chkerss(idx2)
        c2 += a

print(c1 * chkd + c2)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())

def double(s,head = False):
    if head:
        arr = [0]
    else:
        arr = [-1]
    for i in range(len(s)):
        if arr[-1] != i-1:
            if s[i] == s[i-1]:
                arr.append(i)
    arr.remove(-1)
    return arr

if s[0] != s[-1]:
    count = len(double(s)) * k
else:
    arr = double(s)
    if len(arr) == 0:
        if len(s) == 1:
            print(k//2)
        else:
            print(k-1)
    else:
        if arr[-1] == len(s)-1:
            print(len(arr)*k)
        else:
            l = len(arr)
            arr = [0]
            for i in range(len(s)):
                if i == 0:
                    continue
                if arr[-1] != i-1:
                    if s[i] == s[i-1]:
                        arr.append(i)
            if arr[-1] != len(s)-1:
                print(l + len(arr)*(k-1))
            else:
                if k%2 == 0:
                    print((l+len(arr))*k//2)
                else:
                    print((l+len(arr))*(k//2)+l)

Submission status: Wrong Answer

Solution code:
def resolve():
    s = input()
    k = int(input())
    c = 1
    ls = len(s)
    if len(set(s)) == 1 and ls % 2 == 1:
        if k % 2 == 0:
            ans = ls * (k // 2)
        else:
            ans = ls * (k // 2) + ls // 2
        print(ans)
    else:
        ans = 0
        for i in range(len(s) - 1):
            if s[i] == s[i + 1]:
                c += 1
            else:
                ans += c // 2
                c = 1
        ans += c // 2
        if s[-2] != s[-1] and s[0] == s[-1] and s[0] != s[1]:
            ans += 1
            print(ans * k - 1)
        else:
            print(ans * k)


if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:

s=input()
n=int(input())
q=list(s)

count=0
for j in range(len(q)-1):

        if (q[j]==q[j+1]):
            q[j+1]='0'
            count+=1

print(count*n)

Submission status: Wrong Answer

Solution code:
a = input()
j = int(input())

# is0i0is0i0
# print(a, len(a))
i = 0
c = 0
prev = False
for x in a:
  # print(prev)
  if c > 0:
    # print(x, a[c-1], c, prev, x == a[c-1] and not prev)
    if x == a[c-1] and not prev:
      i += 1
      prev = True
      c += 1
      continue
  if c != len(x):
    prev = False
  # print("c++", c)
  c +=1 

#prev and
if a[0] == a[-1]:
  if (i==0) and j>1:
    print(j-1) # this is the error line
  elif not prev:
    print(i*j+(j-1))
  else: 
    print(i*j)
else:
  print(i*j)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())


def c(ss, k):
    s = list(ss)
    s *= k
    n = len(s)
    ret = 0
    for i in range(n - 2):
        if s[i] == s[i + 1] == s[i + 2]:
            s[i + 1] = 'AB' [(i + 1) % 2]
            ret += 1
    for i in range(n - 1):
        if s[i] == s[i + 1]:
            s[i + 1] = 'AB' [(i + 1) % 2]
            ret += 1
    return ret


x1 = c(S, 1)
x2 = c(S, 2)
ans = x2 * (K // 2)
if K % 2:
    ans += x1
print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
l = len(S)

if l == 1:
    print(K // 2)
    exit()

if l == 2:
    if S[0] == S[1]:
        print(K)
    else:
        print(0)
    exit()


if S[0] != S[-1]:
    ans_i = 0
    ans_j = 0
    i = 2
    j = 1

    if l % 2 == 0:
        while j < l - 1:
            if S[j - 1] != S[j] and S[j] != S[j + 1]:
                j += 2
            else:
                ans_j += 1
                j += 2
        if S[-2] == S[-1] or S[-1] == S[0]:
            ans_j += 1

        while i < l:
            if S[i - 1] != S[i] and S[i] != S[i + 1]:
                i += 2
            else:
                ans_i += 1
                i += 2
        ans = min(ans_i, ans_j)
        print(ans * K)

    else:
        while j < l:
            if S[j - 1] != S[j] and S[j] != S[j + 1]:
                j += 2
            else:
                ans_j += 1
                j += 2

        while i < l - 1:
            if S[i - 1] != S[i] and S[i] != S[i + 1]:
                i += 2
            else:
                ans_i += 1
                i += 2
        if S[-2] == S[-1] or S[-1] == S[0]:
            ans_i += 1
        ans = min(ans_i, ans_j)
        print(ans * K)

else:
    ans = 0
    prev = None
    edge = False
    for i, v in enumerate(S):
        if prev == v:
            ans += 1
            if i == 0 or i == l - 1:
                edge = True
            prev = None
        else:
            prev = v

    if edge:
        print(ans * K)
    else:
        print(ans * K + l - 1)

Submission status: Wrong Answer

Solution code:
S = [w for w in input()]
K = int(input())

change_cnt = 0
for i in range(1, len(S)):
    if S[i] == S[i - 1]: # 1つ前と比較
        change_cnt += 1
        S[i] = '0'

# 頭文字と語末の比較
if S[0] == S[-1]: change_cnt += 1 

print('{}'.format(change_cnt * K))

Submission status: Wrong Answer

Solution code:
X=input()
a=int(input())
count=0
ans=0
z=list(X)
b=0
z=list(X)
if len(z)==1:
    ans=int(a/2)
else:
    for k in range(len(z)-1):
        if z[k]==z[len(z)-1]:
            b+=1
    for i in range(len(z)-1):
        if z[i]==z[i+1]:
            z[i+1]=0
            count=count+1
    ans=int(a*count+(a-1)*(int((b+1)/2)-count))
print(ans)

Submission status: Wrong Answer

Solution code:
S = str(input())
K = int(input())
# 全部異なる文字だった場合
if len(set(S)) == len(S):
    result = 0
# １文字めと最後の文字が異なる場合
elif S[0] != S[-1]:
    box = []
    mini_count = 0
    mini_str = ''
    for index, s in enumerate(S[:-1]):
        #print(s, S[index+1])
        if s == S[index+1]: # 次の文字と同じだった場合
            mini_count += 1
            mini_str = s
        else:
            if mini_count != 0:
                #print('#################')
                box.append(mini_str * (mini_count+1))
                mini_str = ''
                mini_count = 0
               
    all_temp = 0
    for each in box:
        if len(each) % 2 == 0: # 偶数の場合
            temp = len(each) / 2
        else: # 奇数の場合
            temp = len(each) // 2
        all_temp += temp
        
    result = all_temp * K

            
# １文字めと最後の文字が同じ場合
else:
    box = []
    mini_count = 0
    mini_str = ''
    for index, s in enumerate(S):
        if index+1 != len(S):# 最後の文字でない場合
            if s == S[index+1]: # 次の文字と同じだった場合
                mini_count += 1
                mini_str = s
            else:
                if mini_count != 0:
                    box.append(mini_str * (mini_count+1))
                    mini_str = ''
                    mini_count = 0
        else: # 最後の文字の場合
            if mini_count != 0:
                box.append(mini_str * (mini_count+1))
                mini_str = ''
                mini_count = 0
            
               
    all_temp = 0
    for each in box:
        if len(each) % 2 == 0: # 偶数の場合
            temp = len(each) / 2
        else: # 奇数の場合
            temp = len(each) // 2
        all_temp += temp
        
    if S[0] in box[-1] or S[0] in box[0]:
        result = (all_temp * K)
    elif len(box) == 0:#中身は全部異なっていた場合
        result = K -1
    else:
        result = (all_temp * K) + (K - 1)
        
    
print(int(result))

Submission status: Wrong Answer

Solution code:
import copy
S=list(input())
SS=copy.deepcopy(S)
K=int(input())
c=0
cc=0
if len(S)==1:
  if K==1:
    print(0)
    exit()
  else:
    print(K//2)
    exit()
for i in range(1,len(S)):
  if S[i]==S[i-1]:
    S[i]="!"
    c+=1
if K==1:
  print(c)
  exit()
if S[0]!=S[-1]:
  print(c*K)
else:
  SS[0]="!"
  for i in range(1,len(S)):
    if SS[i]==SS[i-1]:
      SS[i]="!"
      cc+=1
  print(c+(K-1)*cc+K-1)

Submission status: Wrong Answer

Solution code:
def main():
    S = input()
    K = int(input())
    ans = 0

    if len(set(S)) is 1:
        print(len(S) * K // 2)
        return

    if S[0] is S[-1]:
        tmp_str = S[::-1]
        cnt = 0
        i = 0
        while tmp_str[i] is tmp_str[0]:
            cnt += 1
            i += 1
        if cnt % 2 is not 0:
            ans += 1

    i = 0
    while i < len(S) - 1:
        if S[i] is S[i + 1]:
            ans += 1
            i += 1
        i += 1

    if ans is 0 and S[0] is S[-1]:
        print(K - 1)
    else:
        print(ans * K)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())

lens = len(s)

cnt = 0
ans = 0

sig = lens % 2

i=0
lasti=0
frg = True
while i < lens:
  if i != lens-1 and s[i] == s[i+1]:
    cnt += 1
    lasti = i
    i+=2
  else:
    i+=1

ans = cnt * k

# lensが奇数で最初と最後が同じ場合
if lens > 1 and s[-1] != s[-2] and s[0] == s[-1]:
  ans += k-1


print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
import sys
s = input()
k = int(input())
# print (s,k)
n=len(s)
# print (s[0],s[n-1])

skip=0
if n == 1: 
    print((k-1) // 2 + (k-1) % 2 )

else:
    flag=0
    for i in range(1,n):
        #print("loop",i)
       if s[i] != s[i-1]:
           flag =1
           break
    
    if flag == 0: 
        print((k*n-1) // 2 + (k*n-1) % 2 )
    else:
        p=0
        q=0
        if s[0] == s[n-1]:
            l=1 
            # Start 
            for i in range(1,n):
                #print("loop",i)
                if s[i] == s[i-1]:
                    p +=1
                else:
                    break
            if l < n:
                for i in range(n-2,0,-1):
                    if s[i] == s[i+1]:
                        q +=1
                    else:
                        break
            l = 1 + p + q
            # print("Ring ", l, p,q)
            op = ((l // 2) + (l % 2)) * (k-1)
            op += ((p // 2) + (p % 2)) 
            op += ((q // 2) + (q % 2))
        else: 
            op = 0     
        
        # print("OP",op)
        
        s2 = s[p+1:n-1-q] 
        # print("s2",s2)
        n=len(s2)
                    
        
        l = 0
        for i in range(1,n):
            # print(s2[i],i,l,op)
            if s2[i-1] != s2[i]:
                op += (l // 2 + l % 2) * k
                l = 0 
            else:
                l += 1 
        if l != 0:
            op += (l // 2 + l % 2) * k
        
                
        print(op)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
# 隣り合う文字列を置換した回数をカウント
cnt = 0
for i in range(len(s) - 1):
  # 末尾1文字前まで
  if s[i] == s[i + 1]:
    s[i + 1] = str(i)
    cnt += 1

cnt = cnt * k
    
# 末尾が置換されていなくて、先頭と同じならk-1を足す
try:
  float(s[-1])
except ValueError:
  if s[0] == s[-1]:
    cnt += k - 1
    
print(cnt)

Submission status: Wrong Answer

Solution code:
S=list(input())
K=int(input())

def solve():
    global S,K
    if len(set(list(S)))==1:
        return len(S)*K//2

    count=0
    i=1
    while i<len(S):
        if S[i]==S[i-1]:
            count+=1
            S[i]="."
        i+=1

    a=(S[0]==S[-1])

    if len(S)==1:
        return (K*len(S))//2
    else:
        return(count*K+a*(K-1))


print(solve())

Submission status: Wrong Answer

Solution code:
S = list(input())
K = int(input())
ans = 0

flg = 0
if len(S) % 2 != 0 and len(S) > 2:
    if S[0] == S[1] == S[-1]:
        flg = 1

for i in range(1, len(S)):
    if S[i] == S[i-1] and S[i] != None:
        ans += 1
        S[i] = None
ans *= K

if len(S) == 1:
    ans = K // 2
    print(ans)
    exit()

if S[-1] == S[0] and S[1] != None:
    ans += (K-1)
elif flg:
    ans += K // 2

print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
d= [c  for c in s]
n=int(input())
l=len(d)
count=0
for i in range(l-2):
  if i==0 and d[0]==d[l-1] and d[i]==d[i+1]:
    d[0]="1"
    count+=1
  if d[i]==d[i+1]:
    if d[i]!=d[i+2]:
       d[i]="1"
       count+=1
    elif d[i]==d[i+2]:
      d[i+1]="1"
      count+=1
if l>=3:
  if s[l-2]==s[l-1] and s[l-3]!=s[l-2] :
    count+=1
    count*=n
  elif s[l-2]==s[l-1] and s[l-2]==s[l-3] :
    count*=n
    count+=(n-1)
  else:
    count*=n
if l==2 and s[0]==s[1]:
  count=n
print(count)

Submission status: Wrong Answer

Solution code:
X=input()
a=int(input())
count=0
ans=0
z=list(X)
if len(z)==1:
    ans=int(a/2)
else:
    for i in range(len(z)-1):
        if z[i]==z[i+1]:
            z[i+1]=0
            count=count+1
        ans=count*a
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
k = input()
w = s * 2

s = list(s)
w = list(w)
ans_s = 0
ans_w = 0
for i in range(0, len(s)-1):
    if s[i] == s[i+1]:
        ans_s += 1
        s[i+1] = "0"

for i in range(0, len(w)-1):
    if w[i] == w[i+1]:
        ans_w += 1
        w[i+1] = "0"

if len(s) == 1:
    ans_s = int(k) // 2
elif (ans_s*2) == ans_w:
    ans_s *= int(k)
else:
    ans_s = ans_s*int(k) + int(k) - 1

print(ans_s)