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(input())
K = int(input())

if len(S) == 1:
    print(K // 2)
else :
    pre = S[0]
    flg = 0
    cnt = 0
    cnt_list =[]
    
    for i in range(1, len(S)):
        cnt += 1
        if flg == 1:
            flg = 0
            pre = S[i]
            continue
        if S[i] == pre:
            cnt_list.append(cnt)
            flg = 1
        pre = S[i]
    
    if S[0] == S[-1] and ((S[-1] == S[-2] and len(S)-2 in cnt_list) or (S[0] == S[1] and 1 in cnt_list)):
        print(len(cnt_list)*K)
    elif S[0] == S[-1]:
        print(len(cnt_list)*K+K)
    else :
        print(len(cnt_list)*K)

Submission status: Wrong Answer

Solution code:
S, K = input(), int(input())
ans = 0
S_ = S + S[0]
i = 1
while(i<len(S_)):
    try:
        if (S_[i-1] == S_[i]) and (S_[i] == S_[i+1]):
            ans += 1
        elif S_[i] == S_[i+1]:
            ans += 1
        i += 2
    except IndexError:
        break
print(ans*K)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
N = len(S)
M = [0 for i in range(N)]

for i in range(N-1):
    if S[i]==S[i+1]:
        M[i]=1
if S[0]==S[N-1]:
    M[N-1]=1

count = 0
total = 0

for i in range(N):
    if M[i] == 1:
        count+=1
    elif count==1:
        total+=1
        count=0
    elif count!=0:
        total+=int((1+count)/2)
        count=0
if count==1:
    total+=1
elif count!=0:
    total+=int((1+count)/2)

print(total*K)

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

if flag:
  if firstChart == S[len(S)-1]:
    ans += (firstCount + count + 2) // 2 * (K-1)
    ans += math.ceil(count/2)
  else:
    ans += math.ceil(count/2)*K
else:
  ans += (count+1)*K//2
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=""
if s[0]==s[-1]:
  for i in range(len(s)):
    if i==len(s)-1:
      print(len(s)*k//2)
      sys.exit()
    elif k==1:
      break
    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:
# -*- 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 > 1 and j%2 == 1:
    cnt+= K-1

print(cnt)

Submission status: Wrong Answer

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

def func(ms):
    r = 0
    k = 0
    m = ms[0]
    for i in range(1, len(ms)):
        if ms[i] == ms[i-1]:
            k+=1
        else:
            r += int((k+1)/2)
            k = 0
        m = ms[i]
    r += int((k+1)/2)
    return r            
    
if S[0] * len(S) == S:
    if len(S) == 1:
        print(int(K/2))
    else:
        print(int(len(S) * K / 2))
elif len(set(S)) == len(S):
    print(0)
elif S[0] == S[-1]:
    r = 0
    m = S[0]
    f = 0
    for c in S:
        if c == m:
            r += 1
            f += 1
        else:
            break   
    s = S[::-1]
    fe = 0
    for c in s:
        if c == m:
            r += 1
            fe += 1
        else:
            break
    S = S[f:-fe]
    tr = int(r/2) * (K-1)
    r = func(S) * K
    print(r+tr)
else:
    ms = S
    r = 0
    k = 0
    m = ms[0]
    for i in range(1, len(ms)):
        if ms[i] == ms[i-1]:
            k+=1
        else:
            r += int((k+1)/2)
            k = 0
        m = ms[i]
    r += int((k+1)/2)
    print(r*K)

Submission status: Wrong Answer

Solution code:
import re

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

series = [1,]
for i in range(0, len(s)-1):
    if s[i] == s[i+1]:
        series[-1] += 1
    else:
        series.append(1)
changeCharCount = 0
for sameCharBlockLen in series:
    changeCharCount += int(sameCharBlockLen / 2)
changeCharCount *= k
if s[0] == s[-1]:
    if (series[0] % 2 == 1) and (series[-1] % 2 == 1):
        changeCharCount += (k - 1)
print(changeCharCount)

Submission status: Wrong Answer

Solution code:
import math
s=list(input())
k=int(input())
s=s*1000
ans=0
for i in range(1,len(s)):
    if s[i-1]==s[i]:
        ans+=1
        s[i]="A"
print(round(ans*(k/1000)))

Submission status: Wrong Answer

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

count = 0
change = False
for i in range(1, len(s)):
    if s[i-1] == s[i] and not change:
        count += 1
        change = True
    else:
        change = False

count2 = 0
change2 = False
for i in range(2, len(s)):
    if s[i-1] == s[i] and not change2:
        count2 += 1
        change2 = True
    else:
        change2 = False

if len(s) == 1:
    print(k//2)
elif s[0] == s[-1] and not change:
    if change2:
        print((count+count2+1)*(k//2)*count)
    else:
        print(count+(count2+1)*(k-1))
else:
    print(count*k)

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
print(j)
if j > 1 and j%2 == 1:
    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+1)*k-1)
else:
    print(cnt//2*k)

Submission status: Wrong Answer

Solution code:
s=list(input())
k=int(input())
s=s*1000
ans=0
for i in range(1,len(s)):
    if s[i-1]==s[i]:
        ans+=1
        s[i]="A"
print(ans*(k/1000))

Submission status: Wrong Answer

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


if __name__ == '__main__':
    S = input()
    K = int(input())
    if len(S) == 1:
        S = S * K
        K = 1

    print(min(f(S, K), f(S[::-1], K)))

Submission status: Wrong Answer

Solution code:
import sys

def main():
    input = sys.stdin.readline
    S = str(input().strip())
    K = int(input())

    ans = 0
    if len(S) == 1:
        return K // 2
    
    if len(set(S)) == 1:
        return len(S) * K // 2
    
    l, r = 0, len(S)-1
    if S[0] == S[-1]:
        while len(set(S[0:l+1])) <= 1:
            l += 1
        while len(set(S[r-1:])) <= 1:
            r -= 1

    cnt = 0
    Y = S[l:r]
    T = Y[0]
    for y in Y[1:]:
        if y == T[-1]:
            cnt += 1
            T += '_'
        else:
            T += y
    
    ans = cnt * K
    ans += (l + (len(S) - r)) // 2 * (K - 1) + l // 2 + (len(S) - r) // 2
    return ans

if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
ans = 0
cnt = 0
ans_fin = 0
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        cnt = cnt +1
        if i == len(s)-2:
            if cnt%2 == 1:
                ans = ans + int((cnt+1)/2)
            else:
                ans = ans + int(cnt/2)
    else:
        if cnt%2 == 1:
            ans = ans + int((cnt+1)/2)
            cnt = 0
        elif (cnt%2 == 0 and not(cnt == 0)):
            ans = ans + int(cnt/2)
            cnt = 0
        else:
            pass
ans_fin = ans*k
#print(ans_fin)
if not(s[0] == s[len(s)-1]):
    pass
if len(s) == 1:
    if k%2 ==0:
        ans_fin = int(k/2)
    else:
        ans_fin = int((k-1)/2)
if len(set(s)) == 1:
    if (len(s)*k)%2 == 0:
        ans_fin = int((len(s)*k)/2)
    else:
        ans_fin = int((len(s)*k-1)/2)
else:
    cnt2 = 0
    cnt3 = 0
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            cnt2 = cnt2 + 1
        else:
            break
    for i in range(len(s)-1):
        if s[-i] == s[-i-1]:
            cnt3 = cnt3 + 1
    
    if (cnt2%2 ==0 and cnt2 >0) and(cnt3%2 == 0 and cnt3>0):
        ans_fin = ans_fin + (k-1)
print(ans_fin)

Submission status: Wrong Answer

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

if len(S) == 1:
    print(K // 2)
else :
    pre = S[0]
    flg = 0
    cnt = 0
    cnt_list =[]
    
    for i in range(1, len(S)):
        cnt += 1
        if flg == 1:
            flg = 0
            pre = S[i]
            continue
        if S[i] == pre:
            cnt_list.append(cnt)
            flg = 1
        pre = S[i]
    
    if S[0] == S[-1] and ((S[-1] == S[-2] and len(S)-1 in cnt_list) or (S[0] == S[1] and 0 in cnt_list)):
        print(len(cnt_list)*K)
    elif S[0] == S[-1]:
        print(len(cnt_list)*K+K)
    else :
        print(len(cnt_list)*K)

Submission status: Wrong Answer

Solution code:
s=list(input())
K=int(input())
S=s.copy()

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

zcount=0
ju=0
for i in range(len(S)):
  if S[i]==S[0]:
    zcount+=1
    if i==len(S)-1:
      ju=1
  else:
    break

if ju==0:
  for i in reversed(range(len(S))):
    if S[i]==S[0]:
      zcount+=1
    else:
      break
  
if S[0]==S[1]:
  S[0]="#"
for i in range(1,len(S)):
  if S[i-1]==S[i]:
    S[i]="#"

ans=S.count("#")
if ju==1 and len(S)%2==1:
  print((len(S)*K)//2)
  exit()
  
if s[-1]==s[0] and s[1]!=s[0] and s[-2]!=s[0]:
  print(K*ans+(K-1))
  exit()

if S[0]=="#" and zcount>1 and zcount%2==1 and ju==0:
  ans-=1
  if S[-2]!=S[0] and s[0]==s[-1]:
    ans+=1

if S[0]!="#" and zcount>1 and zcount%2==0 and ju==0:
  print((ans+1)*K-1)
  exit()

print(ans*K)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
ans = 0
cnt = 0
ans_fin = 0
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        cnt = cnt +1
        if i == len(s)-2:
            if cnt%2 == 1:
                ans = ans + int((cnt+1)/2)
            else:
                ans = ans + int(cnt/2)
    else:
        if cnt%2 == 1:
            ans = ans + int((cnt+1)/2)
            cnt = 0
        elif (cnt%2 == 0 and not(cnt == 0)):
            ans = ans + int(cnt/2)
            cnt = 0
        else:
            pass
ans_fin = ans*k
print(ans_fin)
if not(s[0] == s[len(s)-1]):
    pass
if len(s) == 1:
    if k%2 ==0:
        ans_fin = int(k/2)
    else:
        ans_fin = int((k-1)/2)
if len(set(s)) == 1:
    if (len(s)*k)%2 == 0:
        ans_fin = int((len(s)*k)/2)
    else:
        ans_fin = int((len(s)*k-1)/2)
else:
    cnt2 = 0
    cnt3 = 0
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            cnt2 = cnt2 + 1
        else:
            break
    for i in range(len(s)-1):
        if s[-i] == s[-i-1]:
            cnt3 = cnt3 + 1
    
    if (cnt2%2 ==0 and cnt2 >0) and(cnt3%2 == 0 and cnt3>0):
        ans_fin = ans_fin + (k-1)
print(ans_fin)

Submission status: Wrong Answer

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

cnt=0
i=1
while i <len(s):
  if i>0:
    if s[i]==s[i-1]:
      cnt+=1
      i+=2
    else:
      i+=1
  
if k==1:
  print(cnt)
  exit()
  
ss=s+s
cntcnt=0
i=1
while i <len(ss):
  if i>0:
    if ss[i]==ss[i-1]:
      cntcnt+=1
      i+=2
    else:
      i+=1
if k==2:
  print(cntcnt)
  exit()
  
sss=s+s+s
cntcntcnt=0
i=1
while i <len(sss):
  if i>0:
    if sss[i]==sss[i-1]:
      cntcntcnt+=1
      i+=2
    else:
      i+=1
if k==3:
  print(cntcntcnt)
  exit()
      
if len(s)==1:
  print(k//2)
  exit()

print(cnt-(cntcnt-cnt)*((1-k)//2)+(cntcntcnt-cntcnt)*(k-1)//2)

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<=l-1:
    if LS[n-1]==LS[n]:
      first+=1
      LS[n]=str(n)
  if l<=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] or LS[n]==LS[0]:
      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)

Submission status: Wrong Answer

Solution code:

from sys import stdin

S = str(stdin.readline().rstrip())
K = int(stdin.readline().rstrip())

lenS = len(S)
ans = 0

for i in range(lenS):
    mo = S[0]*lenS
    if mo == S:
        ans = len(mo)//2
        print(ans)
        exit()
    else:
        pass

if S[0] == S[lenS-1]:
    S2 = S*2
    a = 0#位置
    while a < lenS*2-1:
        ans2 = 0
        if S2[a] == S2[a+1]:
            b = 2
            x = S2[a]
            for i in range(1,lenS*2-a):
                try:
                    if x == S2[a+1+i]:
                        b += 1
                    else:
                        break
                except:
                    b = 2
            ans2 += b//2
            a += 1+b
        else:
            a += 1
    a = 0#位置
    while a < lenS-1:
        ans1 = 0
        if S[a] == S[a+1]:
            b = 2
            x = S[a]
            for i in range(1,lenS-a):
                try:
                    if x == S[a+1+i]:
                        b += 1
                    else:
                        break
                except:
                    b = 2
            ans1 += b//2
            a += 1+b
        else:
            a += 1
    differ = ans2-ans1
    ans = differ*(K-1) + ans1*K
else:
    a = 0#位置
    while a < lenS-1:
        if S[a] == S[a+1]:
            b = 2
            x = S[a]
            for i in range(1,lenS-a):
                try:
                    if x == S[a+1+i]:
                        b += 1
                    else:
                        break
                except:
                    b = 2
            ans += b//2
            a += 1+b
        else:
            a += 1
    ans = ans*K


print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
ans = 0
t = s+s
if len(s)== 1:
    ans += k//2
else:
    if t[0]==t[1]==t[-1]:
        ans += k//2
        t[0] = "0"
    for i in range(0,len(t)-1):
        if t[i]==t[i+1]:
            ans += k//2
            t[i+1] = "0"
    if t[0] == t[-1] and t[1] != "0":
        ans += k//2-1
    if k%2:
        u = t[len(t)//2:len(t)] + s
        for i in range(0,len(u)-1):
            if u[i]==u[i+1]:
                ans += 1
                u[i+1] = "0"
print(ans)

Submission status: Wrong Answer

Solution code:
def map_all(es):
    return all([e == es[0] for e in es[1:]]) if es else False

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

count = 0
xi = 0

if len(s) == 1:
    print(int(k // 2))
elif map_all(s) == True:
    print(int((len(s)*k)//2))
else:
    for i in range(len(s) - 1):
        if s[i] == s[i + 1]:
            count += 1
            p = i % 10
            s[i + 1] = str(p)

    if s[0] == s[-1]:
        xi = 1
        p = i % 10
        s[-1] = str(p)

    print(int(count * k + xi * (k - 1)))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict as df
s=input().rstrip()
ini=len(s)
k=int(input())
s=[i for i in s]
if len(s)==2:
    if s[0]!=s[-1]:
        print(0)
    else:
        print((len(s)*k)//2)
    exit()
if len(s)==1:
    print(k//2)
    exit()
else:
    count=0
    temp=[]+s
    temp=[ i for i in temp]
    for i in range(1,len(s)):
        if temp[i]==temp[i-1]:
            temp[i]='randi'
            
            count+=1
    if s[0]!=s[-1]:
        print(count*k)
        exit()
    if s.count(s[0])==len(s):
        print((len(s)*k)//2)
        exit()
    if k==1:
        print(count)
        exit()
    if s[-1]==s[0] and s[0]!=s[1] and s[-1]!=s[-2]:
        print(count*k + k-1)
        exit()
    
    if s[-1]==s[0]==s[1]==s[-2]:
        print(count*k)
        exit()
    if s[i]==s[-1] and s[0]!=s[1] and s[-1]==s[-2]:
        print(count*k)
        exit()
    else:
        print(count*k)

Submission status: Wrong Answer

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

if s.count(s[0]) == len(s):
  print((len(s) * k) // 2)
  sys.exit()

c_c = 0
c_e = 0
counter = 1

for i in range(-1, -len(s)-1, -1):
  print(s[i])
  if s[0] == s[i]:
    counter += 1
  else:
    break
if counter % 2 == 0:
  c_e = 1

for i in range(len(s) - 1):
  
  if s[i] != s[i+1]:
    counter = 1
  else:
    counter += 1
    if counter % 2 == 0:
      c_c += 1

ans = c_c * k + c_e * (k-1)
print(ans)

Submission status: Wrong Answer

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

if len(s)==1:
  print(k//2)
else:
  
  if s[0]==s[-1]:
    p+=1

  l=[1]
  for i in range(len(s)-1):
    if s[i]!=s[i+1]:
      l.append(1)
    else:
      l[-1]+=1
  x=[i//2 for i in l]
  y=sum(x)

  if l[-1]!=1:
    if l[-1]%2==1:
      print(y*k+p*(k-1))
    else:
      print(y*k)
  else:
    print(y*k+p*(k-1))

Submission status: Wrong Answer

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

import re
def nchars(s, n):
    """文字列 s に、同じ文字が n 個以上連続している部分文字列を見つける
    """

    assert n > 0
    reg = re.compile("(.)\\1{%d,}" % (n - 1))  # カンマを取ると n 個ちょうどになる
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]
        
if len(s)==1:
    print(k//2)
elif len(s)==2:
    if len(set(s))==1:
        print(k)
    else:
        print(0)
elif len(s)==3:
    if len(set(s))==1:
        print(3*k//2)
    elif len(set(s))==2:
        print(k)
else:
    if s[0]!=s[-1]:
        a=list(nchars(s, 2))
        b=0
        for i in a:
            b+=len(i)//2
        print(b*k)
    else:
        if s[0]!=s[1] and s[-1]!=s[-2]:
            a=list(nchars(s, 2))
            b=0
            for i in a:
                b+=len(i)//2
            print(b*k+k-1)
        else:
            a=list(nchars(s, 2))
            b=0
            c=list(nchars(s+s, 2))
            l1_l2_sym_diff = set(a) ^ set(c)
            if len(l1_l2_sym_diff)%2==1 and (s[0]!=s[1] or s[-1]!=s[-2]):
                for i in a:
                    b+=len(i)//2
                print(b*k)
            else:
                for i in a:
                    b+=len(i)//2
                print(b*k+k-1)

Submission status: Wrong Answer

Solution code:

import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
# A = [ int(input()) for _ in range(N) ]
##############################

S = list(input().rstrip())
K = int(input())

T = S[:]

if len(T) == 1:
    x = (K+1) // 2
    print(K-x)
    exit()

count = 0
for i in range(1, len(T)):
    if T[i-1] == T[i]:
        count += 1
        T[i] = '-'

count *= K

T = S[:]

if T[0] == T[-1]:
    if T[0] != T[1] and T[-1] != T[-2]:
        count += (K-1)

print(count)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
result = 0
if S[0] != S[-1]:
    t = None
    for i in range(len(S)):

        if i == 0:
            t = S[i]
            continue

        if t == S[i]:
            result += 1
            t = None
            continue

        t = S[i]

    print(result * K)
    exit()

else:
    m = None
    for i in range(len(S)):

        if i == 0:
            m = S[i]
            continue

        if m == S[i]:
            result += 1
            m = None
            continue

        m = S[i]
    t = S[0]
    count_a = 1
    for i in range(len(S)):
        if i == 0:
            continue

        if t == S[i]:
            count_a += 1
        else:
            break
    if count_a == len(S):
        print((len(S) * K) // 2)
        exit()
    count_b = 0

    for i in range(len(S)):
        if t == S[-i-1]:
            count_b += 1
        else:
            break

    count = count_a + count_b
    change = count // 2
    print(change)
    change_a = count_a // 2
    print(change_a)
    change_b = count_b // 2
    print(change_b)
    if change == change_a + change_b:
        print(result * K)
    else:
        print(result * K + K - 1)

Submission status: Wrong Answer

Solution code:
s = input()
n = len(s)
K = int(input())
L = 2
t = list(s*L)
s = list(s)
m = len(t)
cnt = 0
c = 0
for i in range(1, m):
    if t[i] == t[i-1]:
        t[i] = 'A'
        cnt+=1
for i in range(1, n):
    if s[i] == s[i-1]:
        s[i] = 'A'
        c+=1
res = c+(cnt-c)*(K-1)//(L-1)
if n == 1:
    res = K//2
print(res)

Submission status: Wrong Answer

Solution code:
import numpy as np

S = raw_input()
K = int(raw_input())

S_twice = S + S

A = np.full(len(S_twice),0)
A_twice = np.full(len(S_twice),0)

count = 0
for i in range(1, len(S)):
    if S[i] == S[i-1]:
        count = count + 1
    else:
        A[count] = A[count] + 1
        count = 0

A[count] = A[count] + 1

count = 0
for i in range(1, len(S_twice)):
    if S_twice[i] == S_twice[i-1]:
        count = count + 1
    else:
        A_twice[count] = A_twice[count] + 1
        count = 0

A_twice[count] = A_twice[count] + 1

if count == len(S_twice)-1:
    print len(S)*K/2
    exit()

A_k = A + (K-1)*(A_twice-A)

ans = 0

for i in range(A_k.shape[0]):
    ans = ans + (i+1)/2*A_k[i]

print ans

Submission status: Wrong Answer

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

moji = []

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

ans = 0
if len(moji) == 1:
  ans = (moji[len(moji)-1] * K) //2
elif len(moji) != 1 and S[0] == S[len(S)-1]:
  for i in range(1, len(moji)-1):
    ans += (moji[i] // 2) * K
    
  ans += ((moji[0] + moji[len(moji)-1]) // 2) * (K-1)
  ans += moji[0] // 2
  ans += moji[len(moji)-1] // 2

else:
  for i in range(1, len(moji)-1):
    ans += (moji[i] // 2) * K
    
print(ans)

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
T0=0
count=1
ans=0
l=0
c0=0

for i in range(len(S)):
	if S[i]==T0:
		count+=1
	else:
		ans+=count//2
		l+=1
		if l==1:
			c0=count
		count=1
	T0=S[i]

if S[0]==S[-1]:
	a=count+c0
else:
	a=count

print(ans*K+(a//2)*(K-1)+count//2)

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 or k==1:
    print(len(s)*k//2)
    sys.exit()
  elif k==1:
    break
  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 sys
input = sys.stdin.readline

s = str(input()).replace('\n', '')
k = int(input())

s = s + s[0]

lst = []
last = ''
cnt = 0
for i in range(len(s)):
  if s[i] == last or last == '':
    cnt += 1
  else:
    lst.append(cnt)
    cnt = 1
  last = s[i]
lst.append(cnt)

    
n_lst = [i // 2 for i in lst]
answer = sum(n_lst)*k
if lst[-1]%2 == 0:
  answer -=  1
  
if len(lst) == 1:
  answer = (len(s)-1)*k//2

print(answer)

Submission status: Wrong Answer

Solution code:
#code
s=input()
k=int(input())
l=list()
curr=0
if(len(s)==1):
    print(int(k//2))
else:    
    for i in range(1,len(s)):
    
        if(s[i]!=s[i-1]and curr!=0):
            l.append(curr+1)
            curr=0
        if(s[i]==s[i-1]):
            curr+=1
    if(curr!=0):
        l.append(curr+1)
#for start
    start=0
    if(s[0]==s[len(s)-1] ):
        for i in range(1,len(s)):
            if(s[i]==s[i-1]):
                start+=1
            else:
                break
        start+=l[len(l)-1]+1
    ans=0
    if(start==0):
        for i in range(len(l)):
            ans+=(l[i]//2)
    
        ans=ans*k    
    else:
        if(len(l)==1):
            ans+=((l[0])//2)*k
        else:
            ans+=(l[0]+l[len(l)-1])//2
            ans=ans*(k-1)
            ans+=l[0]//2
            ans+=l[len(l)-1]//2
            if(len(l)>=3):
                for i in range(1,len(l)-1):
                    ans+=((l[i]//2)*k)
    print(int(ans))

Submission status: Wrong Answer

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

def func(ms):
    r = 0
    k = 0
    m = ms[0]
    for i in range(1, len(ms)):
        if ms[i] == ms[i-1]:
            k+=1
        else:
            r += int((k+1)/2)
            k = 0
        m = ms[i]
    r += int((k+1)/2)
    return r            
    
if S[0] * len(S) == S:
    if len(S) == 1:
        print(int(K/2))
    else:
        print(int(len(S) * K / 2))
elif len(set(S)) == len(S):
    print(0)
elif S[0] == S[-1]:
    r = 0
    m = S[0]
    f = 0
    for c in S:
        if c == m:
            r += 1
            f += 1
        else:
            break   
    s = S[::-1]
    fe = 0
    for c in s:
        if c == m:
            r += 1
            fe += 1
        else:
            break
    S = S[f:-fe]
    tr = int(r/2) * (K-1)
    r = func(S) * K
    print(r+tr)
else:
    print(func(S)*K)

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

if S[0] == S[-1]:
    result = count * K + (K-1)
else:
    result = count * K


print(result)

Submission status: Wrong Answer

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

buf=''
cnt = 1
total = 0
cnts = []


for s in S:
    #print(buf,s,cnt)
    if s == buf:
        cnt += 1       
    else:
        cnts.append(cnt)
        total += int(cnt/2)
        cnt = 1        
    buf = s
cnts.append(cnt)    
total += int(cnt/2)

if S[0]==S[-1] and cnts[1] % 2 != 0:
    if (cnts[1]+cnts[-1]) % 2 == 0:
        print(total*K+K-1)
else:
    print(total*K)

Submission status: Wrong Answer

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

x = 1
for i in range(n):
    if i == n-1:
        if s[i] == s[0]:
            x += 1
            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

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import groupby


s = input()
s_doub = s + s
k = int(input())
s_sub = [[h, len(list(g))] for h, g in groupby(s)]
cnt_s = 0
if s[0] == s[-1]:
    if len(s_sub) == 1:
        if s_sub[0][1] % 2 == 1:
            ans = (s_sub[0][1] // 2) * k + 1
        else:
            ans = (s_sub[0][1] // 2) * k
    else:
        for i in range(1, len(s_sub)-1):
            cnt_s += s_sub[i][1] // 2
        ans = cnt_s * k + s_sub[0][1] // 2 + s_sub[-1][1] // 2 + ((s_sub[0][1] + s_sub[-1][1]) // 2) * (k - 1)
else:
    for i in range(len(s_sub)):
        cnt_s += s_sub[i][1] // 2
    ans = cnt_s * k
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict as df
s=input().rstrip()
ini=len(s)
k=int(input())
s=[i for i in s]
if len(s)==2:
    if s[0]!=s[-1]:
        print(0)
    else:
        print((len(s)*k)//2)
    exit()
if len(s)==1:
    print(k//2)
    exit()
else:
    count=0
    temp=[]+s
    temp=[ i for i in temp]
    for i in range(1,len(s)):
        if temp[i]==temp[i-1]:
            temp[i]='randi'
            
            count+=1
    if s[0]!=s[-1]:
        print(count*k)
        exit()
    if s.count(s[0])==len(s):
        print((len(s)*k)//2)
        exit()
    if k==1:
        print(count)
        exit()
    if s[-1]==s[0] and s[0]!=s[1] and s[-1]!=s[-2]:
        print(count*k + k-1)
        exit()
    
    if s[-1]==s[0]==s[1]==s[-2]:
        print(count*k)
        exit()
    if s[i]==s[i-1] and s[0]!=s[1] and s[-1]==s[-2]:
        print(count*k)
        exit()
    else:
        print(count*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
    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 * (K - v)) + (g2 * (v))

print(cnt)

Submission status: Wrong Answer

Solution code:
s=list(input())
c=0
k=int(input())
n=len(s)
l=set(s)

if len(l)==1:
    k*=n
    n=1

if n==1:
    print((k-1)//2 if k%2==1 else k//2)
    quit()

flag=False
if s[0]==s[-1]:
    s[-1]=-1
    c+=1
    flag=True

def ccount(v, l):  # v: finding val, l: list
    ans=[]
    nv = 0
    i = 0
    n = len(l)
    while i < n:
        if l[i]!=v:
            i += 1
        else:
            while i < n and l[i] == v:
                nv += 1
                i += 1
        if nv>1: ans.append(nv)
        nv = 0
    return ans

for i in l:
    for v in ccount(i,s):
        if v%2==0:
            c+=v//2
        else:
            c+=(v-1)//2

c*=k

if s[-2]==s[0]:
    flag=False
if flag:
    c-=1

print(c)

Submission status: Wrong Answer

Solution code:
s = input()
n = len(s)
K = int(input())
L = 2
t = list(s*L)
s = list(s)
m = len(t)
cnt = 0
c = 0
for i in range(1, m):
    if t[i] == t[i-1]:
        t[i] = 'A'
        cnt+=1
for i in range(1, n):
    if s[i] == s[i-1]:
        s[i] = 'A'
        c+=1
res = c+(cnt-c)//L*(K-1)
if n == 1:
    res = K//2
print(res)