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())
n = int(input())
ans = 0
if s[0] != s[-1]:
    cnt = 1
    tmp = ''
    for i in range(len(s)):
        if i == 0:
            tmp = s[i]
            continue
        if tmp == s[i]:
            cnt += 1
        else:
            ans += cnt // 2
            cnt = 1
            tmp = s[i]
    ans += cnt // 2
    print(ans*n)
else:
    cnt = 1
    tmp = ''
    f = 0
    for i in range(len(s)):
        if i == 0:
            tmp = s[i]
            continue
        if tmp == s[i]:
            cnt += 1
        else:
            if f == 0:
                f = cnt
            ans += cnt // 2
            cnt = 1
            tmp = s[i]
    ans += cnt // 2
    if cnt // 2 != (cnt+f)//2:
        t = (cnt+f)//2 - cnt // 2
        print(ans*n + (n-1)*t)
    else:
        print(ans*n)

Submission status: Wrong Answer

Solution code:
#AGC039-A Connection and Disconnection
"""
sをk回繰り返して得られる文字列tを、全て隣り合う文字が異なるようにする
為に必要な操作回数を求めよ
"""
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0
s = readline().rstrip().decode('utf-8')
k = int(readline())
count = 0
res = ""
for i in s:
    if i == res:
        count += 1
        res = ""
    else:
        res = i

res = s[-1]
ct2 = 0
for i in s[::-1]:
    if i == res:
        ct2 += 1
    else:
        break
if not even(ct2):
    flag = 0
else:
    flag = 1
res = s[0]
ct2 = 0
for i in s:
    if i == res:
        ct2 += 1
    else:
        break
if not flag:
    if even(ct2):
        flag = 1



if s[0] == s[-1] and not flag:
    count += 1
    print(count*k-1)

else:
    print(count*k)

Submission status: Wrong Answer

Solution code:
#AGC039-A Connection and Disconnection
"""
sをk回繰り返して得られる文字列tを、全て隣り合う文字が異なるようにする
為に必要な操作回数を求めよ
"""
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0
s = readline().rstrip().decode('utf-8')
k = int(readline())
count = 0
res = ""
for i in s:
    if i == res:
        count += 1
        res = ""
    else:
        res = i

res = s[-1]
ct2 = 0
for i in s[::-1]:
    if i == res:
        ct2 += 1
    else:
        break

if not even(ct2) and ct2 != 1:
    flag = 0
else:
    flag = 1

if s[0] == s[-1] and not flag:
    count += 1
    print(count*k-1)

else:
    print(count*k)

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
l=len(s)
now=1
seq=[]
re=False
all=False
for i in range(l):
    if s[i%l]==s[(i+1)%l]:
        now+=1
        if i==l-1:
            re=True
            if len(seq)==0:
                all=True
            else:
                first=seq[0]//2
                end=now//2
                seq[0]=(seq[0]+now)
    else:
        seq.append(now)
        now=1
if all:
    ans=len(s)*k//2
else:
    seq=[i//2 for i in seq]
    if re:
        ans=sum(seq)*k-seq[0]+first+end
    else:
        ans=sum(seq)*k

print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
l=len(s)
now=1
seq=[]
re=False
all=False
for i in range(l):
    if s[i%l]==s[(i+1)%l]:
        now+=1
        if i==l-1:
            re=True
            if len(seq)==0:
                all=True
            else:
                first=seq[0]
                end=now
                seq[0]=(seq[0]+now)
    else:
        seq.append(now)
        now=1
if all:
    ans=len(s)*k//2
else:
    seq=[i//2 for i in seq]
    if re:
        ans=sum(seq)*k-seq[0]+first+end
    else:
        ans=sum(seq)*k

print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
l=len(s)
now=1
seq=[]
re=False
all=False
for i in range(l):
    if s[i%l]==s[(i+1)%l]:
        now+=1
        if i==l-1:
            re=True
            if len(seq)==0:
                all=True
            else:
                first=seq[0]//2
                end=now//2
                seq[0]=(seq[0]+now)//2
    else:
        seq.append(now//2)
        now=1
if all:
    ans=len(s)*k//2
else:
    if re:
        ans=sum(seq)*k-seq[0]+first+end
    else:
        ans=sum(seq)*k

print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
l=len(s)
now=0
seq=[]
re=False
all=False
for i in range(l):
    if s[i%l]==s[(i+1)%l]:
        now+=1
        if i==l-1:
            re=True
            if len(seq)==0:
                all=True
            else:
                first=seq[0]//2
                end=now//2
                seq[0]=(seq[0]+now)//2
    else:
        seq.append(now//2)
        now=1
if all:
    ans=len(s)*k//2
else:
    if re:
        ans=sum(seq)*k-seq[0]+first+end
    else:
        ans=sum(seq)*k

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import groupby
s = input()
k = int(input())
ans = 0
count = []
for i, j in groupby(s):
    count.append(len(list(j)))
if k > 1 and s[0] == s[-1] and len(set(s)) != 1:
    f = count[0]
    e = count[-1]
    ans += f//2 + e//2 + (f+e)//2*(k-1)
    for n in count[1:-1]:
        ans += n//2 * k
else:
    for n in count:
        ans += n//2 * k
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
def main():
  S = input()
  K = int(input())
  cc = 0
  gg = itertools.groupby(S)
  for key, item in gg:
    cc += len(list(item))//2
  a = b = 0
  if S[0] == S[-1]:
    while a < len(S) and S[a] == S[0]:
      a += 1
    S = S[::-1]
    while b < len(S) and S[b] == S[0]:
      b += 1
  output = K * cc - (K-1) * (a//2 + b//2 - (a+b)//2)
  print(output)
  return

if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
def main():
  S = input()
  K = int(input())
  cc, i = 0, 0
  while i < len(S)-1:
    if S[i] == S[i+1]:
      cc += 1
      i += 2
    else: i += 1
  S = S * 2
  i, cc1 = 0, 0
  while i < len(S)-1:
    if S[i] == S[i+1]:
      cc1 += 1
      i += 2
    else: i += 1
  print(cc * K + (cc1 - 2*cc) * (K-1))
  return

if __name__ == '__main__':
  main()

Submission status: Wrong Answer

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

ans = 0
prev = ''
for c in S:
    if c == prev:
        ans += 1
        prev = ''
        continue
    prev = c
print(ans*K)

Submission status: Wrong Answer

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

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

if k%2==0:
    print(cnt*k)
elif k%2==1 and s[0]!=s[-1]:
    print(cnt*(k-1)+(cnt-1))

else:
    print(cnt*k)

Submission status: Wrong Answer

Solution code:
S = list(input())
k = int(input())
		
if k == 1:
	cnt = 0
	prev = S[0]
	for i in range(1, len(S)):
		if prev == "":
			prev = S[i]
		else:
			if prev == S[i]:
				cnt += 1
				prev = ""
			else:
				prev = S[i]	
	print(cnt)
else:
	SS = S + S
	cnt = 0
	prev = SS[0]
	for i in range(1, len(SS)):
		if prev == "":
			prev = SS[i]
		else:
			if prev == SS[i]:
				cnt += 1
				prev = ""
			else:
				prev = SS[i]
	if S[0] != S[-1]:
		if k % 2 == 0:
			res = cnt * (k // 2)
		else:
			res = cnt * (k // 2) + cnt // 2
	else:
		if len(set(S)) == 1:
			if len(S) % 2 == 0:
				if k % 2 == 0:
					res = cnt * (k // 2)
				else:
					res = cnt * (k // 2) + cnt // 2
			else:
				if k % 2 == 0:
					res = (cnt + 1) * (k // 2) - 1
				else:
					res = (cnt + 1) * (k // 2) - 1 + (cnt + 1) // 2
		else:
			i = 0
			cnt_head = 0
			while S[i] == S[0]:
				cnt_head += 1
				i += 1
				
			i = -1
			cnt_foot = 0	
			while S[i] == S[0]:
				cnt_foot += 1
				i -= 1
			
			if cnt_head % 2 == 0 or cnt_foot % 2 == 0:
				if k % 2 == 0:
					res = cnt * (k // 2)
				else:
					res = cnt * (k // 2) + cnt // 2
			else:
				if k % 2 == 0:
					res = (cnt + 1) * (k // 2) - 1
				else:
					res = (cnt + 1) * (k // 2) - 1 + (cnt + 1) // 2
			
	print(res)

Submission status: Wrong Answer

Solution code:
S = list(input())
N = len(S)
K = int(input())
  
if len(set(S)) == 1:
  print((N*K)//2)
  exit()

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

a = 0
while S[0] == S[a+1]:
  a += 1

b = 0
while S[N-1] == S[N-2-b]:
  b += 1

print(ans*K - (K-1)*(a//2 + b//2 - (a+b)//2))

Submission status: Wrong Answer

Solution code:
S = list(input())
N = len(S)
K = int(input())
  
if len(set(S)) == 1:
  print((N*K)//2)
  exit()

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

if K != 1 and S[N-1] == S[0]:
  ans += K-1
print(ans)

Submission status: Wrong Answer

Solution code:
S = list(input())
N = len(S)
K = int(input())
  
if len(set(S)) == 1:
  print(N*K//2)
  exit()
  
S.append(S[0])

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

if K != 1 and S[N-1] == S[N]:
  ans += K-1
print(ans)

Submission status: Wrong Answer

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

if N == 1:
  print(K//2)
  exit()
S.append(S[0])

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

if K == 1 and S[N-1] == S[N]:
  ans += K-1
print(ans)

Submission status: Wrong Answer

Solution code:
S = list(input())
N = len(S)
S.append(S[0])
K = int(input())
ans = 0

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

if K == 1 and S[N-1] == S[N]:
  ans += K-1
print(ans)

Submission status: Wrong Answer

Solution code:
S = list(input())
N = len(S)
S.append(S[0])

K = int(input())
ans = 0
for i in range(N):
  if S[i] == S[i+1]:
    ans += 1
    S[i+1] = '0'
print(ans*K)

Submission status: Wrong Answer

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

prev = ''
cnt = 0
for s in S:
    if s == prev:
        cnt += 1
        prev = ''
    else:
        prev = s

if K % 2 == 0:
    res = cnt * (K // 2)
else:
    res = cnt * (K // 2) + cnt // 2
print(res)

Submission status: Wrong Answer

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

prev = ''
cnt = 0
for s in S:
    if s == prev:
        cnt += 1
        prev = ''
    else:
        prev = s

res = cnt * K
print(res)

Submission status: Wrong Answer

Solution code:
s = input()
n = int(input())
ar = []
pre = ''
cnt = 0
tcnt = 0
for idx, c in enumerate(s):
    if pre == c:
        tcnt += 1
    else:
        t = (tcnt+1) // 2
        ar.append(tcnt)
        cnt += t
        tcnt = 0
    pre = c
cnt += ((tcnt+1) // 2)
if len(s) > 1 and s[0] == s[-1]:
    t = n*cnt
    d = ((ar[0]+1)//2) + ((ar[-1]+1)//2) - ((ar[0]+ar[-1]+1)//2)
    print(t-(d*n))
else:    
    print(n*cnt)

Submission status: Wrong Answer

Solution code:
s = input()
n = int(input())
pre = ''
cnt = 0
tcnt = 0
for c in s:
    if pre == c:
        tcnt += 1
    else:
        cnt += ((tcnt+1) // 2)
        tcnt = 0
    pre = c
cnt += ((tcnt+1) // 2)
print(n*cnt)

Submission status: Wrong Answer

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


else:
    for i in range(len(s)-1):
        if s[i]==s[i+1]:
            count+=1
        else:
            if 2<=count:
                ans+=count//2
            count=1
    ans+=count//2
    print(ans*k)

Submission status: Wrong Answer

Solution code:
s=input()
n=int(input())
set_s=set(list(s))
if len(set_s)==1:
    print(len(s)*n//2)
    exit()

ans=0
same=False
cnt=0
for i in range(len(s)):
    if i>0 and s[i-1]==s[i]:
        cnt+=1
    else:
        ans+=(cnt+1)//2
        cnt=0

ans+=(cnt+1)//2

if s[0]==s[-1] and s[-1]!=s[-2]:
    ans+=1
    same=True

print(ans*n if same==False else ans*n-1)

Submission status: Wrong Answer

Solution code:
s=input()
n=int(input())
set_s=set(list(s))
if len(set_s)==1:
    print(len(s)*n//2)
    exit()

if n==1:
    print(0)
    exit()
ans=0
same=False
cnt=0
for i in range(len(s)):
    if i>0 and s[i-1]==s[i]:
        cnt+=1
    else:
        ans+=(cnt+1)//2
        cnt=0

ans+=(cnt+1)//2

if s[0]==s[-1] and s[-1]!=s[-2]:
    ans+=1
    same=True

print(ans*n if same==False else ans*n-1)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
n = len(s)
c = 0
x = []
a = 1

for i in range(n):
    if i<n-1 and s[i]==s[i+1]:
        a += 1
    else:
        c += a//2
        x.append(a)
        a = 1

c2 = 0
if s[0]==s[-1]:
    c2 = x[0]//2+x[-1]//2-(x[0]+x[-1])//2

print(c*k-c2*(k-1) if len(set(s))!=0 else n//2)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
n = len(s)
c = 0
x = []
a = 1

for i in range(n):
    if i<n-1 and s[i]==s[i+1]:
        a += 1
    else:
        c += a//2
        x.append(a)
        a = 1

c2 = 0
if s[0]==s[-1]:
    c2 = x[0]//2+x[-1]//2-(x[0]+x[-1])//2

print(c*k-c2*(k-1))

Submission status: Wrong Answer

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

a = 1
s2 = s*2
c2 = 0
for i in range(n*2):
    if i<n*2-1 and s2[i]==s2[i+1]:
        a += 1
    else:
        c2 += a//2
        a = 1

a = 1
c = 0
for i in range(n):
    if i<n-1 and s[i]==s[i+1]:
        a += 1
    else:
        c += a//2
        a = 1
c = c2-c
print(c*(k%2)+c2*(k//2))

Submission status: Wrong Answer

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

a = 1
s2 = s*2
c2 = 0
for i in range(n*2):
    if i<n*2-1 and s2[i]==s2[i+1]:
        a += 1
    else:
        c2 += a//2
        a = 1

a = 1
c = 0
for i in range(n*3):
    if i<n-1 and s[i]==s[i+1]:
        a += 1
    else:
        c += a//2
        a = 1
c = c2-c
print(c*(k%2)+c2*(k//2))

Submission status: Wrong Answer

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

ans = 0
if len(S) == 1:
    print(1)
    exit()
if k > 1 and  S[0] == S[-1]:
    S[-1] = 0
    ans += 1
b = ""
for s in S:
    if s == b: 
        ans += 1
        b = ""
    else: b = s
print(ans*k)

Submission status: Wrong Answer

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

ans = 0
if k > 1 and  S[0] == S[-1]:
    S[-1] = 0
    ans += 1
b = ""
for s in S:
    if s == b: 
        ans += 1
        b = ""
    else: b = s
print(ans*k)

Submission status: Wrong Answer

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

ans = 0
if S[0] == S[-1]:
    S[-1] = 0
    ans += 1
b = ""
for s in S:
    if s == b: 
        ans += 1
        b = ""
    else: b = s
print(ans*k)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
n = len(s)
c = 0
a = 1
f = True
for i in range(n):
    if i<n-1 and s[i]==s[i+1]:
        a += 1
    else:
        c += a//2
        a = 1
a = 1
s = s*2
c2 = 0
for i in range(n*2):
    if i<n*2-1 and s[i]==s[i+1]:
        a += 1
    else:
        c2 += a//2
        a = 1

print(c*(k%2)+c2*(k//2))

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    s = list(input())
    k = int(input())
    if len(s) == 1:
        print(0)
        exit(0)

    ans = 0
    for i in range(len(s) - 1):
        if s[i] == s[i + 1]:
            s[i + 1] = '-1'
            ans += 1
    if s[0] == s[-1]:
        ans += 1
    print(ans * k)

Submission status: Wrong Answer

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

if S[0] != S[-1]:
    ans = 0
    for i in range(len(S) - 1):
        if S[i] == S[i + 1]:
            S = S[:i + 1] + '*' + S[i + 2:]
            ans += 1
    print(ans * K)
else:
    # S -> s_1 s_2 ... s_k + A + t_l t_l-1 ... t_1 (s_i = t_j)と分解
    k, l = 0, len(S) - 1
    while k < len(S) and S[k] == S[-1]:
        k += 1
    while l > -1 and S[l] == S[0]:
        l -= 1

    # 連結に関係ない部分のカウント
    A = S[k:l + 1]
    ans = 0
    for i in range(len(A) - 1):
        if A[i + 1] == A[i]:
            A = A[:i + 1] + '*' + A[i + 2:]
            ans += 1
    ans *= K

    # 連結に関係ある部分のカウント
    if l == -1 and k == len(S):
        # 全部同じ
        print((len(S) * K) // 2)
    else:
        ans += (len(S) - l + k - 1) // 2 * (K - 1)
        ans += (len(S) - l) // 2 + (k - 1) // 2
        print(ans)

Submission status: Wrong Answer

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

if len(S) == 1:
    print(K // 2)  
elif S[0] != S[-1]:
    ans = 0
    for i in range(len(S[1:-1]) - 1):
        if S[i + 2] == S[i + 1]:
            S = S[:i + 2] + '*' + S[i + 3:]
            ans += 1
    print(ans * K)
else:
    # S -> s_1 s_2 ... s_k + A + t_l t_l-1 ... t_1 (s_i = t_j)と分解
    k, l = 0, len(S) - 1
    while k < len(S) and S[k] == S[-1]:
        k += 1
    while l > -1 and S[l] == S[0]:
        l -= 1

    # 連結に関係ない部分のカウント
    A = S[k:l + 1]
    ans = 0
    for i in range(len(A) - 1):
        if A[i + 1] == A[i]:
            A = A[:i + 1] + '*' + A[i + 2:]
            ans += 1
    ans *= K

    # 連結に関係ある部分のカウント
    if l == -1 and k == len(S):
        # 全部同じ
        print((len(S) * K) // 2)
    else:
        ans += (len(S) - l + k - 1) // 2 * (K - 1)
        ans += (len(S) - l) // 2 + (k - 1) // 2
        print(ans)

Submission status: Wrong Answer

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

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

ans = 0
if s[0] == s[-1]:
    ans += k - 1
    char = s[-1]
    s[-1] = '0'

skip = -1
cnt = 0
for i in range(len(s) - 1):
    if i == skip:
        continue
    
    if s[i] == s[i + 1]:
        s[i + 1] = '1'
        cnt += 1
        skip = i + 1

if s[-1] == '0':
    if char == s[-2]:
        ans += 1

print(ans + cnt * k)

Submission status: Wrong Answer

Solution code:
s = list(input())

n = int(input())
t = 0
for i in range(1, len(s)):
    if s[i - 1] == s[i]:
        s[i] = '0'
        t += 1
u = 0
if len(s) == 1:
    print(n//2)
else:    
    if s[0] == s[-1]:
        u = 1
    print(t * n + u * (n - 1))

Submission status: Wrong Answer

Solution code:
s = list(input())

n = int(input())
t = 0
for i in range(1, len(s)):
    if s[i - 1] == s[i]:
        s[i] = '0'
        t += 1
u = 0
if s[0] == s[-1]:
    u = 1
print(t * n + u * (n - 1))

Submission status: Wrong Answer

Solution code:
s = list(input())

n = int(input())
t = 0
for i in range(1, len(s)):
    if s[i - 1] == s[i]:
        s[i] = '0'
        t += 1
print(t * n)

Submission status: Wrong Answer

Solution code:
S = input()
k = int(input())
ans, tmp = 0, S[0]
for s in S[1:]:
    if tmp[-1]==s:
        s = '*'
        ans += 1
    tmp += s
print(ans*k)

Submission status: Wrong Answer

Solution code:
s=list(input())
k=int(input())
ans=0
tmp=1
for i in range(1,len(s)):
    if s[i]==s[i-1]:
        tmp+=1
    else:
        ans+=tmp//2
        tmp=1
ans+=tmp//2

head=1
tail=tmp
for i in range(1,len(s)):
    if s[0]==s[i]:
        head+=1
if head==len(s):
    ans=len(s)*k//2
else:
    ans*=k
    if s[0]==s[-1]:
        ans-= (k-1)*(head//2 + tail//2 - (head+tail)//2)
print(ans)

Submission status: Wrong Answer

Solution code:
s=list(input())
k=int(input())
ans=0
tmp=1
for i in range(1,len(s)):
    if s[i]==s[i-1]:
        tmp+=1
    else:
        ans+=tmp//2
        tmp=1
ans+=tmp//2

head=1
tail=tmp
for i in range(1,len(s)):
    if s[0]==s[i]:
        head+=1

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

Submission status: Wrong Answer

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

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

def n_change(sss):
    s = sss.copy()
    x = 0
    for i in range(1,len(s)):
        if s[i] == s[i-1]:
            s[i] = "X"
            x += 1
    return x

print(n_change(S) + (K-1) * (n_change(2*S) - n_change(S)))

Submission status: Wrong Answer

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

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

x = 0
for i in range(1,len(S)):
    if S[i] == S[i-1]:
        if i == 1 and S[0] == S[-1]:
            S[0] = "X"
        else:
            S[i] = "X"
        x += 1

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

print(x*K + y*(K-1))

Submission status: Wrong Answer

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

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

x = 0
if S[0] == S[-1] == S[-2]:
    S[-1] = "X"
    x += 1
if S[0] == S[1] == S[-1]:
    S[0] = "X"
    x += 1

for i in range(1,len(S)):
    if S[i] == S[i-1]:
        S[i] = "X"
        x += 1
if S[-1] == S[0]:
    S[-1] = "X"
    x += 1

print(x*K)

Submission status: Wrong Answer

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


ans = 0
if N[0] == N[-1]:
    length = []
    for i in range(len(N)-1):
        if N[i] != N[i+1]:
            length.append(i+1)
            
    if length == []:
        lenght = len(N)
    else:
        length.append(len(N) - length[-1])
        ans += (length[0] + length[-1]) // 2
    
    tmp = 1
    ans2 = 0
    if length != []:
        for i in range(length[0], length[1]):
            if N[i-1] == N[i]:
                tmp += 1
            else:
                ans2 += tmp // 2
                tmp = 1

        ans2 += tmp // 2

        print(ans + ans2 * 2 + length[-1] // 2)
    else:
        print((len(N) * K) // 2 )
    
else:
    tmp = 1
    for i in range(1, len(N)):
        if N[i-1] == N[i]:
            tmp += 1

        else:
            ans += tmp // 2
            tmp = 1
            
    ans += tmp // 2
            
    print(ans * K)

Submission status: Wrong Answer

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


ans = 0
if N[0] == N[-1]:
    length = []
    for i in range(len(N)-1):
        if N[i] != N[i+1]:
            length.append(i+1)
            
    if length == []:
        lenght = len(N)
    else:
        length.append(len(N) - length[-1])
        ans += (length[0] + length[-1]) // 2
    
    tmp = 1
    ans2 = 0
    if length != []:
        for i in range(length[0], length[1]):
            if N[i-1] == N[i]:
                tmp += 1
            else:
                ans2 += tmp // 2
                tmp = 1

        ans2 += tmp // 2

        print(ans + ans2 * 2 + length[-1] // 2)
    else:
        print(len(N) // 2 * K)
    
else:
    tmp = 1
    for i in range(1, len(N)):
        if N[i-1] == N[i]:
            tmp += 1

        else:
            ans += tmp // 2
            tmp = 1
            
    ans += tmp // 2
            
    print(ans * K)

Submission status: Wrong Answer

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

x = 0
for i in range(1,len(S)):
    if S[i] == S[i-1]:
        S[i] = "X"
        x += 1
if S[-1] == S[0]:
    S[-1] = "X"
    x += 1

print(x*K)

Submission status: Wrong Answer

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

tmp = 1
ans = 0
for i in range(1, len(N)):
    if N[i-1] == N[i]:
        tmp += 1
        
    if tmp != 1:
        ans += tmp // 2
        tmp = 0
            
print(ans * K)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# from heapq import heapify, heappop, heappush
from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np  # Pythonのみ！
# from operator import xor
# import re
# from scipy.sparse.csgraph import connected_components  # Pythonのみ！
# ↑cf.  https://note.nkmk.me/python-scipy-connected-components/
# from scipy.sparse import csr_matrix
# import string
import sys

sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():
    S = list(input())
    K = int(input())
    l = []
    cnt = 0
    if len(S)==1:
        print(K//2)
    else:
        gr = groupby(S)
        for key, group in gr:
            l.append(list(group))
        for i in range(len(l)):
            cnt += len(l[i]) // 2

        if l[0][0] == l[-1][0]:
            dev = len(l[0]) // 2 + len(l[-1]) // 2 - (len(l[-1]) + len(l[0])) // 2
        else:
            dev = 0
        print(cnt * K - dev * (K - 1))


resolve()

Submission status: Wrong Answer

Solution code:
from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np  # Pythonのみ！
# from operator import xor
# import re
# from scipy.sparse.csgraph import connected_components  # Pythonのみ！
# ↑cf.  https://note.nkmk.me/python-scipy-connected-components/
# from scipy.sparse import csr_matrix
# import string
import sys

sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():
    S = list(input())
    K = int(input())
    l = []
    cnt = 0
    gr = groupby(S)
    for key, group in gr:
        l.append(list(group))
    for i in range(len(l)):
        cnt += len(l[i]) // 2

    if l[0][0]==l[-1][0]:
        dev = len(l[0]) // 2 + len(l[-1]) // 2 - (len(l[-1]) + len(l[0])) // 2
    else:
        dev=0
    print(cnt * K - dev * (K - 1))


resolve()

Submission status: Wrong Answer

Solution code:
# import sys
# sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]


def solve():
    S = list(input())
    k = II()

    n = len(S)
    cnt = 0

    if len(set(S)) == 1:
        print(len(S) * k // 2)
        return


    rs = S[::-1]

    for i in range(0, n-1):
        j = i + 1
        s1 = S[i]
        s2 = S[j]
        if s1 == s2:
            S[j] = i
            cnt += 1

    if S[0] == S[-1] and len(S) > 1:
        S[-1] = -1
        cnt += 1
    # print(S)
    # print(cnt, cnt * k)
    # print(cnt * k)

    mn = cnt
    cnt = 0
    # print(rs)
    for i in range(0, n-1):
        j = i + 1
        s1 = rs[i]
        s2 = rs[j]
        if s1 == s2:
            rs[j] = i
            cnt += 1
    # print(rs)
    if rs[0] == rs[-1] and len(rs) > 1:
        rs[-1] = -1
        cnt += 1

    # print(rs)
    # print(len(rs) * k // 2)
    # print(mn, cnt)
    ans = min(mn, cnt)
    print(ans * k)

if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
# import sys
# sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]


def solve():
    S = list(input())
    k = II()

    n = len(S)
    cnt = 0

    rs = S[::-1]

    for i in range(0, n-1):
        j = i + 1
        s1 = S[i]
        s2 = S[j]
        if s1 == s2:
            S[j] = i
            cnt += 1

    if S[0] == S[-1]:
        S[-1] = -1
        cnt += 1
    # print(S)
    # print(cnt, cnt * k)
    # print(cnt * k)

    mn = cnt
    cnt = 0
    # print(rs)
    for i in range(0, n-1):
        j = i + 1
        s1 = rs[i]
        s2 = rs[j]
        if s1 == s2:
            rs[j] = i
            cnt += 1
    # print(rs)
    if rs[0] == rs[-1]:
        rs[-1] = -1
        cnt += 1

    # print(rs)
    # print(mn, cnt)
    ans = min(mn, cnt)
    print(ans * k)

if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
s=input()

n=int(input())

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

ss = s+s[0]

ss=list(ss)

ss.reverse()

if ss[0] == ss[1] and ss[1] != ss[2]:
    sa = 1
else:
    sa = 0

count = 0

for i in range(1,len(ss)):
    if ss[i] == ss[i-1]:
        ss[i] = '!'
        count += 1
print(count*n-sa)

Submission status: Wrong Answer

Solution code:
s=input()

n=int(input())

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

ss = s+s[0]

ss=list(ss)

ss.reverse()

count = 0

for i in range(1,len(ss)):
    if ss[i] == ss[i-1]:
        ss[i] = '!'
        count += 1
print(count*n)

Submission status: Wrong Answer

Solution code:
s=input()

n=int(input())

ss = s+s[0]

ss=list(ss)

ss.reverse()

count = 0

for i in range(1,len(ss)):
    if ss[i] == ss[i-1]:
        ss[i] = '!'
        count += 1
print(count*n)

Submission status: Wrong Answer

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



def func(S):
    inner = 0
    S = list(S)
    for i in range(len(S) - 1):
        if S[i] == S[i + 1]:
            S[i + 1] = '#'
            inner += 1
    return inner
inner = func(S)
inter = func(S * 2) - func(S) * 2
print(inner * K + inter * (K - 1))

Submission status: Wrong Answer

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

inter = 0
inner = 0

for i in range(len(S) - 1):
    if S[i] == S[i + 1]:
        S[i + 1] = '#'
        inner += 1
if S[0] == S[-1]:
    inter += K - 1
# print(S)
# print(inter)
print(inter + inner * K)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def run_length(s):
    ans=[]
    if len(s)==1:
        ans.append((s,1))
        return ans
    s_pre=s[0]
    cunt=1
    for i,ss in enumerate(s[1:],1):
        if ss==s_pre:
            cunt+=1
        else:
            ans.append((s_pre,cunt))
            cunt=1
            s_pre=ss
    ans.append((s_pre,cunt))
    return ans

def main():
    s=input()
    k=int(input())
    s_lis=run_length(s)
    ans=0    
    if len(s_lis)==1:
        if len(s)==1:
            print(k//2)
        else:
            print((s_lis[0][1]+1)//2*k)
    else:
        for tup in s_lis:
            ans+=tup[1]//2
        if s_lis[0][0]==s_lis[-1][0] and s_lis[0][1]%2==1 and s_lis[-1][1]%2==1:
            print(ans*k+k-1)
        else:
            print(ans*k)


if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def run_length(s):
    ans=[]
    if len(s)==1:
        ans.append((s,1))
        return ans
    s_pre=s[0]
    cunt=1
    for i,ss in enumerate(s[1:],1):
        if ss==s_pre:
            cunt+=1
        else:
            ans.append((s_pre,cunt))
            cunt=1
            s_pre=ss
    ans.append((s_pre,cunt))
    return ans

def main():
    s=input()
    k=int(input())
    s_lis=run_length(s)
    ans=0
    if len(s_lis)==1:
        print((s_lis[0][1]+1)//2*k)
    else:
        for tup in s_lis:
            ans+=tup[1]//2
        if s_lis[0][0]==s_lis[-1][0] and s_lis[0][1]%2==1 and s_lis[-1][1]%2==1:
            print(ans*k+k-1)
        else:
            print(ans*k)


if __name__=='__main__':
    main()