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 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


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

Submission status: Wrong Answer

Solution code:
import numpy as np

S = list(map(str,input().split()))
S = [c for c in S[0]]
K = int(input())

count = 0
S2 = S[1:] + [S[0]]

countflag = True
tmp = 0
for i in range(len(S2)):
    if S[i] == S2[i]:
        if tmp == 1:
            tmp = 0
            countflag = True
        if countflag:
            count += 1
        else:
            tmp += 1
        
        countflag = False
    else:
        countflag = True
count *= K



print(count)

Submission status: Wrong Answer

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

if len(S) == 1:
    print(0)
    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:
        if streak >= 2:
            L.append(streak)
            LS.append(s)
        if streak == len(S):
            ans = K*(len(S)//2)
            print(ans)
            exit()

if LS[0] != LS[-1]:
    for l in L:
        if i >=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:
import math


S = input()
K = int(input())

if K == 1:
    cnt = 0
    S2 = list(S)
    for i in range(1, len(S2)):
        if S2[i-1] == S2[i]:
            S2[i] = "A"
            cnt += 1

    cnt = cnt * K

    if S2[0] == S2[-1]:
        cnt += K-1

    print(cnt)

else:
    S2 = list(S)
    S2 = S2 + S2
    cnt1 = 0
    for i in range(1, int(len(S2)/2)):
        if S2[i-1] == S2[i]:
            S2[i] = "A"
            cnt1 += 1

    cnt2 = 0
    for i in range(int(len(S2)/2)+1, len(S2)):
        if S2[i-1] == S2[i]:
            S2[i] = "A"
            cnt2 += 1

    cnt = cnt1 * math.ceil(K/2)
    cnt += cnt2 * (K//2)


    if S2[0] == S2[-1]:
        if K > 2:
            cnt += math.ceil(K/2) - 1

    print(cnt)

Submission status: Wrong Answer

Solution code:
a = input()
if len(a) == 1:
  e = int(input())
  print(e//2)
else:
  d = []
  for i in range(len(a)):
    d.append(a[i])
  b = 0
  for i in range(len(d)-2):
    if d[i] == d[i+1]:
      d[i+1] = 'S'
      b += 1
  if d[0] == d[-1]:
    b += 1
  c = int(input())
  print(b*c)

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
      i += 1
      last = i
    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 and s[0] == s[length-1]:
      ans += k-1

print(ans)

Submission status: Wrong Answer

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

ans = 0
tmp = 0
head = 1
hip = 1
if S[0] == S[-1]: key = True
else: key = None
itr = 0
b = 0
for s1, s2 in zip(S[:-1], S[1:]):
    if s1 == s2:
        tmp += 1
    else:
        ans += (tmp+1)//2
        if (key == True) and (b == 0):
            head = tmp
            b+=1
        tmp = 0

    if (key == True) and (itr == len(S)-2):
        hip = tmp
    itr += 1
ans += (tmp+1)//2
ans = ans*K
if (head%2 == 0) and (hip%2 == 0):
    ans += K-1
print(ans)

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
    while(A > 0):
        count = 0
        while(S[i] == S[i+1]):
            count += 1
            i += 1
        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
    if K >= 2 and S[-2] == S[0] and S[-3] != S[-2] and S[0] != S[1]:
        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:
S = input()
K = input()

L = []
tmp = []
pre = ""
for s in S:
    if len(tmp) == 0:
        pre = s
        tmp.append(s)
    elif pre == s:
        tmp.append(s)
    else:
        L.append(tmp)
        pre = s
        tmp = [s]
L.append(tmp)

count = 0
for l in L:
    count += int(len(l) / 2)

count *= int(K)
if len(L) == 1:
    print(count)
    exit()
if L[0][0] == L[len(L)-1][0]:
    if (len(L[0]) + len(L[len(L) - 1])) % 2 == 0:
        count = count + 1 * int(K) - 1
print(count)

Submission status: Wrong Answer

Solution code:
from sys import stdin
S = (stdin.readline().rstrip())
K = int(stdin.readline().rstrip())
ans = 0
N=len(S)
cnt = 1
G = S+"@"
flag = False
for i in range(0,N):
    if G[i] == G[i+1]:
        cnt += 1
        flag = True
    else:
        if cnt != 1:
            #print(cnt)
            ans += (cnt//2)
            cnt = 1
        #print(ans,flag)
if flag != True:
  ans = 0
for_eq = 1
rev_eq = 1
revS = S[::-1]
for j in range(0,N-1):
    if S[j] == S[j+1]:
        for_eq += 1
    else:
        break
for k in range(0,N-1):
    if revS[k] == revS[k+1]:
        rev_eq += 1
    else:
        break
#print(ans)
ans = ans*K
#print(rev_eq,for_eq)
if K != 1:
    if S[0] == S[-1]:
        ans+=((K-1)*((rev_eq+for_eq)//2))
        """
        if (rev_eq % 2 != 0) and (for_eq % 2 != 0):
            ans += ((K+2)//2)
        """
        """
        if rev_eq == 1 and for_eq != 1:
            ans -= (K//2)
        if rev_eq != 1 and for_eq == 1:
            ans -= (K//2)
        """
        if (flag != True) and K == 1:
            ans = 0
if len(set(S))==1:
    ans = (len(S)*K)//2

print(ans)

Submission status: Wrong Answer

Solution code:
lsorg=list(input())
n=int(input())
mode='normal'
    
while True:
    if len(lsorg)==1:
        print(n//2)
        break
    else:
        ls=lsorg[:]+lsorg[:]
        ls.append(ls[0])
        count=0
        dounat=False
        for i in range(len(ls)-2):
            a,b=ls[i],ls[i+1]
            if a==b:
                if i == len(ls)-3:
                    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)-2):
                a,b=ls[i],ls[i+1]
                if a==b:
                    ls[i+1]='?'
                    extra_count+=1
            ans += extra_count
        print(ans)
        break

Submission status: Wrong Answer

Solution code:
S=list(input())
K=int(input())
c=0
for i in range(1,len(S)):
  if S[i]==S[i-1]:
    S[i]="!"
    c+=1
print(c*K)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
# s = 'cooooooooonteeeeeeeeeest'
# k = 999993333

s_cnt = 0
for i in range(len(s) - 1):
    if s[i] != s[i + 1]:
        s_cnt = i + 1
        break
# print(s_cnt)
for i in range(len(s) - 1, 1, -1):
    if s[i] != s[i - 1]:
        e_cnt = len(s) - i
        break
# print(e_cnt)
is_start = False
idx_list = []
for i in range(len(s) - 1):
    if s[i] == s[i + 1]:
        if not is_start:
            is_start = True
            s_idx = i
    else:
        if is_start:
            is_start = False
            e_idx = i
            idx_list.append((s_idx, e_idx))
else:
    if is_start:
        idx_list.append((s_idx, len(s) - 1))
ans = 0
for s_idx, e_idx in idx_list:
    ans += (e_idx - s_idx + 1) // 2
ans *= k
if s_cnt % 2 == 1 and e_cnt % 2 == 1 and s[0] == s[-1]:
    ans += k - 1
print(ans)

Submission status: Wrong Answer

Solution code:
S = str(input())
K = int(input())
C = i = 0

if len(S) == 1:
    C = int(K / 2)
else:
    while i + 1 < len(S):
        if S[i] == S[i + 1]:
            C += 1
            i += 1
        i += 1
    if S[0] == S[-1]:
        S += S[0]
        i = c = 0
        while i + 1 < len(S):
            if S[i] == S[i + 1]:
              c += 1
              i += 1
            i += 1
        if c != C:
            C = C * K + K - 1
        else:
            C *= K
    else:
        C *= K

print(C)

Submission status: Wrong Answer

Solution code:
# A - Connection and Disconnection
S = str(input())
K = int(input())
count =  0
tmp = 0
N = len(S)

if N == 1:
    if K >= 2:
        count = 1 * (K//2)
    else:
        count = 0
elif S[0] == S[N-1]:
    count += 1
else:
    pass

for i in range(1,N-1):
    if S[i-1] == S[i] == S[i+1] and tmp == 0:
        tmp = 1
    else:
        tmp = 0
    if S[i-1] == S[i] and tmp == 0:
        count += 1
    else:
        pass
if S[N-2] == S[N-1] and tmp == 1:
    count += 1
else:
    pass


if N == 1 and tmp == 0:
    print(count)
elif N == 3 and tmp == 0:
    print(count*(K-1))
elif N == 3 and tmp == 1:
    print(count + K)
elif N >= 4 and S[0] == S[N-1]:
     print((count-1)+(K-1)*count)
else:
    ans = count * K
    print(ans)

Submission status: Wrong Answer

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

def func(s, k):
    ans = 0
    n = len(s)
    s_list = list(s)
    for i in range(n - 1):
        if s_list[i] == s_list[i + 1]:
            ans += 1
            s_list[i + 1] = str(i + 1)
    ans *= k
    if s_list[0] == s_list[n - 1]:
        ans += k - 1
    return ans

ans1 = func(s, k)
s_inv = ''.join(list(reversed(s)))
ans2 = func(s_inv, k)

print(min(ans1, ans2))

Submission status: Wrong Answer

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

count = 0
count_list = []
for i in range(len(S)):
  if S[i-1] == S[i]:
    count += 1
  else:
    count_list.append(count)
    count=0

count_list.append(count)
ans = 0


if(S[0] != S[-1]):
  for i in range(len(count_list)):
    if count_list[i] > 0:
      ans += (count_list[i] + 1) // 2
  ans = ans * K   
else:
  lst = 0
  for i in range(len(count_list)):
    if count_list[i] > 0:
      lst += (count_list[i] + 1) // 2
  if count_list[0] % 2 == 1:
      lst -= 1
   
  b = count_list[0] - 1  
  pp = count_list.pop(0)
  if len(count_list) > 0:
    count_list[-1] += pp
  else: 
    count_list.append(pp)
  
  for i in range(len(count_list)):
    if count_list[i] > 0:
      ans += (count_list[i] + 1) // 2

  ans = ans * (K-1) + lst

print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
a = []
ans = 0
count = 0
for i in range(len(S)-1):
    if S[i] == S[i+1] :
        count += 1
    else:
        if count!=0:
            ans += (count+1)//2
            a.append(count+1)
            count = 0
if count !=0:
    ans += (count+1)//2 
ans = ans*K
a.append(0)
if len(S) == 1:
    ans = K//2
else:    
    
    if (count+1)%2==1 and a[0]%2 == 1 and S[0]== S[1] and S[0] == S[len(S)-1]:
    
        ans+=K-1
    if S[0]!= S[1] and (count+1)%2 ==1 and  S[0] == S[len(S)-1]:
        ans+=K-1

 
print(ans)

Submission status: Wrong Answer

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

cnt,ans=0,0
T=S+S
for i in range(1,len(T)):
  if T[i-1]==T[i]:
    cnt+=1
  else:
    ans+=(cnt+1)//2
    cnt=0
  if i==len(S)-1:
    ans+=(cnt+1)//2
    tmp=ans

if len(S)==1:
  print(k//2)
  exit()
if len(S)==2:
  if S[0]==S[1]:
    print(k)
  else:
    print(0)
  exit()
if len(set(T))==1:
  if len(S)%2==0:
    print(len(S)//2*k)
  else:
    print(len(S)//2+(len(S)//2+1)*(k-1))
  exit()
      
print(tmp+(ans-tmp)*(k-1))

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


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

Submission status: Wrong Answer

Solution code:
from sys import stdin
from itertools import groupby

s = stdin.readline().rstrip()
k = int(stdin.readline().rstrip())

def cou(st):
    li = []
    point = 1
    for i in range(len(st)-1):
        if st[i] == st[i+1]:
            point += 1
            if i == len(st)-2:
                li.append(point)
        else:
            li.append(point)
            point = 1
    if st[-1] != st[-2]:
        li.append(1)
    return li

if len(s) == 1:
    lin = [1]
else:
    lin = cou(s)

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

lin2 = lin

if k > 1:
    if s[0] == s[-1]:
        lin[-1] += lin[0]
else:
    if len(s) == 1:
        print(0)
        exit()

point = 0
point2 = 0
for i in range(1,len(lin)):
    point += lin[i]//2
for w in range(1,len(lin2)):
    point2 += lin2[w]//2
point = point*(k-1)
point += point2
point += lin[0]//2
print(point)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
t = ''
i = 0
count =0



if s[0] != s[len(s)-1]:
	while i < len(s)-2:
		if s[i] == s[i+1]:
			if i == len(s)-2:
				count +=1
				i += 1
			if s[i+1] == s[i+2]:
				count += 1
				i += 2
			else: 
				count += 1
				i += 1
		else:
			i += 1
	if i == len(s)-2:
		if s[i] == s[i+1]:
			count +=1
	print(count*k)

else:
	while i < len(s)-2:
		if s[i] == s[i+1]:
			if i == len(s)-2:
				count +=1
				i += 1
			if s[i+1] == s[i+2]:
				count += 1
				i += 2
			else: 
				count += 1
				i += 1
		else:
			i += 1
	if i == len(s)-2:
		if s[i] == s[i+1]:
			count +=1
		else:
			count+=1
	print(count*k)

Submission status: Wrong Answer

Solution code:
a = list(input())
b = int(input())
c = len(a)
sum = 0
sum1 = 1
sum2 = 0
for i in range(0,c-1):
  if a[i]==a[i+1]:
    a[i+1]=" "
    sum+=1
if a[0] == a[c-1] and a[c-2]!=a[c-1]:
  sum1=2
  sum2=1
print((sum*b*sum1)-sum2)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
m = 0
n = 0
j = 0
for i in range(len(s)):
    if s.count(s[0]) == len(s):
        print(len(s) * k // 2)
        exit()
    if len(s) <= j + 1:
        if s[0] == s[-1]:
            n += 1
        break
    if s[j] == s[j + 1]:
        if j == len(s) - 1:
            n += 1
        else:
            m += 1
        j += 1
    j += 1
print(m * k + n * (k - 1))

Submission status: Wrong Answer

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

def makerawlist(S):
    sigma = 0
    counter = 1
    for i in range(len(S)-1):
        if S[i]==S[i+1]:
            counter += 1
        else:
            sigma += counter//2
            counter = 1
    sigma += counter//2
    return sigma

sigma = makerawlist(S)
if sigma == len(S)//2 and (len(S)*K)%2 == 1:
    print((len(S)*K)//2)
elif sigma == len(S)//2 and (len(S)*K)%2 == 0:
    print(len(S)//2*K)
elif S[0]==S[len(S)-1] and S[0]!=S[1] and S[len(S)-2]!=S[len(S)-1]:
    print(sigma*K+K-1)
else:
    print(sigma*K)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys

debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        # for line in sys.stdin:
        #     lines.append(line)
        lines.append(input())
        lines.append(input())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]

    s = lines[0]
    k = int(lines[1])

    return (s, k)


def solve(s, k):

    if len(s) == 1:
       return k // 2

    target_1 = s[0]
    index_1 = 0
    for i in range(len(s)):
        if s[i] != target_1:
            index_1 = i
            break
    
    target_3 = s[-1]
    index_3 = len(s) - 1
    for i in range(len(s) - 1, -1, -1):
        if s[i] != target_3:
            index_3 = i
            break
    
    s_head = s[0:index_1]
    s_body = s[index_1:index_3 + 1]
    s_tail = s[index_3 + 1:]
    if debug:
        log("s_head=%s" % s_head)
        log("s_body=%s" % s_body)
        log("s_tail=%s" % s_tail)

    total_replace_count = 0

    last_c = None
    count = 1
    replace_count = 0
    for c in s_body:
        if c == last_c:
            count = count + 1    
        else:
            replace_count = replace_count + count // 2
            count = 1
        last_c = c
    replace_count = replace_count + count // 2

    total_replace_count = total_replace_count + replace_count * k
    if debug:
        log("total_replace_count=%d" % total_replace_count)

    if s[0] == s[-1]:
        s_h_t = s_head + s_tail
        total_replace_count = total_replace_count + len(s_h_t) // 2 * (k-1)
        if debug:
            log("total_replace_count=%d" % total_replace_count)
        total_replace_count = total_replace_count + len(s_head) // 2
        if debug:
            log("total_replace_count=%d" % total_replace_count)
        total_replace_count = total_replace_count + len(s_tail) // 2
        if debug:
            log("total_replace_count=%d" % total_replace_count)
    else: 
        total_replace_count = total_replace_count + len(s_head) // 2 * k
        if debug:
            log("total_replace_count=%d" % total_replace_count)
        total_replace_count = total_replace_count + len(s_tail) // 2 * k
        if debug:
            log("total_replace_count=%d" % total_replace_count)

    return total_replace_count


def main():
    # 出力
    result = solve(*parse_input())
    if isinstance(result, list):
        for r in result:
            print("%s" % r, sep='')
    else:
        print("%s" % result, sep='')

if __name__ == '__main__':

    main()

Submission status: Wrong Answer

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

count = 0 
f = False
for index, st in enumerate(S):
  try:
    if st == S[index + 1]:
      if f == True:
        f = False
      else:
      	count += 1
      	f = True
      
    else:
      f = False

  except:
    pass
  
if len(S) > 2:
	if S[-1] == S[-2]:
		if S[-2] == S[-3]:
			count = count * K + count - 1
		else:
			count = count * K
    	
	else:
  		count = count * K
        
elif len(S) == 1:
  count = K - 1
else:
  count = count * K 
 
print(count)

Submission status: Wrong Answer

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

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

    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)
    else:
        print(ans * K)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
s = [c for c in input()]
k = int(input())

import copy

if len(s) == 1:
    print(k-1)

elif len(s) == len(set(s)):
    print(0)
    
elif s[0] != s[-1]:
    c = 0
    for i in range(1, len(s)):
        if s[i-1] == s[i]:
            c = c + 1
            s[i] = 'Z'
    print(c*k)

else: # s[0]==s[-1]
    s2 = copy.copy(s)
    c = 0
    for i in range(1, len(s)):
        if s[i-1] == s[i]:
            c = c + 1
            s[i] = 'Z'
    if s[-1] == 'Z':
        print(c*k)
    else:
        c2 = 1
        s2[0] = 'Z'
        for i in range(1, len(s)):
            if s2[i-1] == s2[i]:
                s2[i] = 'Z'
                c2 = c2 + 1
        if s2[-1] == 'Z':
            if k % 2 == 0:
                print(int((c+c2)*(k/2)))
            else:
                print(int((c+c2)*((k-1)/2)+c))
        else:
            print(c + c2*(k-1))

Submission status: Wrong Answer

Solution code:
from itertools import groupby
s=input()
k=int(input())
l=len(s)
g=groupby(s)
a=[]
for i,j in g:
  a.append(len(list(j)))
if len(a)==1:
  x=len(s)*k
  if x%2==0:
    print(x//2)
  else:
    print((x-1)//2)
else:
  if s[0]==s[l-1]:
    a[0]=a[0]+a[len(a)-1]
    a=a[:len(a)-1]
  c=0
  for i in a:
    if i%2==0:
      t=i//2
    else:
      t=(i-1)//2
    c+=t
  print(c*k)

Submission status: Wrong Answer

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

    c = []
    
    i = 0
    if S[0] == S[-1]:
        connected = True
    else:
        connected = False

    while i < len(S):

        char = S[i]

        cnt = 1
        cnt2 = 1
        for j in range(i + 1, len(S) * 2):
            i = j
            if char == S[j % len(S)]:
                if j >= len(S):
                    cnt2 += 1
                else:
                    cnt += 1
                    cnt2 +=1
            else:
                if j-1 < len(S):
                    cnt2 = 0
                    i = j
                else:
                    i = len(S)

                    if len(c) >= 1:
                        c[0][1] = 0
                break
        
        if cnt >= 2:
            c.append([char, cnt, cnt2])
        else:
            c.append([char, 0, cnt2])
    
    #print(c)

    ans = 0
    flg = True
    for elem in c:
        if connected:
            if flg and len(c) >= 2:
                flg = False
                continue
            if len(c) == 1:
                ans = (elem[1] //2) * K
            elif elem[2] >= 2:
                ans += (elem[2] // 2) * (K - 1)  + (elem[1] // 2) + c[0][1] // 2
            else:
                ans += (elem[1] // 2) * K

        else:
            ans += (elem[1] // 2) * K
        
    print(ans)

if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

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

memc = ""
resnum = 0
t = s + s[0]
a = []
for c in t:
    if c == memc:
        resnum +=1
    else:
        a += [int((resnum+1)/2)]
        memc = c
        resnum = 0
a += [int((resnum+1)/2)]
res = sum(a) * k
if len(s) == 1:
    print(int(k/2))
else:
    print(res)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import math


def solve(*args: str) -> str:
    s = args[0]
    k = int(args[1])

    if all([s[0] == c for c in s]):
        return str(int(len(s) * k / 2))

    return str(
        min(count_adjacent_dup(s), count_adjacent_dup(s[::-1])) * k
    )


def count_adjacent_dup(s: str) -> int:
    ret = 0
    cnt = 0

    for i in range(len(s)):
        if s[i] == s[(i+1) % len(s)]:
            cnt += 1
        else:
            ret += math.ceil(cnt/2)
            cnt = 0

    if 0 < cnt:
        ret += math.ceil(cnt/2)

    return ret


if __name__ == "__main__":
    print(solve(*[input() for _ in range(2)]))

Submission status: Wrong Answer

Solution code:
from sys import stdin
S = (stdin.readline().rstrip())
K = int(stdin.readline().rstrip())
ans = 0
N=len(S)
cnt = 1
G = S+"@"
flag = False
for i in range(0,N):
    if G[i] == G[i+1]:
        cnt += 1
        flag = True
    else:
        if cnt != 1:
            #print(cnt)
            ans += (cnt//2)
            cnt = 1
        #print(ans,flag)
if flag != True:
  ans = 0
for_eq = 1
rev_eq = 1
revS = S[::-1]
for j in range(0,N-1):
    if S[j] == S[j+1]:
        for_eq += 1
    else:
        break
for k in range(0,N-1):
    if revS[k] == revS[k+1]:
        rev_eq += 1
    else:
        break
#print(ans)
ans = ans*K
#print(rev_eq,for_eq)
if K != 1:
    if S[0] == S[-1]:
        if (rev_eq % 2 != 0) and (for_eq % 2 != 0):
            ans += ((K+2)//2)
        """
        if rev_eq == 1 and for_eq != 1:
            ans -= (K//2)
        if rev_eq != 1 and for_eq == 1:
            ans -= (K//2)
        """
        if (flag != True) and K == 1:
            ans = 0
if len(set(S))==1:
    ans = (len(S)*K)//2

print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
d = '*'
c = 0
i = 0
st = s + [s[0]]
li = len(st)-1
while i < li:
    if st[i] == st[i+1]:
        st[i+1] = d
        c += 1
        i += 1
    i += 1
c *= k
if st[li] == d:
    c -= 1

print(c)

Submission status: Wrong Answer

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

count = 0
count2 = 0

if len(S)%2 == 1:
    for i in range(len(S)//2):
        if S[2*i+1] == S[2*i] or S[2*i+1] == S[2*i+2]:
            count +=1
else:
    for i in range(len(S)//2):
        if S[2*i] == S[2*i+1]:
            count +=1

if S[0] == S[len(S)-1]:
    count2+=1

if count < 1 and count2 == 1:
    print(count2)
else:
    print(count*K)

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):
    print(int((j+j%2)/2))
  else:
    print(i*j)
else:
  print(i*j)

Submission status: Wrong Answer

Solution code:
S_list = [s for s in input()]
K = int(input())
count = 0
if K > 1:
    S_list.append(S_list[0])
    for idx, s in enumerate(S_list):
        if idx == len(S_list) - 1:
            break
        if S_list[idx + 1] == s:
            count += 1
            S_list[idx + 1] = "*"
else:
    for idx, s in enumerate(S_list):
        if idx == len(S_list) - 1:
            break
        if S_list[idx + 1] == s:
            count += 1
            S_list[idx + 1] = "*"
print(count * K)

Submission status: Wrong Answer

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

count = 0 
f = False
for index, st in enumerate(S):
  try:
    if st == S[index + 1]:
      if f == True:
        f = False
      else:
      	count += 1
      	f = True
      
    else:
      f = False

  except:
    pass
  
if len(S) > 2:
	if S[-1] == S[-2]:
		if S[-2] == S[-3]:
			count = count * K + count - 1
		else:
			count = count * K
    	
	else:
  		count = count * K
        
elif len(S):
  count = K - 1
else:
  count = count * K 
 
print(count)