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 = str(input())
k = int(input())

n = len(s)

s_ = list(s)
if len(set(s_)) == 1:
    print((n*k)//2)
    exit()

cur = s[0]
X = []
cnt = 0
for i in range(n):
    if s[i] != cur:
        X.append(cnt)
        cur = s[i]
        cnt = 1
    else:
        cnt += 1
else:
    X.append(cnt)

#print(X)
ans = 0
if s[0] != s[-1]:
    for x in X:
        ans += k*(x//2)
else:
    for i, x in enumerate(X):
        if i == 0 or i == 1:
            continue
        else:
            ans += k*(x//2)
    ans += X[0]//2
    ans += X[-1]//2
    ans += (k-1)*((X[0]+X[-1])//2)
print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
if len(s) == 1:
    print(k//2)
    exit()

cnt = 0

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

Submission status: Wrong Answer

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

n = len(s)

s_ = list(s)
if len(set(s_)) == 1:
    print((n*k)//2)
    exit()

cur = s[0]
X = []
cnt = 0
for i in range(n):
    if s[i] != cur:
        X.append(cnt)
        cur = s[i]
        cnt = 1
    else:
        cnt += 1
else:
    X.append(cnt)

#print(X)
ans = 0
if s[0] != s[-1]:
    for x in X:
        ans += k*(x//2)
else:
    x = X.pop()
    X[0] += x
    for x in X:
        ans += k*(x//2)
print(ans)

Submission status: Wrong Answer

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

i = 0
cnt = 0
T = []
while i <= N - 1:
    if i == N - 1:
        T.append(S[-1])
        break
    if S[i] == S[i + 1]:
        T.append(S[i] + "?")
        cnt += 1
        i += 2
    else:
        T.append(S[i])
        i += 1
T = "".join(T)
is_head_tail_same = T[0] == T[-1]
print(K * T.count("?") + (K - 1) * is_head_tail_same)

Submission status: Wrong Answer

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

n = len(s)

cur = s[0]
X = []
cnt = 0
for i in range(n):
    if s[i] != cur:
        X.append(cnt)
        cur = s[i]
        cnt = 1
    else:
        cnt += 1
else:
    X.append(cnt)

#print(X)
ans = 0
if s[0] != s[-1]:
    for x in X:
        ans += k*(x//2)
else:
    if len(X) != 1:
        x = X.pop()
        X[0] += x
        for x in X:
            ans += k*(x//2)
    else:
        if n%2 == 0:
            ans += (n//2)*k
        else:
            ans = (k//2)*(n//2+n//2+1)+(k%2)*(n//2)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
s=input()
k=int(input())
distinct=0
for i in range(len(s)-1):
  if s[i]!=s[i+1]:
    distinct=1
    first=i
    break
if distinct==0:
  print((len(s)*k)//2)
  sys.exit()
for i in range(len(s)-1,0,-1):
  if s[i]!=s[i-1]:
    last=i
    break
ans=(first+1)//2
ans+=((len(s)-last)//2)
count=1
for i in range(first+1,last):
  if s[i]!=s[i+1]:
    ans+=k*(count//2)
    count=1
  else:
    count+=1
ans+=((len(s)+first-last)//2)*(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] != "" and s[i] == s[i+1]:
        s[i+1] = ""
        ans += k
if s[0] != "" and 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:
t=input()
s=list(t)
k=int(input())
if s==[s[0]]*len(s):
  print((len(s)*k)//2)
  exit()
ans=0
for i in range(len(s)-1):
  if s[i]==s[i+1]:
    s[i+1]=0
    ans+=k
if t[0]==t[-1]:ans+=k-1
print(ans)

Submission status: Wrong Answer

Solution code:
s=list(input())
k=int(input())
if s==[s[0]]*len(s):
  print((len(s)*k)//2)
  exit()
ans=0
for i in range(len(s)-1):
  if s[i]==s[i+1]:
    s[i+1]=0
    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]=0
    ans+=k
if s[0]==s[-1]:ans+=k-1
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

lst = []
b = ""
tmp = 0
rlt = 0
for s in S:
  if s == b:
    tmp += 1
  else:
    lst.append(tmp)
    tmp = 1
  b = s
    
lst.append(tmp)

if S[0] == S[-1] and (len(lst) > 2 or lst[1]%2 == 1):
  rlt = ((lst[1]+lst[-1])//2)*(K-1) + lst[1]//2 + lst[-1]//2
  for t in lst[2:-1]:
    rlt += (t//2)*K
else:
  for t in lst[1:]:
    rlt += (t//2)*K
    
print(rlt)

Submission status: Wrong Answer

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

lst = []
b = ""
tmp = 0
rlt = 0
for s in S:
  if s == b:
    tmp += 1
  else:
    lst.append(tmp)
    tmp = 1
  b = s
    
lst.append(tmp)

if S[0] == S[-1] and len(lst) > 2:
  rlt = ((lst[1]+lst[-1])//2)*(K-1) + lst[1]//2 + lst[-1]//2
  for t in lst[2:-1]:
    rlt += (t//2)*K
else:
  for t in lst[1:]:
    rlt += (t//2)*K
    
print(rlt)

Submission status: Wrong Answer

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

    S = list(S)

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

        c = 0
        for i in range(len(S)-1):
            if i == len(S) - 2:
                if S[i] == S[i + 1]:
                    c += 1
                else:
                    if S[-1] == S[0]:
                        c += 1
            else:
                if S[i] == S[i + 1]:
                    c += 1
                    S[i + 1] = "-"

        print(c*K)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

    c = 0
    for i in range(len(S)-1):
        if i == len(S) - 2:
            if S[i] == S[i + 1]:
                c += 1
            else:
                if S[-1] == S[0]:
                    c += 1
        else:
            if S[i] == S[i + 1]:
                c += 1
                S[i + 1] = "-"

    print(c*K)
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
s = str(input())
k = int(input())
 
n = len(s)
l = [0] * 2*n
slarge = s+s
 
for i in range(2*n-1):
    if slarge[i] == slarge[i+1] and l[i] == 0:
        l[i+1] = 1

if s.count(s[0]) == n:
    print(n*k//2)    
elif k % 2 == 0:
    print(sum(l) * k//2)
else:
    print(sum(l) * int(k//2) + sum(l[0:n]))

Submission status: Wrong Answer

Solution code:
s = str(input())
k = int(input())
 
n = len(s)
l = [0] * 2*n
slarge = s+s
 
for i in range(1, 2*n):
    if slarge[i-1] == slarge[i] and l[i-1] == 0:
        l[i] = 1

if s.count(s[0]) == n:
    print(n*k//2)    
elif k % 2 == 0:
    print(sum(l) * k//2)
else:
    print(sum(l) * int(k//2) + sum(l[0:n]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
if len(s) == 1:
    print(k // 2)
    exit()
press = []
temp = s[0]
cnt = 0
for i in range(len(s)):
    if temp == s[i]:cnt += 1
    else:
        press.append((temp, cnt))
        cnt = 1
        temp = s[i]
press.append((temp, cnt))

count = 0
for str, num in press:
    if num > 1:
        count += num // 2

count *= k
if s[0] == s[-1]:
    count -= (press[0][1] // 2 + press[-1][1] // 2 - (press[0][1] + press[-1][1]) // 2) * (k - 1)
print(count)

Submission status: Wrong Answer

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

press = []
temp = s[0]
cnt = 0
for i in range(len(s)):
    if temp == s[i]:cnt += 1
    else:
        press.append((temp, cnt))
        cnt = 1
        temp = s[i]
press.append((temp, cnt))

count = 0
for str, num in press:
    if num > 1:
        count += num // 2

count *= k
if s[0] == s[-1]:
    count -= (press[0][1] // 2 + press[-1][1] // 2 - (press[0][1] + press[-1][1]) // 2) * (k - 1)
print(count)

Submission status: Wrong Answer

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

press = []
temp = s[0]
cnt = 0
for i in range(len(s)):
    if temp == s[i]:cnt += 1
    else:
        press.append((temp, cnt))
        cnt = 1
        temp = s[i]
press.append((temp, cnt))

count = 0
for str, num in press:
    if num > 1:
        count += num // 2

if s[0] == s[-1]:
    count -= press[0][1] // 2 + press[-1][1] // 2 - (press[0][1] + press[-1][1]) // 2
print(count * k)

Submission status: Wrong Answer

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

press = []
temp = s[0]
cnt = 0
for i in range(len(s)):
    if temp == s[i]:cnt += 1
    else:
        press.append((temp, cnt))
        cnt = 1
        temp = s[i]
press.append((temp, cnt))

count = 0
for i, l in enumerate(press):
    str, num = l
    if num > 1:
        if i != 0 and i != len(s) - 1:
            count += num // 2
        else:
            count += (num - 1) // 2

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

if s[0] != s[-1]:
    if press[0][1] % 2 == 0:count += 1
    if press[-1][1] % 2 == 0:count += 1
    print(count * k)
else:
    if len(press) == 1:
        count = press[0][1] // 2
    print(count * k)

Submission status: Wrong Answer

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

press = []
temp = s[0]
cnt = 0
for i in range(len(s)):
    if temp == s[i]:cnt += 1
    else:
        press.append((temp, cnt))
        cnt = 1
        temp = s[i]
press.append((temp, cnt))

count = 0
for i, l in enumerate(press):
    str, num = l
    if num > 1:
        if i != 0 and i != len(s) - 1:
            count += num // 2
        else:
            count += (num - 1) // 2
            
if len(s) == 1:
    print(k // 2)
    exit()

if s[0] != s[-1]:
    if press[0][1] % 2 == 0:count += 1
    if press[-1][1] % 2 == 0:count += 1
    print(count * k)
else:
    print(count * k)

Submission status: Wrong Answer

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

press = []
temp = s[0]
cnt = 0
for i in range(len(s)):
    if temp == s[i]:cnt += 1
    else:
        press.append((temp, cnt))
        cnt = 1
        temp = s[i]
press.append((temp, cnt))

count = 0
for i, l in enumerate(press):
    str, num = l
    if num > 1:
        if i != 0 and i != len(s) - 1:
            count += num // 2
        else:
            count += (num - 1) // 2
if len(s) == 1:
    print(k // 2)
    exit()

if s[0] != s[-1]:
    if press[0][1] % 2 == 0:count += 1
    if press[-1][1] % 2 == 0:count += 1
    print(count * k)
else:
    if press[0][1] % 2 == 0 and press[-1][1] % 2 == 0:
        count += 1
    print(count * k)

Submission status: Wrong Answer

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

press = []
temp = s[0]
cnt = 0
for i in range(len(s)):
    if temp == s[i]:cnt += 1
    else:
        press.append((temp, cnt))
        cnt = 1
        temp = s[i]
press.append((temp, cnt))

count = 0
for i, l in enumerate(press):
    str, num = l
    if num > 1:
        if i != 0 and i != len(s) - 1:
            count += num // 2
        else:
            count += (num - 1) // 2

if s[0] != s[-1]:
    print(count * k)
else:
    if press[0][1] % 2 == 0 and press[-1][1] % 2 == 0:
        count += 1
    print(count * k)

Submission status: Wrong Answer

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

press = []
temp = s[0]
cnt = 0
for i in range(len(s)):
    if temp == s[i]:cnt += 1
    else:
        press.append((temp, cnt))
        cnt = 1
        temp = s[i]
press.append((temp, cnt))

count = 0
for i, l in enumerate(press):
    str, num = l
    if num > 1:
        if i != 0 and i != len(s) - 1 or len(press) == 1:
            count += num // 2
        else:
            count += (num - 1) // 2

if s[0] != s[-1] or len(press) == 1:
    print(count * k)
else:
    if not(press[0][1] % 2 == 0 or press[-1][1] % 2 == 0):
        count += 1
    print(count * k)

Submission status: Wrong Answer

Solution code:
S = list(input())
K = int(input())
N = len(S)
a = 0
for i in range(N-1):
  if S[i] == S[i+1]:
    S[i+1] = '0'
    a += 1
b = 0
if N > 1:
  if S[0] == S[N-1]:
    if S[1] == '0':
      if S[N-2] == '0':
        b = 1
      else:
        b = 0
    else:
      b = 1
ans = K * a + (K - 1) * b
print(ans)

Submission status: Wrong Answer

Solution code:
S = list(input())
K = int(input())
N = len(S)
a = 0
for i in range(N-1):
  if S[i] == S[i+1]:
    S[i+1] = '0'
    a += 1
b = 0
if N > 1:
  if S[0] == S[N-1]:
    if S[1] == '0':
      b = 0
    else:
      b = 1
ans = K * a + (K - 1) * b
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

check=0
for i in range(1,len(S)):
    if S[i]==S[i-1]:
        check=1

if check==0:
    print(len(S)*K//2)
    exit()
import copy
s=copy.deepcopy(S)

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

ss=S*2
cntss=0
for i in range(1,len(ss)):
    if ss[i]==ss[i-1]:
        ss[i]=str(i)
        cntss+=1
print(cnt*(K)+(cntss-cnt*2)*(K-1))
#print(cnt,cntss)

Submission status: Wrong Answer

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

check=0
for i in range(1,len(S)):
    if S[i]==S[i-1]:
        check=1

if check==0:
    print(len(S)*K//2)
    exit()
s=S*1

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

ss=S*2
cntss=0
for i in range(1,len(ss)):
    if ss[i]==ss[i-1]:
        ss[i]=str(i)
        cntss+=1
print(cnt*(K)+(cntss-cnt*2)*(K-1))
#print(cnt,cntss)

Submission status: Wrong Answer

Solution code:
S = list(input())
K = int(input())
N = len(S)
a = 0
for i in range(N-1):
  if S[i] == S[i+1]:
    S[i+1] = '0'
    a += 1
b = 0

if N > 1:
  if S[0] == S[-1]:
    b = 1
ans = K * a + (K - 1) * b
if N == 0:
  if K != 1:
    ans = K-1
  else:
    ans = 0
if K == 1:
  ans = a
print(ans)

Submission status: Wrong Answer

Solution code:
S = list(input())
K = int(input())
N = len(S)
a = 0
for i in range(N-1):
  if S[i] == S[i+1]:
    S[i+1] = '0'
    a += 1
b = 0

if N > 1:
  if S[0] == S[-1]:
    b = 1
ans = K * a + (K - 1) * b
if N == 0:
  if K != 1:
    ans = K-1
  else:
    ans = 0
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
S=list(input())
K=int(input())
cnt=0
D={}
for i in range(len(S)):
    if S[i] not in D:
        D[S[i]]=1
if len(D)==1:
    print((len(S)*K)//2)
    exit()
for i in range(1,len(S)):
    if S[i]==S[i-1]:
        S[i]=str(i)
        cnt+=1
#print(S)
if S[0]==S[-1]:
    print(cnt*K+(K-1))
else:
    print(cnt*K)

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]:
        S[i]=str(i)
        cnt+=1
#print(S)
if S[0]==S[-1]:
    print(cnt*K+(K-1))
else:
    print(cnt*K)

Submission status: Wrong Answer

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

n = len(s)
l = [0] * 2*n
slarge = s+s

if n == 1 and k == 1:
    print(0)
    exit()

for i in range(1, 2*n):
    if slarge[i-1] == slarge[i] and l[i-1] == 0:
        l[i] = 1

if k % 2 == 0:
    print(sum(l) * k//2)
else:
    print(sum(l) * int(k//2) + sum(l[0:n]))

Submission status: Wrong Answer

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

n = len(s)
l = [0] * 2*n
slarge = s+s

for i in range(1, 2*n):
    if slarge[i-1] == slarge[i] and l[i-1] == 0:
        l[i] = 1

if k % 2 == 0:
    print(sum(l) * k//2)
else:
    print(sum(l) * int(k//2) + sum(l[0:n]))

Submission status: Wrong Answer

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

n = len(s)
l = [0] * n

ans = 0
sub = 0
for i in range(n):
    if i == 0 and s[-1] == s[0]:
        l[0] == 1
        sub = 2
    if s[i-1] == s[i] and l[i-1] == 0:
        ans += 1
        l[i] = 1

print(ans*k - sub * k//2)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from itertools import groupby
S=input()
K=int(input())
s=groupby(S)
count=0
keys=[]
values=[]
for key,value in s:
    keys.append(key)
    values.append(list(value))
for v in values:
    count+=len(list(v))//2
count*=K
if keys[0]==keys[-1]:
    count+=(-(len(values[0])//2)-(len(values[-1])//2)+(len(values[0])+len(values[-1]))//2)*(K-1)
print(count)

Submission status: Wrong Answer

Solution code:
from itertools import groupby
S=input()
K=int(input())
s=groupby(S)
count=0
keys=[]
values=[]
for key,value in s:
    keys.append(key)
    values.append(list(value))
for v in values:
    count+=len(list(v))//2
count*=K
if keys[0]==keys[-1] and len(values[0])%2==1 and len(values[-1])%2==1:
    count+=K-1
print(count)

Submission status: Wrong Answer

Solution code:
S = list(input())
K = int(input())
ans = 0
SS = S.copy()

if len(S) == 1:
    ans = K//2
elif len(set(S)) == 1:
    ans = int(len(S)*K/2)
else:
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            ans += 1
            S[i+1] = "#"

    ans *= K
    cnt1 = 0
    cnt2 = 0

    if SS[0] == SS[-1]:
        for i in range(1, len(S)):
            if SS[0] == SS[i]:
                cnt1 += 1
            else:
                break
        
        for j in range(1, len(S)):
            if SS[-1] == SS[-i-1]:
                cnt2 += 1
            else:
                break
        ans -= (int(cnt1/2)+int(cnt2/2)-int((cnt1+cnt2)/2))*(K-1)
    # print(S)
print(ans)

Submission status: Wrong Answer

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

if len(S) == 1:
    ans = K//2
elif len(set(S)) == 1:
    ans = int(len(S)*K/2)
else:
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            ans += 1
            S[i+1] = "#"

    ans *= K
    cnt1 = 0
    cnt2 = 0

    if S[0] == S[-1]:
        for i in range(1, len(S)):
            if S[0] == S[i]:
                cnt1 += 1
            else:
                break
        
        for j in range(1, len(S)):
            if S[-1] == S[-i-1]:
                cnt2 += 1
            else:
                break
        ans -= (int(cnt1/2)+int(cnt2/2)-int((cnt1+cnt2)/2))*(K-1)
    # print(S)
print(ans)

Submission status: Wrong Answer

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

if len(S) == 1:
    ans = K//2
elif len(set(S)) == 1:
    ans = int(len(S)*K/2)
else:
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            ans += 1
            S[i+1] = "#"

    ans *= K

    if S[0] == S[-1]:
        ans += K-1
    # print(S)
print(ans)

Submission status: Wrong Answer

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

if len(S) == 1:
    ans = K//2
else:
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            ans += 1
            S[i+1] = "#"

    ans *= K

    if S[0] == S[-1]:
        ans += K-1
    # print(S)
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]:
        ans += 1
        S[i+1] = "#"

ans *= K

if S[0] == S[-1]:
    ans += K-1
# print(S)
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import groupby
S=input()
K=int(input())
s=groupby(S)
count=0
if len(S)==1:
    print(len(S)//2)
    exit()
flag=False
if S[0]==S[-1]:
    flag=True
n=[]
for key,group in s:
    count+=len(list(group))//2
    if flag:
        n.append(len(list(group)))
count*=K
if flag:
    count+=((n[0]+n[-1])//2-(n[0]//2+n[-1]//2))*(K-1)
print(count)

Submission status: Wrong Answer

Solution code:
from itertools import groupby
S=input()
K=int(input())
s=groupby(S)
count=0
if len(S)==1:
    print(len(S)//2)
    exit()
flag=False
if S[0]==S[-1]:
    flag=True
n=[]
for key,group in s:
    count+=len(list(group))//2
    if flag:
        n.append(len(list(group)))
count*=K
if flag:
    if n[0]%2==1 and n[-1]%2==1:
        count+=K-1
print(count)

Submission status: Wrong Answer

Solution code:
from itertools import groupby
S=input()
K=int(input())
s=groupby(S)
count=0
flag=False
if S[0]==S[-1]:
    flag=True
n=[]
for key,group in s:
    count+=len(list(group))//2
    if flag:
        n.append(len(list(group)))
count*=K
if flag:
    if n[0]%2==1 and n[-1]%2==1:
        count+=K-1
print(count)

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *


S = input()
K = INT()

cnt = 0
tmp = False
for i in range(1, len(S)):
	if tmp:
		tmp = False
	elif S[i] == S[i-1]:
		cnt += 1
		tmp = True

if tmp or S[0] != S[-1]:
	print(cnt*K)
	exit()

cnt2 = 1
tmp = True
for i in range(1, len(S)):
	if tmp:
		tmp = False
	elif S[i] == S[i-1]:
		cnt2 += 1
		tmp = True

print((cnt+cnt2)*(K//2) + cnt*(K%2))

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *


S = input()
K = INT()

cnt = 0
tmp = False
for i in range(1, len(S)):
	if tmp:
		tmp = False
	elif S[i] == S[i-1]:
		cnt += 1
		tmp = True

if tmp or S[0] != S[-1]:
	print(cnt*K)
	exit()

cnt2 = 1
tmp = True
for i in range(1, len(S)):
	if tmp:
		tmp = False
	elif S[i] == S[i-1]:
		cnt2 += 1
		tmp = True

print(cnt + cnt2*(K-1))

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *


S = input()
K = INT()

cnt = 0
tmp = False
for i in range(1, len(S)):
	if tmp:
		tmp = False
	elif S[i] == S[i-1]:
		cnt += 1
		tmp = True

tmp2 = 0
if not tmp and S[0] == S[-1]:
	cnt += 1
	tmp2 = 1

print(cnt*K+(-K)//2*tmp2)

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *


S = input()
K = INT()

cnt = 0
tmp = False
for i in range(1, len(S)):
	if tmp:
		tmp = False
	elif S[i] == S[i-1]:
		cnt += 1
		tmp = True


if not tmp and S[0] == S[-1]:
	cnt += 1

print(cnt*K)

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *


S = input()
K = INT()

cnt = 0
tmp = False
for i in range(1, len(S)):
	if tmp:
		tmp = False
	elif S[i] == S[i-1]:
		cnt += 1
		tmp = True

tmp2 = 0
if not tmp and S[0] == S[-1]:
	cnt += 1
	tmp2 = 1

print(cnt*K-tmp2)

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *


S = input()
K = INT()

cnt = 0
tmp = False
for i in range(1, len(S)):
	if tmp:
		tmp = False
	elif S[i] == S[i-1]:
		cnt += 1
		tmp = True

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

print(cnt*K- (S[0]==S[-1]))

Submission status: Wrong Answer

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

def cnt(st):
    x = st.copy()
    ret = 0
    for i in range(1,len(st)):
        if x[i]==x[i-1]:
            ret += 1
            x[i] = '0'
    return ret

def main():
    S = list(input())
    K = int(input())
    res1 = cnt(S)
    res2 = cnt(S+S)
    print((res2-res1)*(K-1)+res1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
oans = 0
head = 0
tail = 0
N = len(S)
count = 1

for i in range(N-1):
  if S[i+1] == S[i]:
    count += 1
  else:
    oans += count // 2
    if head == 0:
      head = count
    count = 1
  if i == N-2:
    oans += count // 2
    tail = count
    if head == 0:
      print(N*K//2)
      exit()
plus = 0

if S[0] != S[-1]:
  print(oans * K)
  exit()

d = head//2 + tail //2 -(head + tail)//2

print(oans*K -(K-1)*d)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
oans = 0
head = 0
tail = 0
N = len(S)
count = 1

for i in range(N-1):
  if S[i+1] == S[i]:
    count += 1
  else:
    oans += count // 2
    if head == 0:
      head = count
    count = 1
  if i == N-2:
    oans += count // 2
    tail = count
    if head == 0:
      print(N*K//2)
      exit()
plus = 0

if S[0] == S[-1] and head % 2 == 1 and tail % 2 == 1:
  plus = 1
print(oans * K + (K -1)*plus)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
oans = 0
head = 0
tail = 0
N = len(S)
count = 1

for i in range(N-1):
  if S[i+1] == S[i]:
    count += 1
  else:
    oans += count // 2
    if head == 0:
      head = count
    count = 1
  if i == N-2:
    oans += count // 2
    tail = count
    if head == 0:
      head = count
plus = 0
if S[0] == S[-1] and head % 2 == 1 and tail % 2 == 1:
  plus = 1
print(oans * K + (K -1)*plus)

Submission status: Wrong Answer

Solution code:
#! /usr/bin/env python3

import sys
import numpy as np
from itertools import groupby
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

S = list(readline().decode().rstrip())
K = int(readline())
flag = False
g = [list(g) for k, g in groupby(S)]

if S[0] == S[-1]:
    flag = True

cnt = 0
for i in range(len(S) - 1):
    if S[i] == S[i + 1]:
        S[i + 1] = "0"
        cnt += 1
if flag:
    acc = len(g[0]) // 2 + len(g[-1]) // 2 - len(g[0] + g[-1]) // 2
    print(cnt * K - acc * (K - 1))
else:
    print(cnt * K)