Problem:


Score : 300 points

Problem Statement

Given is a string S. Let T be the concatenation of K copies of S.
We can repeatedly perform the following operation: choose a character in T and replace it with a different character.
Find the minimum number of operations required to satisfy the following condition: any two adjacent characters in T are different.

Constraints

1 \leq |S| \leq 100

S consists of lowercase English letters.

1 \leq K \leq 10^9

K is an integer.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the minimum number of operations required.

Sample Input 1

issii
2

Sample Output 1

4

T is issiiissii.  For example, we can rewrite it into ispiqisyhi, and now any two adjacent characters are different.

Sample Input 2

qq
81

Sample Output 2

81

Sample Input 3

cooooooooonteeeeeeeeeest
999993333

Sample Output 3

8999939997



Sample input:
issii
2


Sample output:
4

Submission status: Wrong Answer

Solution code:
s=input()
S=s+s
K=int(input())
post=S[0]
rep=0
later=0
for i in range(1,len(S)):
  if S[i]==post:
    rep+=1
    if i>=len(s):
      later+=1
    post="1"
  else:
    post=S[i]
ans=rep*(K//2)
if K%2==1:
  ans+=later
print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
n = len(s)
res = 0
i = 1
if n == 1:
    print(k//2)
    exit()
if n == 2:
    if s[0] == s[1]:
        print(k)
    else:
        print(0)
    exit()
if s[0] == s[n-1]:
    res += 1
    s[n-1] = 'C'
while i < n:
    if s[i-1] == s[i]:
        res += 1
        i += 1
    i += 1
print(res*k)

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
post=S[0]
rep=0
for i in range(1,len(S)):
  if S[i]==post:
    rep+=1
    post="1"
  else:
    post=S[i]
ans=rep*K

print(ans)

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
post=S[0]
rep=0
for i in range(1,len(S)):
  if S[i]==post:
    rep+=1
    post="1"
  else:
    post=S[i]
ans=rep*K
if post==S[0]:
  ans+=K-1
print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
n = len(s)
res = 0
i = 0
if s[0] == s[n-1]:
    res += 1
    s[n-1] = 'C'

while i < n-1:
    if s[i] == s[i+1]:
        res += 1
        i +=1
    i+=1
print(res*k)

Submission status: Wrong Answer

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

s_2=s*2
pre=""
ans=0

if s.count(s[0])==len(s):
  print((len(s)*k)//2)
  sys.exit()

for i in range(len(s)):
  if pre==s[i]:
    ans+=1
    pre=""
  else:
    pre=s[i]

pre=""
ans1=0
for i in range(len(s_2)):
  if pre==s_2[i]:
    ans1+=1
    pre=""
  else:
    pre=s_2[i]

if 2*ans<ans1:
  print(ans+(k-1)*ans1)
else:
  print(ans*k)

Submission status: Wrong Answer

Solution code:
import sys

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

pre=""
ans=0
for i in range(len(s)):
  if pre==s[i]:
    ans+=1
    pre = " "
  else:
    pre = s[i]

start=s[0]
s_num=-1
for i in range(len(s)):
  if start==s[i]:
    s_num+=1
  else:
    break

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

end=s[-1]
e_num=-1
for i in range(len(s)-2,-1,-1):
  if end==s[i]:
    e_num+=1
  else:
    break


if (s_num%2==1 and e_num%2==1) and (e_num!=0 and s_num!=0):
  print((ans+1)*(k-1)+ans)
  print(1)
else:
  print(ans*k)

Submission status: Wrong Answer

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

pre=""
ans=0
for i in range(len(s)):
  if pre==s[i]:
    ans+=1
    pre = " "
  else:
    pre = s[i]

start=s[0]
s_num=-1
for i in range(len(s)):
  if start==s[i]:
    s_num+=1
  else:
    break

end=s[-1]
e_num=-1
for i in range(len(s)-2,-1,-1):
  if end==s[i]:
    e_num+=1
  else:
    break


if (s_num%2==1 and e_num%2==1) and (e_num!=0 and s_num!=0):
  print((ans+1)*(k-1)+ans)
  print(1)
else:
  print(ans*k)

Submission status: Wrong Answer

Solution code:
import bisect,copy,heapq,string
from collections import *
from math import *
from itertools import *
import sys
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

s = input()
k = INT()
ans = 0
cnt = 1
s = s + s

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

print(ans*k//2)

Submission status: Wrong Answer

Solution code:
import bisect,copy,heapq,string
from collections import *
from math import *
from itertools import *
import sys
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

s = input()
k = INT()
ans = 0
cnt = 1
s = s + s

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

if ans % 2 == 0:
    print(ans//2*k)
else:
    print(ans//2+(ans+1)//2*(k-1))

Submission status: Wrong Answer

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

#ランレングス圧縮
rle = []
pre = 'A'
chain = 1
for c in S:
    if c == pre:
        chain += 1
    else:
        if pre != 'A':
            rle.append([pre,chain])
        
        pre = c
        chain = 1
rle.append([pre,chain])

ans = 0

#1種類の文字列の場合
if len(rle) == 1:
    ans = (rle[0][1] * K) // 2

#2種類以上の文字列の場合
else:
    #先頭と末尾が同じ文字
    if rle[0][0] == rle[-1][0]:
        ans += rle[0][1]//2
        ans += rle[-1][1]//2
        rle[0][1] += rle[-1][1]
        rle[-1][1] = 0

        mid_chains = 0
        for i in range(len(rle)):
            mid_chains += rle[i][1]//2
        ans += mid_chains * (K-1)

    #先頭と末尾が異なる文字
    else:
        mid_chains = 0
        for i in range(len(rle)):
            mid_chains += rle[i][1]//2
        ans += mid_chains * K


#print(rle)
print(ans)

Submission status: Wrong Answer

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

rle = []
pre = 'A'
chain = 1

for c in S:
    if c == pre:
        chain += 1
    else:
        if pre != 'A':
            rle.append([pre,chain])
        
        pre = c
        chain = 1
rle.append([pre,chain])

if len(rle) != 1:
    if rle[0][0] == rle[-1][0]:
        rle[0][1] += rle[-1][1]
        rle[-1][1] = 0

ans = 0
for i in range(len(rle)):
    ans += rle[i][1]//2

print(ans*K)

Submission status: Wrong Answer

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

ss=S*2

ss=[a for a in ss]
answer=0
N=len(ss)
for n in range(N-1):
    if ss[n]==ss[n+1]:
        answer+=1
        ss[n+1]=0

sss=S*3

sss=[a for a in sss]
suuji=0
N=len(sss)
for n in range(N-1):
    if sss[n]==sss[n+1]:
        suuji+=1
        sss[n+1]=0

number=0
s=[a for a in S]
N=len(s)
for n in range(N-1):
    if s[n]==s[n+1]:
        number+=1
        s[n+1]=0
if K==1:
    print(number)
else:
    if answer - number == suuji - answer:
        print(number + (answer - number) * (K - 1))
    else:
        if K % 2 == 0:
            print(number + (answer - number) * (K - 1))
        else:
            print(number + (answer - number) * (K - 2) + suuji - answer)

Submission status: Wrong Answer

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

ss=S*2

ss=[a for a in ss]
answer=0
N=len(ss)
for n in range(N-1):
    if ss[n]==ss[n+1]:
        answer+=1
        ss[n+1]=0
print(math.ceil(answer*K/2))

Submission status: Wrong Answer

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

ss=S*2

ss=[a for a in ss]
answer=0
N=len(ss)
for n in range(N-1):
    if ss[n]==ss[n+1]:
        answer+=1
        ss[n+1]=0
if K==1:
    number=0
    s=[a for a in S]
    N=len(s)
    for n in range(N-1):
        if s[n]==s[n+1]:
            s[n+1]=0
            number+=1
    print(number)
elif K==2:
    print(int(answer * K / 2))

else:
    if ss[0] == ss[-1]:
        print(int(answer * K / 2) + 1)
    else:
        print(int(answer * K / 2))

Submission status: Wrong Answer

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

ss=S*2

ss=[a for a in ss]
answer=0
N=len(ss)
for n in range(N-1):
    if ss[n]==ss[n+1]:
        answer+=1
        ss[n+1]=0
if ss[0]==ss[-1]:
    print(int(answer * K / 2)+1)
else:
    print(int(answer * K / 2))

Submission status: Wrong Answer

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

ss=S*2

ss=[a for a in ss]
answer=0
N=len(ss)
for n in range(N-1):
    if ss[n]==ss[n+1]:
        answer+=1
        ss[n+1]=0
print(int(answer*K/2))

Submission status: Wrong Answer

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

ss=S*2

ss=[a for a in ss]
answer=0
N=len(ss)
for n in range(N-1):
    if ss[n]==ss[n+1]:
        answer+=1
        ss[n+1]='.'
print(int(answer*K/2))

Submission status: Wrong Answer

Solution code:
S = list(input())
NS = [s for s in S]
K = int(input())
L = len(S)

if "".join(S) == S[0] * L:
  print(L * K // 2)
  exit()

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

# 先頭と末尾が異なる
if S[0] != S[-1]:
  ans *= K
# 一緒
else:
  a = 0
  for i in range(L):
    if NS[i] == NS[0]:
      a += 1
    else:
      break
  
  b = 0
  for i in range(L)[::-1]:
    if NS[i] == NS[-1]:
      b += 1
    else:
      break
      
  print(a,b)
      
  ans *= K
  #t = (- (-a // 2)) + (- (-b // 2)) - (- (-a-b) // 2)
  #t = -(-a // 2) - (-b // 2) + (-(a+b) // 2)
  t = a // 2 + b // 2 - (a+b) // 2
  ans -= t * (K-1)
  
print(ans)

Submission status: Wrong Answer

Solution code:
S = list(input())
NS = [s for s in S]
K = int(input())
L = len(S)

if "".join(S) == S[0] * L:
  print(L * K // 2)
  exit()

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

# 先頭と末尾が異なる
if S[0] != S[-1]:
  ans *= K
# 一緒
else:
  a = 0
  for i in range(L):
    if NS[i] == NS[0]:
      a += 1
    else:
      break
  
  b = 0
  for i in range(L)[::-1]:
    if NS[i] == NS[-1]:
      b += 1
    else:
      break
      
  ans *= K
  t = (- (-a // 2)) + (- (-b // 2)) - (- (-a-b) // 2)
  ans -= t * (K-1)
  
print(ans)

Submission status: Wrong Answer

Solution code:
S = list(input())
NS = [s for s in S]
K = int(input())
L = len(S)

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

# 先頭と末尾が異なる
if S[0] != S[-1]:
  ans *= K
# 一緒
else:
  a = 0
  for i in range(L):
    if NS[i] == NS[0]:
      a += 1
    else:
      break
  
  b = 0
  for i in range(L)[::-1]:
    if NS[i] == NS[-1]:
      b += 1
    else:
      break
      
  ans *= K
  t = (- (-a // 2)) + (- (-b // 2)) - (- (-a-b) // 2)
  ans -= t * (K-1)
  
print(ans)

Submission status: Wrong Answer

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

# 全て同じ
if S == S[0] * L:
  print(-(-S * K // 2))
  exit()

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

# 先頭と末尾が異なる
if S[0] != S[-1]:
  ans *= K
# 一緒
else:
  a = 0
  for i in range(L):
    if S[i] == S[0]:
      a += 1
    else:
      break
  
  b = 0
  for i in range(L)[::-1]:
    if S[i] == S[-1]:
      b += 1
    else:
      break
      
  ans *= K
  t = (- (-a // 2)) + (- (-b // 2)) - (- (-a-b) // 2)
  ans -= t * (K-1)
  
print(ans)

Submission status: Wrong Answer

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

# 全て同じ
if S == S[0] * L:
  print(-(-S * K // 2))
  exit()

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

# 先頭と末尾が異なる
if S[0] != S[-1]:
  ans *= K
# 一緒
else:
  a = 0
  for i in range(L):
    if S[i] == S[0]:
      a += 1
    else:
      break
  
  b = 0
  for i in range(L)[::-1]:
    if S[i] == S[-1]:
      b += 1
    else:
      break
      
  ans *= K
  t = (- (-a // 2)) + (- (-b // 2)) - (- (-a-b) // 2)
  ans -= t + (K-1)
  
print(ans)

Submission status: Wrong Answer

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

# 全て同じ
if S == S[0] * L:
  print(-(-S * K // 2))
  exit()

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

# 先頭と末尾が異なる
if S[0] != S[-1]:
  ans *= K
# 一緒
else:
  a = 0
  for i in range(L):
    if S[i] == S[0]:
      a += 1
    else:
      break
  
  b = 0
  for i in range(L)[::-1]:
    if S[i] == S[-1]:
      b += 1
    else:
      break
      
  ans = ans * K - ( (- (-a // 2)) + (- (-b // 2)) - (- (-a - b) // 2)) * (K-1)
  
print(ans)

Submission status: Wrong Answer

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

# 全て同じ
if S == S[0] * L:
  print(-(-S * K // 2))
  exit()

# 先頭と末尾が異なる
ans = 0
for i in range(L-1):
  f_s = S[i]
  s_s = S[i+1]
  if f_s == s_s:
    S[i+1] = '-'
    ans += 1

if S[0] != S[-1]:
  ans *= K
else:
  a = 0
  for i in range(L):
    if i == 0:
      continue
    if S[i] == S[0]:
      a += 1
    else:
      break
  
  b = 0
  for i in range(L)[::-1]:
    if i == L-1:
      continue
    if S[i] == S[-1]:
      b += 1
    else:
      break
      
  ans = ans * K - ( (- (-a // 2)) + (- (-b // 2)) - (- (-a - b) // 2)) * (K-1)
  
print(ans)

Submission status: Wrong Answer

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

pat1 = 0
for i in range(L-1):
  f_s = S[i]
  s_s = S[i+1]
  if f_s == s_s:
    S[i+1] = '-'
    pat1 += 1
    
if S[-1] != '-' and S[0] == S[-1]:
  pat1 = (pat1 + 1) * K - 1
else:
  pat1 *= K
  
pat2 = 0
for i in range(L-1):
  f_s = S[i]
  s_s = S[i+1]
  if f_s == s_s:
    S[i] = '-'
    pat2 += 1

if S[-1] != '-' and S[0] == S[-1]:
  pat2 = (pat2 + 1) * K - 1
else:
  pat2 *= K
  
print(min(pat1,pat2))

Submission status: Wrong Answer

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

ans = 0
for i in range(L-1):
  f_s = S[i]
  s_s = S[i+1]
  if f_s == s_s:
    S[i+1] = '-'
    ans += 1
    
if S[-1] != '-' and S[0] == S[-1]:
  ans = (ans + 1) * K - 1
else:
  ans *= K
  
print(ans)

Submission status: Wrong Answer

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

ans = 0
for i in range(L-1):
  f_s = S[i]
  s_s = S[i+1]
  if f_s == s_s:
    s_s = '-'
    ans += 1
    
if S[-1] != '-' and S[0] == S[-1]:
  ans = (ans + 1) * K - 1
else:
  ans *= K
  
print(ans)

Submission status: Wrong Answer

Solution code:
a = input()
n = int(input())
count = 1
t = 0
ar = []
if len(a) == 1:
    print(n - 1)
    exit()
for i in range(1,len(a)):
    if a[i] == a[i-1]:
        count += 1
    else:
        ar.append(count)
        if count >= 2:
            t += count // 2
        count = 1
ar.append(count)
if count >= 2:
    t += count // 2
t *= n
if a[0] == a[-1]:
    if ar[0] % 2 == 1 and ar[-1] % 2 == 1:
        t += n - 1
print(t)

Submission status: Wrong Answer

Solution code:
a = input()
n = int(input())
count = 1
t = 0
ar = []
if len(a) == 1:
    print(n - 1)
    exit()
for i in range(1,len(a)):
    if a[i] == a[i-1]:
        count += 1
    else:
        ar.append(count)
        if count >= 2:
            t += count // 2
        count = 1
ar.append(count)
if count >= 2:
    t += count // 2
t *= n
if a[0] == a[-1]:
    if (ar[0] + ar[-1]) % 2 == 0:
        t += n - 1
print(t)

Submission status: Wrong Answer

Solution code:
a = input()
n = int(input())
count = 1
t = 0
ar = []
if len(a) == 1:
    print(n - 1)
    exit()
for i in range(1,len(a)):
    if a[i] == a[i-1]:
        count += 1
    else:
        ar.append(count)
        if count >= 2:
            t += count // 2
        count = 1
ar.append(count)
if count >= 2:
    t += count // 2
if a[0] == a[-1]:
    if ar[0] % 2 == 1 and ar[-1] % 2 == 1:
        t += n - 1
print(t * n)

Submission status: Wrong Answer

Solution code:
a = input()
n = int(input())
count = 1
t = 0
if len(a) == 1:
    print(n - 1)
    exit()
for i in range(1,len(a)):
    if a[i] == a[i-1]:
        count += 1
    else:
        if count >= 2:
            t += count // 2
        count = 1
if count >= 2:
    t += count // 2
if a[0] == a[-1]:
    if a[0] != a[1] and a[-1] != a[-2]:
        t += n - 1
print(t * n)

Submission status: Wrong Answer

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

tmp = list(s * 2)

fst = 0
snd = 0
for i in range(len(tmp)-1):
    if i < len(s) and tmp[i] == tmp[i+1]:
        tmp[i + 1] = "#"
    elif i >= len(s) and tmp[i] == tmp[i+1]:
        tmp[i + 1] = "#"

fst = tmp[:len(s)].count("#")
snd = tmp[len(s):].count("#")

if k == 1:
    print(fst)
else:
    print(fst + snd * (k-1))

Submission status: Wrong Answer

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

tmp = list(s * 2)

fst = 0
snd = 0
for i in range(len(tmp)-1):
    if i < len(s) and tmp[i] == tmp[i+1]:
        tmp[i+1] = "#"
        fst += 1
    elif i >= len(s) and tmp[i] == tmp[i+1]:
        tmp[i + 1] = "#"
        snd += 1

if k == 1:
    print(fst)
else:
    print(fst + snd * (k-1))

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/agc039/tasks/agc039_a

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

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/agc039/tasks/agc039_a

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 += 1
ans *= k
print(ans)

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
flag=False
if len(S)==1:
    print(K-1)
    exit()
if len(set(list(S)))==1:
    print((len(S)*K)//2)
    exit()
if S[0]==S[-1]:
    flag=True
cnt=1
A=[]
for i in range(1,len(S)):
    if S[i-1]==S[i]:
        cnt+=1
    else:
        A.append(cnt)
        cnt=1
else:
    A.append(cnt)
ans=0
for a in A:
    ans+=a//2
if flag and (A[0]+A[-1])%2==0:
    ans*=K
    ans+=K-1
    print(ans)
else:
    ans*=K
    print(ans)

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
flag=False
if len(S)==1:
    print(K-1)
    exit()
if len(set(list(S)))==1:
    print((len(S)*K)//2)
    exit()
if S[0]==S[-1]:
    flag=True
cnt=1
A=[]
for i in range(1,len(S)):
    if S[i-1]==S[i]:
        cnt+=1
    else:
        A.append(cnt)
        cnt=1
else:
    A.append(cnt)
ans=0
for a in A:
    ans+=a//2
if flag:
    f,m,l=A[0]//2,(A[0]+A[-1])//2,A[-1]//2
    ans=(ans*K)-(f+l-m)*(K-1)
    print(ans)
else:
    ans*=K
    print(ans)

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
flag=False
if len(S)==1:
    print(K-1)
    exit()
if len(set(list(S)))==1:
    print((len(S)*K)//2)
    exit()
if S[0]==S[-1]:
    flag=True
cnt=1
A=[]
for i in range(1,len(S)):
    if S[i-1]==S[i]:
        cnt+=1
    else:
        A.append(cnt)
        cnt=1
else:
    A.append(cnt)
ans=0
for a in A:
    ans+=a//2
if flag:
    ans=(ans*K)+((A[0]+A[-1])//2-(A[0]//2+A[-1]//2))*(K-1)
    print(ans)
else:
    ans*=K
    print(ans)

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
flag=False
if len(S)==1:
    print(K-1)
    exit()
if len(set(list(S)))==1:
    print((len(S)*K)//2)
    exit()
if S[0]==S[-1]:
    flag=True
cnt=1
A=[]
for i in range(1,len(S)):
    if S[i-1]==S[i]:
        cnt+=1
    else:
        A.append(cnt)
        cnt=1
if cnt>=2:
    A.append(cnt)
ans=0
for a in A:
    ans+=a//2
if flag:
    ans=(ans*K)+((A[0]+A[-1])//2-(A[0]//2+A[-1]//2))*(K-1)
    print(ans)
else:
    ans*=K
    print(ans)

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
flag=False
if len(S)==1:
    print(K-1)
    exit()
if S[0]==S[-1]:
    flag=True
cnt=1
A=[]
for i in range(1,len(S)):
    if S[i-1]==S[i]:
        cnt+=1
    else:
        A.append(cnt)
        cnt=1
if cnt>=2:
    A.append(cnt)
ans=0
for a in A:
    ans+=a//2
if flag:
    ans=(ans*K)+((A[0]+A[-1])//2-(A[0]//2+A[-1]//2))*(K-1)
    print(ans)
else:
    ans*=K
    print(ans)

Submission status: Wrong Answer

Solution code:
import math

s = list(input())
k = int(input())
cnt = 0
temp = []
for i in range(1, len(s)):
    if s[i] == s[i-1]:
       cnt+=1 
    elif cnt > 0:
        temp.append(cnt)
        cnt = 0
buff = 0
if cnt > 0:
    temp.append(cnt)
elif s[-1] == s[0]:
    temp.append(1)
    buff = 1
total = 0
for i in range(len(temp)):
    total += k*math.ceil(temp[i]/2.0)
print(total - buff)

Submission status: Wrong Answer

Solution code:
s=input()
n=int(input())
sn=[s[i] for i in range(len(s))]
ani=0
ans=0
for j in range(len(sn)-1):
  if sn[j]==sn[j+1]:
    sn[j+1]="0"
    ans+=1

sn=sn*2
for k in range(len(sn)-1):
  if sn[k]==sn[k+1]:
    sn[k+1]="0"
    ani+=1
print(ans*n+ani*(n-1))

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

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
ans = 0
if S == S[0]*len(S):
    print(len(S)//2 * K)
    exit()

num = [1]
for i in range(1,len(S)):
    if S[i-1] == S[i]:
        num[-1] += 1
    else:
        num.append(1)
for n in num:
    ans += n//2
if S[0] == S[-1] and (num[0]+num[-1])%2 == 0:
    print(ans * K + K-1)
else:
    print(ans * K)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
ans = 0
if S[0] == S[-1]:
    ans += K-1
if S == S[0]*len(S):
    print(len(S)//2)
    exit()

num = [1]
for i in range(1,len(S)):
    if S[i-1] == S[i]:
        num[-1] += 1
    else:
        num.append(1)
for n in num:
    ans += n//2
print(ans * K)

Submission status: Wrong Answer

Solution code:
# import sys
# input = sys.stdin.readline
# import re
import itertools

def main():
    s = input()
    k = int(input())
    c = 1
    if len(set(s)) == 1:
        print(len(s) * k//2)
        exit()
    pre_s = ''
    if (s[0] != s[-1] and k > 1) or k == 1:
        a = 0
        for i in range(len(s) - 1):
            if s[i] == s[i+1]:
                c += 1
            else:
                # print(i, c, a)
                a += c // 2
                c = 1
        if c > 1:
            a += c // 2
        print(a*k)
    else:
        a = 0
        for i in range(len(s) - 1):
            if s[i] == s[i+1]:
                c += 1
            else:
                # print(i, c, a)
                a += c // 2
                c = 1
        if c > 1:
            a += c // 2
        ss = s*2
        aa = 0
        for i in range(len(ss) - 1):
            if ss[i] == ss[i+1]:
                c += 1
            else:
                # print(i, c, a)
                aa += c // 2
                c = 1
        if c > 1:
            aa += c // 2
        d = aa - a
        print(a + (d * k-1))


def count_tento(l, cur):
    count = 0
    for v in l:
        if v > cur:
            count += 1
    return count

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

def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
import collections
a = []
ans = 0
num = 1
if S[0] == S[-1]:
    ans += 1
chr = S[0]
for i in range(1,len(S)):
    if S[i] == chr:
        num += 1
    else:
        ans += num//2
        chr = S[i]
        num = 1

print(ans * K)

Submission status: Wrong Answer

Solution code:
# import sys
# input = sys.stdin.readline
# import re
import itertools

def main():
    s = input()
    k = int(input())
    c = 1
    pre_s = ''

    a = 0
    for i in range(len(s) - 1):
        if s[i] == s[i+1]:
            c += 1
        else:
            # print(i, c, a)
            a += c // 2
            c = 1
    if c > 1:
        a += c // 2
    print(a*k)


def count_tento(l, cur):
    count = 0
    for v in l:
        if v > cur:
            count += 1
    return count

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

def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def run_length_compress(S):
    res = [[S[0], 1]]
    for c in S[1:]:
        if c == res[-1][0]:
            res[-1][1] += 1
        else:
            res.append([c, 1])
    return res


S = input()
N = len(S)
K = int(input())
T = run_length_compress(S)
ans = sum(l // 2 for _, l in T) * K
if T[0][0] == T[-1][0]:
    head, tail = T[0][1], T[-1][1]
    ans += ((head + tail) // 2 - head // 2 - tail // 2) * (K - 1)
print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
if len(s) == 1:
    print(k//2)
    exit()
cnt = 0
# ランレングス圧縮
def rle(lst):
    ans = []
    cnt = 1
    ini = lst[0]
    for i in range(1, len(lst)):
        if ini == lst[i]:
            cnt += 1
        else:
            ans.append((ini, cnt))
            cnt = 1
            ini = lst[i]
    ans.append((ini, cnt))
    return ans

sr = rle(s)
if len(sr) == 1:
    print(sr[0][1]*k//2)
    exit()
for i in range(len(sr)):
    if sr[i][1] >= 2:
        cnt += sr[i][1]//2 * k
if sr[0][0] == sr[-1][0]:
    x = sr[0][1]
    y = sr[-1][1]
    cnt += (x//2 + y//2 - (x+y)//2) *(k-1)
print(cnt)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
if len(s) == 1:
    print(k//2)
    exit()
cnt = 0
# ランレングス圧縮
def rle(lst):
    ans = []
    cnt = 1
    ini = lst[0]
    for i in range(1, len(lst)):
        if ini == lst[i]:
            cnt += 1
        else:
            ans.append((ini, cnt))
            cnt = 1
            ini = lst[i]
    ans.append((ini, cnt))
    return ans

sr = rle(s)
if len(sr) == 1:
    print(sr[0][1]*k//2)
    exit()
for i in range(len(sr)):
    if sr[i][1] >= 2:
        cnt += sr[i][1]//2 * k
if sr[0][0] == sr[-1][0]:
    x = sr[0][1]//2
    y = sr[-1][1]//2
    sr[-1] = (sr[-1][0], sr[-1][1]+sr[0][1])
    cnt += (x + y - sr[-1][1]//2) *(k-1)
print(cnt)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
if len(s) == 1:
    print(k//2)
    exit()
cnt = 0
# ランレングス圧縮
def rle(lst):
    ans = []
    cnt = 1
    ini = lst[0]
    for i in range(1, len(lst)):
        if ini == lst[i]:
            cnt += 1
        else:
            ans.append((ini, cnt))
            cnt = 1
            ini = lst[i]
    ans.append((ini, cnt))
    return ans

sr = rle(s)
if len(sr) == 1:
    print(sr[0][1]*k//2)
    exit()
for i in range(len(sr)):
    if sr[i][1] >= 2:
        cnt += sr[i][1]//2
if sr[0][0] == sr[-1][0]:
    sr[-1] = (sr[-1][0], sr[-1][1]+sr[0][1])
for i in range(1,len(sr)):
    if sr[i][1] >= 2:
        cnt += sr[i][1]//2 *(k-1)
print(cnt)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
if len(s) == 1:
    print(k//2)
    exit()
cnt = 0
# ランレングス圧縮
def rle(lst):
    ans = []
    cnt = 1
    ini = lst[0]
    for i in range(1, len(lst)):
        if ini == lst[i]:
            cnt += 1
        else:
            ans.append((ini, cnt))
            cnt = 1
            ini = lst[i]
    ans.append((ini, cnt))
    return ans

sr = rle(s)
if len(sr) == 1:
    print(sr[0][1]*k//2)
    exit()
for i in range(len(sr)):
    if sr[i][1] >= 2:
        cnt += sr[i][1]//2
if sr[0][0] == sr[-1][0]:
    sr[-1] = (sr[-1][0], sr[-1][1]+sr[0][1])
sr= sr[1:]
for i in range(len(sr)):
    if sr[i][1] >= 2:
        cnt += sr[i][1]//2 *(k-1)
print(cnt)

Submission status: Wrong Answer

Solution code:
C = {}
S = input().strip()
N = len(S)
K = int(input())
for i in range(N):
    s = S[i]
    if s not in C:
        C[s]=0
    C[s] += 1
if len(C)>1:
    a = 0
    cnt = 1
    for i in range(1,N):
        if S[i]==S[i-1]:
            cnt += 1
        else:
            a += cnt//2
            cnt = 1
    a += cnt//2
    X = S+S
    b = 0
    cnt = 1
    for i in range(1,2*N):
        if X[i]==X[i-1]:
            cnt += 1
        else:
            b += cnt//2
            cnt = 1
    b += cnt//2
    d = b-a
    print(a+d*(K-1))
else:
    A = list(C.items())
    k = A[0][1]
    if k%2==0:
        print((k//2)*K)
    else:
        if K%2==0:
            a = k
            b = 2*k
            d = k
            print(a+d*((K//2)-1))
        else:
            a = k//2
            b = (a*3)//2
            d = b-a
            print(a+d*(K//2))

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
if len(s) == 1:
    print(k//2)
    exit()
cnt = 0
# ランレングス圧縮
def rle(lst):
    ans = []
    cnt = 1
    ini = lst[0]
    for i in range(1, len(lst)):
        if ini == lst[i]:
            cnt += 1
        else:
            ans.append((ini, cnt))
            cnt = 1
            ini = lst[i]
    ans.append((ini, cnt))
    return ans

sr = rle(s)
if len(sr) == 1:
    print(sr[0][1]*k//2)
    exit()
for i in range(len(sr)):
    if sr[i][1] >= 2:
        cnt += sr[i][1]//2

if sr[0][0] == sr[-1][0]:
    sr[-1] = (sr[-1][0], sr[-1][1]+sr[0][1])
for i in range(len(sr)):
    if sr[i][1] >= 2:
        cnt += sr[i][1]//2 *(k-1)
print(cnt)

Submission status: Wrong Answer

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

cnt = 1
l,r = 1,1
T = S[::-1]
for i in range(l):
    if S[i] == S[i+1]:
        l += 1
    else:
        break

for i in range(l):
    if T[i] == T[i+1]:
        r += 1
    else:
        break

for i in range(l,len(S)-r):
    if S[i] == S[i+1]:
        cnt += 1

print(K*(r//2+l//2+cnt//2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
s=str(input())
k=int(input())
cnt=0
for i in range(len(s)-1):
    if s[i]==s[i+1]:
        s=s[:i+1]+'-'+s[i+2:]
        cnt+=1

print(cnt*k)

Submission status: Wrong Answer

Solution code:
S = input().strip()
N = len(S)
K = int(input())
a = 0
cnt = 1
for i in range(1,N):
    if S[i]==S[i-1]:
        cnt += 1
    else:
        a += cnt//2
        cnt = 1
a += cnt//2
X = S+S
b = 0
cnt = 1
for i in range(1,2*N):
    if X[i]==X[i-1]:
        cnt += 1
    else:
        b += cnt//2
        cnt = 1
b += cnt//2
d = b-a
print(a+d*(K-1))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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