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

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

rep = [1]
prev = s[0]
for i in range(1, len(s)):
    if s[i] == prev:
        rep[-1] += 1
    else:
        rep.append(1)
        prev = s[i]

cnt = 0
for r in rep:
    cnt += r//2

if s[0] == s[-1]:
    ans = cnt*k + ((rep[0]+rep[-1])//2 - rep[0]//2 - rep[-1]//2)*(k-1)
    print(ans)
else:
    print(cnt*k)

Submission status: Wrong Answer

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

rep = [1]
prev = s[0]
for i in range(1, len(s)):
    if s[i] == prev:
        rep[-1] += 1
    else:
        rep.append(1)
        prev = s[i]

cnt = 0
for r in rep:
    cnt += r//2

if s[0] == s[-1]:
    ans = cnt*k + ((rep[0]+rep[-1])//2 - rep[0]//2 - rep[-1]//2)*(k-1)
    print(ans)
else:
    print(cnt*k)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

N = len(S)
p = S[0]
c = 0

if N == 1:
    print(K//2)
    exit()

for i in range(1,N):
    if S[i] == p:
        c += 1
        p = ''
    else:
        p = S[i]

ans = c*K
if S[0] == S[-1]:
    p = S[0]
    c = 1
    for i in range(1,N):
        if p == S[i]:
            c += 1
        else:
            break

    d = 1
    for i in range(2,N+1):
        if p == S[-i]:
            d += 1
        else:
            break

    if (c%2)*(d%2) == 1:
        ans += K-1

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
s = input()
k = int(input())

# 全部同じ
C = Counter(s)
if len(C) == 1:
    print(len(s) * k // 2)
    exit()

# 両端が異なる
if s[0] != s[-1]:
    aida = 0
    cnt = 0
    for i in range(1,len(s)):
        if s[i] == s[i-1]:
            cnt += 1
        else:
            tmp = cnt // 2
            if cnt % 2 != 0:
                aida += tmp
            else:
                aida += tmp
            cnt = 0
    print(aida * k)

# 両端が同じ
else:
    # 先端の個数
    cnt = 1
    for i in range(1,len(s)):
        if s[i] == s[i-1]:
            cnt += 1
        else:
            break
    # 末端の個数
    cnt2 = 1
    for i in range(len(s)-1,-1,-1):
        if s[i] == s[i-1]:
            cnt2 += 1
        else:
            break

    # 先端で書き換える個数
    syoko = cnt // 2

    # 末端＋先端で書き換える個数
    sotogawa = (cnt + cnt2) // 2

    # 先端と末端の間で書き換える個数
    aida = 0
    cnt3 = 0
    #print(s[cnt : len(s)-cnt2])
    for i in range(cnt+1,len(s)-cnt2+1):
        if s[i] == s[i-1]:
            cnt3 += 1
        else:
            tmp = cnt3 // 2
            if cnt3 % 2 != 0:
                aida += tmp
            else:
                aida += tmp
            cnt3 = 0

    saikuru = aida + sotogawa
    print(syoko + saikuru * k - saikuru + aida + cnt2//2)

Submission status: Wrong Answer

Solution code:
from collections import Counter
s = input()
k = int(input())

# 全部同じ
C = Counter(s)
if len(C) == 1:
    print(len(s) * k // 2)
    exit()

# 両端が異なる
if s[0] != s[-1]:
    aida = 0
    cnt = 0
    for i in range(1,len(s)):
        if s[i] == s[i-1]:
            cnt += 1
        else:
            tmp = cnt // 2
            if cnt % 2 != 0:
                aida += tmp + 1
            else:
                aida += tmp
            cnt = 0
    print(aida * k)

# 両端が同じ
else:
    # 先端の個数
    cnt = 1
    for i in range(1,len(s)):
        if s[i] == s[i-1]:
            cnt += 1
        else:
            break
    # 末端の個数
    cnt2 = 1
    for i in range(len(s)-1,-1,-1):
        if s[i] == s[i-1]:
            cnt2 += 1
        else:
            break

    # 先端で書き換える個数
    syoko = cnt // 2

    # 末端＋先端で書き換える個数
    sotogawa = (cnt + cnt2) // 2

    # 先端と末端の間で書き換える個数
    aida = 0
    cnt3 = 0
    #print(s[cnt : len(s)-cnt2])
    for i in range(cnt+1,len(s)-cnt2+1):
        if s[i] == s[i-1]:
            cnt3 += 1
        else:
            tmp = cnt3 // 2
            if cnt3 % 2 != 0:
                aida += tmp + 1
            else:
                aida += tmp
            cnt3 = 0
    
    saikuru = aida + sotogawa
    print(syoko + saikuru * k - saikuru + aida + cnt2//2)

Submission status: Wrong Answer

Solution code:
from collections import Counter
s = input()
k = int(input())

# 全部同じ
C = Counter(s)
if len(C) == 1:
    print(len(s) * k // 2)
    exit()

# 両端が異なる
if s[0] != s[-1]:
    aida = 0
    cnt = 0
    for i in range(1,len(s)):
        if s[i] == s[i-1]:
            cnt += 1
        else:
            tmp = cnt // 2
            if cnt % 2 != 0:
                aida += tmp + 1
            else:
                aida += tmp
            cnt = 0
    print(aida * k)

# 両端が同じ
else:
    # 先端の個数
    cnt = 1
    for i in range(1,len(s)):
        if s[i] == s[i-1]:
            cnt += 1
        else:
            break
    # 末端の個数
    cnt2 = 1
    for i in range(len(s)-1,-1,-1):
        if s[i] == s[i-1]:
            cnt2 += 1
        else:
            break

    # 先端で書き換える個数
    syoko = cnt // 2

    # 末端＋先端で書き換える個数
    sotogawa = (cnt + cnt2) // 2

    # 先端と末端の間で書き換える個数
    aida = 0
    cnt3 = 0
    #print(s[cnt : len(s)-cnt2])
    for i in range(cnt,len(s)-cnt2+1):
        if s[i] == s[i-1]:
            cnt3 += 1
        else:
            tmp = cnt3 // 2
            if cnt3 % 2 != 0:
                aida += tmp + 1
            else:
                aida += tmp
            cnt3 = 0
    
    saikuru = aida + sotogawa
    print(syoko + saikuru * k - saikuru + aida + cnt2//2)

Submission status: Wrong Answer

Solution code:
from collections import Counter
s = input()
k = int(input())

# 全部同じ
C = Counter(s)
if len(C) == 1:
    print(len(s) * k // 2)
    exit()

# 両端が異なる
if s[0] != s[-1]:
    aida = 0
    cnt = 0
    for i in range(1,len(s)):
        if s[i] == s[i-1]:
            cnt += 1
        else:
            tmp = cnt // 2
            aida += tmp
            cnt = 0
    print(aida * k)

# 両端が同じ
else:
    # 先端の個数
    cnt = 1
    for i in range(1,len(s)):
        if s[i] == s[i-1]:
            cnt += 1
        else:
            break
    # 末端の個数
    cnt2 = 1
    for i in range(len(s)-1,-1,-1):
        if s[i] == s[i-1]:
            cnt2 += 1
        else:
            break

    # 先端で書き換える個数
    syoko = cnt // 2

    # 末端＋先端で書き換える個数
    sotogawa = (cnt + cnt2) // 2

    # 先端と末端の間で書き換える個数
    aida = 0
    cnt3 = 0
    print(s[cnt : len(s)-cnt2])
    for i in range(cnt+1,len(s)-cnt2+1):
        if s[i] == s[i-1]:
            cnt3 += 1
        else:
            tmp = cnt3 // 2
            aida += tmp
            cnt3 = 0
    
    saikuru = aida + sotogawa
    print(syoko + saikuru * k - saikuru + aida + cnt2//2)

Submission status: Wrong Answer

Solution code:
from collections import Counter
s = input()
k = int(input())

# 全部同じ
C = Counter(s)
if len(C) == 1:
    print(len(s) * k // 2)
    exit()

# 両端が異なる
if s[0] != s[-1]:
    aida = 0
    cnt = 0
    for i in range(1,len(s)):
        if s[i] == s[i-1]:
            cnt += 1
        else:
            tmp = cnt // 2
            if cnt % 2 != 0:
                aida += tmp + 1
            else:
                aida += tmp
            cnt = 0
    print(aida * k)

# 両端が同じ
else:
    # 先端の個数
    cnt = 1
    for i in range(1,len(s)):
        if s[i] == s[i-1]:
            cnt += 1
        else:
            break
    # 末端の個数
    cnt2 = 1
    for i in range(len(s)-1,-1,-1):
        if s[i] == s[i-1]:
            cnt2 += 1
        else:
            break

    # 先端で書き換える個数
    syoko = cnt // 2

    # 末端＋先端で書き換える個数
    sotogawa = (cnt + cnt2) // 2

    # 先端と末端の間で書き換える個数
    aida = 0
    cnt3 = 0
    #print(s[cnt : len(s)-cnt2])
    for i in range(cnt,len(s)-cnt2+1):
        if s[i] == s[i-1]:
            cnt3 += 1
        else:
            tmp = cnt3 // 2
            if cnt3 % 2 != 0:
                aida += tmp + 1
            else:
                aida += tmp
            cnt = 0
    saikuru = aida + sotogawa
    print(syoko + saikuru * k - saikuru + aida + cnt2//2)

Submission status: Wrong Answer

Solution code:
from collections import Counter
s = input()
k = int(input())

# 全部同じ
C = Counter(s)
if len(C) == 1:
    print(len(s) * k // 2)
    exit()

# 両端が異なる
if s[0] != s[-1]:
    total = 0
    cnt = 0
    for i in range(1,len(s)):
        if s[i] == s[i-1]:
            cnt += 1
        else:
            tmp = cnt // 2
            if cnt % 2 != 0:
                total += tmp + 1
            else:
                total += tmp
            cnt = 0
    print(total * k)

# 両端が同じ
else:
    # 先端の個数
    cnt = 1
    for i in range(1,len(s)):
        if s[i] == s[i-1]:
            cnt += 1
        else:
            break
    # 末端の個数
    cnt2 = 1
    for i in range(len(s)-1,-1,-1):
        if s[i] == s[i-1]:
            cnt2 += 1
        else:
            break

    # 先端で書き換える個数
    syoko = cnt // 2

    # 末端＋先端で書き換える個数
    saikuru = (cnt + cnt2) // 2

    # 先端と末端の間で書き換える個数
    total = 0
    cnt3 = 0
    #print(s[cnt : len(s)-cnt2])
    for i in range(cnt,len(s)-cnt2+1):
        if s[i] == s[i-1]:
            cnt3 += 1
        else:
            tmp = cnt3 // 2
            if cnt3 % 2 != 0:
                total += tmp + 1
            else:
                total += tmp
            cnt = 0
    saikuru += total
    print(syoko + saikuru * k - saikuru + cnt2//2 + total)

Submission status: Wrong Answer

Solution code:
from collections import Counter
s = input()
k = int(input())

# 全部同じ
C = Counter(s)
if len(C) == 1:
    print(len(s) * k // 2)
    exit()

# 両端が異なる
if s[0] != s[-1]:
    total = 0
    cnt = 0
    for i in range(1,len(s)):
        if s[i] == s[i-1]:
            cnt += 1
        else:
            tmp = cnt // 2
            if cnt % 2 != 0:
                total += tmp + 1
            else:
                total += tmp
            cnt = 0
    print(total * k)

# 両端が同じ
else:
    # 先端の個数
    cnt = 1
    for i in range(1,len(s)):
        if s[i] == s[i-1]:
            cnt += 1
        else:
            break
    # 末端の個数
    cnt2 = 1
    for i in range(len(s)-1,-1,-1):
        if s[i] == s[i-1]:
            cnt2 += 1
        else:
            break

    # 先端で書き換える個数
    syoko = cnt // 2

    # 末端＋先端で書き換える個数
    saikuru = (cnt + cnt2) // 2

    # 先端と末端の間で書き換える個数
    total = 0
    cnt3 = 0
    
    for i in range(cnt,len(s)-cnt2):
        if s[i] == s[i-1]:
            cnt3 += 1
        else:
            tmp = cnt3 // 2
            if cnt3 % 2 != 0:
                total += tmp + 1
            else:
                total += tmp
            cnt = 0
    saikuru += total
    print(syoko + saikuru * k - saikuru + cnt2//2 + total)

Submission status: Wrong Answer

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

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

prev = s[0]
ans = 0
for i in range(1, len(s)):
    if s[i] == prev:
        ans += 1
        s = s[:i] + '@' + s[i+1:]
        prev = '@'
    else:
        prev = s[i]

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

Submission status: Wrong Answer

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

prev = s[0]
ans = 0
for i in range(1, len(s)):
    if s[i] == prev:
        ans += 1
        s = s[:i] + '@' + s[i+1:]
        prev = '@'
    else:
        prev = s[i]
if s[0] == s[-1]:
    print(ans*k + k-1)
else:
    print(ans*k)

Submission status: Wrong Answer

Solution code:
from collections import Counter
s = input()
k = int(input())

C = Counter(s)
if len(C) == 1:
    if len(s) % 2 == 0:
        print(len(s)//2 * k)
        exit()
    else:
        print(len(s) * k // 2)
        exit()

if s[0] != s[-1]:
    total = 0
    cnt = 0
    for i in range(1,len(s)):
        if s[i] == s[i-1]:
            cnt += 1
        else:
            tmp = cnt // 2
            if cnt % 2 != 0:
                total += tmp + 1
            else:
                total += tmp
            cnt = 0
    print(total * k)

else:
    cnt = 1
    for i in range(1,len(s)):
        if s[i] == s[i-1]:
            cnt += 1
        else:
            break
    cnt2 = 1
    for i in range(len(s)-1,-1,-1):
        if s[i] == s[i-1]:
            cnt2 += 1
        else:
            break
    syoko = cnt // 2
    saikuru = (cnt + cnt2) // 2

    total = 0
    cnt3 = 0
    for i in range(cnt+1,len(s)-cnt2+1):
        if s[i] == s[i-1]:
            cnt3 += 1
        else:
            tmp = cnt3 // 2
            if cnt3 % 2 != 0:
                total += tmp + 1
            else:
                total += tmp
            cnt = 0
    saikuru += total
    print(syoko + saikuru * k - saikuru + cnt2//2 + total)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
now,cnt,num = S[0],1,0
for i in S[1:-1]:
    if i == now:
        cnt += 1
    else:
        num += cnt//2
        cnt = 1
        now = i
if S[-1] == now:
    num += (cnt+1)//2
else: num += cnt//2

S = S + S
now,cnt,num2 = S[0],1,0
for i in S[1:-1]:
    if i == now:
        cnt += 1
    else:
        num2 += cnt//2
        cnt = 1
        now = i
if S[-1] == now:
    num2 += (cnt+1)//2
else: num2 += cnt//2

length = len(S)//2
if len(set(S)) == 1 and length%2 == 1:
    print(999)
    print(length//2*K + K//2)
    exit()

if num2 - num*2 == 1:
    ans = K - 1 + num*K
else:
    ans = num*K
print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
now,cnt,num = S[0],1,0
for i in S[1:-1]:
    if i == now:
        cnt += 1
    else:
        num += cnt//2
        cnt = 1
        now = i
if S[-1] == now:
    num += (cnt+1)//2
else: num += cnt //2

S = S + S
now,cnt,num2 = S[0],1,0
for i in S[1:-1]:
    if i == now:
        cnt += 1
    else:
        num2 += cnt//2
        cnt = 1
        now = i
if S[-1] == now:
    num2 += (cnt+1)//2
else: num2 += cnt//2

if num2 - num*2 == 1:
    ans = K - 1 + num*K
else:
    ans = num*K
print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
now,cnt,num = S[0],1,0
for i in S[1:-1]:
    if i == now:
        cnt += 1
    else:
        num += cnt//2
        cnt = 1
        now = i
if S[-1] == now:
    num += (cnt+1)//2

S = S + S
now,cnt,num2 = S[0],1,0
for i in S[1:-1]:
    if i == now:
        cnt += 1
    else:
        num2 += cnt//2
        cnt = 1
        now = i
if S[-1] == now:
    num2 += (cnt+1)//2

if num2 - num*2 == 1:
    ans = K - 1 + num*K
else:
    ans = num*K
print(ans)

Submission status: Wrong Answer

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

N = len(S)
#全て同じ文字の時
if len(set(S)) == 1:
    print(N*K//2)
    exit()

#隣合う文字列が何個等しいか
cycle = 0
tmp = S[0]
cnt = 1
for i in range(1, N):
    if tmp != S[i]:
        cycle += cnt//2
        tmp = S[i]
        cnt = 1
    else:
        cnt += 1
cycle += cnt//2

if S[0] == S[-1]:
    i = 0
    a = 0
    tmp = S[0]
    while S[i] == tmp:
        i += 1
        a += 1
    j = N-1
    b = 0
    tmp = S[-1]
    while S[j] == tmp:
        j -= 1
        b += 1
    print((cycle*K) - a//2 - b//2 + (a+b)//2)
else:
    print(cycle*K)

Submission status: Wrong Answer

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

N = len(S)

if len(set(list(S))) == 1:
    print(N*K//2)
    exit()

cycle = 0
i = 1

while i < N:
    if S[i-1] == S[i]:
        cycle += 1
        i += 1
    i += 1


if S[0] == S[-1]:
    i = 0
    a = 0
    tmp = S[0]
    while S[i] == tmp:
        i += 1
        a += 1
    j = N-1
    b = 0
    tmp = S[-1]
    while S[j] == tmp:
        j -= 1
        b += 1
    print((cycle*K) - a//2 - b//2 + (a+b)//2)
else:
    print(cycle*K)

Submission status: Wrong Answer

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

def f(S):
    cnt = 0
    t = 0
    for i in range(1, len(S)):
        if S[i-1] == S[i]:
            t += 1
        else:
            t = 0
        if t % 2 == 1:
            cnt += 1
    return cnt

if len(S) == 1:
    print(K // 2)
elif K < 10:
    print(f(S * K))
else:
    v2 = f(S * 2)
    v3 = f(S * 3)
    print(v3 + (v3-v2) * (K - 3))

Submission status: Wrong Answer

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

def f(S):
    cnt = 0
    t = 0
    for i in range(1, len(S)):
        if S[i-1] == S[i]:
            t += 1
        else:
            t = 0
        if t % 2 == 1:
            cnt += 1
    return cnt

if K < 10:
    print(f(S * K))
else:
    v2 = f(S * 2)
    v3 = f(S * 3)
    print(v3 + (v3-v2) * (K - 3))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
 
 
def main():
    S = input().rstrip()
    K = int(input())
    cnt = 0
    grouped = list()
    for s in S:
        if cnt == 0:
            _prev = s
            cnt += 1
        elif _prev == s:
            cnt += 1
        elif _prev != s and cnt > 0:
            grouped.append([_prev, cnt])
            cnt = 1
            _prev = s
    else:
        if cnt > 0:
            grouped.append([s, cnt])
 
    if K > 1 and len(grouped) > 1 and grouped[0][0] == grouped[-1][0]:
        _sum = 0
        for group in grouped[1:-1]:
            _sum += group[1] // 2
        top = grouped[0][1]
        tail = grouped[-1][1]
        dif = (top // 2) + (tail // 2) + (top + tail) // 2 * (K - 1)
        print(_sum * K + dif)
    else:
        _sum = 0
        for group in grouped:
            _sum += group[1] // 2
        print(_sum * K)
 
 
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from itertools import groupby
import sys
 
input = sys.stdin.readline
 
 
def main():
 
    S = input()
    K = int(input().strip())
    a = [len(tuple(s)) for _, s in groupby(S)]
    ans = 0
 
    if S[0] == S[-1] and K > 1:
        if len(a) == 1:
            ans = a[0] * K // 2
        else:
            ans = a[0] // 2 + a[-1] // 2 + (a[0] + a[-1]) // 2 * (K - 1)
            ans += sum(n // 2 for n in a[1:-1]) * K
 
    else:
        ans = sum(n // 2 for n in a) * K
 
    print(ans)
 
 
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from itertools import groupby
import sys

input = sys.stdin.readline


def main():

    S = input()
    K = int(input())
    a = [len(tuple(s)) for _, s in groupby(S)]
    ans = 0

    if S[0] == S[-1] and K > 1:
        if len(a) == 1:
            ans = a[0] * K // 2
        else:
            ans = a[0] // 2 + a[-1] // 2 + (a[0] + a[-1]) // 2 * (K - 1)
            ans += sum(n // 2 for n in a[1:-1]) * K

    else:
        ans = sum(n // 2 for n in a) * K

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from itertools import groupby
import sys

input = sys.stdin.readline


def main():

    S = input()
    K = int(input())
    a = [len(tuple(s)) for _, s in groupby(S)]
    ans = 0

    if S[0] == S[-1] and K > 1:
        if len(a) == 1:
            ans = a[0] * K // 2
        else:
            ans = a[0] // 2 + a[-1] // 2 + (a[0] + a[-1]) // 2 * (K - 1)
            ans += sum(n // 2 for n in a[1:-1]) * K

    else:
        ans += sum(n // 2 for n in a) * K

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from itertools import groupby
import sys

input = sys.stdin.readline


def main():

    S = input()
    K = int(input())
    a = [len(tuple(s)) for _, s in groupby(S)]
    ans = 0

    if S[0] == S[-1] and K > 1:
        if len(a) == 1:
            ans = a[0] * K // 2
        else:
            ans = a[0] // 2 + a[-1] // 2 + (a[0] + a[-1]) // 2 * (K - 1)
            ans += sum(n // 2 for n in a) * K

    else:
        ans += sum(n // 2 for n in a) * K

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from itertools import groupby
import sys

input = sys.stdin.readline


def main():

    S = input()
    K = int(input())
    a = [len(tuple(s)) for _, s in groupby(S)]
    ans = 0

    if S[0] == S[-1] and K > 1:
        if len(a) == 1:
            ans = a[0] * K // 2
        else:
            ans = a[0] // 2 + a[-1] // 2 + (a[0] + a[-1]) // 2 * (K - 1)
            for n in a[1:-1]:
                ans += (n // 2) * K
    else:
        for n in a:
            ans += (n // 2) * K
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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


def main():
    S = input().strip()
    K = int(input())
    cnt = 0
    grouped = list()
    for s in S:
        if cnt == 0:
            _prev = s
            cnt += 1
        elif _prev == s:
            cnt += 1
        elif _prev != s and cnt > 0:
            grouped.append([_prev, cnt])
            cnt = 1
            _prev = s
    else:
        if cnt > 0:
            grouped.append([s, cnt])

    if K > 1 and len(grouped) > 1 and grouped[0][0] == grouped[-1][0]:
        _sum = 0
        for group in grouped[1:-1]:
            _sum += group[1] // 2
        top = grouped[0][1]
        tail = grouped[-1][1]
        dif = (top // 2) + (tail // 2) + (top + tail) // 2 * (K - 1)
        print(_sum * K + dif)
    else:
        _sum = 0
        for group in grouped:
            _sum += group[1] // 2
        print(_sum * K)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from itertools import groupby


def main():
    S = input().strip()
    K = int(input())
    grouped = [len(tuple(s)) for _, s in groupby(S)]

    if K > 1 and S[0] == S[-1]:
        _sum = 0
        top = grouped[0]
        tail = grouped[-1]
        for group in grouped[1:-1]:
            _sum += group // 2
        dif = (top // 2) + (tail // 2) + (top + tail) * (K - 1) // 2
        print(_sum * K + dif)
    else:
        _sum = 0
        for group in grouped:
            _sum += group // 2
        print(_sum * K)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from itertools import groupby


def main():
    S = input().strip()
    K = int(input())
    grouped = [len(tuple(s)) for _, s in groupby(S)]

    if len(grouped) > 1 and S[0] == S[-1]:
        _sum = 0
        top = grouped[0]
        tail = grouped[-1]
        for group in grouped[1:-1]:
            _sum += group // 2
        dif = (top // 2) + (tail // 2) + (top + tail) * (K - 1) // 2
        print(_sum * K + dif)
    else:
        _sum = 0
        for group in grouped:
            _sum += group // 2
        print(_sum * K)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from itertools import groupby
import sys
input = sys.stdin.readline


def main():
    S = input().strip()
    K = int(input())
    grouped = [len(tuple(s)) for _, s in groupby(S)]

    if len(grouped) > 1 and S[0] == S[-1]:
        _sum = 0
        top = grouped[0]
        tail = grouped[-1]
        for group in grouped[1:-1]:
            _sum += group // 2
        dif = (top // 2) + (tail // 2) + (top + tail) * (K - 1) // 2
        print(_sum * K + dif)
    else:
        _sum = 0
        for group in grouped:
            _sum += group // 2
        print(_sum * K)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# from itertools import groupby
import sys
input = sys.stdin.readline


def main():
    S = input().strip()
    K = int(input())
    cnt = 0
    # grouped = [len(tuple(s)) for _, s in groupby(S)]

    grouped = list()
    for s in S:
        if cnt == 0:
            _prev = s
            cnt += 1
        elif _prev == s:
            cnt += 1
        elif _prev != s and cnt > 0:
            grouped.append([_prev, cnt])
            cnt = 1
            _prev = s
    else:
        if cnt > 0:
            grouped.append([s, cnt])

    if len(grouped) > 1 and S[0] == S[-1]:
        _sum = 0
        _, tail = grouped.pop()
        _, top = grouped.pop(0)

        for group in grouped:
            _sum += group[1] // 2
        dif = (top // 2) + (tail // 2) + (top + tail) * (K - 1) // 2
        print(_sum * K + dif)
    else:
        _sum = 0
        for group in grouped:
            _sum += group[1] // 2
        print(_sum * K)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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


def main():
    S = input().strip()
    K = int(input())
    cnt = 0
    grouped = list()
    for s in S:
        if cnt == 0:
            _prev = s
            cnt += 1
        elif _prev == s:
            cnt += 1
        elif _prev != s and cnt > 0:
            grouped.append([_prev, cnt])
            cnt = 1
            _prev = s
    else:
        if cnt > 0:
            grouped.append([s, cnt])

    if len(grouped) > 1 and grouped[0][0] == grouped[-1][0]:
        _sum = 0
        for group in grouped[1:-1]:
            _sum += group[1] // 2
        top = grouped[0][1]
        tail = grouped[-1][1]
        dif = (top // 2) + (tail // 2) + (top + tail) // 2 * (K - 1)
        print(_sum * K + dif)
    else:
        _sum = 0
        for group in grouped:
            _sum += group[1] // 2
        print(_sum * K)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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


def main():
    S = input().strip()
    K = int(input())
    cnt = 0
    grouped = list()
    for s in S:
        if cnt == 0:
            _prev = s
            cnt += 1
        elif _prev == s:
            cnt += 1
        elif _prev != s and cnt > 0:
            grouped.append([_prev, cnt])
            cnt = 1
            _prev = s
    else:
        if cnt > 0:
            grouped.append([s, cnt])

    if len(grouped) < 2 and grouped[0][0] != grouped[-1][0]:
        _sum = 0
        for group in grouped:
            _sum += group[1] // 2
        print(_sum * K)
    else:
        _sum = 0
        for group in grouped[1:-1]:
            _sum += group[1] // 2
        top = grouped[0][1]
        tail = grouped[-1][1]
        dif = (top // 2) + (tail // 2) + (top + tail) // 2 * (K - 1)
        print(_sum * K + dif)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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


def main():
    S = input().strip()
    K = int(input())
    cnt = 0
    grouped = list()
    for s in S:
        if cnt == 0:
            _prev = s
            cnt += 1
        elif _prev == s:
            cnt += 1
        elif _prev != s and cnt > 0:
            grouped.append([_prev, cnt])
            cnt = 1
            _prev = s
    else:
        if cnt > 0:
            grouped.append([s, cnt])

    if len(grouped) < 2 and grouped[0][0] != grouped[-1][0]:
        _sum = 0
        for group in grouped:
            _sum += group[1] // 2
        print(_sum * K)
    else:
        _sum = 0
        for group in grouped[1:-1]:
            _sum += group[1] // 2
        top = grouped[0][1]
        tail = grouped[0][1]
        dif = (top // 2) + (tail // 2) + (top + tail) // 2 * (K - 1)
        print(_sum * K + dif)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
 
# first == end
# 
total = 0
skip = False
first_end_check = True
f = False
 
if len(S) == 1:
  S = S*K
  f = True
 
for i in range(len(S)-1):
  if skip:
    skip = False
    continue
  if S[i] == S[i+1]:
    #print(i)
    if i+1 == len(S)-1:
      first_end_check = False
    total += 1
    skip = True
 
if K > 1 and first_end_check and S[0] == S[-1]:
  total += 1
    
if f:
  print(total)
else:
  print(total*K)

Submission status: Wrong Answer

Solution code:
from itertools import groupby
import sys
input = sys.stdin.readline


def main():

    S = input()
    K = int(input())
    a = [len(tuple(s)) for _, s in groupby(S)]
    ans = 0

    if S[0] == S[-1] and K > 1:
        if len(a) == 1:
            ans = a[0] * K // 2
        else:
            ans = a[0] // 2 + a[-1] // 2 + (a[0] + a[-1]) // 2 * (K - 1)
            ans += sum(n // 2 for n in a[1:-1]) * K
    else:
        ans = sum(n // 2 for n in a) * K
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from itertools import groupby
import sys
input = sys.stdin.readline


def main():
    S = input()
    K = int(input())
    a = [len(tuple(s)) for _, s in groupby(S)]
    ans = 0

    if S[0] == S[-1] and K > 1:
        if len(a) == 1:
            ans = a[0] * K // 2
        else:
            ans = a[0] // 2 + a[-1] // 2 + (a[0] + a[-1]) // 2 * (K - 1)
            ans += sum([n // 2 for n in a[1:-1]]) * K
    else:
        ans = sum([n // 2 for n in a]) * K
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

# first == end
# 
total = 0
skip = False
first_end_check = True
f = False

if len(S) == 1:
  S = S*K
  f = True

for i in range(len(S)-1):
  if skip:
    skip = False
    continue
  if S[i] == S[i+1]:
    #print(i)
    if i+1 == len(S)-1:
      first_end_check = False
    total += 1
    skip = True

if first_end_check and S[0] == S[-1]:
  total += 1
    
if f:
  print(total)
else:
  print(total*K)

Submission status: Wrong Answer

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


def main():
    S = input().strip()
    K = int(input())
    cnt = 0
    grouped = list()
    for s in S:
        if cnt == 0:
            _prev = s
            cnt += 1
        elif _prev == s:
            cnt += 1
        elif _prev != s and cnt > 0:
            grouped.append([_prev, cnt])
            cnt = 1
            _prev = s
    else:
        if cnt > 0:
            grouped.append([s, cnt])

    if len(grouped) > 1 and grouped[0][0] == grouped[-1][0]:
        _, cnt = grouped.pop()
        grouped[0][1] += 1
    _sum = 0
    for group in grouped:
        _sum += group[1] // 2

    print(_sum * K)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

total = 0
skip = False
first_end_check = True

for i in range(len(S)-1):
  if skip:
    skip = False
    continue
  if S[i] == S[i+1]:
    #print(i)
    if i+1 == len(S)-1:
      first_end_check = False
    total += 1
    skip = True

if first_end_check and S[0] == S[-1]:
  total += 1
    
print(total*K)