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

first_s = S[0]
N = len(S)

flg = False
tmp = ""
cnt = 1
cycle = 0
first_cnt = 1
for i in range(1, N):
    if flg:
        if tmp == S[i]:
            cnt += 1
        else:
            cycle += cnt//2
            tmp = S[i]
            cnt = 1
    else:
        if first_s == S[i]:
            first_cnt += 1 
        else:
            flg = True
            tmp = S[i]
ans = 0

if S[0] == S[-1]:
    ans += cycle * K
    ans += ((first_cnt+cnt)//2) * (K-1)
    ans += first_cnt//2
    ans += cnt//2
else:
    cycle += first_cnt//2
    cycle += cnt//2
    ans = cycle*K
print(ans)

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
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            cnt += 1
            S[i+1] = "*"
    res = cnt*K

    if S[0] == S[-1]:
        b = 1
        for i in range(len(S)-1, -1, -1):
            if S[i] == S[i-1]:
                b += 1
            else:
                break
        
        if b % 2 == 1:
            res += K-1
    
    print(res)

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
    for i in range(1, len(S)):
        if S[i-1] == S[i]:
            cnt += 1
            S[i] = "*"
    res = cnt*K
    if S[0] == S[-1]:
        res += K-1
    
    print(res)

Submission status: Wrong Answer

Solution code:
import math

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

ans = 0
i = 0
if len(S) == 1:
    ans = math.floor(K/2)
else:
    while i < len(S)-1:
        if S[i] == S[i+1]:
            ans += 1
            i += 2
        else:
            i += 1
    ans = ans*K
print(ans)

Submission status: Wrong Answer

Solution code:
import math

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

ans = 0
i = 0
if len(S) == 1:
    ans = math.ceil(K/2)
else:
    while i < len(S)-1:
        if S[i] == S[i+1]:
            ans += 1
            i += 2
        else:
            i += 1
    ans = ans*K
print(ans)

Submission status: Wrong Answer

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

ans = 0
i = 0

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

print(ans*K)

Submission status: Wrong Answer

Solution code:
import re

# thanks to https://qiita.com/norioc/items/324d9210ae2db29d08e6
def nchars(s, n):
    assert n > 0
    reg = re.compile("(.)\\1{%d,}" % (n - 1))
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]

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

connection = list(nchars(S, 2))
count = sum([len(c) // 2 for c in connection]) * K

if S[0] == S[-1] and len(connection) > 0:
    l1 = len(connection[0])
    l2 = len(connection[-1])
    count = count + ( -l1 // 2 - l2 // 2 + (l1 + l2) // 2) * (K - 1)

if len(set(S)) == 1:
    count = K * len(S) // 2

print(count)

Submission status: Wrong Answer

Solution code:
import re

# thanks to https://qiita.com/norioc/items/324d9210ae2db29d08e6
def nchars(s, n):
    assert n > 0
    reg = re.compile("(.)\\1{%d,}" % (n - 1))
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]

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

connection = list(nchars(S, 2))
count = sum([len(c) // 2 for c in connection]) * K

if S[0] == S[-1] and len(connection) > 0:
    l1 = len(connection[0])
    l2 = len(connection[-1])
    count = count + ( -l1 // 2 - l2 // 2 + (l1 + l2) // 2) * (K - 1)

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

print(count)

Submission status: Wrong Answer

Solution code:
import re

# thanks to https://qiita.com/norioc/items/324d9210ae2db29d08e6
def nchars(s, n):
    assert n > 0
    reg = re.compile("(.)\\1{%d,}" % (n - 1))
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]

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

connection = list(nchars(S, 2))
count = sum([len(c) // 2 for c in connection]) * K

if S[0] == S[-1] and len(connection) > 0:
    l1 = len(connection[0])
    l2 = len(connection[-1])
    count = count + ( -l1 // 2 - l2 // 2 + (l1 + l2) // 2) * (K - 1)

print(count)

Submission status: Wrong Answer

Solution code:
import re

# thanks to https://qiita.com/norioc/items/324d9210ae2db29d08e6
def nchars(s, n):
    assert n > 0
    reg = re.compile("(.)\\1{%d,}" % (n - 1))
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]

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

connection = list(nchars(S, 2))
count = sum([len(c) // 2 for c in connection]) * K

if len(connection) != 0 and connection[0][0] == connection[-1][0]:
    l1 = len(connection[0])
    l2 = len(connection[-1])
    count = count + ( -l1 // 2 - l2 // 2 + (l1 + l2) // 2) * (K - 1)

print(count)

Submission status: Wrong Answer

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

i, count1 = 1, 0
while i < len(S):
    if S[i] == S[i-1]:
        count1 += 1
        i += 2
    else:
        i += 1

ss = S*2
i, count2 = 1, 0
while i < len(ss):
    if ss[i] == ss[i-1]:
        count2 += 1
        i += 2
    else:
        i += 1

if K == 1:
    print(count1)
else:
    print(count1+(count2-count1)*(K-1))

Submission status: Wrong Answer

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

i, count1 = 1, 0
while i < len(S):
    if S[i] == S[i-1]:
        count1 += 1
        i += 2
    else:
        i += 1

ss = S*2
i, count2 = 1, 0
while i < len(ss):
    if ss[i] == ss[i-1]:
        count2 += 1
        i += 2
    else:
        i += 1

if K == 1:
    print(count1//2)
else:
    print(count1+(count2-count1)*(K-1))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
s = input()
k = int(input())
n = len(s)
b = [1] * n
c = 0
for i in range(1, n):
    if s[i - 1] == s[i] and b[i - 1]:
        c += 1
        b[i] = 0
print(c * k + (s[-1] == s[0] and b[-1]) * (k - 1))

Submission status: Wrong Answer

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

def calc(S):
  ret = 0
  seq = 1
  for i in range(1,len(S)):
    if S[i-1] == S[i]:
      seq += 1
    else:
      ret += seq // 2
      seq = 1
  ret += seq // 2
  return ret
## pK + q（K-1)  として解けるきがします
## pK + q
if K < 1000:
  print(calc(S*K))
else:
  x1 = calc(S)    # p+q
  x2 = calc(S*2)  #2p+q
  print((x2-x1) * K + 2*x1 - x2)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
n = len(s)
if n == 1:
    print(k // 2)
    exit(0)
cnt = 0
flag = False
for i in range(1, n):  
    if s[i] == s[i - 1]:
        if not flag:
            cnt += 1
        flag = not flag
    else:
        flag = False
if s[0] == s[-1]:
    if not flag:
        cnt += 1
ans = cnt * k
print(ans)

Submission status: Wrong Answer

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

    if len(set(S)) == 1:
        print(len(S)*K//2)
    else:
        c, flag = 0, 0
        for i in range(len(S)):
            if i == len(S) - 1:
                if S[-1] == S[0]:
                    c += 1
                    flag = 1
            else:
                if S[i] == S[i + 1]:
                    c += 1
                    S[i + 1] = "-"

        if flag:
            print(c*K - 1)

        else:
            print(c*K)

if __name__ == "__main__":
    main3()

Submission status: Wrong Answer

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

    if len(set(S)) == 1:
        print(len(S)*K//2)
    else:
        c = 0
        flag = 0
        for i in range(len(S) - 1):
            if i == len(S) - 2:
                if S[i] == S[i + 1]:
                    c += 1
                    S[i + 1] = "-"
                else:
                    if S[-1] == S[0]:
                        c += 1
                        flag = 1
            else:
                if S[i] == S[i + 1]:
                    c += 1
                    S[i + 1] = "-"

        if flag:
            print(c*K - 1)

        else:
            print(c*K)

if __name__ == "__main__":
    main3()

Submission status: Wrong Answer

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

    if len(set(S)) == 1:
        print(len(S)*K//2)
    else:
        c = 0
        flag = 0
        for i in range(len(S) - 1):
            if i == len(S) - 2:
                if S[i] == S[i + 1]:
                    c += 1
                else:
                    if S[-1] == S[0]:
                        c += 1
                        flag = 1
            else:
                if S[i] == S[i + 1]:
                    c += 1
                    S[i + 1] = "-"

        if flag:
            print(c*K - 1)

        else:
            print(c*K)

if __name__ == "__main__":
    main2()

Submission status: Wrong Answer

Solution code:
#ACできなかったコード(03.txt/05.txt -> WA)
def main():
    S = input()
    K = int(input())

    S = list(S)

    if len(set(S)) == 1:
        print(len(S)*K//2)
    else:

        c = 0
        for i in range(len(S)-1):
            if i == len(S) - 2:
                if S[i] == S[i + 1]:
                    c += 1
                else:
                    if S[-1] == S[0]:
                        c += 1
            else:
                if S[i] == S[i + 1]:
                    c += 1
                    S[i + 1] = "-"

        print(c*K)

def main2():
    S = input()
    K = int(input())
    S = list(S)

    if len(set(S)) == 1:
        print(len(S)*K//2)
    else:
        c = 0
        for i in range(len(S)):
            if i == len(S) - 1:
                if S[i] == S[0]:
                    c += 1
                    S[i] = "-"
            else:
                if S[i] == S[i + 1]:
                    c += 1
                    S[i + 1] = "-"

        print(c*K)

if __name__ == "__main__":
    main2()

Submission status: Wrong Answer

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

    S = list(S)

    if len(set(S)) == 1:
        print(len(S)*K//2)
    else:

        c = 0
        for i in range(len(S)-1):
            if i == len(S) - 2:
                if S[i] == S[i + 1]:
                    c += 1
                else:
                    if S[-1] == S[0]:
                        c += 1
            else:
                if S[i] == S[i + 1]:
                    c += 1
                    S[i + 1] = "-"

        print(c*K)
        
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

def count(s, ans, count, pre):
    for i in s:
        if pre == i:
            count += 1
        else:
            ans += count//2
            count = 1
        pre = i
    return ans+count//2

first = count(s, 0, 1, '')
second = count(s*2, 0, 1, '')
if 2*first+1 == second and len(s) != 1:
    print(k*first+k-1)
else:
    print(k*second//2)

Submission status: Wrong Answer

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

if len(s) == 1:
    print(k//2)
    sys.exit()
def count(s, ans, count, pre):
    for i in s:
        if pre == i:
            count += 1
        else:
            ans += count//2
            count = 1
        pre = i
    return ans+count//2

first = count(s, 0, 1, '')
second = count(s*2, 0, 1, '')
print(first+(second-first)*(k-1))

Submission status: Wrong Answer

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

first, second = 0, 0

def count(s, ans, count, pre):
    for i in s:
        if pre == i:
            count += 1
        else:
            ans += count//2
            count = 1
        pre = i
    return ans+count//2

first = count(s, 0, 1, '')
second = count(s*2, 0, 1, '')
print(first+(second-first)*(k-1))

Submission status: Wrong Answer

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

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

count = 0
i = 1
flg = [False] * len(S)
while i < len(S):
    if S[i] == S[i-1]:
        count += 1
        flg[i] = True
        i += 2
    else:
        i += 1
count *= K
if (not flg[len(S)-1]) and S[0] == S[-1]:
    count += (K-1)
print(count)

Submission status: Wrong Answer

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

count = 0
i = 1
flg = [False] * len(S)
while i < len(S):
    if S[i] == S[i-1]:
        count += 1
        flg[i] = True
        i += 2
    else:
        i += 1
count *= K
if (not flg[len(S)-1]) and S[0] == S[-1]:
    count += (K-1)
print(count)

Submission status: Wrong Answer

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

count = 0
i = 1
flg = [False] * len(S)
while i < len(S):
    if S[i] == S[i-1]:
        count += 1
        flg[i] = True
        i += 2
    else:
        i += 1
count *= K
if (not flg[len(S)-1]) and S[0] == S[-1]:
    count += 1
print(count)

Submission status: Wrong Answer

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

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

ss=S*2
i=1
count2=0
while i<len(ss):
    if ss[i]==ss[i-1]:
        count2+=1
        i+=1
    i+=1

if 2*count1+1==count2:
    print(K*count1+(K-1))
else:
    print(K*count2//2)

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
S+=S
i=1
count=0
while i<len(S):
    if S[i]==S[i-1]:
        count+=1
        i+=1
    i+=1

if count==1 and len(S)>=6:
    print(K-1)
else:
    print(count*K//2)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

i=1
count=0
N=len(S)
while i<N:
    if S[i]==S[i-1]:
        count+=1
        if i==N-1:
            S+='A'
        i+=1
    i+=1

print(K*count+(K-1)*(S[0]==S[-1]))

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
S_SET = set(S)
if len(S_SET) == 1:
    print(len(S) * K // 2)
else:
    if S[0] == S[-1]:
        head = 1
        for i in range(1, len(S)):
            if S[i] == S[0]:
                head += 1
            else:
                break
        tail = 1
        for i in reversed(range(1, len(S) - 1)):
            if S[i] == S[-1]:
                tail += 1
            else:
                break
        body = 0
        i = 0
        while i < len(S) - 1:
            j = i + 1
            body_tmp = 1
            while j < len(S):
                if S[i] == S[j]:
                    body_tmp += 1
                    j += 1
                else:
                    if body_tmp > 1:
                        body += body_tmp
                    body_tmp = 1
                    i = j - 1
                    break
            i += 1
        print(head // 2 + tail // 2 + (head + tail) // 2 * (K - 1) + body // 2 * K)
    else:
        body = 0
        i = 0
        while i < len(S) - 1:
            body_tmp = 1
            j = i + 1
            while j < len(S):
                if S[i] == S[j]:
                    body_tmp += 1
                    j += 1
                else:
                    if body_tmp > 1:
                        body += body_tmp
                    body_tmp = 1
                    i = j - 1
                    break
            i += 1
        print(body // 2 * K)

Submission status: Wrong Answer

Solution code:
from itertools import groupby

# RUN LENGTH ENCODING str -> tuple
def run_length_encode(S: str):
    """
    'AAAABBBCCDAABBB' -> [('A', 4), ('B', 3), ('C', 2), ('D', 1), ('A', 2), ('B', 3)]
    """
    grouped = groupby(S)
    res = []
    for k, v in grouped:
        res.append((k, len(list(v))))
    return res


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

if set(list(S)) == 1:
    print(len(S) * K // 2)
else:
    rle = run_length_encode(S)
    cnt = 0
    for w, length in rle:
        if length > 1:
            cnt += length // 2

    ans = cnt * K
    if S[0] == S[-1]:
        a = rle[0][1]
        b = rle[-1][1]
        ans -= (a // 2 + b // 2 - (a + b) // 2) * (K - 1)
    print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import groupby

# RUN LENGTH ENCODING str -> tuple
def run_length_encode(S: str):
    """
    'AAAABBBCCDAABBB' -> [('A', 4), ('B', 3), ('C', 2), ('D', 1), ('A', 2), ('B', 3)]
    """
    grouped = groupby(S)
    res = []
    for k, v in grouped:
        res.append((k, len(list(v))))
    return res


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

if set(list(S)) == 1:
    print(K // 2)
else:
    rle = run_length_encode(S)
    cnt = 0
    for w, length in rle:
        if length > 1:
            cnt += length // 2

    ans = cnt * K
    if S[0] == S[-1]:
        a = rle[0][1]
        b = rle[-1][1]
        ans -= (a // 2 + b // 2 - (a + b) // 2) * (K - 1)
    print(ans)

Submission status: Wrong Answer

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

first = 0
first_second = 0
i = 0
while i < len(S) - 1:
    if S[i] == S[i + 1]:
        first += 1
        i += 2
    else:
        i += 1

SS = S + S
i = 0
while i < len(SS) - 1:
    if SS[i] == SS[i + 1]:
        first_second += 1
        i += 2
    else:
        i += 1

# print(first, first_second)
ans = K // 2 * first_second
if K & 1:
    ans += first
print(ans)

Submission status: Wrong Answer

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

li = []
cnt = 1
for i in range(len(S)-1):
  if S[i] == S[i+1]:
    cnt += 1
  else:
    li.append(cnt)
    cnt = 1
li.append(cnt)
  
ans = 0
first = li[0]
last = li[-1]
if (S[0]==S[-1]) and (len(li)>1):
  li[-1] += first
  li.pop(0)
  for l in li:
    ans += l // 2
  ans *= K
  ans -= li[-1] // 2
  ans += first//2 + last//2
else:
  for l in li:
    ans += l // 2
  ans *= K
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd, ceil
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')
import string
# string.ascii_lowercase
from bisect import bisect_left

def solve():
    # n, m = [int(x) for x in input().split()]
    s = [x for x in input()]
    k = int(input())
    n = len(s)
    if len(set(s)) == 1:
        print(n*k//2)
        return
    b = [1]
    for i in range(1, n):
        if s[i] == s[i-1]:
            b[-1] += 1
        else:
            b.append(1)
    ans = 0
    for e in b:
        ans += e // 2 * k

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


t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""

azyxwvutsrqponmlkjihgfedcb

"""

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd, ceil
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')
import string
# string.ascii_lowercase
from bisect import bisect_left

def solve():
    # n, m = [int(x) for x in input().split()]
    s = [x for x in input()]
    k = int(input())
    n = len(s)
    if s == s[0] * n:
        print(n*k//2)
        return
    b = [1]
    for i in range(1, n):
        if s[i] == s[i-1]:
            b[-1] += 1
        else:
            b.append(1)
    ans = 0
    for e in b:
        ans += e // 2 * k

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


t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""

azyxwvutsrqponmlkjihgfedcb

"""

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd, ceil
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')
import string
# string.ascii_lowercase
from bisect import bisect_left

def solve():
    # n, m = [int(x) for x in input().split()]
    s = [x for x in input()]
    k = int(input())
    n = len(s)
    if s == s[0] * n:
        print(n*k//2)
        return
    b = [1]
    for i in range(1, n):
        if s[i] == s[i-1]:
            b[-1] += 1
        else:
            b.append(1)
    ans = 0
    for e in b:
        ans += e // 2 * k

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


t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""

azyxwvutsrqponmlkjihgfedcb

"""

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd, ceil
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')
import string
# string.ascii_lowercase
from bisect import bisect_left

def solve():
    # n, m = [int(x) for x in input().split()]
    s = [x for x in input()]
    k = int(input())
    n = len(s)
    if s == s[0] * n:
        print(n*k//2)
        return
    b = [1]
    for i in range(1, n):
        if s[i] == s[-1]:
            b[-1] += 1
        else:
            b.append(1)
    ans = 0
    for e in b:
        ans += e // 2 * k

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


t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""

azyxwvutsrqponmlkjihgfedcb

"""

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd, ceil
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')
import string
# string.ascii_lowercase
from bisect import bisect_left

def solve():
    # n, m = [int(x) for x in input().split()]
    s = [x for x in input()]
    k = int(input())
    n = len(s)
    b = [1]
    for i in range(1, n):
        if s[i] == s[-1]:
            b[-1] += 1
        else:
            b.append(1)
    ans = 0
    for e in b:
        ans += e // 2 * k
    if s[0] == s[-1]:
        ans -= (b[0] // 2 * k + b[-1] // 2 * k)
        ans += (b[0] + b[-1]) // 2 * k
    
    print(ans)
    

t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""

azyxwvutsrqponmlkjihgfedcb

"""

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd, ceil
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')
import string
# string.ascii_lowercase
from bisect import bisect_left

def solve():
    # n, m = [int(x) for x in input().split()]
    s = [x for x in input()]
    a = s[:]
    k = int(input())
    if len(s) == 1:
        print(k // 2)
        return
    if len(set(s)) == 1:
        print((len(s) * k)//2)
        return
    cnt = 0
    for i in range(1, len(s)):
        if s[i] == s[i-1]:
            s[i] = '.'
            cnt += 1

    cnt *= k
    
    if s[0] == s[-1] and s[0] != '.':
        cnt += (k - 1)

    print(cnt)


t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""

azyxwvutsrqponmlkjihgfedcb

"""

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd, ceil
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')
import string
# string.ascii_lowercase
from bisect import bisect_left

def solve():
    # n, m = [int(x) for x in input().split()]
    s = [x for x in input()]
    a = s[:]
    k = int(input())
    if len(s) == 1:
        print(k // 2)
        return
    cnt = 0
    for i in range(1, len(s)):
        if s[i] == s[i-1]:
            s[i] = '.'
            cnt += 1
    ans = 0
    for i in range(len(s)-1):
        if a[i] == a[i+1]:
            a[i] = '.'
            ans += 1

    ans *= k
    cnt *= k
    if a[0] == a[-1] and a[0] != '.':
        ans += (k - 1)
    
    if s[0] == s[-1] and s[0] != '.':
        cnt += (k - 1)

    print(min(cnt,ans))


t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""

azyxwvutsrqponmlkjihgfedcb

"""

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd, ceil
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')
import string
# string.ascii_lowercase
from bisect import bisect_left

def solve():
    # n, m = [int(x) for x in input().split()]
    s = [x for x in input()]
    a = s[:]
    k = int(input())
    cnt = 0
    for i in range(1, len(s)):
        if s[i] == s[i-1]:
            s[i] = '.'
            cnt += 1
    ans = 0
    for i in range(len(s)-1):
        if a[i] == a[i+1]:
            a[i] = '.'
            ans += 1

    ans *= k
    cnt *= k
    if a[0] == a[-1] and a[0] != '.':
        ans += (k - 1)
    
    if s[0] == s[-1] and s[0] != '.':
        cnt += (k - 1)
        
    print(min(cnt,ans))


t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""

azyxwvutsrqponmlkjihgfedcb

"""

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd, ceil
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')
import string
# string.ascii_lowercase
from bisect import bisect_left

def solve():
    # n, m = [int(x) for x in input().split()]
    s = [x for x in input()]
    k = int(input())
    cnt = 0
    for i in range(1, len(s)):
        if s[i] == s[i-1]:
            s[i] = '.'
            cnt += 1

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


t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""

azyxwvutsrqponmlkjihgfedcb

"""

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

def main():
    S = input()
    K = int(input())

    cnt = 0
    ans = 0
    s_temp = S[0]
    fcnt = 0
    for s in S:
        if s_temp == s:
            cnt += 1
            s_temp = s
        else:
            ans += cnt // 2
            if fcnt == 0:
                fcnt = cnt
            cnt = 1
            s_temp = s
    if fcnt == 0:
        fcnt = cnt
    ans += cnt // 2
    ans *= K
    if len(S) == 1:
        ans == K // 2
    elif len(S) == fcnt:
        ans = (len(S) * K) // 2
    elif S[0] == S[-1]:
        ans -= (fcnt // 2 + cnt // 2 - (fcnt + cnt) // 2) * (K - 1)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

def main():
    S = input()
    K = int(input())

    cnt = 0
    ans = 0
    s_temp = S[0]
    fcnt = 0
    for s in S:
        if s_temp == s:
            cnt += 1
            s_temp = s
        else:
            ans += cnt // 2
            if fcnt == 0:
                fcnt = cnt
            cnt = 1
            s_temp = s
    ans += cnt // 2
    ans *= K
    if len(S)-1 == fcnt:
        ans = (len(S) * K) // 2
    elif S[0] == S[-1]:
        ans -= (fcnt // 2 + cnt // 2 - (fcnt + cnt) // 2) * (K - 1)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

def main():
    S = input()
    K = int(input())

    cnt = 0
    ans = 0
    s_temp = S[0]
    fcnt = 0
    for s in S:
        if s_temp == s:
            cnt += 1
            s_temp = s
        else:
            ans += cnt // 2
            if fcnt == 0:
                fcnt = cnt
            cnt = 1
            s_temp = s
    ans += cnt // 2
    ans *= K
    if len(S) == fcnt:
        ans = (S*K)//2
    elif S[0] == S[-1]:
        ans -= (fcnt // 2 + cnt // 2 - (fcnt + cnt) // 2) * (K - 1)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

def main():
    S = input()
    K = int(input())

    cnt = 0
    ans = 0
    s_temp = S[0]
    fcnt = 0
    for s in S:
        if s_temp == s:
            cnt += 1
            s_temp = s
        else:
            ans += cnt // 2
            if fcnt == 0:
                fcnt = cnt
            cnt = 1
            s_temp = s
    ans += cnt // 2
    ans *= K
    if S[0] == S[-1]:
        ans -= (fcnt // 2 + cnt // 2 - (fcnt + cnt) // 2) * (K - 1)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

def main():
    S = input()
    K = int(input())

    cnt = 0
    ans = 0
    s_temp = S[0]
    fcnt = 0
    for s in S:
        if s_temp == s:
            cnt += 1
            s_temp = s
        else:
            ans += cnt // 2
            if fcnt == 0:
                fcnt = cnt
            cnt = 1
            s_temp = s
    ans += cnt // 2
    ans *= K
    if S[0] == S[-1] and cnt % 2 == 1 and fcnt % 2 == 1:
        ans += K - 1
    if len(S) == 1:
        if K % 2 == 0:
            ans = K // 2
        else:
            ans = K // 2 - 1

    print(ans)


if __name__ == "__main__":
    main()