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()
flag = [1 for _ in range(len(s))]
k = int(input())
counter = 0
for i in range(len(s)):
    if s[i-1] == s[i] and flag[i-1] and flag[i]:
        counter += 1
        flag[i] = 0
        flag[i-1] = 0
print(counter*k - (1 if k == 1 else 0))

Submission status: Wrong Answer

Solution code:
s = input()
flag = [1 for _ in range(len(s))]
k = int(input())
counter = 0
for i in range(len(s)):
    if s[i-1] == s[i] and flag[i-1] and flag[i]:
        counter += 1
        flag[i] = 0
        flag[i-1] = 0
print(counter*k)

Submission status: Wrong Answer

Solution code:
def main():
    S = list(input())
    K = int(input())
    cnt = 0
    if K == 1:
        T = S
        for i in range(len(T)-1):
            if T[i] == T[i+1]:
                T[i+1] = '.'
        ans = T.count('.')
        print(ans)
        return

    if len(S) == 1:
        ans = max(0, (K - (K%2==1))//2)
        print(ans)
        return

    T3 = S + S + S
    for i in range(len(T3)-1):
        if T3[i] == T3[i+1]:
            T3[i+1] = '.'
    
    T2aster = T3[len(S):(2*len(S))].count('.')
    T3aster = T3.count('.')
    ans = T3aster + T2aster * (K-3)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    S = list(input())
    K = int(input())
    cnt = 0
    if K == 1:
        T = S
        for i in range(len(T)-1):
            if T[i] == T[i+1]:
                T[i+1] = '.'
        ans = T.count('.')
        print(ans)
        return
    if K == 2:
        T = S*2
        for i in range(len(T)-1):
            if T[i] == T[i+1]:
                T[i+1] = '.'
        ans = T.count('.')
        print(ans)
        return
    
    T3 = S + S + S
    for i in range(len(T3)-1):
        if T3[i] == T3[i+1]:
            T3[i+1] = '.'
    
    T2aster = T3[len(S):(2*len(S))].count('.')
    T3aster = T3.count('.')
    ans = T3aster + T2aster * (K-3)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    S = list(input())
    K = int(input())
    cnt = 0
    if K == 1:
        T = S
        for i in range(len(T)-1):
            if T[i] == T[i+1]:
                T[i+1] = '.'
        ans = T.count('.')
        print(ans)
        return

    T3 = S + S + S
    for i in range(len(T3)-1):
        if T3[i] == T3[i+1]:
            T3[i+1] = '.'
    
    T2aster = T3[len(S):(2*len(S))].count('.')
    T3aster = T3.count('.')
    ans = T3aster + T2aster * (K-3)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
S,K=list(input()),int(input())
ans=0 
for i in range(1,len(S)):
    if S[i-1]==S[i]:
        S[i]='A'
        ans+=1
print(ans*K+K-1 if(S[0]==S[-1] and K!=1)else ans*K)

Submission status: Wrong Answer

Solution code:
S,K=list(input()),int(input())
ans=0 
for i in range(1,len(S)):
    if S[i-1]==S[i]:
        S[i]='A'
        ans+=1
if S[0]==S[-1]:ans+=1
print(ans*K)

Submission status: Wrong Answer

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
from heapq import heappop, heappush
from collections import defaultdict
from collections import Counter
from collections import deque
from itertools import permutations
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())
S = input()
K = int(input())
ans = 0

S = list(S)
def f(T):
    ret = 0
    l = 0
    r = 1
    n = len(T)
    while l < n:
        while r < n and T[l] == T[r]:
            r += 1
        c = r - l
        ret += c // 2
        l = r
        r = l + 1
    return ret

first = f(S)
second = f(S + S)
diff = second - first

print(first + diff * (K - 1))

Submission status: Wrong Answer

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
from heapq import heappop, heappush
from collections import defaultdict
from collections import Counter
from collections import deque
from itertools import permutations
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())
S = input()
origin = S
K = int(input())
ans = 0
S += S[0]
n = len(S)

S = list(S)
l = 0
r = 1
while l < n:
    while r < n and S[l] == S[r]:
        r += 1
    c = r - l
    for i in range(c):
        if i % 2 == 1: 
            S[l + i] = '.'
    ans += c // 2
    l = r
    r = l + 1
ans *= K
if S[-2] == '.' and origin[-1] != origin[-2]:
    ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
# map(int, input().split())
S = input()
K = int(input())
ans = 0
S += S[0]
n = len(S)

l = 0
r = 1
while l < n:
    while r < n and S[l] == S[r]:
        r += 1
    c = r - l
    ans += c // 2
    l = r
    r = l + 1
ans *= K
print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
before = ''
count = 0
change_cnt = 0
for c in S:
  if c == before:
    count += 1
  else:
    if count >= 0:
      change_cnt += (count+1)//2
      count = 0
    before = c
if count > 0:
    change_cnt += (count+1)//2
c_cnt = 0
if S[0] == S[-1]:
  for c in S:
    if c == before:
      c_cnt += 1
    else:
      break
all_same = False
plus = False
if count%2 == 0 and c_cnt%2 != 0:
  plus = True
if count+c_cnt >= len(S):
  all_same = True
print(all_same, plus, count, change_cnt, c_cnt)  
if all_same:
  print((K//2)*len(S) + (K%2)*(len(S)//2))
elif plus:
  print(K*change_cnt + K-1)
else:
  print(K*change_cnt)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
before = ''
count = 0
change_cnt = 0
for c in S:
  if c == before:
    count += 1
  else:
    if count >= 0:
      change_cnt += (count+1)//2
      count = 0
    before = c
if count > 0:
    change_cnt += (count+1)//2
c_cnt = 0
if S[0] == S[-1]:
  for c in S:
    if c == before:
      c_cnt += 1
    else:
      break
all_same = False
plus = False
if count%2 == 0 and c_cnt%2 != 0:
  plus = True
if count+c_cnt > len(S):
  all_same = True
# print(all_same, plus, count, change_cnt, c_cnt)  
if all_same:
  print((K//2)*len(S) + (K%2)*(len(S)//2))
elif plus:
  print(K*change_cnt + K-1)
else:
  print(K*change_cnt)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
before = ''
count = 0
change_cnt = 0
for c in S:
  if c == before:
    count += 1
  else:
    if count >= 0:
      change_cnt += (count+1)//2
      count = 0
    before = c
if count > 0:
    change_cnt += 1
c_cnt = 0
if S[0] == S[-1]:
  for c in S:
    if c == before:
      c_cnt += 1
    else:
      break
all_same = False
plus = False
if count%2 == 0 and c_cnt%2 != 0:
  plus = True
if count+c_cnt > len(S):
  all_same = True
# print(all_same, plus, count, change_cnt, c_cnt)  
if all_same:
  print((K//2)*len(S) + (K%2)*(len(S)//2))
elif plus:
  print(K*change_cnt + K-1)
else:
  print(K*change_cnt)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
before = ''
count = 0
change_cnt = 0
for c in S:
  if c == before:
    count += 1
  else:
    if count >= 0:
      change_cnt += (count+1)//2
      count = 0
    before = c
if count > 0:
    change_cnt += (count+1)//2
c_cnt = 0
if S[0] == S[-1]:
  for c in S:
    if c == before:
      c_cnt += 1
    else:
      break
all_same = False
plus = False
if count%2 == 0 and c_cnt%2 != 0:
  plus = True
if count > len(S):
  all_same = True
# print(all_same, plus, count, change_cnt, c_cnt)  
if all_same:
  print((K//2)*len(S) + (K%2)*(len(S)//2))
elif plus:
  print(K*change_cnt + K-1)
else:
  print(K*change_cnt)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
before = ''
count = 0
change_cnt = 0
for c in S:
  if c == before:
    count += 1
  else:
    if count >= 0:
      change_cnt += (count+1)//2
      count = 0
    before = c
if count > 0:
    change_cnt += 1
c_cnt = 0
if S[0] == S[-1]:
  for c in S:
    if c == before:
      c_cnt += 1
    else:
      break
all_same = False
plus = False
if count%2 == 0 and c_cnt%2 != 0:
  plus = True
if count > len(S):
  all_same = True
# print(all_same, plus, count, change_cnt, c_cnt)  
if all_same:
  print((K//2)*len(S) + (K%2)*(len(S)//2))
elif plus:
  print(K*change_cnt + K-1)
else:
  print(K*change_cnt)

Submission status: Wrong Answer

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

    count_l = []
    i = 0
    counter = 1
    while True:
        if len(s) == 1:
            count_l.append(counter)
            break
        elif s[i] == s[i+1]:
            counter += 1
        else:
            count_l.append(counter)
            counter = 1
        i += 1
        if i == len(s)-1:
            if counter == 1:
                break
            else:
                count_l.append(counter)
                break

    answer = 0
    if len(count_l) >= 3 and s[0] == s[-1] and k >= 2:
        sub_l = [count_l[0], count_l[-1]] + count_l[1:-2]
        print(sub_l)
        for i in range(len(sub_l)):
            answer += (sub_l[i] // 2) * (k - 1)
        for j in range(len(count_l[:-2])):
            answer += count_l[j] // 2
        answer += count_l[-1] // 2
    elif len(count_l) == 1:
        answer += (count_l[0] * k) // 2
    else:
        for i in range(len(count_l)):
            answer += (count_l[i] // 2) * k

    print(answer)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

    count_l = []
    i = 0
    counter = 1
    while True:
        if len(s) == 1:
            count_l.append(counter)
            break
        elif s[i] == s[i+1]:
            counter += 1
        else:
            count_l.append(counter)
            counter = 1
        i += 1
        if i == len(s)-1:
            if counter == 1:
                break
            else:
                count_l.append(counter)
                break

    answer = 0
    if len(count_l) >= 3 and s[0] == s[:-1] and k >= 2:
        sub_l = list(count_l[0], count_l[-1]) + count_l[1:-2]
        print(sub_l)
        for i in range(len(sub_l)):
            answer += (sub_l[i] // 2) * (k - 1)
        for j in range(len(count_l[:-2])):
            answer += count_l[j] // 2
        answer += count_l[:-1] // 2
    elif len(count_l) == 1:
        answer += (count_l[0] * k) // 2
    else:
        for i in range(len(count_l)):
            answer += (count_l[i] // 2) * k

    print(answer)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

    count_l = []
    i = 0
    counter = 1
    while True:
        if len(s) == 1:
            count_l.append(counter)
            break
        elif s[i] == s[i+1]:
            counter += 1
        else:
            count_l.append(counter)
            counter = 1
        i += 1
        if i == len(s)-1:
            if counter == 1:
                break
            else:
                count_l.append(counter)
                break

    answer = 0
    if len(count_l) >= 3 and s[0] == s[:-1]:
        sub_l = count_l[0]+count_l[-1] + count_l[1:-2]
        print(sub_l)
        if k >= 2:
            for i in range(len(sub_l)):
                answer += (sub_l[i] // 2) * (k - 1)
            for j in range(len(count_l[:-2])):
                answer += count_l[j] // 2
            answer += count_l[:-1] // 2
        else:
            for i in range(count_l):
                answer += (count_l[i]//2) * k
    elif len(count_l) == 1:
        answer += (count_l[0] * k) // 2
    else:
        for i in range(len(count_l)):
            answer += (count_l[i] // 2) * k

    print(answer)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

    count_l = []
    i = 0
    counter = 1
    while True:
        if len(s) == 1:
            count_l.append(counter)
            break
        elif s[i] == s[i+1]:
            counter += 1
        else:
            count_l.append(counter)
            counter = 1
        i += 1
        if i == len(s)-1:
            if counter == 1:
                break
            else:
                count_l.append(counter)
                break

    answer = 0
    if len(count_l) >= 3 and s[0] == s[:-1]:
        sub_l = count_l[0]+count_l[-1] + count_l[1:-2]
        if k >= 2:
            for i in range(len(sub_l)):
                answer += (sub_l[i] // 2) * (k - 1)
            for j in range(len(count_l[:-2])):
                answer += count_l[j] // 2
            answer += count_l[:-1] // 2
    elif len(count_l) == 1:
        answer += (count_l[0] * k) // 2
    else:
        for i in range(len(count_l)):
            answer += (count_l[i] // 2) * k

    print(answer)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

renzoku_flag =False
renzoku_count = 0
count = 0
for i in range(n):
    if i != n-1:
        if s[i] == s[i+1]:
            renzoku_count += 1 #iのをカウントしている
        else:
            renzoku_count += 1 #i+1の分をカウント
            count += renzoku_count // 2
            renzoku_count = 0
    else:
        if s[n-2] == s[n-1]:
            renzoku_count += 1
        count += renzoku_count // 2

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

    start_count = 0
    for i in range(n):
        if s[i] == initial:
            start_count += 1
        else:
            break

    end_count = 0
    for i in range(n)[::-1]:
        if s[i] == initial:
            end_count += 1
        else:
            break

    ans -= (start_count//2 + end_count//2 - (start_count+end_count)//2)*(k-1)
    print(ans)

Submission status: Wrong Answer

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

renzoku_flag =False
renzoku_count = 0
count = 0

if s[0] != s[-1]:
    for i in range(n):
        if i != n-1:
            if s[i] == s[i+1]:
                renzoku_count += 1 #iのをカウントしている
            else:
                renzoku_count += 1 #i+1の分をカウント
                count += renzoku_count // 2
                renzoku_count = 0
        else:
            if s[n-2] == s[n-1]:
                renzoku_count += 1
            count += renzoku_count // 2
    print(count*k)
else:
    ans = 0
    start_count = 0
    initial = s[0]
    start_idx = 0
    for i in range(n):
        if s[i] == initial:
            start_count += 1
        else:
            start_idx = i
            break
    else:
        print(n*k // 2)
        exit()

    ans += start_count // 2
    print(ans)

    end_count = 0
    end_idx = 0
    for i in range(n)[::-1]:
        if s[i] == initial:
            end_count += 1
        else:
            end_idx = i+1
            break

    ans += ((start_count + end_count) // 2)*(k-1)
    ans += end_count // 2

    for i in range(start_idx, end_idx):
        if i != end_idx - 1:
            if s[i] == s[i + 1]:
                renzoku_count += 1  # iのをカウントしている
            else:
                renzoku_count += 1  # i+1の分をカウント
                count += renzoku_count // 2
                renzoku_count = 0
        else:
            if s[end_idx - 2] == s[end_idx - 1]:
                renzoku_count += 1
            count += renzoku_count // 2
    ans += count*k
    print(ans)

Submission status: Wrong Answer

Solution code:
def main():
  S = input()
  N = int(input())
  if len(set(list(S))) == 1 and len(S)%2==1:
    print(N//2)
    return
  Prev = S[0]
  count = 0
  last = 0
  for i in range(1,len(S)-1):
    if S[i] == Prev:
      count+=1
      Prev = '#'
    else:
      Prev=S[i]
  if Prev == S[-1]:
    count+=1
  elif S[1] == S[-1]:
    last+=1
  print(count*N+(N-1)*last)
  
main()

Submission status: Wrong Answer

Solution code:
def main():
  S = input()
  N = int(input())
  if len(set(list(S))) == 1:
    print(N//2)
    return
  Prev = S[0]
  count = 0
  last = 0
  for i in range(1,len(S)-1):
    if S[i] == Prev:
      count+=1
      Prev = '#'
    else:
      Prev=S[i]
  if Prev == S[-1]:
    count+=1
  elif S[1] == S[-1]:
    last+=1
  print(count*N+(N-1)*last)
  
main()

Submission status: Wrong Answer

Solution code:
def main():
  S = input()
  N = int(input())
  if len(S) == 1:
    print(N//2)
    return
  Prev = S[0]
  count = 0
  last = 0
  for i in range(1,len(S)-1):
    if S[i] == Prev:
      count+=1
      Prev = '#'
    else:
      Prev=S[i]
  if Prev == S[-1]:
    count+=1
  elif S[1] == S[-1]:
    last+=1
  print(count*N+(N-1)*last)
  
main()

Submission status: Wrong Answer

Solution code:
def main():
  S = input()
  N = int(input())
  if len(S) == 1:
    print(N//2)
    return
  Prev = S[0]
  count = 0
  last = 0
  for i in range(1,len(S)-1):
    if S[i] == Prev:
      count+=1
      Prev = '#'
    else:
      Prev=S[i]
  if Prev == S[-1]:
    count+=1
  elif S[1] == S[-1]:
    last+=1
  print(count*N+(N-1)*last)

Submission status: Wrong Answer

Solution code:
S = input()
N = int(input())
Prev = S[0]
count = 0
last = 0
for i in range(1,len(S)-1):
  if S[i] == Prev:
    count+=1
    Prev = '#'
  else:
    Prev=S[i]
if Prev == S[-1]:
  count+=1
elif S[1] == S[-1]:
  last+=1
print(count*N+(N-1)*last)

Submission status: Wrong Answer

Solution code:
import math
S = input()
K = int(input())
c = 0
f = False
if len(S) == 1:
    print(math.floor(K/2))
    exit()
if (S.count(S[0]) == len(S)):
    print(math.floor(len(S)*K / 2))
    exit()

for i in range(len(S)-1):
    if S[i] == S[i+1]:
        if f:
            f = False
            continue
        f = True
        c += 1
    else:
        f = False
bs = 1
for i in range(1,len(S)):
    if S[0] == S[i]:
        bs+=1
    else:
        break
aS = 0
for i in range(len(S)-1,-1,-1):
    if S[0] == S[i]:
        aS += 1
    else:
        break
if aS == 0:
    omake = 0
else:
    omake = math.floor(bs/2) + math.floor(aS/2) - math.floor((bs+aS)/2)
print(c*K+omake*(K-1))

Submission status: Wrong Answer

Solution code:
import math
S = input()
K = int(input())
c = 0
f = False
if len(S) == 1:
    print(math.floor(K/2))
    exit()
if (S.count(S[0]) == len(S)):
    print(math.floor(len(S)*K / 2))
    exit()

for i in range(len(S)-1):
    if S[i] == S[i+1]:
        if f:
            f = False
            continue
        f = True
        c += 1
    else:
        f = False
lc = 0
if not f and K > 1:
    if S[0] == S[-1]:
        pc = 1
        for i in range(1,len(S)):
            if S[0] != S[i]:
                break
            pc += 1
        apc = 1
        for i in range(len(S)-2,-1,-1):
            if S[-1] != S[i]:
                break
            apc += 1
        print(pc,apc)
        if (pc + apc) % 2 == 0:
            lc = 1
print(c*K+lc*(K-1))

Submission status: Wrong Answer

Solution code:
import math
S = input()
K = int(input())
c = 0
f = False
if len(S) == 1:
    print(math.floor(K/2))
    exit()
if (S.count(S[0]) == len(S)):
    print(math.floor(len(S)*K / 2))
    exit()

for i in range(len(S)-1):
    if S[i] == S[i+1]:
        if f:
            f = False
            continue
        f = True
        c += 1
    else:
        f = False
lc = 0
if not f and K > 1:
    if S[0] == S[-1]:
        lc = 1
print(c*K+lc*(K-1))

Submission status: Wrong Answer

Solution code:
import math
S = input()
K = int(input())
c = 0
f = False
if len(S) == 1:
    print(math.floor(K/2))
    exit()
for i in range(len(S)-1):
    if S[i] == S[i+1]:
        if f:
            f = False
            continue
        f = True
        c += 1
    else:
        f = False
lc = 0
if not f and K > 1:
    if S[0] == S[-1]:
        lc = 1
print(c*K+lc*(K-1))

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
c = 0
f = False
for i in range(len(S)-1):
    if S[i] == S[i+1]:
        if f:
            f = False
            continue
        f = True
        c += 1
    else:
        f = False
lc = 0
if not f and K > 1:
    if S[0] == S[-1]:
        lc = 1
print(c*K+lc*(K-1))

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
c = 0
f = False
for i in range(len(S)-1):
    if f:
        f = False
        continue
    if S[i] == S[i+1]:
        f = True
        c += 1
lc = 0
if not f and K > 1:
    if S[0] == S[-1]:
        lc = 1
print(c*K+lc*(K-1))

Submission status: Wrong Answer

Solution code:
s=list(input()) ;k=int(input()) ;n=len(s)
ss=s*2
import sys
mod=10**9+7 ; inf=float("inf")
from math import sqrt, ceil
from collections import deque, Counter, defaultdict #すべてのkeyが用意されてる defaultdict(int)で初期化
input=lambda: sys.stdin.readline().strip()
sys.setrecursionlimit(11451419)
from decimal import ROUND_HALF_UP,Decimal as D  #変換後の末尾桁を0や0.01で指定
  #Decimal((str(0.5)).quantize(Decimal('0'), rounding=ROUND_HALF_UP))
from functools import lru_cache
from bisect import bisect_left as bileft, bisect_right as biright
from fractions import Fraction as F  #F(a,b)で正確なa/b
#メモ化再帰defの冒頭に毎回 @lru_cache(maxsize=10**10)
#引数にlistはだめ
#######ここまでテンプレ#######
#ソート、"a"+"b"、再帰ならPython3の方がいい
#######ここから天ぷら########


if n==1:
    if k==1:print(0);exit()
    if k==2:print(1);exit()
    else:print(k//2);exit()

def qq(s,n):
    ans=0
    if n==1 : return 0
    if n==2:
        if s[0]==s[1]:return 1
        return 0
    for i in range(1,n):
        if s[i]==s[i-1] and i!=n-1:
            ans+=1
            if s[i-1]=="a" and s[i+1]=="a":
                s[i]="b"
            elif s[i-1]=="a" and s[i+1]=="b":
                s[i]="c"
            elif s[i-1]=="b" and s[i+1]=="a":
                s[i]="c"
            elif s[i-1]=="b" and s[i+1]=="b":
                s[i]="a"
            else:s[i]="a"
        if s[i]==s[i-1] and i==n-1:
            ans+=1
            s[i]= "a" if s[i-1]!="a" else "b"
    return ans

if len(Counter(s))==1:
    print((n*k)//2)
    exit()
if s[0]!=s[-1]:
    print(qq(s,n)*k)
    exit()
st=1; go=1
for i in range(1,n):
    if s[i]==s[0]:
        st+=1
    else:
        break
for i  in range(-2,-n-1,-1):
    if s[i]==s[-1]:
        go+=1
    else:break
print(qq(s,n)*k - (st//2+go//2-(st+go)//2)*(k-1))

Submission status: Wrong Answer

Solution code:
s=list(input()) ;k=int(input()) ;n=len(s)
ss=s*2
import sys
mod=10**9+7 ; inf=float("inf")
from math import sqrt, ceil
from collections import deque, Counter, defaultdict #すべてのkeyが用意されてる defaultdict(int)で初期化
input=lambda: sys.stdin.readline().strip()
sys.setrecursionlimit(11451419)
from decimal import ROUND_HALF_UP,Decimal as D  #変換後の末尾桁を0や0.01で指定
  #Decimal((str(0.5)).quantize(Decimal('0'), rounding=ROUND_HALF_UP))
from functools import lru_cache
from bisect import bisect_left as bileft, bisect_right as biright
from fractions import Fraction as F  #F(a,b)で正確なa/b
#メモ化再帰defの冒頭に毎回 @lru_cache(maxsize=10**10)
#引数にlistはだめ
#######ここまでテンプレ#######
#ソート、"a"+"b"、再帰ならPython3の方がいい
#######ここから天ぷら########


if n==1:
    if k==1:print(0);exit()
    if k==2:print(1);exit()
    else:print(k-2);exit()

def qq(s,n):
    ans=0
    if n==1 : return 0
    if n==2:
        if s[0]==s[1]:return 1
        return 0
    for i in range(1,n):
        if s[i]==s[i-1] and i!=n-1:
            ans+=1
            if s[i-1]=="a" and s[i+1]=="a":
                s[i]="b"
            elif s[i-1]=="a" and s[i+1]=="b":
                s[i]="c"
            elif s[i-1]=="b" and s[i+1]=="a":
                s[i]="c"
            elif s[i-1]=="b" and s[i+1]=="b":
                s[i]="a"
            else:s[i]="a"
        if s[i]==s[i-1] and i==n-1:
            ans+=1
            s[i]= "a" if s[i-1]!="a" else "b"
    return ans

if len(Counter(s))==1:
    print((n*k)//2)
    exit()
if s[0]!=s[-1]:
    print(qq(s,n)*k)
    exit()
st=1; go=1
for i in range(1,n):
    if s[i]==s[0]:
        st+=1
    else:
        break
for i  in range(-2,-n-1,-1):
    if s[i]==s[-1]:
        go+=1
    else:break
print(qq(s,n)*k - (st//2+go//2-(st+go)//2)*(k-1))

Submission status: Wrong Answer

Solution code:
s=list(input()) ;k=int(input()) ;n=len(s)
ss=s*2
def qq(s,n):
    ans=0
    if n==1 : return 0
    if n==2:
        if s[0]==s[1]:return 1
        return 0
    for i in range(1,n):
        if s[i]==s[i-1] and i!=n-1:
            ans+=1
            if s[i-1]=="a" and s[i+1]=="a":
                s[i]="b"
            elif s[i-1]=="a" and s[i+1]=="b":
                s[i]="c"
            elif s[i-1]=="b" and s[i+1]=="a":
                s[i]="c"
            elif s[i-1]=="b" and s[i+1]=="b":
                s[i]="a"
            else:s[i]="a"
        if s[i]==s[i-1] and i==n-1:
            ans+=1
            s[i]= "a" if s[i-1]!="a" else "b"
    return ans
ki = qq(s,n)
zoku= qq(ss ,n*2)- ki
print(ki+ (k-1)*zoku)
#print(ki, ss)

Submission status: Wrong Answer

Solution code:
s=input()
n=len(s)
k=int(input())
if s[0]==s[n-1]:
  d=1
  ans=0
  for i in range(1,n):
    if s[i-1]==s[i] and d==1:
      ans+=1
      d=0
    elif s[i-1]==s[i] and d==0:
      d=1
    elif s[i-1]!=s[i]:
      d=1
  if d==0:
    print(ans*k)
  else:
    ans2=0
    for i in range(n):
      if s[i-1]==s[i] and d==1:
        ans2+=1
        d=0
      elif s[i-1]==s[i] and d==0:
        d=1
      elif s[i-1]!=s[i]:
        d=1
    print(ans+ans2*(k-1))
elif s[0]!=s[n-1]:
  d=1
  ans=0
  for i in range(1,n):
    if s[i-1]==s[i] and d==1:
      ans+=1
      d=0
    elif s[i-1]==s[i] and d==0:
      d=1
    elif s[i-1]!=s[i]:
      d=1
  print(ans*k)

Submission status: Wrong Answer

Solution code:
s = input()
t = int(input())
v = set(s)
a = []
o = s[0]
c = 0
for i in range(len(s)):
  if o == s[i]:
    c += 1
  else:
    a.append([o,c])
    c = 1
    o = s[i]
a.append([o,c])

ans = 0

for i,j in a:
  if j == 1:
    continue
  ans += (j//2)*t

if a[0][0] == a[-1][0]:
  if a[0][1]%2 == 1 and a[-1][1]%2 == 1:
    ans += t-1

print (ans)

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
n=len(s)
data=[0]*n
cnt=0
for i in range(1,n):
  if s[i]!=s[i-1]:
    cnt=0
  else:
    if cnt%2==0:
      data[i]=1
    cnt+=1
if s[0]==s[n-1] and data[n-1]==1:
  print(sum(data)*k)
elif s[0]!=s[n-1]:
  print(sum(data)*k)
elif s[0]==s[n-1] and data[n-1]==0:
  data=data+[0]*n
  data[n]=1
  res=1
  for i in range(1,n):
    if s[i]!=s[i-1]:
      res=0
    else:
      if cnt%2==0:
        data[n+i]=1
      res+=1
  print(sum(data)*k//2)

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
n=len(s)
data=0
cnt=0
for i in range(1,n):
  if s[i]!=s[i-1]:
    cnt=0
  else:
    if cnt%2==0:
      data+=1
    cnt+=1
print(data*k)

Submission status: Wrong Answer

Solution code:
# A - Connection and Disconnection
S = input()
K = int(input())
S = S + '1'# 番兵

# 連続する文字数をカウント
cnt = []
conti = 1
for i in range(1,len(S)):
    if S[i-1] == S[i]:
        conti += 1
    else:
        cnt.append(conti)
        conti = 1
        
# 最初と最後の文字が同じときの補正値
comp = 0
if S[0] == S[-2]:
    # 奇数個 + 奇数個のとき
    if cnt[0]%2 == 1 and cnt[-1]%2 == 1:
        comp = K - 1
    # S =aaa などのとき
    if len(set(S[:-1])) == 1:
        comp = K // 2
        
ans = [a//2 for a in cnt if a >= 2]
ans = sum(ans)*K + comp
print(ans)

Submission status: Wrong Answer

Solution code:
# A - Connection and Disconnection
S = input()
K = int(input())
S = S + '1'# 番兵

# 連続する文字数をカウント
cnt = []
conti = 1
for i in range(1,len(S)):
    if S[i-1] == S[i]:
        conti += 1
    else:
        cnt.append(conti)
        conti = 1

# 最初と最後の文字が同じとき
comp = 0
if S[0] == S[-2]:
    # 奇数個 + 奇数個のとき
    if cnt[0]%2 == 1 and cnt[-1]%2 == 1:
        comp = K - 1
        
ans = [a//2 for a in cnt if a >= 2]
ans = sum(ans)*K + comp
print(ans)

Submission status: Wrong Answer

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

prev = S[0]
cnt = 0
for i in range(1,len(S)):
    if prev == S[i]:
        cnt += 1
        prev = "$"
        continue
    prev = S[i]

ans = k*cnt

m = 0
for s in S:
    if s == S[0]:
        m += 1
    else:
        break
a = 0
for s in S[::-1]:
    if s == S[-1]:
        a += 1
    else:
        break
if a%2 and m%2 and S[0]==S[-1]:
    ans += k-1

print(ans)

Submission status: Wrong Answer

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

prev = S[0]
cnt = 0
for i in range(1,len(S)):
    if prev == S[i]:
        cnt += 1
        prev = "$"
        continue
    prev = S[i]

ans = k*cnt
if S[-1] == S[0]:
    cnt += k-1

print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
di = []
cnt = 1
ans = 0
if len(s) > 1:
    for i in range(1,len(s)):
        if s[i] == s[i-1]:
            cnt += 1
        else:
            di.append(cnt)
            cnt = 1
    di.append(cnt)
    if di[0] != len(s):
        for j in di:
            ans += j//2
        ans *= k
        if s[0] == s[-1] and di[-1] % 2 == 1:
            ans += k - 1
    else:
        ans = k*len(s)//2
else:
    ans = k//2
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
di = []
cnt = 1
ans = 0
if len(s) > 1:
    for i in range(1,len(s)):
        if s[i] == s[i-1]:
            cnt += 1
        else:
            di.append(cnt)
            cnt = 1
    di.append(cnt)
    for j in di:
        ans += j//2
    ans *= k
    if s[0] == s[-1] and di[-1] % 2 == 1:
        ans += k - 1
else:
    ans = k//2
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
di = []
cnt = 1
ans = 0
for i in range(1,len(s)):
    if s[i] == s[i-1]:
        cnt += 1
    else:
        di.append(cnt)
        cnt = 1
di.append(cnt)
for j in di:
    ans += j//2
ans *= k
if s[0] == s[-1] and di[-1] % 2 == 1:
    ans += k - 1
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import groupby
s = input()
k = int(input())
g = [len(list(v)) for _, v in groupby(s)]
if len(set(s)) == 1:
    ans = g[0]*k//2
else:
    ans = sum(i//2 for i in g)*k
    if s[0] == s[1]:
        a, b = g[0], g[-1]
        ans -= (a//2 + b//2 - ((a+b)//2))*(k-1)
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import groupby
s = input()
k = int(input())
g = [len(list(v)) for _, v in groupby(s)]
if len(set(s)) == 1:
    ans = g[0]*k//2
else:
    ans = sum(i//2 for i in g)*k
    if s[0] == s[1]:
        a, b = g[0], g[-1]
        ans -= (a//2 + b//2 - (a+b)//2)*(k-1)
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import groupby
s = input()
k = int(input())
g = [len(list(v)) for _, v in groupby(s)]
if len(set(s)) == 1:
    ans = g[0]*k//2
else:
    cnt = sum(i//2 for i in g)
    if s[0] != s[1]:
        ans = cnt*k
    else:
        a, b = g[0], g[-1]
        ans = sum(i//2 for i in g)*k - (k-1)*(a//2 + b//2 - (a+b)//2)
print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
for i in range(len(s)-1):
    if s[i] is s[i+1]:
        s[i+1] = 1
cnt = s.count(1)
print(cnt*k + (k-1)*(s[0] is s[-1]))