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())
a = '0'
cnt = 0
flag = 1
out = 0

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

if s[0] == s[len(s)-1]:
    cnt += 1
    flag = -1

out = cnt * k

if flag == -1:
    out -= 2

print(out)

Submission status: Wrong Answer

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

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

is_same = False
b = ''
count = 0
tail = False
for c in s:
  if c == b and not is_same:
    count += 1
    is_same = True
  else:
    is_same = False
  b = c
else:
  if s[0] == s[-1] and not is_same:
    tail = True
ans = count * k
if tail:
  ans += k-1
print(ans)

Submission status: Wrong Answer

Solution code:
import string


def count_changes(s, k, pool, i=0, prev=None):
    if k == 0:
        return 0, None, None
    t = s * k
    count = 0
    prev = prev or t[0]
    for cur in t[1:]:
        if cur == prev:
            count += 1
            prev = pool[i]
            i = (i + 1) % length
        else:
            prev = cur
    return count, i, 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, i, prev = count_changes(s, 3, pool)
        d, rest = divmod(k, 3)
        print(base_count * d + count_changes(s, rest, pool, i, prev)[0])

Submission status: Wrong Answer

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

S2 = S.copy()

T = S * 2
T2 =  S * 2

countA1 = 0
for i in range(len(S) - 1):
    if S[i] == S[i + 1]:
        countA1 += 1
        S[i + 1] = 'A'

countA2 = 0
for i in range(len(S2) - 1):
    if S2[i] == S2[i + 1]:
        countA2 += 1
        S2[i] = 'A'

# 2bai
countB1 = 0
for i in range(len(T) - 1):
    if T[i] == T[i + 1]:
        countB1 += 1
        T[i + 1] = 'A'

countB2 = 0
for i in range(len(T2) - 1):
    if T2[i] == T2[i + 1]:
        countB2 += 1
        T2[i] = 'A'

if K > 1:
    if K % 2 == 1:
        print(min(K//2*countB1+countA1, K//2*countB2+countA2))
    else:
        print(min(K//2*countB1, K//2*countB2))
else:
    print(min(countA1,countA2))

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
c = 0
i = 0
if s[0]*len(s) == s:
    print(len(s)*k//2)
else:   
        
    while i<len(s)-1:
        if s[i] == s[i+1]:
            c += 1
            i += 2
        else:
            i += 1
    if s[0] == s[-1] and i==len(s)-1:
        c += 1
        print(k*c-1)
    else:
        print(k*c)

Submission status: Wrong Answer

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

cnt=0
if len(s)==1:
  ret = k
else:
  tmp=s[0]
  tmp_cnt=0
  for i in s[1:]+s[0]:
    if tmp==i:
      tmp_cnt+=1
    else:
      cnt+=(tmp_cnt+1)//2
      tmp_cnt=0
    tmp=i
  cnt+=(tmp_cnt+1)//2
  ret=cnt*k
print(ret)

Submission status: Wrong Answer

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

idx = 0
res = 0
while idx+1 < len(S):
  if S[idx] == S[idx+1]:
    res += 1
    idx += 2
  else:
    idx += 1

res *= K

if len(S) >= 3 and S[0] == S[-1]:
  if S[0] != S[1] and S[-2] != S[-1]:
    res += K-1
  elif S[-3] == S[-2] == S[-1]:
    res += K-1

if len(S) == 1:
  res = K // 2

print(res)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def f():
    S = input()
    K = int(input())
    S = list(S)
    if K == 1 and len(S) == 1:
        print(0)
        return
    
    if K == 1 and len(S) != 1:
        ans = 0
        for i in range(len(S)-1):
            if S[i] == S[i+1]:
                S[i+1] = '1'
                ans += 1
        print(ans)
        return
    
    if K != 1 and len(S) == 1:
        ans = K // 2
        print(ans)
        return
    
    if K != 1 and len(S) != 1:
        ORG_S = S[:]
        
        # A
        ans_a = 0
        for i in range(len(S)-1):
            if S[i] == S[i+1]:
                S[i+1] = str(i)
                ans_a += 1
                
        if S[0] == S[-1]:
            ans_a = ans_a*K + K-1
        else:
            ans_a = ans_a*K
            
            
        # B
        S = ORG_S[:]
        ans_b = 0
        if S[0] == S[1]:
            S[0] = 'X'
            ans_b = 1
        
        for i in range(len(S)-1):
            if S[i] == S[i+1]:
                S[i+1] = str(i)
                ans_b += 1
                
        if S[0] == S[-1]:
            ans_b = ans_b*K + K-1
        else:
            ans_b = ans_b*K
        
        # C
        S = ORG_S[:]
        ans_c = 0
        if S[0] == S[1]:
            S[0] = 'X'
            ans_c = 1
        if S[-2] == S[-1]:
            S[-1] = 'Y'
            ans_c = 1
        for i in range(len(S)-1):
            if S[i] == S[i+1]:
                S[i+1] = str(i)
                ans_c += 1
                
        if S[0] == S[-1]:
            ans_c = ans_c*K + K-1
        else:
            ans_c = ans_c*K            
        
        ans = min(ans_a, ans_b, ans_c)
        print(ans)
        return

f()

Submission status: Wrong Answer

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

s0 = ''
c = 0
d = 1
idx = 0
for s in S*2:
  s1 = s
  if s0==s1:
    d += 1 
    if idx==l*2-1:
      c += int(d/2)
  else:
    c += int(d/2)
    d = 1
  s0 = s1
  idx = idx+1
    
print(int(c/2) * K)

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) == 2:
        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

    print(ans*k)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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[0]=="#" and zcount%2==1 and ju==0:
  ans-=1
  if S[-2]!=S[0]:
    ans+=1
print(ans*K)

Submission status: Wrong Answer

Solution code:
def read():
    S = str(input().strip())
    K = int(input().strip())
    return S, K

def rle_encode(S):
    S1 = S + '@'
    rle = list()
    head = 0
    for i in range(0, len(S1)):
        if S1[i] != S1[head]:
            rle.append((S1[head], i-head))
            head = i
    return rle

def solve(S, K):
    if len(S) == 1:
        return K // 2
    count = 0
    rle = rle_encode(S)
    for c, n in rle:
        count += n // 2
    count *= K
    if rle[-1][0] == S[0] and rle[-1][1] % 2 == 1:
        count += (K-1)
    return count

if __name__ == '__main__':
    inputs = read()
    print("%d" % solve(*inputs))

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.readline
S = [ord(s)-97 for s in readline().strip()]
K = int(readline())

if len(S) == 1:
    Ans = K//2
else:
    dp = [1]*26
    dp[S[0]] = 0
    inf = 10**9
    
    for s in S[1:]:
        dp2 = [inf]*26
        for i in range(26):
            for j in range(26):
                if i == j:
                    continue
                dp2[i] = min(dp2[i], 1+dp[j])
        
        for j in range(26):
            if s == j:
                continue
            dp2[s] = min(dp2[s], dp[j])
        
        dp = dp2[:]
    
    ans = inf
    for i in range(26):
        if i == S[0]:
            continue
        ans = min(ans, dp[i])
    
    ma = min(dp)
    if ma == ans:
        Ans = ans*K
    else:
        Ans = ma*(-(-K//2)) + ans*(K//2)

print(Ans)

Submission status: Wrong Answer

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

def counter_func(string,x):
  try:
    if string[x] == string[x+1]:
      return 1
    else:
      return 0
  except:
    return 0
  
counter = []
count = 0
j = 0
while j < length:
  if counter_func(s,j) == 1:
    count += 1
  else:
    counter.append(count)
    count = 0
  j += 1
if s[-1] == s[0] and k > 1:
  counter[-1] += 1
sum = 0
for i in range(len(counter)):
  if counter[i] != 0:
    sum += int((counter[i]+1)/2)*k
print(sum)

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
for i in range(len(l)):
  if l[i][1] > 1:
    ans += l[i][1] // 2
print(ans * k)

Submission status: Wrong Answer

Solution code:
k=list(input())
l=k[::]
n=int(input())
for i in range(len(k)):
    if i==0:
        continue
    a,b=k[i-1],k[i]
    if a==b:
        k[i]="0"
l[0]="0"
for i in range(len(l)):
    if i==0:
        continue
    a,b=l[i-1],l[i]
    if a==b:
        l[i]="0"
if k[-1]!=k[0]:
    print(k.count("0")*n)
else:
    if k[0]!=l[-1]:
        print(k.count("0")*(n//2)+l.count("0")*(n-n//2))
    else:
        print(k.count("0")+l.count("0")*(n-1))

Submission status: Wrong Answer

Solution code:
s_list = list(input())
k = int(input())
s_len_list = []
dbls_len_list = []
dbls_list = s_list * 2
temp_len = 1
ans = 0

for i in range(len(s_list)-1):
    if s_list[i+1] == s_list[i]:
        temp_len += 1
    else:
        s_len_list.append(temp_len)
        temp_len = 1
s_len_list.append(temp_len)

temp_len = 1
for i in range(len(dbls_list)-1):
    if dbls_list[i+1] == dbls_list[i]:
        temp_len += 1
    else:
        dbls_len_list.append(temp_len)
        temp_len = 1
dbls_len_list.append(temp_len)
# print(s_list)
# print(s_len_list)
# print(dbls_list)
# print(dbls_len_list)
if s_len_list[len(s_len_list)-1] % 2 == 1 and dbls_len_list[len(s_len_list)-1] % 2 == 0:
    for i in s_len_list:
        ans += i // 2
    ans *= k
    ans += (k-1)
else:
    for i in s_len_list:
        ans += i // 2
    ans *= k
print(ans)

Submission status: Wrong Answer

Solution code:
s = list(map(list, input().split()))[0]
k = int(input())
ans = 0
flg = False
if len(s) == 1:
    print(k//2)
else:
    for i in range(len(s))[:-1]:
        if flg == True:
            flg = False
            continue
        elif s[i]==s[i+1]:
            ans += 1
            flg = True

    if flg == False and s[0] == s[-1]:
        ans += 1

    print(ans * k)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
def unit(s):
    """sの両端を外した状態でいくつ変えるか
    """
    count = 0
    prev = s[0]
    for c in s[1:]:
        if c==prev:
            count += 1
            prev = None
        else:
            prev = c
    return count
if len(s)==2:
    if s[0]==s[1]:
        print(k)
    else:
        print(0)
elif len(s)==1:
    print(k//2)
elif not s:
    print(0)
elif s[0]!=s[-1]:
    result = k * unit(s[1:-1])
    print(result)
else:
    s0 = unit(s[1:-1])
    s1 = unit(s[1:])
    s2 = unit(s[:-1])
    s3 = unit(s)
    result = min(s1*(k-1)+s3+(k-1), s2*(k-1)+s3+(k-1))
    if k%2==0:
        result2 = min(s3*(k/2)+s0*(k/2-1)+s1, s2+s3*(k/2))
    else:
        result2 = min(s3*(k//2+1)+s0, s2+(s3*(k//2)+s0*(k//2-1))+s1)
    print(min(result2, result))

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
L = len(S)
a = S[0]
i = 0
C = []
if L == 1:
    C = [0]
while i < L-1:
    count = 1
    b,c = S[i],S[i+1]
    i+= 1
    while b == c:
        count += 1
        if i == L-1:
            break
        b,c = S[i],S[i+1]
        i += 1
    C.append(count)
if a != S[L-1]:
    num = 0
    for co in C:
        num += co//2
    ans = int(num*K)
else:
    num1 = 0
    for co in C:
        num1 += co//2
    num2 = (C[len(C)-1]+1)//2
    for co in C[:len(C)-1]:
        num2 += co//2
    ans = int(num1+num2*(K-1))
if L== 1:
    ans = K//2
print(ans)

Submission status: Wrong Answer

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


pre = ""
count = 0
first = S[0]
last = S[len(S)-1]
flag = False
for i in range(len(S)):
    if i == (len(S) - 1):
        if 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 copy
S = list(input())
k = int(input())
cnt = 0

for i in range(1, len(S)):
  if S[i] == S[i-1]:
    cnt += 1
    S[i] = 'a' if S[i-1] != 'a' else 'b'
    
cnt *= k
if S[0] == S[-1]:
  cnt += k - 1

print(cnt)

Submission status: Wrong Answer

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

def f(s):
    i = 0
    n = 0
    while i < len(s)-1:
        if s[i] == s[i+1]:
            n += 1
            i += 2
        else:
            i += 1
    return n

def main():
    S = input().strip()
    K = int(input())
    ans = K * f(S)
    if f(S*2) != f(S)*2 and S[0] == S[-1]:
        ans += K-1
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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



if len(s)==1:
    if k<=2:
        ans = 0
    else:
        ans = k//2
elif len(s)==2:
    if s[0]==s[1]:
        ans = k
    else:
        ans = 0
else:
    if k == 1:
        ans = 0
        bef = ""
        befchen = False
        trignum = 0
        for i in range(len(s)):
            now = s[i]
            if bef==now:
                if befchen == False:
                    ans += 1
                    befchen = True
                    trignum = i
            if  befchen == True:
                if trignum == i-1:
                    befchen = False
            bef = now

    elif k >=2:
        ans = 0
        bef = ""
        befchen = False
        trignum = 0
        for i in range(len(s)):
            now = s[i]
            if bef==now:
                if befchen == False:
                    #print(now)
                    ans += 1
                    befchen = True
                    trignum = i
            if  befchen == True:
                if trignum == i-1:
                    befchen = False
            bef = now
        
        if s[0]==s[-1]:
            if befchen == False:
                ans += 1

        #print(ans)
        ans *= k
        ans -= 1

print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
s=list(input())
k=int(input())
cnt=1
cnt2=0
tmp=4
flg=0
flg2=0
for i in range(len(s)):
    if tmp==s[i]:
        cnt+=1
    else:
        cnt2+=cnt//2
        cnt=1
    tmp=s[i]
cnt2+=cnt//2
if len(s)==1:
    flg2=1

elif s[0]==s[-1]:
    m=0
    cnt3=0
    while s[m]==s[0]:
        cnt3+=1
        m+=1
        if m==len(s)-1:
            cnt+=3
            break
    n=1
    cnt4=0
    while s[-n]==s[0]:
        cnt4+=1
        n+=1
        if n==len(s):
            cnt4+=1
            break
    if cnt3%2==1 and cnt4%2==1:
        flg=1

if flg==1:
    ans=cnt2*k+k-1
elif flg2==1:
    ans=k//2
else:
    ans=cnt2*k
    
print(ans)

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

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

end = len(S) -1
j = 0

if S[end] == S[0]:
    chars = list(string.ascii_lowercase)
    pre = S[end -1]
    back = S[0]
    if pre != back:
        chars.pop(chars.index(pre))
        chars.pop(chars.index(back))
    else:
        chars.pop(chars.index(pre))
    S[end] = chars[0]
    j += 1

for i in range(1, len(S)-1):
    if S[i-1] == S[i]:
        chars = list(string.ascii_lowercase)
        pre = S[i-1]
        back = S[i+1]
        if pre != back:
            chars.pop(chars.index(pre))
            chars.pop(chars.index(back))
        else:
            chars.pop(chars.index(pre))
        S[i] = chars[0]
        j += 1
    else:
        continue

print(j*K)

Submission status: Wrong Answer

Solution code:
a=list(input())
b=int(input())
if len(a)==1:
  print(b//2)
else:
  ans1=0
  ans2=0
  i=0
  j=len(a)-1
  k=0
  while i>len(a):
    if a[0]==a[i]:
      k+=1
      i+=1
    else:
      break
  while 1:
    if i==j:
      break
    if a[0]==a[j]:
      k+=1
      j-=1
    else:
      break
      
  if k!=0 and k%2==0:
    ans2=1 
  for i in range(1,len(a)):
    if a[i]==a[i-1]:
      ans1+=1
      a[i]=str(i%10)
  print(ans1*b+ans2*(b-1))

Submission status: Wrong Answer

Solution code:
s=list(input())
k=int(input())
if len(s)==1:
    print(k//2)
    exit()
from collections import Counter
l=Counter(s)
if len(l)==1:
    print(k*len(s)//2)
    exit()
ans=0
f=1
for i in range(len(s)-1):
    if s[i]==s[i+1] and f:
        ans+=1
        f=0
    else:
        f=1
if s[0]==s[1] and s[0]==s[-1] and f==1:
    print(ans*k)
    exit()
if s[-1]!=s[-2] and s[0]==s[-1]:
    print(ans*k+k-1)
    exit()
if s[0]==s[-1] and f==1:
    print(ans*k+k-1)
    exit()


print(ans*k)

Submission status: Wrong Answer

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

if len(set(S)) == 1:
    print((len(S) * K) // 2)
else:
    cnt = 0
    last = 0
    for i, s in enumerate(S):
        if S[(i + 1) % len(S)] == S[(i + 2) % len(S)] == s:
            S[(i + 1) % len(S)] = '_'
            cnt += 1
        elif S[(i + 1) % len(S)] == s:
            S[i] = '_'
            cnt += 1
            if i == len(S) - 1:
                last = 1
    print(cnt * K - last)

Submission status: Wrong Answer

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

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

for i in range(len(S)):
    if S[i]==S[i-1]:
        if i%2==0:
            S[i]="?"
        else:
            S[i]="!"
        count+=1
if S[-1]==S[0]:
    print(count*K-2)
else:
    print(count*K)

Submission status: Wrong Answer

Solution code:
def li():
    return list(map(input().split()))


if __name__ == "__main__":
    s = input()
    k = int(input())

    count = 0
    before = ""
    for si in s:
        if before == si:
            count += 1
            before = ""
        else:
            before = si

    count2 = 0
    before = ""
    for si in s + s:
        if before == si:
            count2 += 1
            before = ""
        else:
            before = si
    diff = count2 - count

    is_corner = before == s[0]
    corner_count = k - 1 if is_corner else 0
    # print(count * k + corner_count)
    if len(s) == 1:
        print(k // 2)
    else:
        print(count + diff * (k - 1))
    # print(count, count2)

Submission status: Wrong Answer

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

if S[0] == S[N-1]:
    count += 1
else:
    pass

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

ans = count * K
print(ans)

Submission status: Wrong Answer

Solution code:
# N, K = map(int, input().split())
S = input()
K = int(input())

# first one
pp = '#'
res1 = 0
for s in S:
    if pp == s:
        res1 += 1
        pp = '#'
    else:
        pp = s

if K >= 2:
    #S = S + S
    pass
else:
    print(res1)
    exit(0)

# pp = S[-1]
res2 = 0
for s in S:
    if pp == s:
        res2 += 1
        pp = '#'
    else:
        pp = s

print(res2 * (K-1) + res1)

Submission status: Wrong Answer

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

def a(SS):
    count = 0
    samestr = 0
    for i in range(len(SS)-1):
        if SS[i] == SS[i+1]:
            samestr += 1
        else:
            count += ((samestr+1)//2)
            samestr = 0
    count += ((samestr+1)//2)
    return count

singlec = a(S)
if K == 1:
    print(singlec)
else:
    doublec = a(S+S)
    sac = doublec - singlec*2
    result = singlec*K + sac*(K-1)

    print(result)

Submission status: Wrong Answer

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

count = 0

for i in range( len(S)//2 ):
    if S[2*i+1] == S[2*i]:
        count +=1
        
print(count*K)

Submission status: Wrong Answer

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    ss = str(input().rstrip('\n'))
    nn = int(input().rstrip('\n'))
    s = 0
    sb = True
    e = 0
    eb = True
    t = 0
    cnt = 0
    if len(ss) == 1:
        print(len(ss) * nn // 2)
        exit()
    else:
        if ss[0] == ss[-1]:
            for i in range(len(ss)):
                if ss[i] == ss[0]:
                    if sb:
                        s += 1
                else:
                    sb = False
                if ss[-i-1] == ss[0]:
                    if eb:
                        e += 1
                else:
                    eb = False
        else:
            sb = False
            eb = False
        if sb and eb:
            cnt += ((s + e) // 2 // 2)
            s = 0
            e = 0
        else:
            for i in range(max(s, 1), min(len(ss) - e + 1, len(ss))):
                if ss[i] == ss[i-1]:
                    t += 1
                else:
                    t += 1
                    cnt += (t // 2)
                    t = 0
            cnt += (t // 2)
    print(cnt * nn + s // 2 + e // 2 + (s + e) // 2 * (nn - 1))


if __name__ == '__main__':
    slove()

Submission status: Wrong Answer

Solution code:
S = list(input())
K = int(input())
ans = 0
if len(S) == 1:
  print(K//2);exit()
elif len(S) == 2:
  if S[0] ==  S[1]:
    print(K);exit()
  else:
    print(0);exit()
elif len(S) == 3:
  if S[0] == S[1] and S[1] == S[2]:
    print(3*K//2);exit()
  else:
    print(K);exit()
else:
  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-1)
else:
  print(ans*K)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import random
import string

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

prev_c = S[0]
num_changes = 0
if S[0] == S[-1]:
    num_changes += 1
    prev_c = 'replaced'

for i, c in enumerate(S[1:]):
    if c == prev_c:
        num_changes += 1
        prev_c = 'replaced'
    else:
        prev_c = c

print(num_changes * K)

Submission status: Wrong Answer

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

if len(S_list) == 1:
    print(K//2)
elif len(S_list) == 2:
    if S_list[0]==S_list[1]:
        print(K)
    else:
        print(0)
else:
    count_1 = 0
    flag = False
    s = S_list[0]
    for i in range(1, len(S_list)):
        if flag == False:
            if s == S_list[i]:
                count_1 += 1
                flag = True
        else:
            flag = False
        s = S_list[i]

    S2_list = S_list + S_list
    count_2 = 0
    flag = False
    s = S2_list[0]
    for i in range(1, len(S2_list)):
        if flag == False:
            if s == S2_list[i]:
                count_2 += 1
                flag = True
        else:
            flag = False
        s = S2_list[i]
    count_2 -= count_1

    if K % 2 == 0:
        print((K//2)*count_1 + (K//2)*count_2)
    elif K % 2 == 1:
        print((K//2+1)*count_1 + (K//2)*count_2)

Submission status: Wrong Answer

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

s.append(s[0])
ans=0

last=1
for n in range(len(s)-1):


	if s[n]==s[n+1]:

		last+=1
		if last%2==0:
			ans+=1

	else:
		last=1


print(ans*k)

Submission status: Wrong Answer

Solution code:
def a(ll):
	cc = 0
	for xx in ll:
		if 0 < xx:
			cc += xx//2 + xx % 2
	return cc

s = input()
C = int(input())
s_ = ""
c = 0
l=[]
for x in s:
# 	print(s_,x)
	if s_ == x:
		c += 1
	else:
		l.append(c)
		c = 0

	s_ = x
# print(l)
print(a(l)*C)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
count_list = []
count = 0
for idx, s in enumerate(S):
    if idx == 0:
        continue
    else:
        if S[idx - 1] == s:
            count += 1
        else:
            if count > 0:
                count_list.append(count)
            count = 0
if count > 0:
    count_list.append(count)
sum = 0
for count in count_list:
    sum += -(-count // 2)
print(sum * K)

Submission status: Wrong Answer

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

if len(s) == 1:
    print(k//2)
else:
    changedLast = False

    result = 0

    i = 1
    while i < len(s):
        if s[i] == s[i-1]:
            result += k
            if i == len(s)-1:
                changedLast = True
            i += 1
        i += 1

    if not changedLast and s[0] == s[-1]:
        result += k-1
    print(result)

Submission status: Wrong Answer

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

l = len(S)
pair = []
current = S[0]
seq = 1
for i in range(1, l):
    if(i == l-1):
        if(S[i]==current):
            seq += 1
        if(seq != 1):
            pair.append(seq)
    elif(S[i] == current):
        seq += 1
    else:
        if(seq != 1):
            pair.append(seq)
        seq = 1
        current = S[i]

ans = 0
#print(pair)
if(l==1):
    ans = K-1
elif(S[0] != S[l-1] or l == 2):
    for i in range(len(pair)):
        ans += K * (pair[i]//2)
else:
    if(S[l-2] != S[l-1]):
        pair.append(1)
    if(len(pair) == 1):
        ans += ((pair[0]+1)//2)*(K-1) + (pair[0]//2)
    else:
        for i in range(len(pair)-1):
            ans += K * i
        ans += ((pair[i+1]+1)//2)*(K-1) + (pair[i+1]//2)

print(ans)

Submission status: Wrong Answer

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

prev = ""

n = 0

if len(s) == 1:
    print(num // 2)
else:
    if len(set(s)) == 1:
        ans = len(s) * num // 2
    
    elif s[0] == s[-1]:
        b = 1
        for i in range(1, len(s)):
            if s[0] == s[i]:
                b += 1
            else:
                start = i
                break
        ss = s[::-1]
        
        c = 1
        for i in range(1, len(s)):
            if ss[0] == ss[i]:
                c += 1
            else:
                end = i
                break
        
        for i in range(start, len(s) - end):
            if prev == s[i]:
                n += 1
                prev = ""
            else:
                prev = s[i]
        ans = (b + c) // 2
        ans += b // 2 + c // 2
        ans += n * num

    else:
        for x in s:
            if prev == x:
                n += 1
                prev = ""
            else:
                prev = x
        ans = n * num


    print(ans)

Submission status: Wrong Answer

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

a = S[0]
c = 0
g = []

for i in range(len(S)):
    if a == S[i]:
        c += 1
    else:
        g.append(c)
        a = S[i]
        c = 1

g.append(c)

if len(g) == 1:
    c = len(S) * K / 2
elif S[0] == S[-1]:
    c = int(g[0] / 2)
    c += int(g[-1] / 2)

    for i in range(1, len(g) - 1):
        c += int(g[i] / 2) * (K - 1)
else:
    c = 0
    for i in g:
        c += int(i / 2) * K


print(int(c))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# coding: utf-8

def ri():
    return int(input())

def rl():
    return list(input().split())

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

def change(s, s0=""):
    t = list(s0+s)
    ret = 0
    for i in range(1, len(t)):
        if t[i] == t[i-1]:
            t[i] = '*'
            ret += 1
    return "".join(t[len(s0):]), ret

def main():
    s = input()
    k = ri()
    memo = dict()
    memo2 = []
    bef = ""
    if k < 100:
        _, c = change(s*k, "")
        print(c)
        return
    rep = 0
    for i in range(k):
        ns, c = change(s, bef)
        # print(bef, s, ns, c)
        if ns in memo:
            print("OK", ns, memo[ns])
            rep = i-memo[ns][1]
            break
        memo[ns] = (c, i)
        memo2.append((c, ns))
        bef = ns
    ans = 0
    for m in memo2:
        ans += m[0]
    assert(len(memo2) < 3)
    rem = k-len(memo2)
    if rep == 1:
        ans += rem*memo2[-rep][0]
    elif rep == 2:
        ans += (rem//2)*sum(m[0] for m in memo2)
        if rem % 2 == 1:
            ans += memo2[0][0]
    print(ans)
    # print(rep, memo, memo2)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from itertools import groupby

s = list(input())
ss = s * 2
k = int(input())
x = 0
y = 0
if len(s) == 1:
    print(k // 2)
else:  
    for key, value in groupby(s):
        x += len(list(value)) // 2
    for key, value in groupby(ss):
        y += len(list(value)) // 2
    print((y-x) * (k-1) + x)

Submission status: Wrong Answer

Solution code:
from itertools import groupby

s = input()
k = int(input())
a = [sum(1 for _ in g) for _, g in groupby(s)]
x, y = a[0], a[-1]
b = (x + y) // 2 if s[0] == s[-1] else x // 2 + y // 2
print(x // 2 + y // 2 + sum(x // 2 for x in a[1:-1]) * k + b * (k - 1))

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
cnt = 0
cntw = 0
i = 0

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

if all(s) == True:
    print((k*len(s))//2)
    exit()
while True:
    if len(s)-1 < i:
        break
    if len(s)-1 == i:
        if s[i] == s[0]:
            cntw = 1
    elif s[i] == s[i+1]:
        cnt+= 1
        i += 1
    i += 1


ans = cnt*k+cntw*(k-1)
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
a = s + s[0]
prev = a[0]
c = 0
if len(s) * k  == 1:
  print(0)
  quit()
t = 0
for i, b in enumerate(list(a)[1:]):
  if b == prev:
    c += 1
    prev = "0"
    if len(s)-1 == i:
      t = -1
  else:
    prev = b
print(c*k + t)