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 = 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)
if s[0] is s[-1]:
    cnt += 1
print(cnt*k)

Submission status: Wrong Answer

Solution code:
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    s = S()
    k = I()
    same = len(set(list(s)))==1
    ans = None

    if same:
        ans = len(s)*k//2
    else:
        if s[0]!=s[-1]:
            cnt = 0
            change = False
            for i in range(2,len(s)-1):
                if s[i] == s[i-1] and not change:
                    cnt += 1
                    change = True
                else:
                    change = False
            ans = cnt*k
        else:
            char = s[0]
            start = len(s)
            goal = -1
            cnt = 0
            while s[start-1] == char:
                start -= 1
            while s[goal+1] == char:
                goal += 1
            lenth = len(s)-start + goal+1
            cnt += lenth//2 * (k-1)

            ccnt = 0
            change = False
            for i in range(goal+1+1,start):
                if s[i] == s[i-1] and not change:
                    ccnt += 1
                    change = True
                else:
                    change = False
            cnt += ccnt * (k-2)

            ccnt = 0
            change = False
            for i in range(1,start):
                if s[i] == s[i-1] and not change:
                    ccnt += 1
                    change = True
                else:
                    change = False
            cnt += ccnt

            
            ccnt = 0
            change = False
            for i in range(goal+1+1, len(s)):
                if s[i] == s[i-1] and not change:
                    ccnt += 1
                    change = True
                else:
                    change = False
            cnt += ccnt
            ans = cnt
    print(ans)
                
main()

Submission status: Wrong Answer

Solution code:
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    s = S()
    k = I()
    same = len(set(list(s)))==1
    ans = None

    if same:
        ans = len(s)*k//2
    else:
        if s[0]!=s[-1]:
            cnt = 0
            change = False
            for i in range(2,len(s)-1):
                if s[i] == s[i-1] and not change:
                    cnt += 1
                    change = True
                else:
                    change = False
            ans = cnt*k
        else:
            char = s[0]
            start = len(s)
            goal = -1
            cnt = 0
            while s[start-1] == char:
                start -= 1
            while s[goal+1] == char:
                goal += 1
            lenth = len(s)-start + goal+1
            cnt += lenth//2 * (k-1)

            ccnt = 0
            change = False
            for i in range(goal+1+1,start):
                if s[i] == s[i-1] and not change:
                    ccnt += 1
                    change = True
                else:
                    change = False
            cnt += ccnt * (k-2)

            ccnt = 0
            change = False
            for i in range(1,start):
                if s[i] == s[i-1] and not change:
                    ccnt += 1
                    chagne = True
                else:
                    chagne = False
            cnt += ccnt

            
            ccnt = 0
            change = False
            for i in range(goal+1+1, len(s)):
                if s[i] == s[i-1] and not change:
                    ccnt += 1
                    chagne = True
                else:
                    chagne = False
            cnt += ccnt
            ans = cnt
    print(ans)
                
main()

Submission status: Wrong Answer

Solution code:
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    s = list(S())
    k = I()
    same = True
    for i in range(1,len(s)):
        if s[i]!=s[i-1]:
            same = False
    ans = len(s)*k//2
    if not same:
        if s[0]!=s[-1]:
            lenth = 0
            for i in range(1,len(s)):
                if s[i]==s[i-1]:
                    lenth+=1
                    s[i] = ""
            ans = lenth*k
        else:
            start = len(s)-1
            goal = 0
            ans = 0
            while s[start-1]==s[-1]:
                start-=1
            while s[goal+1]==s[0]:
                goal+=1
            lenth = len(s)-start + goal+1

            cs = s.copy()
            for i in range(1,start):
                if s[i]==s[i-1]:
                    ans += 1
                    s[i] = ""
            ans += lenth//2
            s = cs.copy()
            for i in range(goal+1+1,len(s)):
                if s[i]==s[i-1]:
                    ans += 1
                    s[i] = ""

    print(ans)
main()

Submission status: Wrong Answer

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

if s2[0] == s2[-1] and len(s) >= 2:
  ans += k - 1
if len(s) == 1:
  ans = k // 2
if len(list(set(list(s)))) == 1:
  ans = len(s) * k // 2
print(ans)

Submission status: Wrong Answer

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

if s2[0] == s2[-1] and len(s) >= 2:
  ans += k - 1
if len(s) == 1:
  ans = k // 2
print(ans)

Submission status: Wrong Answer

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

if s2[0] == s2[-1] and len(s) >= 2:
  ans += k
if len(s) == 1:
  ans = k // 2
print(ans)

Submission status: Wrong Answer

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

if s2[0] == s2[-1]:
  ans += k
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans = 0
if len(set(s)) == 0:
    print(len(s)*k//2)
    exit(0)
x = 1
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        x += 1
    else:
        ans += x//2
        x = 1
ans += x//2
ans *= k
if s[0] == s[-1]:
    a = 1
    for i in range(1,len(s)):
        if s[0] == s[i]:
            a += 1
        else:
            break
    b = 1
    for i in reversed(range(len(s)-1)):
        if s[-1] == s[i]:
            b += 1
        else:
            break
    ans -= (a//2+b//2-(a+b)//2)*(k-1)
print(ans)

Submission status: Wrong Answer

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

ans = 0
x = 1
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        x += 1
    else:
        ans += x//2
        x = 1
ans += x//2
ans *= k
if s[0] == s[-1]:
    a = 1
    for i in range(1,len(s)):
        if s[0] == s[i]:
            a += 1
        else:
            break
    b = 1
    for i in reversed(range(len(s)-1)):
        if s[-1] == s[i]:
            b += 1
        else:
            break
    ans -= (a//2+b//2-(a+b)//2)*(k-1)
print(ans)

Submission status: Wrong Answer

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

ans = 0
x = 1
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        x += 1
    else:
        ans += x//2
        x = 1
ans += x//2
ans *= k
a = 1
for i in range(1,len(s)):
    if s[0] == s[i]:
        a += 1
    else:
        break
b = 1
for i in reversed(range(len(s)-1)):
    if s[-1] == s[i]:
        b += 1
    else:
        break
ans -= a//2+b//2-(a+b)//2
print(ans)

Submission status: Wrong Answer

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

ans = 0

# すべて同じ文字の場合
if s == [s[0]] * n:
    print((n*k)//2)
    exit(0)

# 先頭と末尾が異なる場合
i = 1
while i < n:
    if s[i-1] == s[i]:
        t = 2
        i+=1
        while i < n and s[i-1] == s[i]:
            t+=1
            i+=1
        ans += (t//2)*k
    i+=1
if s[0] != s[-1]:
    print(ans)
    exit(0)
# 先頭と末尾が一致する場合
# まず不一致の場合と同様に数えて、左右端の連続長さを数える
# 連結したとき、左右の1ブロックのみはそのまま数えられる
# 残りのk-1ブロックは、左右がつながった形で数えられる
i = 1
left = 1
right = 1
for i in range(n):
    if s[i] == s[i+1]:
        left += 1
    else:
        break
for i in range(n-1, -1, -1):
    if s[i] == s[i-1]:
        right += 1
    else:
        break
if s[0] == s[-1]:
    ans = ans - (k-1) * (left//2 + right//2) + (k-1) * (left+right)//2
    print(ans)

Submission status: Wrong Answer

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

ans = 0

# すべて同じ文字の場合
if s == [s[0]] * n:
    print((n*k)//2)
    exit(0)

# 先頭と末尾が異なる場合
i = 1
while i < n:
    if s[i-1] == s[i]:
        t = 2
        i+=1
        while i < n and s[i-1] == s[i]:
            t+=1
            i+=1
        ans += (t//2)*k
    i+=1
if s[0] != s[-1]:
    print(ans)
    exit(0)
# 先頭と末尾が一致する場合
# まず不一致の場合と同様に数えて、左右端の連続長さを数える
# 連結したとき、左右の1ブロックのみはそのまま数えられる
# 残りのk-1ブロックは、左右がつながった形で数えられる
i = 1
left = 0
right = 0
while i < n:
    if s[i-1] == s[i]:
        t = 2
        i+=1
        while i < n and s[i-1] == s[i]:
            t+=1
            i+=1
        left += t
        break
    i+=1
i = n-1
while 0 < i < n:
    if s[i-1] == s[i]:
        t = 2
        i-=1
        while 0 < i < n and s[i-1] == s[i]:
            t+=1
            i-=1
        right += t
        break
    i-=1
if s[0] == s[-1]:
    ans = ans - (k-1) * (left//2 - right//2) + (k-1) * (left+right)//2
    print(ans)

Submission status: Wrong Answer

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

ans = 0

# すべて同じ文字の場合
if s == [s[0]] * n:
    print((n*k)//2)
    exit(0)

# 先頭と末尾が異なる場合
i = 1
while i < n:
    if s[i-1] == s[i]:
        t = 2
        i+=1
        while i < n and s[i-1] == s[i]:
            t+=1
            i+=1
        ans += (t//2)*k
    i+=1
if s[0] != s[-1]:
    print(ans)
    exit(0)
# 先頭と末尾が一致する場合
# まず不一致の場合と同様に数えて、左右端の連続長さを数える
# 連結したとき、左右の1ブロックのみはそのまま数えられる
# 残りのk-1ブロックは、左右がつながった形で数えられる
i = 1
left = 0
right = 0
while i < n:
    if s[i-1] == s[i]:
        t = 2
        i+=1
        while i < n and s[i-1] == s[i]:
            t+=1
            i+=1
        left += t
        break
    i+=1
i = n-1
while 0 < i < n:
    if s[i-1] == s[i]:
        t = 1
        i-=1
        while 0 < i < n and s[i-1] == s[i]:
            t-=1
            i-=1
        right += t
        break
    i-=1
if s[0] == s[-1]:
    ans = ans - (k-1) * (left//2 - right//2) + (k-1) * (left+right)//2
    print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
ans = 0
soui = 0
moji = s[0]

for i in range(1,len(s)):
    if s[i] == s[i-1] and soui == 0:
        ans += 1
        soui = 1
    elif soui == 1:
        soui = 0

flag = 0
a = 0
b = 0

while s[0] == s[a] and a != len(s)-1:
    a += 1

while s[len(s)-1-b] == s[len(s)-1] and b != len(s)-1:
    b += 1

for i in range(len(s)):
    if moji != s[i]:
        break
    elif i == len(s)-1:
        flag = 2

if flag == 1:
    print((k*ans)-((k-1)*((a//2)+(b//2)-((a+b)//2))))
elif flag == 2:
    print((k*len(s))//2)
else:
    print(k*ans)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
ans = 0
soui = 0

for i in range(1,len(s)):
    if s[i] == s[i-1] and soui == 0:
        ans += 1
        soui = 1
    elif soui == 1:
        soui = 0

flag = 0

if s[0] == s[len(s)-1] and soui == 0:
    ans += 1
    flag = 1

if flag == 1:
    print((k*ans)-1)
else:
    print(k*ans)

Submission status: Wrong Answer

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

ans = 0

# 3個連続を処理
i = 0
while i < n:
    if s[i] == s[(i+1)%n] == s[(i+2)%n]:
        s[(i+1)%n] = '?'
        ans += k
    i+=1

# 2個連続を処理
i = 0
while i + 1< n:
    if s[i] == s[(i+1)%n]:
        s[i] = '!'
        ans += k
    i+=1

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

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
n = len(s)
"""
sの内部で隣り合う場合、k回入れ替える必要がある
　特にsの内部で入れ替える場合、3つ連続ならば1つ入れ替えるだけで良い
　lens(s)<=100と小さいのでシミュレーションで解ける
sの先頭と末尾で隣あう場合、k-1回入れ替える必要がある
"""
i = 1
ans = 0
while i < n:
    if s[i-1] == s[i]:
        s[i] = '?'
        ans += k
    i+=1
if s[0] == s[-1]:
    if n == 1:
        ans += k//2
    else:
        ans += k-1
print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
n = len(s)
"""
sの内部で隣り合う場合、k回入れ替える必要がある
　特にsの内部で入れ替える場合、3つ連続ならば1つ入れ替えるだけで良い
　lens(s)<=100と小さいのでシミュレーションで解ける
sの先頭と末尾で隣あう場合、k-1回入れ替える必要がある
"""
i = 1
ans = 0
while i < n:
    if s[i-1] == s[i]:
        s[i] = '?'
        ans += k
    i+=1
if s[0] == s[-1]:
    ans += k-1
print(ans)

Submission status: Wrong Answer

Solution code:
S = list(input())
K = int(input())
# print(f'{S=}, {K=}')

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

ans = 0
if S[0] == S[-1]:
    h = 0
    for i in range(len(S)):
        if S[0] == S[i]:
            h += 1
        else:
            break
    t = 0
    for i in range(len(S) - 1, -1, -1):
        if S[-1] == S[i]:
            t += 1
        else:
            break
    if (h % 2 == 1) and (t % 2 == 1):
        ans += K - 1
    # print(f'{h=}, {t=}')
cnt = 0
i = len(S) - 1
for i in range(len(S) - 1):
    if S[i] == S[i + 1]:
        cnt += 1
        S[i + 1] = 0
# print(f'{cnt=}')
ans += cnt * K
print(ans)

Submission status: Wrong Answer

Solution code:
S = list(input())
K = int(input())
# print(f'{S=}, {K=}')

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

ans = 0
if S[0] == S[-1]:
    h = 0
    for i in range(len(S)):
        if S[0] == S[i]:
            h += 1
        else:
            break
    if h == len(S):
        print((len(S) // 2) * K)
        exit()
    t = 0
    for i in range(len(S) - 1, -1, -1):
        if S[-1] == S[i]:
            t += 1
        else:
            break
    if (h % 2 == 1) and (t % 2 == 1):
        ans += K - 1
    # print(f'{h=}, {t=}')
cnt = 0
i = len(S) - 1
for i in range(len(S) - 1):
    if S[i] == S[i + 1]:
        cnt += 1
        S[i + 1] = 0
# print(f'{cnt=}')
ans += cnt * K
print(ans)

Submission status: Wrong Answer

Solution code:
S = list(input())
K = int(input())
# print(f'{S=}, {K=}')

cnt = 0
if S[0] == S[-1]:
    S[-1] = 0
    cnt += 1

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

print(cnt * K)

Submission status: Wrong Answer

Solution code:
'''
Created on 2020/08/30

@author: harurun
'''
def main():
  import sys
  pin=sys.stdin.readline
  pout=sys.stdout.write
  perr=sys.stderr.write
  
  S=pin()[:-1]
  K=int(pin())
  
  cnt=0
  t="_"
  for i in S:
    if t==i:
      cnt+=1
      t="_"
    else:
      t=i 
  print(cnt*K)
  return 
main()

Submission status: Wrong Answer

Solution code:
import copy

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

S_original = copy.copy(S)
N = len(S)
temp_ans = 0

for i in range(N-1):
    if S[i] == S[i+1]:
        S[i+1] = "*"
        temp_ans += 1
        
if S[0] == S[-1]:
    S[-1] = "$"
    temp_ans += 1

if S[-1]=="$" and K!=0:
    print(temp_ans * K - 1)
else:
    print(temp_ans * K)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque
from collections import Counter
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

s = list(input())
k = readInt()

if len(Counter(s))==1:
	ans = k*len(s)//2
elif s[0]!=s[-1]:
	ans = 0
	i = 0
	l = 0
	tmp = s[:]+["!"]
	d = []
	while i<len(s)-1:
		if tmp[i]==tmp[i+1]:
			i+=1
		else:
			d.append(tmp[l:i+1])
			l = i+1
			i+=1
	for i in d:
		ans+=len(i)//2
	ans*=k
else:
	ans = 0
	i = 0
	l = 0
	tmp = s[:]+["!"]
	d = []
	while i<len(s):
		if tmp[i]==tmp[i+1]:
			i+=1
		else:
			d.append(tmp[l:i+1])
			l = i+1
			i+=1
	for i in d[1:-1]:
		ans+=len(i)//2
	ans*=k
	ans+=len(d[0])//2
	ans+=len(d[-1])//2
	ans+=(len(d[0])+len(d[-1]))//2*(k-1)


print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque
from collections import Counter
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

s = list(input())
k = readInt()
tmp = s[:]
ans = float("inf")
same=0
ansk = 0
if k!=1 and tmp[0]==tmp[-1]:
	ansk+=1
	tmp[0] = "-1"
	sama=1
for i in range(1, len(s)):
	if tmp[i]==tmp[i-1]:
		tmp[i] = str(i)
		ansk+=1
ans = min(ans,ansk*k-same)
#print(ans)

same=0
ansk = 0
tmp = s[:]
if k!=1 and tmp[0]==tmp[-1]:
	tmp[0] = "-1"
	ansk+=1
	sama=1
for i in range(len(s)-1, 0, -1):
	if tmp[i]==tmp[i-1]:
		tmp[i-1] = str(i)
		ansk+=1
ans = min(ans,ansk*k-same)
#print(ans,s)

same=0
ansk = 0
tmp = s[:]
for i in range(1, len(s)):
	if tmp[i]==tmp[i-1]:
		tmp[i] = str(i)
		ansk+=1
	sama=1
if k!=1 and tmp[0]==tmp[-1]:
	ansk+=1
	tmp[0] = "-1"
ans = min(ans,ansk*k-same)

same=0
ansk = 0
tmp = s[:]
for i in range(len(s)-1,0,-1):
	if tmp[i]==tmp[i-1]:
		tmp[i-1] = str(i)
		ansk+=1
if k!=1 and tmp[0]==tmp[-1]:
	ansk+=1
	tmp[-1] = "-1"
	sama=1
ans = min(ans,ansk*k-same)


print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque
from collections import Counter
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

s = list(input())
k = readInt()
tmp = s[:]
ans = float("inf")
same=0
ansk = 0
if k!=1 and tmp[0]==tmp[-1]:
	ansk+=1
	tmp[-1] = "-1"
	sama=1
for i in range(1, len(s)):
	if tmp[i]==tmp[i-1]:
		tmp[i] = str(i)
		ansk+=1
ans = min(ans,ansk*k-same)
#print(ans)

same=0
ansk = 0
tmp = s[:]
if k!=1 and tmp[0]==tmp[-1]:
	tmp[0] = "-1"
	ansk+=1
	sama=1
for i in range(1, len(s)):
	if tmp[i]==tmp[i-1]:
		tmp[i] = str(i)
		ansk+=1
ans = min(ans,ansk*k-same)
#print(ans,s)

same=0
ansk = 0
tmp = s[:]
for i in range(1, len(s)):
	if tmp[i]==tmp[i-1]:
		tmp[i] = str(i)
		ansk+=1
	sama=1
if k!=1 and tmp[0]==tmp[-1]:
	ansk+=1
	tmp[0] = "-1"
ans = min(ans,ansk*k-same)

same=0
ansk = 0
tmp = s[:]
for i in range(len(s)-1,0,-1):
	if tmp[i]==tmp[i-1]:
		tmp[i] = str(i)
		ansk+=1
if k!=1 and tmp[0]==tmp[-1]:
	ansk+=1
	tmp[-1] = "-1"
	sama=1
ans = min(ans,ansk*k-same)

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque
from collections import Counter
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

s = list(input())
k = readInt()
tmp = s[:]
ans = float("inf")
same=0
ansk = 0
if k!=1 and tmp[0]==tmp[-1]:
	ansk+=1
	tmp[-1] = "-1"
	sama=1
for i in range(1, len(s)):
	if tmp[i]==tmp[i-1]:
		tmp[i] = str(i)
		ansk+=1
ans = min(ans,ansk*k-same)
#print(ans)

same=0
ansk = 0
tmp = s[:]
if k!=1 and tmp[0]==tmp[-1]:
	tmp[0] = "-1"
	ansk+=1
for i in range(1, len(s)):
	if tmp[i]==tmp[i-1]:
		tmp[i] = str(i)
		ansk+=1
ans = min(ans,ansk*k)
#print(ans,s)

same=0
ansk = 0
tmp = s[:]
for i in range(1, len(s)):
	if tmp[i]==tmp[i-1]:
		tmp[i] = str(i)
		ansk+=1
if k!=1 and tmp[0]==tmp[-1]:
	ansk+=1
	tmp[0] = "-1"
ans = min(ans,ansk*k)

same=0
ansk = 0
tmp = s[:]
for i in range(len(s)-1,0,-1):
	if tmp[i]==tmp[i-1]:
		tmp[i] = str(i)
		ansk+=1
if k!=1 and tmp[0]==tmp[-1]:
	ansk+=1
	tmp[-1] = "-1"
	sama=1
ans = min(ans,ansk*k-same)

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque
from collections import Counter
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

s = list(input())
k = readInt()
tmp = s[:]
ans = float("inf")

ansk = 0
if k!=1 and tmp[0]==tmp[-1]:
	ansk+=1
	tmp[-1] = "-1"
for i in range(1, len(s)):
	if tmp[i]==tmp[i-1]:
		tmp[i] = str(i)
		ansk+=1
ans = min(ans,ansk*k)
#print(ans)

ansk = 0
tmp = s[:]
if k!=1 and tmp[0]==tmp[-1]:
	tmp[0] = "-1"
	ansk+=1
for i in range(1, len(s)):
	if tmp[i]==tmp[i-1]:
		tmp[i] = str(i)
		ansk+=1
ans = min(ans,ansk*k)
#print(ans,s)

ansk = 0
tmp = s[:]
for i in range(1, len(s)):
	if tmp[i]==tmp[i-1]:
		tmp[i] = str(i)
		ansk+=1
if k!=1 and tmp[0]==tmp[-1]:
	ansk+=1
	tmp[0] = "-1"
ans = min(ans,ansk*k)

ansk = 0
tmp = s[:]
for i in range(len(s)-1,0,-1):
	if tmp[i]==tmp[i-1]:
		tmp[i] = str(i)
		ansk+=1
if k!=1 and tmp[0]==tmp[-1]:
	ansk+=1
	tmp[-1] = "-1"
ans = min(ans,ansk*k)

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque
from collections import Counter
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

s = list(input())
k = readInt()
tmp = s[:]
ans = float("inf")

ansk = 0
if k!=1 and tmp[0]==tmp[-1]:
	ansk+=1
	tmp[-1] = "-1"
for i in range(1, len(s)):
	if tmp[i]==tmp[i-1]:
		tmp[i] = str(i)
		ansk+=1
ans = min(ans,ansk*k)
#print(ans)

ansk = 0
tmp = s[:]
if k!=1 and tmp[0]==tmp[-1]:
	tmp[0] = "-1"
	ansk+=1
for i in range(1, len(s)):
	if tmp[i]==tmp[i-1]:
		tmp[i] = str(i)
		ansk+=1
ans = min(ans,ansk*k)
#print(ans,s)

ansk = 0
tmp = s[:]
for i in range(1, len(s)):
	if tmp[i]==tmp[i-1]:
		tmp[i] = str(i)
		ansk+=1
if k!=1 and tmp[0]==tmp[-1]:
	ansk+=1
	tmp[0] = "-1"
ans = min(ans,ansk*k)

ansk = 0
tmp = s[:]
for i in range(1, len(s)):
	if tmp[i]==tmp[i-1]:
		tmp[i] = str(i)
		ansk+=1
if k!=1 and tmp[0]==tmp[-1]:
	ansk+=1
	tmp[-1] = "-1"
ans = min(ans,ansk*k)

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque
from collections import Counter
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

s = list(input())
k = readInt()

ans = 0
if k!=1 and s[0]==s[-1]:
	ans+=1
	s[-1] = "-1"
for i in range(1, len(s)):
	if s[i]==s[i-1]:
		s[i] = str(i)
		ans+=1

print(ans*k)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque
from collections import Counter
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

s = list(input())
k = readInt()

ans = 0
for i in range(1, len(s)):
	if s[i]==s[i-1]:
		s[i] = str(i)
		ans+=1

if k!=1 and s[0]==s[-1]:
	ans+=1

print(ans*k)

Submission status: Wrong Answer

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

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

else:
    cnt = 0
    SS = S
    for i in range(1, len(S)):
        if SS[i] == SS[i-1]:
            cnt += 1
            SS[i] = '*'

    if S[-1] != S[0]:
        ans = cnt * K

    else:
        a, b = 0, 0
        while True:
            a += 1
            if S[-(1 + a)] != S[-a]:
                break
        while True:
            b += 1
            if S[b - 1] != S[b]:
                break

        tmp = a // 2 + b // 2 - (a + b) // 2
        ans = cnt * K - tmp

        print(tmp)

print(ans)

Submission status: Wrong Answer

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

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

else:
    cnt = 0
    for i in range(1, len(S)):
        if S[i] == S[i-1]:
            cnt += 1
            S[i] = '*'

    middle = 1 if S[-1] == S[0] else 0
    ans = cnt * K + middle * (K - 1)

    print(ans)

Submission status: Wrong Answer

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

cnt = 0
for i in range(1, len(S)):
    if S[i] == S[i-1]:
        cnt += 1
        S[i] = '*'

middle = 1 if S[-1] == S[0] else 0
ans = cnt * K + middle * (K - 1)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
s = list(input())
k = int(input())
ans = 0
flg = 0
for i in range(len(s)-1):
  if s[i] != s[i+1]:
    flg = 1
    break
if flg == 0:
  print(len(s)*k//2)
  sys.exit()
for i in range(len(s)-1):
  if s[i] == s[i+1]:
    s[i+1] = " "
    ans += k
if s[0] == s[-1]:
  if s[-2]!=" " and s[1]!=" ":
    ans += k-1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
s = list(input())
k = int(input())
ans = 0
flg = 0
for i in range(len(s)-1):
  if s[i] != s[i+1]:
    flg = 1
    break
if flg == 0:
  print(len(s)*k//2)
  sys.exit()
for i in range(len(s)-1):
  if s[i] == s[i+1]:
    s[i+1] = " "
    ans += k
if s[0] == s[-1]:
  ans += k-1
print(ans)

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] = " "
    ans += k
if s[0] == s[-1]:
  ans += k-1
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil,pi,factorial
    from operator import itemgetter
    def I(): return int(input())
    def MI(): return map(int, input().split())
    def LI(): return list(map(int, input().split()))
    def LI2(): return [int(input()) for i in range(n)]
    def MXI(): return [[LI()]for i in range(n)]
    def SI(): return input().rstrip()
    def printns(x): print('\n'.join(x))
    def printni(x): print('\n'.join(list(map(str,x))))
    inf = 10**17
    mod = 10**9 + 7
#main code here!
    s=SI()
    k=I()
    u=len(s)
    seq=0
    x=1
    for i in range(u-1):
        if s[i]==s[i+1]:
            x+=1
        else:
            seq+=x//2
            x=1
    seq+=x//2
    #print(seq)
            
    a=1
    for i in range(u-1):
        if s[0]==s[i+1]:
            a+=1
        else:
            break
    b=1
    for i in range(u-1):
        if s[-1]==s[u-1-(1+i)]:
            b+=1
        else:
            break
    #print(a,b)
    if x==u-1:
        ans=(u*k)//2
    else:
        if s[0]!=s[-1]:
            ans=(seq)*k
        else:
            ans=(seq)*k-(a//2+b//2-(a+b)//2)*(k-1)
    print(ans)
        





if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil,pi,factorial
    from operator import itemgetter
    def I(): return int(input())
    def MI(): return map(int, input().split())
    def LI(): return list(map(int, input().split()))
    def LI2(): return [int(input()) for i in range(n)]
    def MXI(): return [[LI()]for i in range(n)]
    def SI(): return input().rstrip()
    def printns(x): print('\n'.join(x))
    def printni(x): print('\n'.join(list(map(str,x))))
    inf = 10**17
    mod = 10**9 + 7
#main code here!
    s=SI()
    k=I()
    u=len(s)
    if u==1:
        ans=k//2
    s1=s+s
    s2=s+s+s
    c=1
    ans1,ans2=0,0
    for i in range(2*u-1):
        if s1[i]==s1[i+1]:
            c+=1
        else:
            ans1+=c//2
            #print(c,c//2,i)
            c=1
    ans1+=c//2

        #print(c)
    c=1
    for i in range(3*u-1):
        if s2[i]==s2[i+1]:
            c+=1
        else:
            ans2+=c//2
            c=1
    ans2+=c//2
    #print(ans1)
    #print(ans2)
    if k==1:
        ans=0
        c=1
        for i in range(u-1):
            if s[i]==s[i+1]:
                c+=1
            else:
                ans+=c//2
                c=1
        ans+=c//2
        print(ans)
        
        
    else:
        print(ans1+(ans2-ans1)*(k-2))





if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil,pi,factorial
    from operator import itemgetter
    def I(): return int(input())
    def MI(): return map(int, input().split())
    def LI(): return list(map(int, input().split()))
    def LI2(): return [int(input()) for i in range(n)]
    def MXI(): return [[LI()]for i in range(n)]
    def SI(): return input().rstrip()
    def printns(x): print('\n'.join(x))
    def printni(x): print('\n'.join(list(map(str,x))))
    inf = 10**17
    mod = 10**9 + 7
#main code here!
    s=SI()
    k=I()
    u=len(s)
    if u==1:
        ans=k//2
    s1=s+s
    s2=s+s+s
    c=1
    ans1,ans2=0,0
    for i in range(2*u-1):
        if s1[i]==s1[i+1]:
            c+=1
        else:
            ans1+=c//2
            #print(c,c//2,i)
            c=1
    ans1+=c//2

        #print(c)
    c=1
    for i in range(3*u-1):
        if s2[i]==s2[i+1]:
            c+=1
        else:
            ans2+=c//2
            c=1
    ans2+=c//2
    #print(ans1)
    #print(ans2)
    if k==1:
        ans=0
        c=1
        for i in range(u-1):
            if s[i]==s[i+1]:
                c+=1
            else:
                ans+=c//2
                c=1
        ans+=c//2
        print(ans)
        
        
    if k>=2:
        print(ans1+(ans2-ans1)*(k-2))





if __name__=="__main__":
    main()