Problem:


Score : 300 points

Problem Statement

Given is a string S. Let T be the concatenation of K copies of S.
We can repeatedly perform the following operation: choose a character in T and replace it with a different character.
Find the minimum number of operations required to satisfy the following condition: any two adjacent characters in T are different.

Constraints

1 \leq |S| \leq 100

S consists of lowercase English letters.

1 \leq K \leq 10^9

K is an integer.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the minimum number of operations required.

Sample Input 1

issii
2

Sample Output 1

4

T is issiiissii.  For example, we can rewrite it into ispiqisyhi, and now any two adjacent characters are different.

Sample Input 2

qq
81

Sample Output 2

81

Sample Input 3

cooooooooonteeeeeeeeeest
999993333

Sample Output 3

8999939997



Sample input:
issii
2


Sample output:
4

Submission status: Wrong Answer

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

    i = 0
    while i < len(S) - 1:
        if S[i] is S[i + 1]:
            ans += 1
            i += 1
        i += 1

    if ans is 0 and S[0] is S[-1]:
        print(K - 1)
    else:
        print(ans * K)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
S = str(input())
K = int(input())
tmp = 1
cnt1 = 0
cnt2 = 0
cnt3 = 0
for i in range(1, len(S)):
    if S[i-1] == S[i]:
        tmp += 1
    else:
        cnt1 += tmp // 2
        tmp = 1
if tmp > 1:
    cnt1 += tmp//2
S2 = S + S
tmp = 1
for i in range(1, len(S2)):
    if S2[i-1] == S2[i]:
        tmp += 1
    else:
        cnt2 += tmp // 2
        tmp = 1
if tmp > 1:
    cnt2 += tmp//2
S3 = S2 + S
tmp = 1
for i in range(1, len(S3)):
    if S3[i-1] == S3[i]:
        tmp += 1
    else:
        cnt3 += tmp // 2
        tmp = 1
if tmp > 1:
    cnt3 += tmp//2
if K == 1:
    print(cnt1)
elif K == 2:
    print(cnt2)
else:
    print((cnt3 - cnt2) * (K - 2) + cnt2)

Submission status: Wrong Answer

Solution code:
import numpy as np
import copy

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

A = np.zeros(1)
j = 0 

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

if np.size(A) == 1:
    ans = (len(S) * K) // 2
    
else:   
    A = A + 1
    B = copy.deepcopy(A)
    if S[0] == S[-1]:
        B[0] += A[-1]
        A = np.delete(A, np.size(A)-1)
        ans = np.sum(A // 2) + np.sum(B // 2) * (K-1)
    else:
        ans = np.sum(A // 2) * K

print(int(ans))

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
t = s + s
ans = 0
i = 1
while True:
    c = 0
    while True:
        if i>=len(t):
            break
        elif t[i-1] == t[i]:
            i +=2
            c+= 1
        else:
            break
    ans += c
    if i>=len(t):
        break
    i+=1
if s[0]==s[-1]:
    c = 0
    while True:
        s0 = s[-1]
        if len(s)==1:
            break
        if s0==s[0]:
            c +=1
            s = s[-1:]
if c%2 ==1:
    print(((ans* k)+1)//2)
else:
    print(((ans* k))//2)

Submission status: Wrong Answer

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

if len(S) == 1:
  print(K//2)
  exit()
  
SR=S+[S[0]]
cnt=0
for i in range(len(S)-1):
  if SR[i]==SR[i+1]==SR[i+2]:
    SR[i+1]=0
    cnt+=1

for i in range(len(S)):
  if SR[i]==SR[i+1]:
    SR[i]=1
    cnt+=1

if SR[-2]==1 and (SR[-3]==0):
  ans = cnt*K-1
elif SR[-2] == 1 and S[-1]!=S[-2]:
  ans = cnt*K-1
else:
  ans = cnt*K
print(ans)

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
if len(S)==1:
    print(len(S)*K//2)
elif K==1:
    i=0
    count=0
    while i<len(S)-1:
        if S[i]==S[i+1]:
            count+=1
            i+=2
        else:
            i+=1
    print(count)

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

        else:
            i+=1
    if K%2==0:
        print(count*(K//2))
    else:
        i=0
        Count=0
        while i<len(S)-1:
            if S[i]==S[i+1]:
                Count+=1
                i+=2
            else:
                i+=1
        print(count*(K//2)+Count)

Submission status: Wrong Answer

Solution code:
import string


def count_changes(s, k, pool, prev=None):
    if k == 0:
        return 0, None, 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, i, prev


if __name__ == '__main__':
    s = input()
    k = int(input())
    s_set = set(s)
    if len(s) == len(s_set):
        print(0)
    else:
        pool = list(set(string.ascii_lowercase) - s_set)
        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, prev)[0])

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
l=list(s)
r=list(s*2)
x=0
y=0
if len(l)==1:
    print(int(k/2))
else:
    for i in range(1,len(l)):
      if l[i-1]==l[i]:
        l[i]=0
        x+=1
    for i in range(1,len(r)):
       if r[i-1]==r[i]:
        r[i]=0
        y+=1
    if 2*x==y:
         print(k*x)
    else:
         print(k*x+k-1)

Submission status: Wrong Answer

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

    c = []
    
    i = 0
    while i < len(S):

        char = S[i]

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

    ans = 0
    for elem in c:
        if len(c) == 1:
            ans = (elem[1] //2) * K
        elif elem[2] >= 2:
            ans += (elem[2] // 2) * (K - 1) + elem[1] // 2
        else:
            ans += (elem[1] // 2) * K
        
    print(ans)

if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

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

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

if len(list(set(s))) == 1 and len(s)%2 == 1:
    print((len(s)//2)*(k+1))
    exit(0)

count = 0
    
ngo = 1
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        ngo+=1
    else:
        count += ngo//2
        ngo=1
count += ngo//2

ngongo = 0
while ngongo < len(s) and s[len(s)-ngongo-1] == s[-1]:
    ngongo+=1

ngongongo = 0
while ngongongo < len(s) and s[ngongongo] == s[0]:
    ngongongo += 1
    
f = 0
if ngongo %2 == 1 and ngongongo %2 == 1 and s[0] == s[-1]:
    count += 1
    f += 1

print(count*k-f)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math
import collections


def simple_count(S, changed=False):
    count = 0
    for i in range(len(S) - 1):
        if changed:
            changed = False
        else:
            if S[i] == S[i + 1]:
                changed = True
                count += 1
    return count, changed


def solve(S, K):
    if S[0] != S[-1]:
        return simple_count(S)[0] * K
    else:
        count2, changed = simple_count(S * 2)
        if changed:
            return count2 * (K // 2) + simple_count(S, changed=False)[0] * K % 2
        else:
            return count2 * (K // 2) + 1 + simple_count(S, changed=False)[0] * K % 2
        # return simple_count(S + S[0])[0] * K


def test(S, K):
    assert solve(S, K) == simple_count(S * K)[0], "{} {} = {} {}".format(S, K, solve(S, K), simple_count(S * K)[0])
assert solve("cooooooooonteeeeeeeeeest", 999993333) == 8999939997
assert solve("aqqq", 2) == simple_count("aqqq" * 2)[0]
test("qq", 81)
# print(solve("qqq", 2), simple_count("qqq" * 2)[0])
assert solve("qqq", 2) == simple_count("qqq" * 2)[0]
# print(solve("qqq", 3), simple_count("qqq" * 3)[0])
assert solve("qqq", 3) == simple_count("qqq" * 3)[0]
assert solve("issii", 2) == simple_count("issii" * 2)[0]




test("iqi", 3)
# test("iqqqqiiii", 7)
# test("qqqqqiiiii", 7)

# S = sys.stdin.readline().rstrip()
# K, = map(int, sys.stdin.readline().rstrip().split(" "))
# print(solve(S, K))

exit(0)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from heapq import heappush, heappop, heapreplace

def solve(debug=0):
    S = input()
    l = len(S)
    K = int(input())
    SK = S*min(2,K)
    s1 = s2 = s3 = ''
    swap_count = [0,0]
    T = ''
    for i,s in enumerate(SK+'--', start=-1):
        if s3:
            s1, s2, s3 = s2, s3, s
        elif s2:
            s3 = s
        elif s1:
            s2 = s
        else:
            s1 = s

        if s3 and len({s1,s2})==1:
            s2 = '='
            swap_count[i//l] += 1

        if i >= 1:
            T += s1

    debug and print(SK)
    debug and print(T)
    debug and print(swap_count)
    res = swap_count[0] + max(0, K-1)*swap_count[1]
    return str(res)
    

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

Submission status: Wrong Answer

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

pre_str = ''
count = 1
base_count = 0
for a in list(S):
    if pre_str == a:
        count += 1
    else:
        base_count += int(count / 2)
        count = 1
        pre_str = a
base_count += int(count / 2)
ret = 0
if len(list(set(list(S)))) == 1:
    ret = int(len(S) * K / 2)
elif len(S) > 2 and S[0] != S[1] and S[-1] != S[-2] and S[0] == S[-1]:
    ret = base_count * K
    ret += K - 1
else:
    ret = base_count * K
print(ret)

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]:
    print(k * unit(s))
#     if k==1:
#         print(unit(s))
#     else:
#         result = s2 + s0 * (k-2) + s1
#         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, s2*(k-1)+s3)
    if k%2==0:
        result2 = min(s3*(k/2)+s0*(k/2-1)+s1, s2+s3*(k/2)+s0*(k/2-1))
    else:
        result2 = min(s3*(k//2+1)+s0*(k//2), s2+(s3*(k//2)+s0*(k//2-1))+s1)
    print(min(result2, result) + (k-1))

Submission status: Wrong Answer

Solution code:
a=list(input())
k=int(input())
ans=0
if len(a)==1:
  print(k//2)
elif (a[i]==a[i+1] for i in range(len(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:
def solve():
    s = input()
    len_s = len(s)
    k = int(input())
    li = [1] * len_s + [0]
    if len_s == 1:
        return k // 2
    if len_s == 2:
        if s[0] == s[1]:
            return k
        return 0
    if s[0] != s[-1]:
        cnt = 0
        flg = False
        for i in range(0, len_s - 1):
            if flg:
                flg = False
            elif s[i] == s[i + 1]:
                flg = True
                cnt += 1
        return cnt * k
    s += s
    cnt = 0
    flg = False
    for i in range(0, len_s):
        if s[i] == s[i + 1]:
            if flg:
                flg = False
            elif s[i] == s[i + 1]:
                flg = True
                cnt += 1
    return cnt * k
print(solve())

Submission status: Wrong Answer

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

if len(S) > 1 and S[0] == S[1]:
    flg = 1
if flg:
    for i in range(len(S)-1):
        if S[i] == S[i+1] and S[i] != None:
            ans += 1
            S[i] = None

else:

    for i in range(1, len(S)):
        if S[i] == S[i-1] and S[i] != None:
            ans += 1
            S[i] = None
ans *= K

if len(S) == 1:
    ans = K // 2
    print(ans)
    exit()
if S[-1] == S[0] and S[0] != None:
    ans += (K-1)
print(ans)

Submission status: Wrong Answer

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

if len(S) == 1:
    print(K // 2)
    exit()
res, flag = 0, False
for i in range(1, len(S)):
    if i > 1 and S[i] == S[i-1] == S[i-2] and flag:
        flag = False
    elif S[i] == S[i-1]:
        res += 1
        flag = True
res *= K
if len(S) > 1 and (S[-1] == S[-2] or S[0] == S[1]):
    print(res)
    exit()
if S[0] == S[-1]:
    res += K - 1
print(res)

Submission status: Wrong Answer

Solution code:
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    S = input()
    K = int(input())

    S = S + S
    num = 0
    l, prev = 1, ''
    for s in S:
        if s == prev:
            l += 1
        else:
            num += l // 2
            l = 1
        prev = s
    num += l // 2
    n1 = num // 2
    n2 = (num + 1) // 2
    ans = n1 + n2 * (K - 1)
    print(ans)


main()

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


count=0
n=len(s)
x=s[0]
y=s[-1]

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

if s[0]!=s[-1]:
    for i in range(1,n):
        if s[i]==s[i-1]:
            count+=1
            s[i]='1'
    print(count*k)


else:
    countA=1
    countB=1
    v=0
    w=0
    for i in range(1,n):
        if s[i]==s[i-1]:
            countA+=1
            
        else:
            v=i
            break
    for i in range(1,n):
        if s[n-1-i]==s[n-1]:
            countB+=1
        else:
            w=n-1-i
            break
    
    for i in range(v,w+1):
        if s[i+1]==s[i]:
            count+=1
            s[i+1]='1'
    print(count*k+countA//2+(countA+countB)*(k-1)//2+countB//2)

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 = 0
    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]:
            count_b += 1
        else:
            break

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

Submission status: Wrong Answer

Solution code:
from math import ceil
from itertools import tee


def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)


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

if len(S) == 1:
    print(K-1)
else:
    print(ceil(sum(1 for s, t in pairwise(S) if s == t) / 2) * K)

Submission status: Wrong Answer

Solution code:
import numpy as np
import copy

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

A = np.zeros(1)
j = 0 

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

if np.size(A) == 1:
    ans = (len(S) * K) // 2
    
else:   
    A = A + 1
    print(A)
    B = copy.deepcopy(A)
    if S[0] == S[-1]:
        B[0] += A[-1]
        print(B)
        A = np.delete(A, np.size(A)-1)
        ans = np.sum(A // 2) + np.sum(B // 2) * (K-1)
    else:
        ans = np.sum(A // 2) * K

print(int(ans))

Submission status: Wrong Answer

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

if k == 1:
    cnt1 = 0
    cnt2 = 0
    for i in range(1, n-1):
        if i % 2 == 0:
            if s[i] == s[i-1] or s[i] == s[i+1]:
                cnt1 += 1
        else:
            if s[i] == s[i-1] or s[i] == s[i+1]:
                cnt2 += 1
    print(min(cnt1, cnt2))
else:
    s += s
    s += s
    s1 = ''
    s2 = ''
    for i in range(3*n):
        if i < 3*n - 1 and i > 0:
            if i % 2 == 0:
                s2 += s[i]
                if s[i] == s[i-1] or s[i] == s[i + 1]:
                    s1 += '0'
                else:
                    s1 += s[i]
            else:
                s1 += s[i]
                if s[i] == s[i-1] or s[i] == s[i + 1]:
                    s2 += '0'
                else:
                    s2 += s[i]
        elif i == 3*n - 1:
            if i % 2 == 0:
                s2 += s[i]
                if s[i] == s[i-1]:
                    s1 += '0'
                else:
                    s1 += s[i]
            else:
                s1 += s[i]
                if s[i] == s[i-1]:
                    s2 += '0'
                else:
                    s2 += s[i]
        else:
            if i % 2 == 0:
                s2 += s[i]
                if s[i] == s[i + 1]:
                    s1 += '0'
                else:
                    s1 += s[i]
            else:
                s1 += s[i]
                if s[i] == s[i + 1]:
                    s2 += '0'
                else:
                    s2 += s[i]
    
    s1_arr = [s1[:n].count('0'), s1[n:2*n].count('0'), s1[2*n:].count('0')]
    s2_arr = [s2[:n].count('0'), s1[n:2*n].count('0'), s2[2*n:].count('0')]
    if k == 2:
        print(min(s1_arr[0] + s1_arr[2], s2_arr[0] + s2_arr[2]))
    else:
        print(min(s1_arr[0] + s1_arr[1]*(k-2) + s1_arr[2], s2_arr[0] + s2_arr[1]*(k-2) + s2_arr[2]))

Submission status: Wrong Answer

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

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

if len(list(set(s))) == 1:
    print((len(s)//2)*(k+1))
    exit(0)

count = 0
    
ngo = 1
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        ngo+=1
    else:
        count += ngo//2
        ngo=1
count += ngo//2

ngongo = 0
while ngongo < len(s) and s[len(s)-ngongo-1] == s[-1]:
    ngongo+=1

ngongongo = 0
while ngongongo < len(s) and s[ngongongo] == s[0]:
    ngongongo += 1
    
f = 0
if ngongo %2 == 1 and ngongongo %2 == 1 and s[0] == s[-1]:
    count += 1
    f += 1

print(count*k-f)

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[:]
        ans_min = 10**12
        # A
        ans = 0
        if S[0] == S[-1]:
            S[-1] = 'X'
            ans += 1
        for i in range(len(S)-1):
            if S[i] == S[i+1]:
                S[i+1] = str(i)
                ans += 1
        ans = ans*(K-1)
        S2 = S[:] + ORG_S
        for i in range(len(S2)-1):
            if S2[i] == S2[i+1]:
                S2[i+1] = 'A'+str(i)
                ans += 1
        ans_min = min(ans, ans_min)
        # B
        ans = 0
        S = ORG_S[:]
        if S[0] == S[-1]:
            S[0] = 'X'
            ans += 1
        for i in range(len(S)-1):
            if S[i] == S[i+1]:
                S[i+1] = str(i)
                ans += 1
        ans = ans*(K-1)
        S2 = ORG_S + S[:]
        for i in range(len(S2)-1):
            if S2[i] == S2[i+1]:
                S2[i+1] = 'A'+str(i)
                ans += 1
        ans_min = min(ans, ans_min)         
        # A-rev
        ans = 0
        if S[0] == S[-1]:
            S[-1] = 'X'
            ans += 1
        for i in reversed(range(len(S)-1)):
            if S[i] == S[i+1]:
                S[i+1] = str(i)
                ans += 1
        ans = ans*(K-1)
        S2 = S[:] + ORG_S
        for i in reversed(range(len(S2)-1)):
            if S2[i] == S2[i+1]:
                S2[i+1] = 'A'+str(i)
                ans += 1
        ans_min = min(ans, ans_min)
        # B
        ans = 0
        S = ORG_S[:]
        if S[0] == S[-1]:
            S[0] = 'X'
            ans += 1
        for i in reversed(range(len(S)-1)):
            if S[i] == S[i+1]:
                S[i+1] = str(i)
                ans += 1
        ans = ans*(K-1)
        S2 = ORG_S + S[:]
        for i in reversed(range(len(S2)-1)):
            if S2[i] == S2[i+1]:
                S2[i+1] = 'A'+str(i)
                ans += 1
        ans_min = min(ans, ans_min)   

        ans_min = min(ans, ans_min)   
        
        print(ans_min)
        return

f()

Submission status: Wrong Answer

Solution code:
S = list(input())
K = int(input())
ans = 0
i= 1

if len(S) == 1:
    ans = -(-K//2)
else:
    while i < len(S):
        if S[i-1] == S[i]:
            ans += 1
            i += 1
        i+=1
    ans *= K
    if len(S) != 1:
        if S[0] == S[len(S)-1] and S[len(S)-2] != S[len(S)-1]: ans += K -1
    if len(S) > 2:
        if S[0] == S[len(S)-1] and S[len(S)-3] == S[len(S)-2] == S[len(S)-1]: ans += K -1
        # elif S[0] == S[len(S)-1] and S[len(S)-2] == S[len(S)-1] and S[len(S)-2] == S[len(S)-3]: 



print(ans)

Submission status: Wrong Answer

Solution code:
import string

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

end = len(S) -1
j = 0

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

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]
    print(j*K + K)
else:
    print(j*K)

Submission status: Wrong Answer

Solution code:
s = list(input())
s2 = s[:]
k = int(input())

s1_count = 0
for i in range(1, len(s)):
    if s[i-1] == s[i]:
        s1_count += 1
        s[i] = '-'
if k == 1:
    print(s1_count)
elif s[0] != s[-1]:
    print(s1_count * k)
elif len(list(set(s2))) == 1:
    if len(s) % 2 == 0:
        print(s1_count * k)
    else:
        s2_count = s1_count + 1
        if k % 2 == 0:
            print(int((s1_count + s2_count) * (k // 2)))
        else:
            print(int((s1_count + s2_count) * (k // 2) + s1_count))
else:
    c = s[0]
    s2_count = 0
    if s[-1] == '-':
        print(s1_count * k)
    else:
        s2[0] = '-'
        s2_count += 1
        for i in range(1, len(s2)):
            if s2[i-1] == s[i]:
                s2_count += 1
                s[i] = '-'
        print(s1_count + s2_count * (k-1))

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+lin[0]//2
print(point)

Submission status: Wrong Answer

Solution code:
s=input()
c=0
k=int(input())
a=False
t=s*2
for i in range(len(t)-1):
  if t[i]==t[i+1]:
    c+=1
if c%2==0:
  c/=2
else:
  c=(c-1)/2
  a=True
if a==True:
  c=(c+1)*k-1
  print(c)
else:
  c=c*k
  print(c)

Submission status: Wrong Answer

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

result = 0

# S内の重複
dup_c = 0
c_list = ['XXXXX']
for s in S:
    if c_list[0] == s:
        dup_c += 1
        c_list = ['XXXXX']
    else:
        c_list[0] = s
result = dup_c * K

# 間の重複
if len(S) > 1:
#    if S[0] == S[len(S)-1]:
#        if S[1] != S[0] and S[len(S)-2] != S[0]:
#            result += K - 1
    #後ろから同じ文字をカウント
    Sr = S[::-1]
    same_s = 0
    for i in range(len(Sr)):
        if Sr[0] == Sr[i]:
            same_s += 1
        else:
            break
    
    if same_s != 1:
        if same_s % 2 == 1:
            result += K - 1
    else:
        if S[0] == S[len(S)-1]:
            if S[1] != S[0] and S[len(S)-2] != S[0]:
                result += K - 1

else:
    result += K//2

print(result)

Submission status: Wrong Answer

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

if len(s) == 1:
  print(k // 2)
else:
  cnt = 0
  p = 1
  while p < len(s):
    if s[p-1] == s[p]:
      cnt += 1
      p += 1
    p += 1
      
  cnt2 = 0
  p2 = 1
  s2 = s + s
  while p2 < len(s2):
    if s2[p2-1] == s2[p2]:
      cnt2 += 1
      p2 += 1
    p2 += 1

  if 2*cnt < cnt2:
    print(k*cnt + (k - 1))
  else:
    print(k*cnt)

Submission status: Wrong Answer

Solution code:
def main():
    S = input()
    K = int(input())
    count = 0
    same = True
    for i in range(len(S) - 1):
        if S[i] == S[i+1]:
            continue
        else:
            same = False
            break
    if same:
        count += int((len(S) * K) / 2)
    else:
        i = 0
        while i < len(S) - 1:
            c = 1
            flag = True
            while S[i] == S[i+1]:
                c += 1
                i += 1
                if i + 1 >= len(S):
                    flag = False
                    break
            i += 1
            if c == 1:
                continue
            if c % 2 == 0:
                count += K * int(c / 2)
            else:
                count += K * int((c - 1) / 2)
            if not flag:
                if S[0] == S[-1] and c % 2 == 1:
                    count += K - 1
            
    print(count)

if __name__ == "__main__":
    main()

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)
print(zn*b+(nn//2)*(b-1))

Submission status: Wrong Answer

Solution code:
import numpy as np

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

def connect_cnt(S):
	cnt_tmp = 1
	cnt = []

	for i in range(len(S) - 1):

		if S[i] == S[i+1]:
			cnt_tmp += 1
		else:
			cnt.append(cnt_tmp)
			cnt_tmp = 1

		if i == len(S)-2:
			cnt.append(cnt_tmp)

	connect_cnt = sum(np.array(cnt) // 2)
	return connect_cnt

if len(S) == 1:
  print(connect_cnt(S*K))
  exit(0)

if K == 1:
	print(connect_cnt(S))
	exit(0)

if S[0] != S[-1]:

	connect_cnt = connect_cnt(S)
	ans = connect_cnt * K
	print(ans)

else:

	S_dash = S + S[0]
	connect_cnt_Sdash = connect_cnt(S_dash)
	connect_cnt_S = connect_cnt(S)

	ans = connect_cnt_Sdash * (K - 1) + connect_cnt_S
	print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

length = len(S)
if length == 1:
    print(K)
    exit(0)
if length == 2:
    if S[0] == S[-1]:
        print(K)
    else:
        print(0)
    exit(0)

count = 0
start_end_same = S[0] == S[-1]
i = 2
while i < length:
    if S[i-2] == S[i-1] == S[i]:
        S[i-1] = '-'
        count += 1
        i += 2
    elif S[i-2] == S[i-1]:
        S[i-1] = '-'
        count += 1
        i += 1
    elif S[i-1] == S[i]:
        if i == length-1:
            count += 1
            S[i] = '-'
        i += 1
    else:
        i = min(i+2, length)
ans = count*K
if K > 1 and start_end_same and S[1] != '-' and S[-1] != '-':
    ans += (K-1)
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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
S = list(input())
K = int(input())
n = len(S)
s_pre = '_'
count = 0
last = 0
output = 0
for s in S:
    if s_pre == s:
      s = '_'
      count += 1
    s_pre = s
if S[-1] == S[0]:
  S[-1] = '_'
  last = 1
if last == 1:
  output -= 1
output = K*count

print(output)