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:
# 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))

    H = s[0]

    ret = 0
    h = 1
    t = 1
    if H == s[-1]:
        while H == s[h] and h < len(s):
            h += 1

        while H == s[-t-1] and t < len(s):
            t += 1
        ret += int((h + t) / 2) * (k - 1) + int(h/2) + int(t/2)

    ret += count_adjacent_dup(s[h:len(s)-t]) * k

    return str(ret)


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

    for i in range(len(s) - 1):
        if s[i] == s[i+1]:
            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:
S = list(input())
K = int(input())
ans = 0
a = 0
if len(S) == S.count(S[0]):
  print(len(S)*K//2);exit()
elif len(S) == 2:
  print(0);exit()
if S[0] == S[-1]:
  i = 0
  j = 0
  k = 0
  while S[0] == S[k]:
    i += 1
    k += 1
  k = -1
  while S[-1] == S[k]:   
    j += 1
    k -= 1
  if i%2 == 1 and j%2 == 1:
    a = 1
  else:
    a = 0   

for i in range(0,len(S)-2,1):
  s = S[i:i+3]
  if s[0] == s[1] and s[1] == s[2]:
    ans += 1
    S[i+1] = "0"
  elif s[0] == s[1]:
    ans += 1
    S[i+1] = "0"
  elif s[1] == s[2]:
    ans += 1
    S[i+2] = "0"
if S[0] == S[-1]:
  print(ans*K+K-a)
else:
  print(ans*K)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
n = len(s)
l = []

t = set(list(s))
if n == 1:
    print(k // 2)
    exit()
    
if len(t) == 1:
    if k % 2:
        print(k // 2 * n + n // 2)
    else:
        print(n // 2 * k)
    exit()

x = 1
for i in range(n):
    if i == n-1:
        if s[i] == s[0]:
            l.append(x)
        continue
    
    if s[i] == s[i+1]:
        x += 1
    else:
        l.append(x)
        x = 1

ans = 0
for i in l:
    ans += i // 2
ans *= k
if l[0] % 2 and l[-1] % 2 and s[0] == s[-1]:
    ans += k-1

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,S[-1]

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:
  if k%2==0:
    count1,last=c1(s)
    count21,last21=c2(s,last)
    count22,last22=c2(s,last21)
    count3=c3(s,last22)
    #print(count1,count2,count3)
    print(count1+count21*((k-2)//2)+count22*((k-2)//2)+count3)
  else:
    count1,last=c1(s)
    count21,last21=c2(s,last)
    if k>3:
      count22,last22=c2(s,last21)
    else:
      count22=0
    count3=c3(s,last21)
    #print(count1,count21,count22,count3)
    print(count1+count21*((k-2)//2+1)+count22*((k-2)//2)+count3)

Submission status: Wrong Answer

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

while s[0] ==s[i] and i!=len(s)-1:
	first += 1
	i = i + 1

i = len(s)-1
while s[i] ==s[len(s)-1] and i != 0:
	end += 1
	i = i - 1


i =0
while i < len(s)-2:
	if s[i] == s[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
	if s[0] == s[len(s)-1]:
		if (first + end) %2 ==0:
			count += 1
print(count*k)

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 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
  if S[1]=="!":
    print(c+(K-1)*cc+K-1)
  else:
    print(c*K+K-1)

Submission status: Wrong Answer

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

cnt = 0
i = 0

while i < len(S)-1:
    if S[i] == S[i+1]:
        cnt += 1
        i += 2

    else:
        i += 1

print(cnt*K)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**9)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

s = ns()
k = ni()

if len(s) == 1:
	print(k//2)
	exit()

for i in range(len(s)-1):
	if s[i] != s[i+1]:
		break
else:
	print((len(s)*k)//2)
	exit()

n = 0
m = 0
h = 0
p = False
for i in range(len(s)):
	if i == 0:
		if s[i] == s[-1] and s[-1] != s[-2]:
			n = 1
			if s[0] == s[1]:
				m -= 1
				h += 1
	elif not p and s[i] == s[i-1]:
		m += 1
		p = True
	else:
		p = False

print(m*k+n*(k-1)+h)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
N = len(S)
cnt = 0
flg1 = False
flg2 = False
flg3 = False
flg4 = False
if N == 2:
    if S[0] == S[1]:
        print(K+1)
        exit()
for i in range(N-1):
    if S[i] != S[i+1]:
        flg3 = True
        break
if (flg3 == False) and N % 2 == 1:
    print((N*K)//2)
    exit()
if (S[0] == S[N-1]) and K != 1:
    if S[0] != S[1]:
        cnt += 1
        flg1 = True
if (S[N-2] == S[N-1]):
    flg4 = True
for i in range(N-1):
    if flg2:
        flg2 = False
        continue
    if (i == N - 2) and flg1:
        break
    if S[i] == S[i+1]:
        cnt += 1
        flg2 = True
if flg1 and flg4 == False:
    print(cnt*K-1)
else:
    print(cnt*K)

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)
        if rest and s[0] == prev:
            res += (d - 2)
        print(res)

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 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
  if S[1]=="!":
    print(c+(K-1)*cc+K-1)
  else:
    pass

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
cnt = 0
ss = s
for i in range(len(ss)):
    if i == 0:
        continue
    
    if ss[i-1] == ss[i]:
        ss = ss[:i] + '\\' + ss[i+1:]
        cnt += 1

cnt = cnt * k

if len(s) >= 3 and k >= 2 and s[0] == s[1] and s[0] == s[2]:
    cnt += k -1

if len(s) >=3:
    if len(s) % 2 == 1:
        cnt = cnt -((k + 1) // 2 - 1)

if len(s) >=3:
    if s[0] == s[len(s)-1] and s[0] != s[1] and  s[len(s)-1] != s[len(s)-2]:
        cnt = cnt + (k - 1)
print(cnt)

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]) & (K > 1):
        S += S
        i = c = 0
        while i + 1 < len(S):
            if S[i] == S[i + 1]:
              c += 1
              i += 1
            i += 1
        print(c, C)
        if c == C * 2 + 1:
            C = C * K + K - 1
        else:
            C *= K
    else:
        C *= K

print(C)

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 = cou(s)

if k > 1:
    if s[0] == s[-1]:
        lin[-1] += lin[0]

point = 0
point2 = 0
point3 = 0

for i in range(1,len(lin)):
    point3 += lin[i]//2

for w in range(1,len(lin2)):
    point2 += lin2[w]//2

point += (point3*(k-1))
point += point2
point += lin2[0]//2

print(point)

Submission status: Wrong Answer

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


def count(s):
    s_length = len(s)
    s_types = len(set(s))
    if s_length == s_types:
        return 0
    if s_length == 2:
        if s_types == 1:
            return 1
        else:
            return 0
    else:
        i = 0
        replace = 0
        while i < s_length:
            chunk = s[i:i + 3]
            chunk_types = len(set(chunk))
            if len(chunk) == 1:
                break
            elif len(chunk) == 2:
                if chunk_types == 1:
                    replace += 1
                    break
                else:
                    break
            else:
                if chunk_types == 1:
                    replace += 1
                    i += 2
                elif chunk_types == 2:
                    if chunk[0] == chunk[1]:
                        replace += 1
                        i += 2
                    elif chunk[1] == chunk[2]:
                        replace += 1
                        i += 3
                    else:
                        i += 2
                else:
                    i += 2

    return replace



def check(s, k):
    s_length = len(s)
    s_types = len(set(s))
    if s_types == 1:
        return s_length * k // 2
    elif s_length == 2:
        if s[0] == s[1]:
            return k
        else:
            return 0
    elif s[0] == s[-1] and s[-1] != s[-2] and s[0] != s[1]:
        return count(s) * k + (k - 1)
    else:
        return count(s) * k


print(check(S, K))

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)
        if rest and s[0] == prev:
            res += (d - 1)
        print(res)

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 == 1:
        ans = 0
    else:
        ans = K//2
else:
    ans = n*K

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]) & (K > 1):
        S += S
        i = c = 0
        while i + 1 < len(S):
            if S[i] == S[i + 1]:
              c += 1
              i += 1
            i += 1
        print(c, C)
        if c == C * 2 + 1:
            C = C * K + K - 1
    else:
        C *= K

print(C)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
cnt = 0
ss = s
for i in range(len(ss)):
    if i == 0:
        continue
    
    if ss[i-1] == ss[i]:
        ss = ss[:i] + '\\' + ss[i+1:]
        cnt += 1

cnt = cnt * k

if len(s) >= 3 and k >= 2 and s[0] == s[1] and s[0] == s[2]:
    cnt += k -1

# if len(s) >=3:
#     if len(s) % 2 == 1:
#         cnt = cnt -((k + 1) // 2 - 1)

if len(s) >=3:
    if s[0] == s[len(s)-1] and s[0] != s[1] and  s[len(s)-1] != s[len(s)-2]:
        cnt = cnt + (k - 1)
print(cnt)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
count = 0
i = 0
if k % 2 == 0:
    s *= 2
    k /= 2
else:
    s *= 3
    k /= 3

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(int(count * k))

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 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:
S=input()
K=int(input())
T=S+S
if len(S)!=1:
  A=[]
  D=1
  for i in range(len(S)-1):
    if S[i]==S[i+1]:
      D+=1
    else:
      A.append(D)
      D=1
  A.append(D)
  B=[i for i in A if not i==1]
  C=[(i//2) for i in B]
  Scount=(sum(C))

  E=[]
  H=1
  for j in range(len(T)-1):
    if T[j]==T[j+1]:
      H+=1
    else:
      E.append(H)
      H=1
  E.append(H)
  F=[j for j in E if not j==1]
  G=[(j//2) for j in F]
  Tcount=(sum(G))
  ans=(Tcount-Scount)*(K-1)+Scount
  print(ans)
else:
  print(K//2)

Submission status: Wrong Answer

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

sl = list(s)
cnt = 1
i = 0
su = 0
tmp = "0"
f = 0
ff =0
fst =0
lst = 0

for i in range(len(s)):
  if sl[i] == tmp:
    cnt += 1
  else:
    su += int(cnt / 2)
    if ff == 1:
      fst = cnt
    ff += 1
    cnt = 1
    tmp = sl[i]

if cnt != 1:
  su += int(cnt / 2)
  lst = cnt

if sl[0] == sl[-1]:
  if (fst == 0) or (fst % 2 != 0):
    if (lst == 0) or (lst % 2 != 0):
      if (fst == 1) or (lst == 1):
        f = k - 1
      else:
        f = -(-k // 2)

t = k * su + f

if len(s) == 1:
  ath = k / 2
  print(int(ath))
else:  
  print(int(t))

Submission status: Wrong Answer

Solution code:
s=str(input())
k=int(input())
t=str()
cnt=0
a=0

for i in range(len(s)):
  if s[i-1] == s[i]:
    cnt+=1
if s[0] == s[-1]:
  a=k-1
if s[0] != s[-1]:
  a=0

print(cnt*k+a)

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
if len(S) % 2 != 0 and len(S) > 3:
    if S[0] == S[1] == S[-1] == S[-2]:
        flg = 0

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 == 1:
    ans += K // 2


print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
cnt = 0
ss = s
for i in range(len(ss)):
    if i == 0:
        continue
    
    if ss[i-1] == ss[i]:
        ss = ss[:i] + '\\' + ss[i+1:]
        cnt += 1

cnt = cnt * k

print(cnt)
if len(s) >= 3 and k >= 2 and s[0] == s[1] and s[0] == s[2]:
    cnt += k -1
print(cnt)

# if len(s) >=3:
#     if len(s) % 2 == 1:
#         cnt = cnt -((k + 1) // 2 - 1)

if len(s) >=3:
    if s[0] == s[len(s)-1] and s[0] != s[1] and  s[len(s)-1] != s[len(s)-2]:
        cnt = cnt + (k - 1)
print(cnt)

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)
        if rest and s[0] == prev:
            res += d
        print(res)

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.mod(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:
slist=list(input())
T=input()

i = 0
j = 1
newlist=[]

if len(slist)==1:
  print(int(T)//2)
  exit()

while i < len(slist)-1:
    if slist[i] == slist[i+1]:
      while True:
        if i == len(slist)-1:
          
          newlist.append(j)
          break
        elif slist[i] == slist[i+1]:
          j = j+1
          i = i+1
        else:
          newlist.append(j)
          i = i+1
          j = 1
          break
    else:
      newlist.append(1)
      i = i+1


if len(newlist)==1:
  if slist[0]!=slist[-1] :
    print(0)
  else:
    print(newlist[0]*int(T)//2)
  
elif slist[0] == slist[-1]:
  start = newlist[0]
  end = newlist[-1]
  del newlist[0]
  del newlist[-1]
  newlist = [i for i in newlist if i >= 2]
  newlist = list(map(lambda x: x // 2 , newlist))
  alpha = (start + end)//2
  ans = sum(newlist)*int(T) + alpha*(int(T)-1) + start//2 + end//2
  print(newlist)
  print(ans)
  
else:
  del newlist[0]
  del newlist[-1]
  newlist = [i for i in newlist if i >= 2]
  newlist = list(map(lambda x: x // 2 , newlist))
  ans = sum(newlist)*int(T)
  print(ans)

Submission status: Wrong Answer

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

#S = 'qq'
#K = 2

K = int(K)
ans = 0
S = [c for c in S]

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

cnt = 1
for i in range(len(S)-1):
    if S[i] == S[i+1]:
        if i+1 == len(S)-1:
            ans += cnt//2 * K
        else:
            cnt += 1
    else:
        ans += cnt//2 * K
        cnt = 1

print(ans)

Submission status: Wrong Answer

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

count = 0
S = S + "0"

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

S = S[:-1] #最後の0を削除

if len(S) == 1:
    result = K
elif len(S) == 2:
    result = count * K
else: #S[0]からの連続文字列数とS[-1]からの連続文字列数がどちらも奇数ならば「+ 1 × (K-1)」が必要
    if S[0] == S[-1]:
        len_first = 1
        len_last = 1
        i = 0
        flag = 0
        while S[i] == S[i+1]:
            len_first += 1
            i += 1
            if i == len(S)-1:
                flag = 1
                len_last = len_first
                break

        if flag == 0: 
            len_last = 1
            i = -1
            while S[i] == S[i-1]:
                len_last += 1
                i -= 1
        
        if len_first % 2 == 1 and len_last % 2 == 1:
            result = count * K + (K-1)
        else:
            result = count * K

              
        """
        if S[0] != S[1] and S[-1] != S[-2]:
            result = count * K + (K-1)
            #print("if1")
        elif S[0] == S[1] == S[2] and S[-1] != S[-2]: 
            result = count * K + (K-1)
            #print("if2")
        elif S[-1] == S[-2] == S[-3] and S[0] != S[1]: 
            result = count * K + (K-1)
            #print("if3")    
        else:
            result = count * K
            #print("else1")
        """

    else:
        result = count * K
        #print("else2")


print(result)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
d=[[0 for _ in range(400)] for _ in range(4)]
ans = [0,0,0,0]
l = len(S)
odd_diff = 0
even_diff = 0
if l == 1:
    ans = K // 2
    print(ans)
if l > 1:
    for i in range(4):
        s = S * (i + 1)
        l = len(s)
        for j in range(l-1):
            
            if j == 0 and s[j] == s[j+1]:
                ans[i] += 1
                d[i][0] = 1
            if j > 0 and s[j] == s[j+1] and d[i][j-1] == 0:
                ans[i] += 1
                d[i][j] = 1
    if K <= 4:
        print(ans[K-1])
    elif K > 4 and K % 2 == 0:
        even_diff = ans[3] - ans[1]
        print(int(ans[1]+(K/2)*even_diff))
    elif K > 4 and K % 2 == 1:
        odd_diff = ans[2] - ans[0]
        print(int(ans[0]+(K//2)*odd_diff))

Submission status: Wrong Answer

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

s = input().rstrip()
k = int(input())

def check_duplicate(s):
    pre_s = None
    cnt_list = []
    cnt = 1
    for _s in s:
        if pre_s == _s:
            cnt += 1
        else:
            if cnt>1:
                cnt_list.append(cnt)
            cnt = 1
        # print(pre_s, _s, cnt, cnt_list)
        pre_s = _s
    if cnt>1:
        cnt_list.append(cnt)
    if len(cnt_list)==0:
        cnt_list.append(cnt)
    return cnt_list

def calc_ans(cnt_list, k):
    _ans = 0
    for c in cnt_list:
        _ans += c//2
    return(_ans * k)


if len(s)==1:
    ans = k//2
else:
    cnt_list = check_duplicate(s)
    if len(cnt_list)>1:
        ans = calc_ans(cnt_list, k)
    else:
        if cnt_list[0]%2==0:
            ans = cnt_list[0]//2 * k
        elif cnt_list[0]%2==1 and cnt_list[0]>1:
            ans = cnt_list[0] * k //2
        else:
            if s[0]==s[-1]:
                ans = k - 1
            else:
                ans = 0
print(ans)

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

    H = s[0]

    ret = 0
    h = 1
    t = 1
    if H == s[-1]:
        while H == s[h] and h < len(s):
            h += 1

        while H == s[-t-1] and t < len(s):
            t += 1
        ret += math.ceil((h + t) / 2) * (k - 1)

    ret += count_adjacent_dup(s[h:len(s)-t]) * k

    return str(ret)


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

    for i in range(len(s) - 1):
        if s[i] == s[i+1]:
            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:
import math

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 len(s) == 1:
    cnt += math.floor(k / 2)
  elif s[0] == s[-1]:
    cnt += k - 1
    
print(cnt)