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())
# 一番効率が良いのは3連続で同じ文字列が続くときに真ん中を別の文字に変換する場合
# 2連続で同じ文字が続く場合は後ろの文字を変える.次の文字からまた数え直す
# 最後の文字列については無視すれば良い

# S=''.join([S for _ in range(K)])
same=False
count = 0
for i in range(1,len(S)):
    if not same:
        if S[i]==S[i-1]:
            same=True
            count+=1
        else:
            pass
    else:
        same=False
    if not same and i==K and S[0] == S[-1]:
        count+=1


same=False
count_odd = 0
for i in range(0,len(S)-1):
    if not same:
        if S[i]==S[i+1]:
            same=True
            count_odd+=1
        else:
            pass
    else:
        same=False
    if not same and i==0 and S[0] == S[-1]:
        count_odd+=1
        same=True
    # 文字列の最後尾が変更されずに、かつ前の文字列と同じであった場合は最後尾もしくは、最前を変える必要がある
min_count = min(count, count_odd)
print(min_count*K)

# countは一つの文字列中に存在する必要変換回数.ただし、(文末が変化しない場合)文末の文字が文頭のものとかぶる場合があるので、その場合については、
# 文頭を変化させる

Submission status: Wrong Answer

Solution code:
from itertools import groupby
s = input()
k = int(input())
l = [[k, len(list(g))] for k, g in groupby(s)]
ans = 0
ans2 = 0
for i in range(len(l)):
  if l[i][1] > 1:
    ans += l[i][1] // 2
if len(l) > 1:
  if l[0][0] == l[-1][0]:
    if l[0][1] % 2 == 1 and  l[-1][1] % 2 == 1 :
        ans2 = k - 1
if len(s) == 1:
  print(k // 2)
else:
  print(ans * k + ans2)

Submission status: Wrong Answer

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

c_c = 0
c_e = 0
counter = 1

for i in range(-1, -len(s)-1, -1):
  if s[0] == s[i]:
    counter += 1
  else:
    break
if counter % 2 == 0:
  c_e = 1

for i in range(len(s) - 1):
  if s[i] != s[i+1]:
    counter = 1
  else:
    counter += 1
    if counter % 2 == 0:
      c_c += 1

ans = c_c * k + c_e * (k-1)
print(ans)

Submission status: Wrong Answer

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

cnt=0
tmp=[]
flg=0

for i in range(len(s)-1):
    if s[0]==s[-1] and s[-1]!=s[-2]:
        flg=1

    if s[i] == s[i+1]:
        tmp.append(s[i])
    elif(len(tmp) >= 1):
        cnt += len(tmp)+1
        tmp =[]

    if(i == len(s)-2):
        if(len(tmp) >= 1):
            cnt += len(tmp)+1
            tmp =[]

if(flg==1):
    print(cnt//2*k+1)
else:
    print(cnt//2*k)

Submission status: Wrong Answer

Solution code:
S = input()
T = "".join(list(reversed(S)))
K = int(input())

first = S[0]
ans = 0
pre = first

for i in S[1:]:
    if pre == i:
        ans += 1
        pre = "tigauyo"
    else:
        pre = i
ans_e = 1 if first == pre else 0
Sans = (ans+ans_e)*K - ans_e

first = T[0]
ans = 0
pre = first

for i in T[1:]:
    if pre == i:
        ans += 1
        pre = "tigauyo"
    else:
        pre = i
ans_e = 1 if first == pre else 0
Tans = (ans+ans_e)*K - ans_e

print(min(Sans,Tans))

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]:
      is_one_char = False
    
    if s[i] == s[i+1]:
      sep += 1
      i += 1
      last = i
    i += 1
  
  if is_one_char:
    ans = (length // 2) * k
    if length % 2 != 0:
      ans += k // 2
  else:
    ans = sep * k
    if last != length-1 and s[0] == s[length-1]:
      ans += k-1

print(ans)

Submission status: Wrong Answer

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

if lengthofs == 1:
	print(k//2)
	sys.exit()

i=0
j=0
while i+1 < lengthofs :
	if s[i] != s[i+1]:
		j=1
	i=i+1
if j == 0:
	print((lengthofs*k)//2)
	sys.exit()

i=0
j=1
ans=0
while i+1 < lengthofs :
	if s[i] == s[i+1]:
		j=j+1
	else:
		ans=ans+(j//2)
		j=1
	i=i+1
ans=ans+(j//2)
ans=ans*k

if s[0]!=s[lengthofs-1]:
	print(ans)
else:
	i=0
	while s[i]==s[i+1]:
		i=i+1
	i=(i+1)%2
	j=1
	while s[lengthofs-j] == s[lengthofs-j-1]:
		j=j+1
	j=j%2
	if i*j==1:
		ans=ans+k-1
		print(ans)

Submission status: Wrong Answer

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

if len(s) == 1:
    print(k // 2)
else: 
    x = 0
    if s[0] == s[-1]:
        # head
        h = 1
        for i in range(1,len(s)):
            if s[0] == s[i]:
                h += 1
            else:
                break
        # tail
        t = 1
        for i in range(len(s)-2, -1, -1):
            if s[-1] == s[i]:
                t += 1
            else:
                break
        if h == len(s):
            # s only consists of the same letter
            x = (h * k) // 2
            k = 1
        else:
            #x = ((h + t) // 2) * ((k + 1) // 2)
            x = (h // 2) + (t // 2) + ((h + t) // 2) * (k - 1)
        #print(111,s,k,h,t,x)
    else:
        h = t = 0
    #print(222,s,k,h,t)
    y = 0
    if (h + t) < len(s):
        m = s[h]
        n = 1
        for i in range(h+1, len(s)-t):
            if m == s[i]:
                n += 1
                if (n & 2):
                    y += 1
            else:
                m = s[i]
                n = 1
    y *= k
    print(x + y)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
result = 0
if S[0] != S[-1]:
    t = None
    for i in range(len(S)):

        if i == 0:
            t = S[i]
            continue

        if t == S[i]:
            result += 1
            t = None
            continue

        t = S[i]

    print(result * K)
    exit()

else:
    m = None
    for i in range(len(S)):

        if i == 0:
            m = S[i]
            continue

        if m == S[i]:
            result += 1
            m = None
            continue

        m = S[i]
    t = S[0]
    count_a = 1
    for i in range(len(S)):
        if i == 0:
            continue

        if t == S[i]:
            count_a += 1
        else:
            break
    if count_a == len(S):
        print((len(S) * K) // 2)
        exit()
    count_b = 0

    for i in range(len(S)):
        if t == S[-i]:
            count_b += 1
        else:
            break

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

Submission status: Wrong Answer

Solution code:
S=input()
S_=S+"A"
K=int(input())
cnt=0
if len(S)==1:
    ans=K//2
elif len(S)==2:
    ans=K
else:
    import queue
    q=queue.Queue()
    for i in S_:
        if not q.empty():
            if not i == q.get():
                n=1
                while not q.empty():
                    q.get()
                    n+=1
                cnt+=n//2
                q.put(i)
            else:
                q.put(i)
                q.put(i)#出した分戻さないと           
        else:
            q.put(i)
        #最後、入れっぱなしで終わってしまっていた
    if S[0]==S[-1]:
        if S[-2]==S[-1]:
            if S[0]==S[1]:
                ans=((cnt+1)*K)-1
            else:
                ans=cnt*K
        else:
            ans=((cnt+1)*K)-1
    else:
        ans=cnt*K
        
print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
c1 = 0
xd = 0
def func(plane,v):
    global xd
    c2 = 0
    while True:
        if v == len(s)-1:
            break
        elif s[v] == plane:
            c2 += 1
        else:
            break
        v += 1
    xd = c2
    if c2 == 1:
        return 1
    else:
        return c2//2

if s[0] == s[-1]:
    for i in range(1,len(s)):
        if i == len(s) -1:
            if len(s)%2 == 0:
                print(func(s[0],0)*k)
                exit()
            else:
                print(func(s[0],0)*k +1)
                exit()
        elif s[i] == s[0]:
            c1 += 1
        else:
            break
    liter = s[0:c1+1]
    del s[0:c1+1]
    s += liter
c3 = 0
i= 0
while True:
    if i == len(s)-1:
        break
    elif s[i] == s[i + 1]:
        c3 += func(s[i],i)
        i += xd
    else:
        i+=1
    

print(c3*k)

Submission status: Wrong Answer

Solution code:
def f():
    S = input()
    K = int(input())
    S = list(S)
    if K == 1 and len(S) == 1:
        print(0)
        return
    
    if K == 1 and len(S) != 1:
        ans = 0
        for i in range(len(S)-1):
            if S[i] == S[i+1]:
                S[i+1] = '1'
                ans += 1
        print(ans)
        return
    
    if K != 1 and len(S) == 1:
        ans = K // 2
        print(ans)
        return
    
    if K != 1 and len(S) != 1:
        ORG_S = S[:]
        ans_min = 10**12
        # A
        ans = 0
        if S[0] == S[-1]:
            S[-1] = 'X'
            ans += 1
        for i in range(len(S)-1):
            if S[i] == S[i+1]:
                S[i+1] = str(i)
                ans += 1
        ans = ans*(K-1)
        S2 = S[:] + ORG_S
        for i in range(len(S2)-1):
            if S2[i] == S2[i+1]:
                S2[i+1] = 'A'+str(i)
                ans += 1
        ans_min = min(ans, ans_min)
        # B
        ans = 0
        S = ORG_S[:]
        if S[0] == S[-1]:
            S[0] = 'X'
            ans += 1
        for i in range(len(S)-1):
            if S[i] == S[i+1]:
                S[i+1] = str(i)
                ans += 1
        ans = ans*(K-1)
        S2 = ORG_S + S[:]
        for i in range(len(S2)-1):
            if S2[i] == S2[i+1]:
                S2[i+1] = 'A'+str(i)
                ans += 1
        ans_min = min(ans, ans_min)         
        # A-rev
        ans = 0
        S = ORG_S[:]
        if S[0] == S[-1]:
            S[-1] = 'X'
            ans += 1
        for i in reversed(range(len(S)-1)):
            if S[i] == S[i+1]:
                S[i] = str(i)
                ans += 1
        ans = ans*(K-1)
        S2 = S[:] + ORG_S
        for i in reversed(range(len(S2)-1)):
            if S2[i] == S2[i+1]:
                S2[i] = 'A'+str(i)
                ans += 1
        ans_min = min(ans, ans_min)
        # B-rev
        ans = 0
        S = ORG_S[:]
        if S[0] == S[-1]:
            S[0] = 'X'
            ans += 1
        for i in reversed(range(len(S)-1)):
            if S[i] == S[i+1]:
                S[i] = str(i)
                ans += 1
        ans = ans*(K-1)
        S2 = ORG_S + S[:]
        for i in reversed(range(len(S2)-1)):
            if S2[i] == S2[i+1]:
                S2[i] = 'A'+str(i)
                ans += 1
        ans_min = min(ans, ans_min)   

        
        print(ans_min)
        return

f()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(int(1e6))

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

count = 0
s1 = ''
s2 = ''
s3 = ''
prev_rewrite = False
for s2 in S:
    if s1 == s2 and not prev_rewrite:
        code = ord(s2) + 1
        s3 = chr(code) if code <= ord('z') else 'a'
        count += 1
        prev_rewrite = True
    else:
        s3 = s2
        prev_rewrite = False

    s1 = s3

ans = count * K

if s1 == S[0] and not prev_rewrite:
    ans += K - 1

print(ans)

Submission status: Wrong Answer

Solution code:

# import sys
# input = sys.stdin.readline
from collections import Counter
import string


def main():
    # while True:
        S = input()
        K = int(input())

        count_1st = 0
        S1 = list(S * 1)
        for i in range(len(S) - 1):
            if S1[i] == S1[i+1]:
                for c in string.ascii_lowercase:
                    if i == 0:
                        if c != S1[i+1]:
                            count_1st += 1
                            S1[i+1] = c
                            break
                    else:
                        if c != S1[i-1] and c != S1[i+1]:
                            count_1st += 1
                            S1[i+1] = c
                            break

        count_2nd = 0
        S2 = list(S * 2)
        for i in range(len(S) * 2 - 1):
            if S2[i] == S2[i+1]:
                for c in string.ascii_lowercase:
                    if i == 0:
                        if c != S2[i+1]:
                            count_2nd += 1
                            S2[i+1] = c
                            break
                    else:
                        if c != S2[i-1] and c != S2[i+1]:
                            count_2nd += 1
                            S2[i+1] = c
                            break

        count_3rd = 0
        S3 = list(S * 3)
        for i in range(len(S) * 3 - 1):
            if S3[i] == S3[i+1]:
                for c in string.ascii_lowercase:
                    if i == 0:
                        if c != S3[i+1]:
                            count_3rd += 1
                            S3[i+1] = c
                            break
                    else:
                        if c != S3[i-1] and c != S3[i+1]:
                            count_3rd += 1
                            S3[i+1] = c
                            break

        # count_k = 0
        # SK = list(S * K)
        # for i in range(len(S) * K - 1):
        #     if SK[i] == SK[i+1]:
        #         for c in string.ascii_lowercase:
        #             if i == 0:
        #                 if c != SK[i+1]:
        #                     count_k += 1
        #                     SK[i+1] = c
        #                     break
        #             else:
        #                 if c != SK[i-1] and c != SK[i+1]:
        #                     count_k += 1
        #                     SK[i+1] = c
        #                     break

        d = count_3rd - count_2nd
        # print(K, d, count_1st)
        # print(count_1st, count_2nd, count_3rd)
        # print(d * K)
        print(count_1st + d * (K-1))
        # print(count_k)
        # print(*SK)
        # print(*S1)
        # print(*S2)
        # print(*S3)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

count=0

isOdd = False
if K%2 != 0: isOdd = True

if K==1:
	for i in range(len(S)-1):
		if S[i] == S[i+1]:
			S[i+1] = '?'
			count += 1

else:
	S2 = S+S

	for i in range(len(S2)-1):
		if S2[i] == S2[i+1]:
			S2[i+1] = '?'
			count += 1
	
	if isOdd:
		K -= 1
	
	count *= K//2

	if S2[0] == S2[-1]:
		count += (K/2)-1

	if isOdd:
		S.insert(0, S2[-1])

		for i in range(len(S)-1):
			if S[i] == S[i+1]:
				S[i+1] = '?'
				count += 1

print(int(count))

Submission status: Wrong Answer

Solution code:
import math
s = input()
k = int(input())
s = list(s)
ind = 0
anss = [[] for i in range(100)]
for i in range(len(s)-1):
  anss[ind].append(s[i])
  if s[i] != s[i+1]:
    ind += 1
#print(anss)
for i in range(99-ind-1):
  anss.remove([])
if s[0] == s[len(s)-1] and s[len(s)-2] == s[len(s)-1]:
  anss[0] += anss[len(anss)-1]
  anss.remove(anss[len(anss)-1])
  #print(anss)
  #print(s)
  anss[0].append(s[len(s)-1])
elif s[0] == s[len(s)-1]:
  anss[0].append(s[len(s)-1])
elif s[len(s)-2] == s[len(s)-1]:
  anss[len(anss)-1].append(s[len(s)-1])
else:
  anss.append([s[len(s)-1]])
pans = 0
for i in anss:
  pans += math.floor(len(i) / 2)
print(pans*k)

Submission status: Wrong Answer

Solution code:
S = list(str(input()))
N = len(S)
K = int(input())

ans1 = 0
old = ""
flag = 0
for i in range(N):
    if old == "":
        old = S[i]
    else:
        if old == S[i]:
            old = ""
            ans1+=1
        else:
            old = S[i]
            
    if i == N-1:
        if old == S[i]:
            flag = 1

if flag == 0:
    print(ans1*K)
else:
    ans2 = 0
    old = ""
    for i in range(N):
        if i == 0:
            if S[0] == S[-1]:
                if flag == 1:
                    ans2+=1
                else:
                    old = S[0]
        elif old == "":
            old = S[i]
        else:
            if old == S[i]:
                old = ""
                ans2+=1
            else:
                old = S[i]
    if i == N-1:
        if old == S[i]:
            flag = 2
    
    k = K//2
    if K%2 == 0:
        print((ans1+ans2)*k)
    else:
        if flag == 2:
            print(ans1*(k)+ans2*(K-k))
        else:
            print(ans1*(K-k)+ans2*(k))

Submission status: Wrong Answer

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


a = S[0]
c = 0
S2 = copy.deepcopy(S)
for i in range(1, len(S)):
    if S2[i] == S2[i-1]:
        S2[i] = '1'
        c += 1

a = S[-1]
last = 0
for i in range(2, len(S)+1):
    last += 1
    if S[-i] != a:
        break

if S[0] == S[-1] and last % 2 == 1:
    c3 = 0
    S3 = copy.deepcopy(S)
    for i in range(2, len(S)):
        if S3[i] == S3[i-1]:
            S3[i] = '1'
            c3 += 1
    print(c + (c3+1)*(K-1))
else:
    print(c*K)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
pre_character = ''
S_list=list(S)
s_len = len(S)
def get_abc(s1,s2):
    if s1 == 'a':
        if s2 == 'b':
            return 'c'
        else:
            return 'b'
    elif s1 == 'b':
        if s2 == 'a':
            return 'c'
        else:
            return 'a'
    else:
        if s2 == 'a':
            return 'b'
        else:
            return 'a'

count = 0
for i in range(s_len):
    if pre_character == S_list[i]:
        count += 1
        if (s_len - 1) != i:
            S_list[i] = get_abc(pre_character,S_list[i+1])
        else:
            S_list[i] = get_abc(pre_character,S[0])
    pre_character = S_list[i]
#print(S_list)

pre_character = S_list[-1]
S_list=list(S)
second_count = 0
for i in range(s_len):
    if pre_character == S_list[i]:
        second_count += 1
        if (s_len - 1) != i:
            S_list[i] = get_abc(pre_character,S_list[i+1])
        else:
            S_list[i] = get_abc(pre_character,S[0])
    pre_character = S_list[i]
#print(S_list)
gusu = K//2
kisu = K - gusu
print((count*kisu)+ (second_count*gusu))

Submission status: Wrong Answer

Solution code:
#coding: utf-8
strings = str(input())
k = int(input())
s = strings + strings
# print(s)
count = 0

for i in range(len(s)-1):
	# print('strj'+str(j))
	if s[i] == s[i+1]:
		# print(str('s[i]=') + s[i] + str(' s[i+1]=') + s[i+1])
		count += 1
		# s[i] = str(i)
		s = s[:i+1] + str('A') + s[i+2:]
		# print(s)

# print(count)
ans = count*k//2

# if k % 2 == 0:
# 	count = count * k // 2
# else:
# 	print(count*k//2)
# 	print(count//2)
# 	count = count * k // 2 - (count // 2)
if strings[0] == strings[len(strings)-1] and strings[len(strings)-1] != strings[len(strings)-2] and k % 2 == 1:
	ans += (k - 1) // 2

print(ans)

Submission status: Wrong Answer

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

leng = len(S)
result = 0
flg = 0

for i in range(1,len(S)):
    #print(i)
    #print(S)
    if S[i] == S[i-1]:
        #print("change")
        result = result + 1
        S = S[:i] + '.' + S[i+1:]
        
#print(S)

if S[0] == S[leng-1]:
    result = result + 1
    flg = 1

result = result * K
if flg == 1 :
    result = result - 1
print(result)

#aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
#1000000000

Submission status: Wrong Answer

Solution code:
from itertools import groupby

S = input()
N = int(input())

if len(S) == 1:
    print(N-1)
    exit()

if len(S) == 2 and S[0] == S[1]:
    print(N)
    exit()

check = S
check_num = sum(len(list(v))//2 for k, v in groupby(check))
check2 = S * 2
check2_num = sum(len(list(v))//2 for k, v in groupby(check2))
check3 = S * 3
check3_num = sum(len(list(v))//2 for k, v in groupby(check3))
check4 = S * 4
check4_num = sum(len(list(v))//2 for k, v in groupby(check4))

if N % 2 == 0:
    print(check2_num + (check4_num-check2_num)*(N//2-1))
else:
    print(check_num + (check3_num-check_num)*(N//2))

#ans = check_num*N + (check2_num-2*check_num)*(N-1)
#ans3 = check_num*3 + (check2_num-2*check_num)*(3-1)
#if check3_num == ans3:
#    print(ans)
#else:
#    print(round(N/2)*check_num + N//2*(check2_num-check_num))

Submission status: Wrong Answer

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

import re


def nchars(s, n):
    """文字列 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():]
lst = list(nchars(s,2))
count = 0
for i in lst:
    count += len(i)//2
count = count*k

if s[0]==s[-1]:

    first = s[0]
    f = 0
    e = 0
    end = s[-1]
    for i in s:
        if first == i:
            f += 1
        else:
            break
    for i in reversed(s):
        if end == i:
            e += 1
        else:
            break
    if f % 2 == 1 and e % 2 == 1:
        if f == len(s):
            count+=k-2
        else:
            count += k-1

print(count)

Submission status: Wrong Answer

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

S = input().strip()
K = int(input())
f = 0
if len(set(S)) == 1:
    print(len(S)*K//2)
else:
    c = 0
    first = 0
    _s = ''
    for s in S:
        if s != _s:
            first += c // 2
            c = 1
            _s = s
        else:
            c += 1
    c = 0
    middle = 0
    f = False
    _s = S[0]
    for s in S:
        if s != _s:
            if not f:
                f = True
            else:
                middle += c // 2
                c = 1
                _s = s
        else:
            c += 1
    c = 0
    bit = ''
    _s = ''
    for s in S:
        if _s == '':
            bit = s
            _s = s
        elif s == _s:
            bit += s
        else:
            break
    _s = ''
    for s in S[::-1]:
        if _s == '':
            bit = s
            _s = s
        elif s == _s:
            bit += s
        else:
            break
    if len(set(bit)) == 1:
        bit = len(bit) // 2
    else:
        bit = bit.count(bit[0]) // 2 + bit.count(bit[-1]) // 2
    c = 0
    last = 0
    _s = ''
    for s in S[::-1]:
        if s != _s:
            last += c // 2
            c = 1
            _s = s
        else:
            c += 1

    print(first + bit * (K - 1) + middle * (K - 2) + last)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
result = 0
if S[0] != S[-1]:
    t = None
    for i in range(len(S)):

        if i == 0:
            t = S[i]
            continue

        if t == S[i]:
            result += 1
            t = None
            continue

        t = S[i]

    print(result * K)
    exit()

else:
    m = None
    for i in range(len(S)):

        if i == 0:
            m = S[i]
            continue

        if m == S[i]:
            result += 1
            m = None
            continue

        m = S[i]
    t = S[0]
    count_a = 0
    for i in range(len(S)):
        if i == 0:
            continue

        if t == S[i]:
            count_a += 1
        else:
            break
    if count_a == len(S):
        print((len(S) * K) // 2)
        exit()
    count_b = 0

    for i in range(len(S)):
        if t == S[-i]:
            count_b += 1
        else:
            break

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

Submission status: Wrong Answer

Solution code:
def f():
    S = input()
    K = int(input())
    S = list(S)
    if K == 1 and len(S) == 1:
        print(0)
        return
    
    if K == 1 and len(S) != 1:
        ans = 0
        for i in range(len(S)-1):
            if S[i] == S[i+1]:
                S[i+1] = '1'
                ans += 1
        print(ans)
        return
    
    if K != 1 and len(S) == 1:
        ans = K // 2
        print(ans)
        return
    
    if K != 1 and len(S) != 1:
        ORG_S = S[:]
        ans_min = 10**12
        # A
        ans = 0
        if S[0] == S[-1]:
            S[-1] = 'X'
            ans += 1
        for i in range(len(S)-1):
            if S[i] == S[i+1]:
                S[i+1] = str(i)
                ans += 1
        ans = ans*(K-1)
        S2 = S[:] + ORG_S
        for i in range(len(S2)-1):
            if S2[i] == S2[i+1]:
                S2[i+1] = 'A'+str(i)
                ans += 1
        ans_min = min(ans, ans_min)
        # B
        ans = 0
        S = ORG_S[:]
        if S[0] == S[-1]:
            S[0] = 'X'
            ans += 1
        for i in range(len(S)-1):
            if S[i] == S[i+1]:
                S[i+1] = str(i)
                ans += 1
        ans = ans*(K-1)
        S2 = ORG_S + S[:]
        for i in range(len(S2)-1):
            if S2[i] == S2[i+1]:
                S2[i+1] = 'A'+str(i)
                ans += 1
        ans_min = min(ans, ans_min)         
        # A-rev
        ans = 0
        S = ORG_S[:]
        if S[0] == S[-1]:
            S[-1] = 'X'
            ans += 1
        for i in reversed(range(len(S)-1)):
            if S[i] == S[i+1]:
                S[i+1] = str(i)
                ans += 1
        ans = ans*(K-1)
        S2 = S[:] + ORG_S
        for i in reversed(range(len(S2)-1)):
            if S2[i] == S2[i+1]:
                S2[i+1] = 'A'+str(i)
                ans += 1
        ans_min = min(ans, ans_min)
        # B-rev
        ans = 0
        S = ORG_S[:]
        if S[0] == S[-1]:
            S[0] = 'X'
            ans += 1
        for i in reversed(range(len(S)-1)):
            if S[i] == S[i+1]:
                S[i+1] = str(i)
                ans += 1
        ans = ans*(K-1)
        S2 = ORG_S + S[:]
        for i in reversed(range(len(S2)-1)):
            if S2[i] == S2[i+1]:
                S2[i+1] = 'A'+str(i)
                ans += 1
        ans_min = min(ans, ans_min)   

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

f()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
ans = 0
for i in range(len(s) - 1):
    if s[i] == s[i + 1]:
        s[i + 1] = "Z"
        ans += 1
if s[0] == s[-1]:
    ans = 1
print(ans*k)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
S = input()
K = int(input())

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

cnt_one = 0
i = 0
while i < lenS-1:
    if S[i] == S[i+1]:
        cnt_one += 1
        i += 2
    else:
        i += 1

if K == 1:
    print(cnt_one)

SS = S*2
lenS = len(SS)

cnt_two = 0
i = 0
while i < lenS-1:
    if SS[i] == SS[i+1]:
        cnt_two += 1
        i += 2
    else:
        i += 1

print((cnt_two - cnt_one)*(K-1) + cnt_one)

Submission status: Wrong Answer

Solution code:
word_line=input().rstrip()
#print(word_line)
numK=int(input())

char_list=list(word_line)
#print(char_list)

if(char_list[0]!=char_list[len(char_list)-1]):
    s=0
    for i in range(1,len(char_list)-1):
       if(char_list[i-1]==char_list[i]):
           char_list[i]="wa"
           s+=1
    s=s*numK    

else:
    allsame=0
    for i in range(len(char_list)-1):
        if(char_list[i]==char_list[i+1]):
            allsame+=1
    if allsame==len(char_list)-1:
        s=(len(char_list)*numK)//2
    
    else:
        count_a=1
        i=0
        while (char_list[i]==char_list[i+1])and(i<len(char_list)-2):
            count_a+=1
            i+=1
        
        count_b=1
        i=1
        while(char_list[len(char_list)-i]==char_list[len(char_list)-i-1])and(i<len(char_list)-2):
            count_b+=1
            i+=1
            
        #print(count_a)
        #print(count_b)
        
        s=0
        #print(len(char_list)-count_b-1)
        #for i in range(2,3):
        #    print("aaa")
        for i in range(count_a+1,len(char_list)-count_b):
            if(char_list[i-1]==char_list[i]):
                char_list[i]="wa"
                s+=1
        
        #print(s)
        s=s*numK
        #print(s)
        s+=(count_a//2+count_b//2+(count_a+count_b)//2*(numK-1))
    
print(s)


#print(s*numK)

Submission status: Wrong Answer

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


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


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


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




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

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

exit(0)

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
Length=len(S)
change=0

#S内の文字連続をcountする
count=0
compare_flag=True

if Length==1:
  print(K-1)
  
else:
  for i in range(1,Length):
    if compare_flag==False:
      compare_flag=True
    else:
      if S[i]==S[i-1]:
        count+=1
        compare_flag=False
      else:
        pass

  if Length>=2:
    if S[Length-1]==S[Length-2]:
      pass
    else:
      if S[Length-1]==S[0]:
        change+=1
  ans=(K-1)*change+K*count
  print(ans)

Submission status: Wrong Answer

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

def makerawlist(S):
    sigma = 0
    counter = 1
    for i in range(len(S)-1):
        if S[i]==S[i+1]:
            counter += 1
        else:
            sigma += counter//2
            counter = 1
    sigma += counter//2
    return sigma

sigma = makerawlist(S)
if sigma == len(S) and (sigma*K)%2 == 1:
    print((sigma*K)//2)
else:
    print(sigma*K)