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:
from collections import defaultdict, deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys
import random
import itertools
import math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt


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


def LF(): return list(map(float, input().split()))


def LI_(): return list(map(lambda x: int(x)-1, input().split()))


def II(): return int(input())


def IF(): return float(input())


def LS(): return list(map(list, input().split()))


def S(): return list(input().rstrip())


def IR(n): return [II() for _ in range(n)]


def LIR(n): return [LI() for _ in range(n)]


def FR(n): return [IF() for _ in range(n)]


def LFR(n): return [LI() for _ in range(n)]


def LIR_(n): return [LI_() for _ in range(n)]


def SR(n): return [S() for _ in range(n)]


def LSR(n): return [LS() for _ in range(n)]


def gcd(x, y):
    if y == 0:
        return x
    return gcd(y, x % y)


def my_index_multi(l, x):
    return [i for i, _x in enumerate(l) if _x == x]


mod = 1000000007
inf = float('INF')


def A():
    S = input()

    K = II()

    if len(S) == 2:
        print(K//2)
        return

    i = 0
    ans = 0
    A = 1
    while(A > 0):
        count = 0
        while(S[i] == S[i+1]):
            count += 1
            i += 1
        if(S[i] != S[i+1]):
            i += 1
        if count == len(S)-2:
            print(((len(S)-1)*K)//2)
            return

        if count > 0:
            ans += (count+1)//2

        if S[-1] == S[i]:
            A = -1
    if K >= 2 and S[-2] == S[0] and S[-3] != S[-2] and S[0] != S[1]:
        ans += 1
        print((ans*K)-1)
    elif K >= 2 and S[-2] == S[0] and S[-3] == S[-2] and S[0] == S[1]:
        print(ans*K)
    elif (K >= 2 and S[-2] == S[0] and S[-3] == S[-2] and S[-3] == S[-4])or (S[0] == S[1] and K >= 2 and S[-2] == S[0]and S[2] == S[1]) :
        ans += 1
        print((ans*K)-1)
    else:
        print(ans*K)

    return

# B


def B():
    N, K = map(int, input().split())
    h = LI()
    count = 0

    for i in h:
        if i >= K:
            count += 1
        else:
            continue
    print(count)
    return

# C


def C():
    N = II()
    A = LI()
    J = [0] * N
    for i, n in enumerate(A):
        J[n-1] = i+1
    for s in J:
        print(s, end=" ")

    return

# D


def D():
    A, B = map(int, input().split())
    g = gcd(A, B)
    # go = g
    # myset = set([1])
    sq = int(sqrt(g)) + 2
    ans = 1

    for i in range(2, sq):
        if g % i == 0:
            ans += 1
        while g % i == 0:
            g /= i

            # myset.add(i)
        if g == 1:
            break
        if i == sq - 1:
            # myset.add(go)
            ans += 1

    print(ans)

    return

# E


def E():
    N, M = LI()
    bit_max = 1 << N
    dp = [[inf for i in range(bit_max)] for j in range(M+1)]
    dp[0][0] = 0

    for i in range(M):
        a, b = LI()

        c = LI()
        bit = 0

        for d in c:
            bit |= 1 << (d-1)

        for w in range(bit_max):

            t = w | bit
            cost = dp[i][w] + a

            dp[i+1][t] = min(dp[i+1][t], cost)

            dp[i+1][w] = min(dp[i][w], dp[i+1][w])

    print(-1) if dp[-1][-1] == inf else print(dp[-1][-1])

    return

# F


def F():
    N, M = LI()

    value = [0] * M
    power = [0] * M
    for i in range(M):
        a, b = LI()
        value[i] = a
        c = LI()
        L = ['0'] * N
        for k in c:
            L[k - 1] = '1'
        bint = ''.join(L)
        power[i] = int(bint, 2)
    bint = ''.join(list(['1'] * N))
    dp = [inf] * (int(bint, 2)+1)
    t = 0
    cost = 0

    dp[0] = 0
    for s in range(int(bint, 2) + 1):
        for i in range(M):
            t = s | power[i]
            cost = dp[s] + value[i]
            dp[t] = min(dp[t], cost)
    print(-1) if(dp[-1] == inf) else print(dp[-1])

    return

    # you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")


def solution(S):
    # write your code in Python 3.6
    import math

    vowels = "AIUEO"
    count = 0
    consonants = 0
    newC = []
    newV = []

    for i in S[:-1]:
        if i in vowels:
            count += 1
            newV.append(i)
        else:
            consonants += 1
            newC.append(i)

    if consonants < count or consonants - 2 > count:
        return 0

    a = math.factorial(len(set(newC))) * math.factorial(len(set(newV)))
    return a

    pass


# Solve
if __name__ == '__main__':
    A()

Submission status: Wrong Answer

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

#print(link_S)
#S = S.append(S[len(S)-1])
i = 0
sum = 1
total_sum = 0
sum_ketu = 0

if len(S) == 1:
    print( K -1)
    exit()
    
while(1):
    if i == len(S)-1:
        if (S[i] == S[0] and S[0] != S[1]) or (S[i] == S[0] and S[i-1] != S[i]):
            if sum % 2 == 1:
                sum += 1
                sum_ketu = -1
        break
    if S[i] == S[i+1]:
     #   print("i is", i)
     #   print("S[i] is", S[i], "S[i+1] is", S[i+1])
        sum += 1
     #   print(sum)
        i += 1
    else:
        total_sum += sum // 2
        sum = 1
        i += 1
        
total_sum += sum // 2
#print(sum_ketu)
#print(total_sum)
print(total_sum * K + sum_ketu)

Submission status: Wrong Answer

Solution code:
S = input().replace(" ", "")
K = int(input())

str1 = S
str2 = S * 2
str3 = S * 3
str4 = S * 4
n1 = 0
n2 = 0
n3 = 0
n4 = 0
ln1 = []
ln2 = []
ln3 = []
ln4 = []

for c in str1:
  ln1.append(c)
for i in range(len(ln1)):
  if i == 0:
    pass
  elif ln1[i-1] == ln1[i]:
    n1 += 1
    ln1[i] = "A"
    
for c in str2:
  ln2.append(c)
for i in range(len(ln2)):
  if i == 0:
    pass
  elif ln2[i-1] == ln2[i]:
    n2 += 1
    ln2[i] = "A"
    
for c in str3:
  ln3.append(c)
for i in range(len(ln3)):
  if i == 0:
    pass
  elif ln3[i-1] == ln3[i]:
    n3 += 1
    ln3[i] = "A"
    
for c in str4:
  ln4.append(c)
for i in range(len(ln4)):
  if i == 0:
    pass
  elif ln4[i-1] == ln4[i]:
    n1 += 1
    ln4[i] = "A"
  
if len(S) == 1:
  print(K//2)
elif n1 == (n2 - n1) and (n2 - n1) == (n3 - n2):
  print(int(n1 * K))
elif n1 != (n2 - n1) and (n2 - n1) == (n3 - n2) and n1 == (n4 - n3):
  if K >= 3:
    print(int(n1 + (n3 - n1) * (K - 1)//2 - 1))
  elif K == 2:
    print(int(n2))
  else:
    print(int(n1))
elif n1 != (n2 - n1) and (n2 - n1) == (n3 - n2):
  if K >= 3:
    print(int(n1 + (n3 - n1) * (K - 1)//2))
  elif K == 2:
    print(int(n2))
  else:
    print(int(n1))
else:
  if K % 2 == 1:
    print(int(n1 + (n3 - n1) * (K - 1)//2))
  else:
    print(int(n2 * K / 2))

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
c = 0
d = 0
a = []
s += "A"
for i in range(len(s)-1):
    if (s[i] == s[i+1]):
        c += 1
    else:
        a.append(c+1)
        d += 1
        c = 0
c = 0
if s[0] != s[len(s)-2]:
    for i in range(d):
        c += a[i]//2
    print(int(c*k))
elif d == 1:
    print(int(a[0]*k//2))
else:
    for i in range(1,d-1):
        c += a[i]//2
    c += (k-1)*((a[0]+a[d-1])//2) + a[0]//2 + a[d-1]//2
    print(int(c))
    print(a)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
N = len(S)
counts = 0
count2 = 0
adder = 0
adder2 = 0

if len(S) == 1:
    print(K // 2)
elif len(S) == 2:
    if S[0] == S[1]:
        print(K)
    else:
        print(0)

elif len(S) == 3 and(S[0] == S[1]) and (S[1] == S[2]):
    if K % 2 == 0:
        print(int(3*K/2))
    elif K == 1:
        print(1)
    else:
        print(int(2+ 2*((K-1)/2) +(((K-1)/2)-1)))



elif K <= 3:
    S_arranged = list(S*K)
    for i in range(len(S_arranged)-2):
        if (S_arranged[i] == S_arranged[i+1]) and (S_arranged[i+1] == S_arranged[i+2]):
            S_arranged[i+1] = "0"
            counts += 1
        
    for i in range(len(S_arranged)-1):
        if S_arranged[i] == S_arranged[i+1]:
            S_arranged[i] = "1"
            counts += 1
    print(S_arranged)
    print(counts)

else:
    S_arranged = list(S*4)
    for i in range(len(S_arranged) - 2):
        if (S_arranged[i] == S_arranged[i+1]) and (S_arranged[i+1] == S_arranged[i+2]):
            S_arranged[i+1] = "0"
            if i+1 <= len(S)-1:
                adder += 1
            elif i+1 > len(S)*3 -1:
                adder2 += 1
            elif i+1 > len(S) - 1 and i+1 <= len(S)*2 - 1:
                counts +=1
            else:
                count2 +=1

    for i in range(len(S_arranged)-1):
        if S_arranged[i] == S_arranged[i+1]:
            S_arranged[i] = "1"
            if i <= len(S)-1:
                adder += 1
            elif i > len(S)*3 -1:
                adder2 += 1
            elif i > len(S)-1 and i+1 <= len(S)*2-1:
                counts +=1
            else:
                count2 +=1


    if K % 2 == 0:
        print(int(adder + (counts + count2) * ((K-2)/2) + adder2))

    else:
        print(int((adder + counts * ((K-1)/2) + count2 * ((K-1)/2 -1) +adder2)))

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
ans=0
cnt=1
add=0
lead=1
if len(s)==1:
  print(k//2)
  exit()
for i in range(1,len(s)):
  if s[i-1] == s[i]:
    lead+=1
  else:
    break
for i in range(1,len(s)):
  if s[i-1] == s[i]:
    cnt+=1
  else:
    ans+=cnt//2
    cnt=1
  if s[0] == s[-1] and lead%2!=0 and i == (len(s)-1) and cnt%2==1:
    ans+=cnt//2
    add=1*(k-1)
  elif i == (len(s)-1):
    ans+=cnt//2
print(ans*k+add)

Submission status: Wrong Answer

Solution code:
word = input()
n = int(input())
a = 0
ans1 = 0
ans2 = 0
change = 0
for i in range(len(word)-1):
  if i + a <= len(word)-2:
    if word[i + a] == word[i + a + 1]:
      ans1 += 1
      a += 1
      change = i + a
if change != len(word) - 1:
  if word[-1] == word[0]:
    ans2 = 1
if len(word) == 1:
  print(n//2)
else:
  print(ans1 * n + ans2 * (n-1))

Submission status: Wrong Answer

Solution code:
import copy
S=list(input())
SS=copy.deepcopy(S)
K=int(input())
c=0
cc=0
if len(S)==1:
  if K==1:
    print(0)
    exit()
  else:
    print(K//2)
    exit()
for i in range(1,len(S)):
  if S[i]==S[i-1]:
    S[i]="!"
    c+=1
if S[0]!=S[-1]:
  print(c*K)
else:
  SS[0]="!"
  for i in range(1,len(S)):
    if SS[i]==SS[i-1]:
      SS[i]="!"
      cc+=1
  print(min(c+(K-1)*cc+K-1,c*K))

Submission status: Wrong Answer

Solution code:
st = input()
s = [i for i in st]
k = int(input())
l = len(s)
c=0
i=0
f=0

def count(s,l):
    c=0
    i=0
    while i<l-1:
        if i+1 <=l-1 and s[i]==s[i+1]:
            s[i+1]='#'
            c+=1
            i+=1
        else:
            i+=1
    return c

c = count(s,l)
if k>1:
    s1 = []
    s1 = [ i for i in st]
    for i in range(l):
        s1.append(st[i])
    l1 = len(s1)
    c1 = count(s1,l1)
    kr = k-2
    # print(c1,c,kr)
    if kr%2==0:
        res = c1+c1*kr//2
    else:
        res = c1+c1*(kr-1)//2+c

    s2 = []
    s2 = [i for i in st]
    for i in range(l):
        s2.append(st[i])
    for i in range(l):
        s2.append(st[i])
    l2 = len(s2)
    c2 = count(s2,l2)
    r1 = c1+c
    # print(c2, r1)
    df = c2-r1
    # if kr%2==1:
    res = res + df
else:
    res = c

if l==1:
    if k%2==0:
        res=k//2
    else:
        res = (k-1)//2

print(res)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**9)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

s = ns()
k = ni()

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

n = 0
m = 0
p = False
for i in range(len(s)):
	if i == 0:
		if s[i] == s[-1] and s[-1] != s[-2]:
			n = 1
	elif not p and s[i] == s[i-1]:
		m += 1
		p = True
	else:
		p = False

print(m*k+n*(k-1))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
import sys
s = input()
k = int(input())
# print (s,k)
n=len(s)
# print (s[0],s[n-1])

if n == 1: 
    print(k // 2 + k % 2 )
    sys.exit(0)

flag=0
for i in range(1,n):
    #print("loop",i)
   if s[i] != s[i-1]:
       flag =1
       break

if flag == 0: 
    print((k*n) // 2 + (k*n) % 2 )
    sys.exit(0)



p=0
q=0
if s[0] == s[n-1]:
    l=1 
    # Start 
    for i in range(1,n):
        #print("loop",i)
        if s[i] == s[i-1]:
            p +=1
        else:
            break
    if l < n:
        for i in range(n-2,0,-1):
            if s[i] == s[i+1]:
                q +=1
            else:
                break
    l = 1 + p + q
    # print("Ring ", l, p,q)
    op = ((l // 2) + (l % 2)) * (k-1)
    op += ((p // 2) + (p % 2)) 
    op += ((q // 2) + (q % 2))
else: 
    op = 0     

# print("OP",op)

s2 = s[p+1:n-1-q] 
# print("s2",s2)
n=len(s2)
            

l = 0
for i in range(1,n):
    # print(s2[i],i,l,op)
    if s2[i-1] != s2[i]:
        op += (l // 2 + l % 2) * k
        l = 0 
    else:
        l += 1 
if l != 0:
    op += (l // 2 + l % 2) * k

        
print(op)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
 
ans = 0
length = len(s)
 
if length == 1:
  ans = k // 2
elif length == 2:
  if s[0] == s[1]:
    ans = k
else:
  sep = 0
  i = 0
  last = 0
  firstchar = s[0]
  is_one_char = True
  while i < length-1:
    if firstchar != s[i] or firstchar != s[i+1]:
      is_one_char = False
    
    if s[i] == s[i+1]:
      sep += 1
      last = i
      i += 1
    i += 1
  
  if is_one_char:
    ans = (length // 2) * k
    if length % 2 != 0:
      ans += k // 2
  else:
    ans = sep * k
    # 最後の2文字が一致しない
    if s[length-1] != s[length-2]:
      if s[0] == s[length-1] and s[0] != s[1]:
        ans += k-1
 
print(ans)

Submission status: Wrong Answer

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

if len(S) == 1:
    print(K // 2)
else :
    pre = S[0]
    flg = 0
    cnt = 0
    cnt_list =[]
    
    for i in range(1, len(S)):
        cnt += 1
        if flg == 1:
            flg = 0
            pre = S[i]
            continue
        if S[i] == pre:
            cnt_list.append(cnt)
            flg = 1
        pre = S[i]
    
    S = S[::-1]
    pre = S[0]
    flg = 0
    cnt = 0
    cnt_list2 =[]
    
    for i in range(1, len(S)):
        cnt += 1
        if flg == 1:
            flg = 0
            pre = S[i]
            continue
        if S[i] == pre:
            cnt_list2.append(len(S)-1-cnt)
            flg = 1
        pre = S[i]
    
    if S[0] == S[-1] and (S[-1] == S[-2] and len(S)-1 in cnt_list):
        print(len(cnt_list)*K)
    elif S[0] == S[-1] and (S[0] == S[1] and 0 in cnt_list2):
        print(len(cnt_list2)*K)
    elif S[0] == S[-1]:
        print(len(cnt_list)*K+K-1)
    else :
        print(len(cnt_list)*K)

Submission status: Wrong Answer

Solution code:
s = [c for c in input()]
k = int(input())

import copy

if len(s) == 1:
    print(k-1)

elif len(s) == len(set(s)):
    print(0)

else:
    ss = copy.copy(s)
    for i in range(k-1):
        s.extend(ss)
    c = 0
    for i in range(1, len(s)):
        if s[i-1]==s[i]:
            s[i]='Z'
            c = c+1
    print(c)

Submission status: Wrong Answer

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

tail = s[0]
plus = 0
if tail == s[-1]:
  tmp_cnt1 = 0
  tmp_cnt2 = 0
  for i in s:
    if tail == i:
      tmp_cnt1 += 1
    else:
      break
  for i in s[::-1]:
    if tail == i:
      tmp_cnt2 += 1
    else:
      break
  if (tmp_cnt1 % 2) == 1 and (tmp_cnt2%2)==1:
    plus = 1

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

Submission status: Wrong Answer

Solution code:
S = list(input())
K = int(input())
A = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
num = 0
TTL_num = 0
first = 0

if len(S)==1:
	num += 1
	first += 1
else:
	for i in range(len(S)-1):
		if S[i] == S[i+1]:
			if i <= (len(S)-3):
				for j in range(len(A)):
					if S[i] != A[j] and S[i+2] != A[j]:
						S[i+1] = A[j]
						num += 1
						break
			if i == (len(S)-2):
				for k in range(len(A)):
					if S[i] != A[k] and S[0] != A[k]:
						S[i+1] = A[k]
						num += 1
						break
		if i == (len(S)-1) and S[i+1] == S[0]:
			for l in range(len(A)):
				if S[0] != A[l]:
					S[i+1] = A[l]
					num += 1
					first += 1
					break

if first == 1:
	TTL_num = num*K -1
else:
	TTL_num = num*K

print(TTL_num)

Submission status: Wrong Answer

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


def c(ss, k):
    s = list(ss)
    s *= k
    n = len(s)
    ret = 0
    for i in range(n - 2):
        if s[i] == s[i + 1] == s[i + 2]:
            s[i + 1] = 'AB' [(i + 1) % 2]
            ret += 1
    for i in range(n - 1):
        if s[i] == s[i + 1]:
            s[i + 1] = 'AB' [(i + 1) % 2]
            ret += 1
    return ret


x1 = c(S, 1)
x2 = c(S, 2)
x3 = c(S, 3)
if len(S) == 1:
    print(x1)
else:
    print(x2 + (x3 - x2) * (K - 2))

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
cnt = 0
len_cnt = len(s)
#print(len_cnt)
submit = 0
tokusyu = 0
add = 0
if len_cnt == 1:
    print(k//2)
else:
    ato = 1
    while cnt < len_cnt-1:
        #print(len_cnt)
        if s[cnt] == s[cnt+1]:
            ato = cnt+1
            while s[cnt] == s[ato]:
                ato+=1
                #print("ここ",ato)
                if len_cnt == ato:
                    if len_cnt == ato-cnt:
                        tokusyu = 1
                        cnt = len_cnt
                        break

                    submit = submit + (ato-cnt)//2
                    cnt = len_cnt
                    break
            submit = submit + (ato-cnt)//2
            #print((ato-cnt)//2)
            cnt = ato
        else:
            cnt+=1
#print(submit)
#print(len_cnt)
if tokusyu == 0:
    #print(len_cnt)
    if s[0] == s[-1]:
        add = k-1
    print(submit*k+add)
else:
    #print(len_cnt)
    print((len_cnt*k)//2)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import copy
S=list(input())
SS=copy.deepcopy(S)
K=int(input())
c=0
cc=0
if len(S)==1:
  if K==1:
    print(0)
    exit()
  else:
    print(K//2)
    exit()
for i in range(1,len(S)):
  if S[i]==S[i-1]:
    S[i]="!"
    c+=1
if S[0]!=S[-1]:
  print(c*K)
else:
  SS[0]="!"
  for i in range(1,len(S)):
    if SS[i]==SS[i-1]:
      SS[i]="!"
      cc+=1
  print(0)

Submission status: Wrong Answer

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

def main():
    s = list(input().rstrip())
    k = int(input())


    if len(s) == 1:
        print(k//2)
    else:
        sa = s[:]

        c1 = 0
        c2 = 0
        c3 = 0

        for i in range(len(s) - 1):
            if sa[i] == sa[i+1]:
                c1 += 1
                sa[i+1] = '1'

        if sa[0] == sa[-1]:
            s[0] = '2'
            c2 += 1

            for i in range(len(s) - 1):
                if s[i] == s[i+1]:
                    c2 += 1
                    s[i+1] = '2'

            if s[-1] == sa[0] and k > 2:
                c3 += 1


            if k % 2 == 0:
                x = k // 2
                print(c1*x + c2*x+c3*(x-1))
            else:
                x = k // 2
                print(c1*(x+1) + c2*x+c3*x)
        else:
            print(c1*k)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
a = []
ans = 0
count = 0
for i in range(len(S)-1):
    if S[i] == S[i+1] :
        count += 1
    else:
        if count!=0:
            ans += (count+1)//2
            a.append(count+1)
            count = 0
if count !=0:
    ans += (count+1)//2 
ans = ans*K
a.append(0)
if len(S) == 1:
    ans = K//2
else:    
    
    if (count+1)%2==1 and a[0]%2 == 1 and S[0]== S[1] and S[0] == S[len(S)-1]:
    
        ans+=K-1
    if S[0]!= S[1] and (count+1)%2 ==1 and  S[0] == S[len(S)-1]:
        ans+=K-1
    if count+1 == len(S):
        if len(S)%2==1:
            ans+=K-1
 
print(ans)

Submission status: Wrong Answer

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

s_cnt = 0
for i in range(len(s) - 1):
    if s[i] != s[i + 1]:
        s_cnt = i + 1
        break
else:
    s_cnt = len(s)
for i in range(len(s) - 1, 1, -1):
    if s[i] != s[i - 1]:
        e_cnt = len(s) - i
        break
else:
    e_cnt = len(s)
is_start = False
idx_list = []
ans = 0
for i in range(len(s) - 1):
    if s[i] == s[i + 1]:
        if not is_start:
            is_start = True
            s_idx = i
    else:
        if is_start:
            is_start = False
            e_idx = i
            ans += (e_idx - s_idx + 1) // 2
            idx_list.append((s_idx, e_idx))
else:
    if is_start:
        e_idx = len(s) - 1
        ans += (e_idx - s_idx + 1) // 2
ans *= k
if len(s) == 1:
    ans = k // 2
elif s_cnt == len(s) and e_cnt == len(s) and len(s) % 2 == 1:
    ans += 1
elif s_cnt % 2 == 1 and e_cnt % 2 == 1 and s[0] == s[-1]:
    ans += k - 1
print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
N = len(S)
M = [0 for i in range(N)]

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

for i in range(N-1):
    if S[i]==S[i+1]:
        M[i]=1
if S[0]==S[N-1]:
    M[N-1]=1

count = 0
total = 0

for i in range(N):
    if M[i] == 1:
        count+=1
    elif count==1:
        total+=1
        count=0
    elif count!=0:
        total+=int((1+count)/2)
        count=0
if count==1:
    total+=1
elif count!=0:
    total+=int(count/2)

print(total*K)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
S = input()
 
K = int(input())
 
mycnt = 0
myflag = 0
 
if len(S) == 1:
    print(K//2)
 
else:
    mys = S[0]
    mcnt = 0
    myflag = 0
    for i in range(1,len(S)):
        if S[i] == mys and myflag == 0:
            mycnt += 1
            myflag = 1
        else:
            myflag = 0
        mys = S[i]
    
    if S[0] == S[-1] and len(S) > 2:
        if mycnt == 0:
            print(K-1)
            
        elif S[-1] == S[-2]:
                print(mycnt *K)
            
        else:
            if myflag == 1:
                if (len(S)*K)%2 == 0: 
                    mycnt = (mycnt * K) + (K-2)
                else:
                    mycnt = (mycnt * K) + (K-1)
                print(mycnt)
            else:
                print(mycnt)
                
    else:
        mycnt = mycnt * K
    
        print(mycnt)

Submission status: Wrong Answer

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

ss = s + s

def counter(string):
    count = 0
    answer = 0
    prev = ""
    for index,sss in enumerate(string):
        if prev == "" or sss == prev:
            count+=1
            if count == 2: 
                answer+=1
                count=0
                string[index] = "1"
        else:
            count=1
        prev = sss
    return answer,string

kk = k//2

if k%2 == 0: 
    answer, string = counter(ss)
    if string[0] == string[-1]:
        print(answer*kk+kk-1)
    else:
        print(answer*kk)
else:
    answer, string = counter(s)
    answer2, string2 = counter(ss)

    if string2[0] == string2[-1]:
        if string[0] == string2[-1]: 
            print(answer2*kk + answer + kk)
        else:
            print(answer2*kk + answer + kk-1)

    else:
        if string[0] == string2[-1]: 
            print(answer2*kk + answer + 1)
        else:
            print(answer2*kk + answer)

Submission status: Wrong Answer

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

if not(1 <= len(S) and len(S) <= 100):
    exit()
if not (S.isalpha() and S.islower()):
    exit()
if not float(K).is_integer():
    exit()
if not (1 <= int(K) and int(K) <= 10 ** 9):
    exit()

L = []
tmp = []
pre = ""
for s in S:
    if len(tmp) == 0:
        pre = s
        tmp.append(s)
    elif pre == s:
        tmp.append(s)
    else:
        L.append(tmp)
        pre = s
        tmp = [s]
L.append(tmp)

count = 0
for l in L:
    count += int(len(l) / 2)

count *= int(K)
if len(L) == 1:
    pass
elif L[0][0] == L[len(L)-1][0]:
    if len(L[0]) % 2 == 1 and len(L[len(L)-1]) % 2 == 1:
        count = count + int(K) - 1
print(count)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
cnt = 0
len_cnt = len(s)
#print(len_cnt)
submit = 0
tokusyu = 0
if len_cnt == 1:
    print(k//2)
else:
    ato = 1
    while cnt < len_cnt-1:
        #print(len_cnt)
        if s[cnt] == s[cnt+1]:
            ato = cnt+1
            while s[cnt] == s[ato]:
                ato+=1
                #print("ここ",ato)
                if len_cnt == ato:
                    if len_cnt == ato-cnt:
                        tokusyu = 1
                        cnt = len_cnt
                        break

                    submit = submit + (ato-cnt)//2
                    cnt = len_cnt
                    break
            submit = submit + (ato-cnt)//2
            #print((ato-cnt)//2)
            cnt = ato
        else:
            cnt+=1
#print(submit)
#print(len_cnt)
if tokusyu == 0:
    #print(len_cnt)
    print(submit*k)
else:
    #print(len_cnt)
    print((len_cnt*k)//2)

Submission status: Wrong Answer

Solution code:
def main():
    S = input()
    K = int(input())
    a = list(S)
    cnt = 0
    if len(set(a))==1 and len(a) != 1:
        cnt = (len(a)*K)//2
        print(cnt)
    elif len(a) ==1:
        cnt += K//2
        print(cnt)
    else:
        temp = 0
        temp_2 = 0
        i = 0
        while a[i] == a[i+1]:
            temp += 1
            i += 1
        j = len(a) - 1
        while a[j] == a[j-1]:
            temp_2 += 1
            j -= 1
        for k in range(i+1,j-1):
            if a[k+1] == a[k]:
                a[k+1] = str(k)
                cnt += 1
        cnt *= K
        cnt += temp//2
        cnt += temp_2//2
        
        if a[0] == a[-1]:
            temp_3 = (temp + temp_2)//2
            cnt += temp_3 * (K-1)

        print(cnt)

    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def count_changes(s, k, prev=None):
    count = 0
    for _ in range(k):
        for cur in s:
            if cur == prev:
                count += 1
                prev = None
            else:
                prev = cur
    return count, prev


if __name__ == '__main__':
    s = input()
    k = int(input())
    if k <= 3:
        print(count_changes(s, k)[0])
    else:
        base_count, prev = count_changes(s, 3)
        d, rest = divmod(k, 3)
        res = base_count * d
        if rest:
            res += count_changes(s, rest, prev)[0]
        if not rest and len(s) >= 3 and s[0] == prev:
            res += (d - 1)
        if rest and len(s) >= 3 and s[0] == prev:
            res += d

        print(res)

Submission status: Wrong Answer

Solution code:
#l=list(map(int, input().split()))
#n,m=map(int, input().split())
#l=list(map(int, input().split()))
#n = int(input())
def map_all(s):
    temp = s[0]
    
    for i in range(1,len(s)):
        if (temp != s[i]):
            return False

    return True

s = input()
k = int(input())

if map_all(s):
    temp = k * len(s)
    print(temp//2)
    exit()

le = len(s) -1
ans = []
temp = 0
de = True
for i in range(le):
    if s[i] == s[i+1]:
        temp+=1
        if (de == True):
            de = False
    else:
        if (de == False):
            ans.append(temp)
            temp = 0
            de= True
if (temp != 0):
    ans.append(temp)

#print(ans)
ansVal = 0
for i in ans:
    temp = (i+1)//2
    ansVal+=temp*k

print(ansVal)

Submission status: Wrong Answer

Solution code:
S = list(input())
#取得文字列
K = int(input())
#繰り返し回数

count = 0
befStr = ""


for i in range(1,len(S)):
  befStr = S[i - 1]
  if befStr == S[i]:
    count += 1
    S[i] = "B"
    
if K!=1 and S[0] == S[len(S)-1]:
  print(count*K + K)
else:
  print(count*K)

Submission status: Wrong Answer

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

if len(S) == 1:
    print(K // 2)
else :
    pre = S[0]
    flg = 0
    cnt = 0
    cnt_list =[]
    
    for i in range(1, len(S)):
        cnt += 1
        if flg == 1:
            flg = 0
            pre = S[i]
            continue
        if S[i] == pre:
            cnt_list.append(cnt)
            flg = 1
        pre = S[i]
    
    S = S[::-1]
    pre = S[0]
    flg = 0
    cnt = 0
    cnt_list2 =[]
    
    for i in range(1, len(S)):
        cnt += 1
        if flg == 1:
            flg = 0
            pre = S[i]
            continue
        if S[i] == pre:
            cnt_list2.append(cnt)
            flg = 1
        pre = S[i]
    
    if S[0] == S[-1] and (S[-1] == S[-2] and len(S)-1 in cnt_list):
        print(len(cnt_list)*K)
    elif S[0] == S[-1] and (S[0] == S[1] and 0 in cnt_list2):
        print(len(cnt_list2)*K)
    elif S[0] == S[-1]:
        print(len(cnt_list)*K+K-1)
    else :
        print(len(cnt_list)*K)