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:
S = list(str(input()))
N = len(S)
K = int(input())

ans1 = 0
old = ""
flag = 0
for i in range(N):
    if old == "":
        old = S[i]
    else:
        if old == S[i]:
            old = ""
            ans1+=1
        else:
            old = S[i]
            
    if i == N-1:
        if old == S[i]:
            flag = 1

if flag == 0:
    print(ans1*K)
else:
    ans2 = 0
    old = ""
    for i in range(N):
        if i == 0:
            if S[0] == S[-1]:
                ans2+=1
        elif old == "":
            old = S[i]
        else:
            if old == S[i]:
                old = ""
                ans2+=1
            else:
                old = S[i]
    if i == N-1:
        if old == S[i] and S[0] == S[-1]:
            flag = 2
    
    k = K//2
    if K%2 == 0:
        print((ans1+ans2)*k)
    else:
        if flag == 2:
            print(ans1*(k)+ans2*(K-k))
        else:
            print(ans1*(K-k)+ans2*(k))

Submission status: Wrong Answer

Solution code:
import string


def count_changes(s, k, pool, prev=None):
    count = 0
    i = 0
    for _ in range(k):
        for cur in s:
            if cur == prev:
                count += 1
                prev = pool[i]
                i = (i + 1) % length
            else:
                prev = cur
    return count, prev


if __name__ == '__main__':
    s = input()
    k = int(input())
    pool = list(set(string.ascii_lowercase) - set(s))
    length = len(pool)
    if k <= 2:
        print(count_changes(s, k, pool)[0])
    else:
        base_count, prev = count_changes(s, 3, pool)
        d, rest = divmod(k, 3)
        if rest:
            print(count_changes(s, k, pool)[0])
        else:
            print(base_count * d)

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

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
ans_s2, ans_s3 = 0, 0

S_double = S + S
for i in range(1, len(S_double)):
    if S_double[i] == S_double[i - 1]:
        S_double = S_double[: i] + "?" + S_double[i + 1:]
        ans_s2 += 1

S_triple = S + S + S
for j in range(1, len(S_triple)):
    if S_triple[j] == S_triple[j - 1]:
        S_triple = S_triple[: j] + "?" + S_triple[j + 1:]
        ans_s3 += 1

ans_increase = ans_s3 - ans_s2  #K>=3のときの増分

ans = ans_s2 + max(0, ans_increase * (K - 2))
print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
s1=s
k=int(input())
ans1=0
ans2=0
for i in range(len(s)-1):
  if s1[i]==s1[i+1]:
    s1=s1[:(i+1)]+'0'+s1[(i+2):]
    ans1+=1
if s1[0]==s1[-1]:
  s2='0'+s[1:]
  ans2+=1
  k-=1
  for i in range(len(s)-1):
    if s2[i]==s2[i+1]:
      s2=s2[:(i+1)]+'0'+s1[(i+2):]
      ans2+=1 
  ans=(ans1+ans2)*(k//2)+ans1
else:
  ans=ans1*k
print(ans)

Submission status: Wrong Answer

Solution code:
word_line=input().rstrip()
#print(word_line)
numK=int(input())

char_list=list(word_line)
#print(char_list)

if(char_list[0]!=char_list[len(char_list)-1]):
    s=0
    for i in range(1,len(char_list)-1):
       if(char_list[i-1]==char_list[i]):
           char_list[i]="wa"
           s+=1
    s=s*numK    

else:
    allsame=0
    for i in range(len(char_list)-1):
        if(char_list[i]==char_list[i+1]):
            allsame+=1
    if(allsame==len(char_list)-1)or(len(char_list)==1):
        s=(len(char_list)*numK)//2
    
    else:
        count_a=1
        i=0
        while (char_list[i]==char_list[i+1])and(i<len(char_list)-2):
            count_a+=1
            i+=1
        
        count_b=1
        i=1
        while(char_list[len(char_list)-i]==char_list[len(char_list)-i-1])and(i<len(char_list)-2):
            count_b+=1
            i+=1
            
        #print(count_a)
        #print(count_b)
        
        s=0
        #print(len(char_list)-count_b-1)
        #for i in range(2,3):
        #    print("aaa")
        for i in range(count_a+1,len(char_list)-count_b):
            if(char_list[i-1]==char_list[i]):
                char_list[i]="wa"
                s+=1
        
        #print(s)
        s=s*numK
        #print(s)
        s+=(count_a//2+count_b//2+(count_a+count_b)//2*(numK-1))
    
print(s)


#print(s*numK)

Submission status: Wrong Answer

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

cnt = 0
p = ''
for i, s in enumerate(S[1:]):
    if p == s:
        cnt += 1
        p = ''
    else:
        p = s
cnt *= K
if p == S[0]:
    cnt += K - 1
print(cnt)

Submission status: Wrong Answer

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

cnt=0
tmp=[]
flg=0

for i in range(len(s)-1):
    if s[0]==s[-1] and s[-1]!=s[-2] and (k>=2):
        flg=1

    if s[i] == s[i+1]:
        tmp.append(s[i])
    elif(len(tmp) >= 1):
        cnt += len(tmp)+1
        tmp =[]

    if(i == len(s)-2):
        if(len(tmp) >= 1):
            cnt += len(tmp)+1
            tmp =[]

if(flg==1):
    print(cnt//2*k+1)
else:
    print(cnt//2*k)

Submission status: Wrong Answer

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


# for s in S:
#     if s.isupper():
#         exit()
#     try:
#         float(s)
#     except ValueError:
#         pass
#     else:
#         exit()
# try:
#     float(K)
# except ValueError:
#     exit()
# else:
#     if not float(K).is_integer():
#         exit()

# if not (1 <= len(S) and len(S) <= 100):
#     exit()
# if not (1 <= int(K) and int(K) <= 10**9):
#     exit()
pre = ""
count = 0
first = S[0]
flag = False
for i in range(len(S)):
    if i == (len(S) - 1):
        if pre != S[i] and pre != "" and first == S[i]:
            flag = True
    if pre != S[i]:
        pre = S[i]

    elif pre == S[i]:
        count += 1
        pre = ""


count *= int(K)
if flag:
    count = count + 1*int(K) -1
print(count)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(int(1e6))

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

count = 0
s1 = ''
s2 = ''
s3 = ''
prev_rewrite = False
for s2 in S:
    if s1 == s2 and not prev_rewrite:
        code = ord(s2) + 1
        s3 = chr(code) if code <= ord('z') else 'a'
        count += 1
        prev_rewrite = True
    else:
        s3 = s2
        prev_rewrite = False

    s1 = s3

ans = count * K

if s1 == S[0] and not prev_rewrite:
    if len(S) == 1:
        ans += K // 2
    else:
        ans += K - 1

print(ans)

Submission status: Wrong Answer

Solution code:
import math

S = input()
K = int(input())
cnt = 0
tmp = 1
if K == 1:
    for i in range(1, len(S)):
        if S[i - 1] == S[i]:
            tmp += 1
        elif tmp >= 2:
            cnt += tmp // 2
            tmp = 1
        else:
            tmp = 1
    if tmp >= 2:
        cnt += tmp // 2
else:
    g1 = 0
    g2 = 0
    jlst = S + S
    for i in range(1, len(S) * 2):
        if jlst[i - 1] == jlst[i]:
            tmp += 1
        elif tmp >= 2:
            cnt += tmp // 2
            tmp = 1
        else:
            tmp = 1

    if tmp >= 2:
        cnt += tmp // 2

    if cnt % 2 == 0:
        cnt = K * (cnt // 2)
    else:
        g1 = cnt // 2
        g2 = cnt - g1
        v = math.ceil(K / 2)
        cnt = (g1 * v) + (g2 * (K - v))

print(cnt)

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

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

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

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

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

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
n = len(s)
p = ['']*(4*n)

for i in range(n):
    p[i] = s[i]
    p[i+n] = s[i]
    p[i+2*n] = s[i]
    p[i+3*n] = s[i]

a = [0,0,0,0]

judge = 0
for i in range(1,4*n):
    if p[i] == p[i-1]:
        a[i//n] += 1
        p[i] = ''
        if i == n:
            judge = 1
        if judge == 1 and i == 2*n:
            judge = 0

if judge == 0:
    print((k//4)*sum(a) + sum(a[:k%4]))
else:
    if k == 1:
        print(a[0])
    else:
        print(a[0]+a[1]*(k-1))

Submission status: Wrong Answer

Solution code:
from itertools import groupby
s = input()
k = int(input())
l = [[k, len(list(g))] for k, g in groupby(s)]
ans = 0
ans2 = 0
for i in range(len(l)):
  if l[i][1] > 1:
    ans += l[i][1] // 2
if len(l) > 1:
  if l[0][0] == l[-1][0]:
    if l[0][1] % 2 == 1 and  l[-1][1] % 2 == 1 :
        ans2 = k - 1
elif len(l) == 1:
  if l[0][1] % 2 == 1:
     ans2 = k - 1
if len(s) == 1:
  print(k // 2)
else:
  print(ans * k + ans2)

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
ans=0
cnt=1
add=0
if len(s)==1:
  print(k//2)
  exit()
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 s[0] != s[1] and i == (len(s)-1) and cnt%2==1:
    ans+=cnt//2
    add=1*(k-1)
  elif i == (len(s)-1) and cnt>=2:
    ans+=cnt//2
print(ans*k+add)

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 S[0] == S[-1]:
    if (rev_eq % 2 != 0) and (for_eq % 2 != 0):
        ans += ((K+1)//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())

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()
      
print(tmp+(ans-tmp)*(k-1))

Submission status: Wrong Answer

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

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

before = ""
cnt = 0
h = S[0]
for s in S:
    if before == s:
        cnt+=1
        before = ""
    else:
        before = s
cnt *= K
j = 1
for s in S[1:]:
    if s == h:
        j+=1
    else:
        break
if len(S) == j:
    for s in reversed(S[1:]):
        if s == h:
            j+=1
        else:
            break
if j%2 == 1:
    cnt+= K-1


print(cnt)

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

Submission status: Wrong Answer

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



counter = 0
ans_L = []

s = [i for i in j]
s.append("0")
renzoku = 1

for val in range(0,len(s)-1):
    tmp = s[val+1]
    if s[val] == tmp:
        renzoku += 1
    elif renzoku > 1 and s[val] != tmp:
        ans_L.append(renzoku//2)
        renzoku = 1



# 繋がっている場合
# kbk
renzoku = 0
if s[0] == [-2]:
    for val in range(len(s)+1):
        if s[val+1] == s[0]:
            renzoku += 1
        else:
            break

print(sum(ans_L)*k + renzoku%2)

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
count=1
ans=0
old='old'
for x in S:
    if old==x:
        count+=1
    else:
        ans+=count//2
        count=1
    old=x

ans+=count//2
check1=0
check1+=ans

ans-=count//2

for x in S:
    if old==x:
        count+=1
    else:
        ans+=count//2
        count=1
#     print(count)
    old=x

ans+=count//2
check2=0
check2+=ans

ans-=count//2

for x in S:
    if old==x:
        count+=1
    else:
        ans+=count//2
        count=1
#     print(count)
    old=x

ans+=count//2

if K==1:
    print(check1)
elif K==2:
    print(check2)
elif K==3:
    print(ans)
else:
    if (check2-check1)==(ans-check2):
        print((check2-check1)*(K-1)+check1)
    else:
        print((check2-check1)*(K//2+K%2)+ (ans-check2)*(K//2) )

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(int(1e6))

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

count = 0
s1 = ''
s2 = ''
s3 = ''
prev_rewrite = False
for s2 in S:
    if s1 == s2 and not prev_rewrite:
        code = ord(s2) + 1
        s3 = chr(code) if code <= ord('z') else 'a'
        count += 1
        prev_rewrite = True
    else:
        s3 = s2
        prev_rewrite = False

    s1 = s3

ans = count * K

if s1 == S[0] and not prev_rewrite:
    if K == 1:
        ans += K // 2
    else:
        ans += K - 1

print(ans)

Submission status: Wrong Answer

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

def change_a(x):
    lx = list(x)
    k = 0
    for i in range(len(x)-1):
        if lx[i] == lx[i+1]:
            k += 1
            lx[i+1] = "@"
    return k, "".join(lx)

def change_b(x):
    lx = list(x)
    k = 0
    for i in range(1,len(x)-1):
        if lx[i] == lx[i-1]:
            k += 1
            lx[i-1] = "@"
        if lx[i] == lx[i+1]:
            k += 1
            lx[i+1] = "@"
    return k, "".join(lx)

ca, castr = change_a(s)
cb, cbstr = change_b(s)
a, b = (k+1)//2, k//2


if castr[-1] != castr[0]:
    print(ca*k)
else:
    if cbstr[-1] != castr[0]:
        print(ca*a+cb*b)
    else:
        print(ca+cb*(k-1))

Submission status: Wrong Answer

Solution code:
a=input()
b=int(input())
s="A"
zn=0
n=0
nn=0
i=0
aa=[]
ab=[]

for x in a:
    if aa==[]:
        n+=1
    elif x==s:
        n+=1
    elif x!=s:
        ab.append(n)
        n=1
    s=x
    i+=1
    aa.append(x)
    if i==len(a):
        ab.append(n)
if aa[0]==aa[-1] and ab[-1]==1:
    s=a[0]
    i=0
    for x in aa:
        if a[i]==s:
            nn+=1
        else:
            break
        i+=1
    i=1
    for x in aa:
        if a[-i]==s:
            nn+=1
        else:
            break
        i+=1
for x in ab:
    zn+=(x//2)
if len(a)>1:
    print(zn*b+(nn//2)*(b-1))
else:
    print(b//2)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
lists=[]
flag=False
for i in range(len(s)):
    lists.append(s[i]) 
if len(s)>=2 and len(set(lists))>=2:
    lists.append("good")
    sums=0
    cnt=1
    ind=[]
    for j in range(len(s)):
        if lists[j]!=lists[j+1]:
            if cnt==1:
                cnt=1
            elif cnt>=2:
                ind.append(cnt)
                sums+=(cnt//2)
                cnt=1
                cnt=1
        elif lists[j]==lists[j+1]:
            cnt+=1  
    del lists[-1]
    if lists[-1]==lists[-2] and ind[-1]%2==0:

        pass
         
    if lists[-1]==lists[-2] and ind[-1]%2!=0:

        if lists[-1]==lists[0]:
            sums+=1
            flag=True
        else:
            pass
    if lists[-1]!=lists[-1]:
        if lists[-1]==lists[0]:
            sums+=1
        else:
            pass
    if flag:
        print(sums*k-1)
    else:
        print(sums*k)
 
elif len(s)==1:
        print(k//2)

elif len(set(lists))==1:
    print(k*len(lists)//2)

Submission status: Wrong Answer

Solution code:
import numpy as np
S = input()
K = int(input())
unique = list()
for x in S:
    if x not in unique:
        unique.append(x)
res = 0
if len(unique) == 1:
    res = int(len(S)*K/2)
    S = ""
if len(S) == 2:
    if S[0] == S[-1]:
        res += K
if len(S) > 2:
    S += S[0]
    n = 0
    while n <= len(S)-3:
        if S[n] == S[n+1]:
            res += K
            n += 1
        n += 1
    if S[len(S)-2] == S[len(S)-1]:
        res += K-1
print(res)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
cnt = 0
i = 0
c0 = 0
c1 = 0
key = 0
endkey = 0
while i < len(s) - 1:
    if s[i] == s[i + 1]:
        start = i
        end = i
        for j in range(i + 1, len(s)):
            end = j
            if s[j] != s[i]:
                break

        if end == len(s) - 1: #もし最後の一文字が連続に含まれるとき
            if (end - start) % 2 == 0 and s[-1] == s[0]: #最後の一文字が連続でかつ最初の文字と同じでかつ奇数の時
                end += 1
                endkey = 1
            elif s[-1] == s[0]:
                endkey = 2
            end += 1

        if start == 0 and end == len(s) + 1 and len(s) % 2 == 1:
            key = 1
            cnt += (end - start - 1) * (k // 2)
            if k % 2 == 1:
                cnt += (end - start - 1) // 2
        else:
            cnt += (end - start) // 2 * k
        i = end - 1
    i += 1
if len(s) == 1:
    cnt += k // 2
elif not endkey and not key and s[0] == s[-1]:
    cnt += k - 1
if endkey == 1 and not key:
    cnt -= 1
print(cnt)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
count1 = 0
count2 = 1
f = s[0]
memo = [0]
for c1, c2 in zip(s[:], s[1:]):
    if not memo[-1] and c1 == c2:
        count1 += 1
        memo.append(1)
    else:
        memo.append(0)
s[0] = "#"
memo2 = [0]
for c1, c2 in zip(s[:], s[1:]):
    if not memo2[-1] and c1 == c2:
        count2 += 1
        memo2.append(1)
    else:
        memo2.append(0)
if f == s[-1]:
    if memo[-1]:
        print(k * count1)
    else:
        if memo2[-1]:
            print(count1 * (k - k // 2) + count2 * (k // 2))
        else:
            print(count1 + count2 * (k-1))
else:
    print(k * count1)

Submission status: Wrong Answer

Solution code:
S=list(input())
K=int(input())
c=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:
  if S[0]==S[1]:
    print(c*K)
  else:
    print(c*K+K-1)

Submission status: Wrong Answer

Solution code:
import math
S=list(input())
K=int(input())
count = 0
ans = 0
flag = False
firstCount = 0
firstChart = ""
for i in range(1,len(S)):
  if S[i-1] == S[i]:
    count += 1
  else:
    if flag == False:
      firstCount = count
      firstChart = S[i-1]
      ans += math.ceil(count/2)
    else:
      ans += math.ceil(count/2)*K
    flag = True
    count = 0
ans += math.ceil(count/2)
if flag:
  if firstChart == S[len(S)-1]:
    ans += (firstCount + count + 2) // 2 * (K-1)

else:
  ans += math.ceil(count/2)*(K-1) 
if len(S) == 1:
  if K == 1:
    print("0")
  else:
    print(math.ceil(K/2))
else:
  print(ans)

Submission status: Wrong Answer

Solution code:
import sys
s=input()
k=int(input())
count=1
answer=0
lastChange=False
roopContinue=False
lastWord=""
for i in range(len(s)):
  if i==len(s)-1:
    print(len(s)*k//2)
    sys.exit()
  elif s[i+1]==s[0]:
    lastWord+=s[0]
  else:
    break
s+=lastWord
for i in range(len(s)-1):
  if s[i]==s[i+1]:
    count+=1
    if i==len(s)-2:
      answer+=(count//2)
  else:
    answer+=(count//2)
    count=1
    if i==len(s)-2:
      answer+=(count//2)
print(answer*k)

Submission status: Wrong Answer

Solution code:
import math

S = input()
K = int(input())
cnt = 0
tmp = 1
if K == 1:
    for i in range(1, len(S)):
        if S[i - 1] == S[i]:
            tmp += 1
        elif tmp >= 2:
            cnt += tmp // 2
            tmp = 1
        else:
            tmp = 1
else:
    g1 = 0
    g2 = 0
    jlst = S + S
    for i in range(1, len(S) * 2):
        if jlst[i - 1] == jlst[i]:
            tmp += 1
        elif tmp >= 2:
            cnt += tmp // 2
            tmp = 1
        else:
            tmp = 1

    if tmp >= 2:
        cnt += tmp // 2

    if cnt % 2 == 0:
        cnt = K * (cnt // 2)
    else:
        g1 = cnt // 2
        g2 = cnt - g1
        v = math.ceil(K / 2)
        cnt = (g1 * v) + (g2 * (K - v))

print(cnt)

Submission status: Wrong Answer

Solution code:
t=list(input())
k=int(input())
count=0
for i in range(len(t)-1):
    if t[i]==t[i+1]:
        t[i+1]='*'
        count+=1
count*=k
if t[0]==t[len(t)-1]:count+=k-1
print(count)

Submission status: Wrong Answer

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

itr = 0
cnt = 1
len_cnt = []
for x in S :
  if itr == 0 :
    pre = x
    itr += 1
    continue
  now = x
  if pre == now :
    cnt += 1
    itr += 1
    if itr == len(S) :
      len_cnt.append(cnt)
    continue
  if pre != now :
    len_cnt.append(cnt)
    itr+=1
    if itr == len(S) and cnt == 1 :
      len_cnt.append(cnt)
    cnt = 1
    pre = x
    continue

if sum(len_cnt) != len(S) :
  len_cnt.append(1)

ans = 0
for x in len_cnt :
  if x == 1 :
    continue
  else :
    ans += int(x / 2)

if S[0] == S[-1] and len_cnt[-1] > 1 and len_cnt[-1] % 2 == 1 and K > 1:
  ans += 1

ANS = K * ans
if len_cnt[-1] % 2 == 1 and len_cnt[-1] != 1 :
  ANS -= 1
print(ANS)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
 
if len(S) == 1:
    print(int(K/2))
else:    
    count = 0
    bef = ""
    isbefchange = False
    for i in range(0, len(S)):
        if(isbefchange == False and S[i] == bef):
            count += 1
            isbefchange = True
            bef = S[i]
            continue
        bef = S[i]
        isbefchange = False
    count *= K
    if(isbefchange == False and S[len(S)-1] == S[0] and S[0] != S[1]):
        count += (K-1)
    print(count)

Submission status: Wrong Answer

Solution code:
def cad(s, k):
    if len(s) == 1:
        return k // 2
    l = list(s)

    count = 0
    new_l = l.copy()

    first_len = 1
    first_block = True
    for i, c in enumerate(s[1:]):
        if c == new_l[i]:
            new_l[i + 1] = 0
            count += 1

        if first_block:
            if c == s[0]:
                first_len += 1
            else:
                first_block = False

    result = count * k

    if new_l[0] == new_l[-1] and first_len % 2 == 1:
        result += k - 1
    return result


s = input()
k = int(input())
print(cad(s, k))

Submission status: Wrong Answer

Solution code:
S = list(str(input()))
N = len(S)
K = int(input())

ans1 = 0
old = ""
flag = 0
for i in range(N):
    if old == "":
        old = S[i]
    else:
        if old == S[i]:
            old = ""
            ans1+=1
        else:
            old = S[i]
            
    if i == N-1:
        if old == S[i]:
            flag = 1

if flag == 0:
    print(ans1*K)
else:
    ans2 = 0
    old = ""
    for i in range(N):
        if i == 0:
            if S[0] == S[-1]:
                if flag == 1:
                    ans2+=1
                else:
                    old = S[0]
        elif old == "":
            old = S[i]
        else:
            if old == S[i]:
                old = ""
                ans2+=1
            else:
                old = S[i]
    if i == N-1:
        if old == S[i] and S[0] == S[-1]:
            flag = 2
    
    k = K//2
    if K%2 == 0:
        print((ans1+ans2)*k)
    else:
        if flag == 2:
            print(ans1*(k)+ans2*(K-k))
        else:
            print(ans1*(K-k)+ans2*(k))

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
if len(S) == 1:
  print((len(S) * K) // 2)
  exit()
if len(S) == 2:
  if S[0] == S[1]:
    ans = K
  else:
    ans = 0
  print(ans)
  exit()
SS = S + S
SSS = S + S + S
cntSS = 0
flg = True
for i in range(1,len(SS)):
  if SS[i - 1] == SS[i] and flg:
    cntSS += 1
    flg = False
  elif flg == False:
    flg = True
    
cntSSS = 0
flg = True
for i in range(1,len(SSS)):
  if SSS[i - 1] == SSS[i] and flg:
    cntSSS += 1
    flg = False
  elif flg == False:
    flg = True
print((cntSSS - cntSS) * (K - 1) + (cntSS * 2 - cntSSS))

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
import copy
s1 = list(set(s))
count = 0
x = 0
if len(s1) == 1:
    count = int((k * len(s)) / 2)
elif s[0] != s[-1] or k == 1:
    for i in range(len(s) - 1):
        if s[i] == s[i + 1]:
            s.pop(i + 1)
            s.insert(i + 1, "")
            count += 1
    count = count * k
else:
    if s[-2] == s[-1]:
        x = 1
    s.append(s[0])
    for i in range(len(s) - 1):
        if s[i] == s[i + 1]:
            s.pop(i + 1)
            s.insert(i + 1, "")
            count += 1
    count = count * k - 1
    if x == 1:
        count += 1
print(int(count))

Submission status: Wrong Answer

Solution code:
import string


def count_changes(s, k, pool, prev=None):
    t = s * k
    count = 0
    i = 0
    for cur in t:
        if cur == prev:
            count += 1
            prev = pool[i]
            i = (i + 1) % length
        else:
            prev = cur
    return count, prev


if __name__ == '__main__':
    s = input()
    k = int(input())
    pool = list(set(string.ascii_lowercase) - set(s))
    length = len(pool)
    if k <= 2:
        print(count_changes(s, k, pool)[0])
    else:
        base_count, prev = count_changes(s, 3, pool)
        d, rest = divmod(k, 3)
        if rest:
            print(0)
        else:
            print(base_count * d)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
s = list(s)
s.append(s[0])
h = len(s)
saigo = False
j = 0
num = 0

for i in range(1,h):
    if s[i] == s[i-1] and j == 0:
        if i == h-1:
            saigo = True
        num = num + 1
        j = 1
    else:
        j = 0

num = num * k
if saigo:
    num = num - 1
print(num)

Submission status: Wrong Answer

Solution code:
import numpy as np
S = input()
K = int(input())
unique = list()
for x in S:
    if x not in unique:
        unique.append(x)
res = 0
if len(unique) == 1:
    res = int(len(S)*K/2)
    S = ""
if len(S) == 2:
    if S[0] == S[-1]:
        res += K
if len(S) > 2:
    S += S[0]
    n = 0
    while n <= len(S)-3:
        if S[n] == S[n+1]:
            res += K
            n += 1
        n += 1
    if S[n-2] == S[n-1]:
        res += K-1
print(res)

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]

S = list(input())
K = int(input())
N = len(S)
ans = 0
last_same = False
if N==1:
    print(K//2)
    exit()
for i in range(1,N):
    if S[i]!=S[i-1]:
        break
else:
    print(N*K//2)
    exit()
pre = S[0]
p = False
for i in range(1,N):
    if p:
        pre = S[i]
        p = False
        continue
    s = S[i]
    if pre==s:
        ans += 1
        p = True
        if i==N-1:
            last_same = True
    pre = s

l_same = False
if not last_same:
    if not S[0]==S[1]:
        if S[0]==S[-1]:
            ans += 1
            l_same = True

ANS = ans*K
if l_same:
    ANS -= 1
print(ANS)

Submission status: Wrong Answer

Solution code:
import sys
# input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def main():
    s = input()
    k = int(input())

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

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

        else:
            ans += cnt // 2
            cnt = 1
    ans += cnt // 2

    if s[0] == s[-1] and (s[0] != s[1] and s[-1] != s[-2]):
        print(ans*k+k-1)
        exit()

    print(ans*k)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def main():
    s = list(input().rstrip())
    k = int(input())

    c = 0

    for i in range(len(s) - 1):
        if s[i] == s[i+1]:
            c += 1
            s[i+1] = '1'

    print(c*k)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

l=len(s)
zero=0
first=0
second=0
last=0
S=s+s
LS=list(S)

for n in range(2*l):
  if n==0:
    if LS[0]==LS[-1]:
      zero+=1
  if 0<n and n<=l-1:
    if LS[n-1]==LS[n]:
      first+=1
      LS[n]=str(n)
  if l<=n and n<2*l-1:
    if LS[n-1]==LS[n]:
      second+=1
      LS[n]=str(n)
  if n==2*l-1:
    if LS[n-1]==LS[n]:
      last+=1
      LS[n]=str(n)
      zero-=1

if K%2==0:
  ans=(zero+first+second+last)*(K//2)
else:
  ans=(zero+first+second+last)*(K//2)+zero+first

print(ans)