Problem:


Score : 300 points

Problem Statement

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

Constraints

1 \leq |S| \leq 100

S consists of lowercase English letters.

1 \leq K \leq 10^9

K is an integer.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the minimum number of operations required.

Sample Input 1

issii
2

Sample Output 1

4

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

Sample Input 2

qq
81

Sample Output 2

81

Sample Input 3

cooooooooonteeeeeeeeeest
999993333

Sample Output 3

8999939997



Sample input:
issii
2


Sample output:
4

Submission status: Wrong Answer

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

lens = len(s)

cnt = 0
ans = 0

sig = lens % 2

i=0
while i < lens:
  if i != lens-1 and s[i] == s[i+1]:
    cnt += 1
    i+=2
  else:
    i+=1

ans = cnt * k

# lensが奇数で最初と最後が同じ場合
# if sig == 1 and s[0] == s[-1]:
#   ans += k-1


print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    S = input()
    K = int(input())
    count = 0
    same = True
    for i in range(len(S) - 1):
        if S[i] == S[i+1]:
            continue
        else:
            same = False
            break
    if same:
        count += int((len(S) * K) / 2)
    else:
        i = 0
        while i < len(S):
            c = 1
            flag = False
            if i < len(S) - 1:
                flag = True
                while S[i] == S[i+1]:
                    c += 1
                    i += 1
                    if i + 1 >= len(S):
                        flag = False
                        break
            i += 1
            if c % 2 == 0:
                count += K * int(c / 2)
            else:
                count += K * int((c - 1) / 2)
            if not flag:
                if S[0] == S[-1] and c % 2 == 1:
                    count += K - 1
            
    print(count)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

mycnt = 0
myflag = 0

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

else:
    mys = S[0]
    mcnt = 0
    myflag = 0
    for i in range(1,len(S)):
        if S[i] == mys and myflag == 0:
            mycnt += 1
            myflag = 1
        else:
            myflag = 0
        mys = S[i]
    
    if mycnt == 0 and S[0] == S[-1]:
        print(K-1)
    
    else:
        mycnt = mycnt * K
    
        print(mycnt)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
count = 0
import copy
s1 = list(set(s))
if len(s1) == 1:
    count = int((k * len(s)) / 2)
elif k >= 3:
    t1 = s * 5
    for i in range(len(t1) - 1):
        if t1[i] == t1[i + 1]:
            t1.pop(i + 1)
            t1.insert(i + 1, "")
            count += 1
    if count % 5 == 0:
        count = (count / 5) * k
    else:
        count = ((count + 2) / 5) * k - 2
else:
    t1 = s * k
    for i in range(len(t1) - 1):
        if t1[i] == t1[i + 1]:
            t1.pop(i + 1)
            t1.insert(i + 1, "")
            count += 1
print(int(count))

Submission status: Wrong Answer

Solution code:
a=input()
b=int(input())
s="A"
zn=0
n=0
nn=0
i=0
aa=[]
ab=[]

for x in a:
    if aa==[]:
        n+=1
    elif x==s:
        n+=1
    elif x!=s:
        ab.append(n)
        n=1
    s=x
    i+=1
    aa.append(x)
    if i==len(a):
        ab.append(n)
if aa[0]==aa[-1] and ab[-1]==1:
    s=a[0]
    i=0
    for x in aa:
        if a[i]==s:
            nn+=1
        else:
            break
        i+=1
    i=1
    for x in aa:
        if a[-i]==s:
            nn+=1
        else:
            break
        i+=1
for x in ab:
    zn+=(x//2)
if len(a)==1:
    print(b//2)
if len(ab)!=1:
    print(zn*b+(nn//2)*(b-1))
else:
    print((b*ab[0])//2)

Submission status: Wrong Answer

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

def change_a(x):
    lx = list(x)
    k = 0
    for i in range(len(x)-1):
        if lx[i] == lx[i+1]:
            k += 1
            lx[i+1] = "@"
    return k, "".join(lx)

def change_b(x):
    lx = list(x)
    k = 0
    for i in range(1,len(x)-1):
        if lx[i] == lx[i-1]:
            k += 1
            lx[i-1] = "@"
        if lx[i] == lx[i+1]:
            k += 1
            lx[i+1] = "@"
    return k, "".join(lx)

ca, castr = change_a(s)
cb, cbstr = change_b(s)
a, b = (k+1)//2, k//2

if s[0] != s[-1]:
    print(ca*k)
else:
    if castr[-1] != castr[0]:
        print(ca*k)
    else:
        if cbstr[-1] != castr[0]:
            print(ca*a+cb*b)
        else:
            print(max(k-1,ca+cb*(k-1)))

Submission status: Wrong Answer

Solution code:
import numpy as np
S = input()
K = int(input())
unique = list()
for x in S:
    if x not in unique:
        unique.append(x)
res = 0
if len(unique) == 1:
    res = int(len(S)*K/2)
    S = ""
if len(S) == 2:
    if S[0] == S[-1]:
        res += K
if len(S) > 2:
    S += S[0]
    n = 0
    while n <= len(S)-3:
        if S[n] == S[n+1]:
            res += K
            n += 1
        n += 1
    if n != len(S)-1:
        if S[len(S)-2] == S[len(S)-1]:
            res += K-1
print(res)

Submission status: Wrong Answer

Solution code:
a=list(input())
k=int(input())
ans=0
if len(a)==1:
  print(k//2)
elif len(list(set(a)))==1:
  print((len(a)*k)//2)
else:
  for i in range(len(a)-1):
    if a[i]==a[i+1]:
      a[i+1]='1'
      ans+=1
  if a[0]==a[-1]:
    print(ans*k+k-1)
  else:
    print(ans*k)

Submission status: Wrong Answer

Solution code:
s = list(map(str, input()))
k = int(input())
basic = 0
repeat = 0
counter = 0
s_a = s*2
#basic
for i in range(1,len(s)):
    if s[i] == s[i-1]:
        counter += 1
        basic += (counter % 2)
    else:
        counter = 0
#repeat
counter = 0
for i in range(1,len(s_a)):
    if s_a[i] == s_a[i-1]:
        counter += 1
        repeat += (counter % 2)
    else:
        counter = 0
repeat -= basic
print(basic + repeat * (k - 1))

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
l = len(s)
a = [0]*l
ans = 0
for i in range(l):
    if s[i] == s[(i-1)%l]: a[i] += 2
    if s[i] == s[(i+1)%l]: a[i] += 1
#print(a)
for i in range(l):
    if a[i] == 3:
        ans += 1
        a[i] = 0
        if a[(i+1)%l] >= 2: a[(i+1)%l] -= 2
        if a[(i-1)%l] == 1: a[(i-1)%l] = 0 
#print(ans)  
#print(a)
ans += len([i for i in range(l) if a[i] == 1 and a[(i+1)%l] == 2])
ans *= k
if a[0] == 2 and a[-1] == 1: ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
from math import ceil
from itertools import tee


def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)


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

if len(S) == 1:
    print(K // 2)
else:
    print(ceil(K * sum(1 for s, t in pairwise(S) if s == t) / 2))

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
s = list(s)
s.append(s[0])
h = len(s)
saigo = False
j = 0
num = 0

for i in range(1,h):
    if s[i] == s[i-1] and j == 0:
        if i == h-1:
            saigo = True
        num = num + 1
        j = 1
    else:
        j = 0

num = num * k
if saigo:
    num = num - 1

if h == 2:
    num = k // 2

print(num)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
def unit(s):
    """sの両端を外した状態でいくつ変えるか
    """
    count = 0
    prev = s[0]
    for c in s[1:]:
        if c==prev:
            count += 1
            prev = None
        else:
            prev = c
    return count
if len(s)==2:
    if s[0]==s[1]:
        print(k)
    else:
        print(0)
elif k==1:
    print(unit(s))
elif len(s)==1:
    print(k//2)
elif not s:
    print(0)
elif s[0]!=s[-1]:
    print(k * unit(s))
#     if k==1:
#         print(unit(s))
#     else:
#         result = s2 + s0 * (k-2) + s1
#         print(result)
else:
    s0 = unit(s[1:-1])
    s1 = unit(s[1:])
    s2 = unit(s[:-1])
    s3 = unit(s)
    result = min(s1*(k-1)+s3, s2*(k-1)+s3)
    if k%2==0:
        result2 = min(s3*(k/2)+s0*(k/2-1)+s1, s2+s3*(k/2)+s0*(k/2-1))
    else:
        result2 = min(s3*(k//2+1)+s0*(k//2), s2+(s3*(k//2)+s0*(k//2-1))+s1)
    print(min(result, result) + (k-1))

Submission status: Wrong Answer

Solution code:
from sys import stdin
from itertools import groupby

s = stdin.readline().rstrip()
k = int(stdin.readline().rstrip())

def cou(st):
    li = []
    point = 1
    for i in range(len(st)-1):
        if st[i] == st[i+1]:
            point += 1
            if i == len(st)-2:
                li.append(point)
        else:
            li.append(point)
            point = 1
    return li

if len(s) == 1:
    lin = [1]
else:
    lin = cou(s)

if len(lin) == 1:
    print(lin[0]*k//2)
    exit()

lin2 = lin

if k > 1:
    if s[0] == s[-1]:
        lin[-1] += lin[0]
else:
    if len(s) == 1:
        print(0)
        exit()

point = 0
point2 = 0
for i in range(1,len(lin)):
    point += lin[i]//2
for w in range(1,len(lin2)):
    point2 += lin2[w]//2
point = point*(k-1)
point += point2
point += lin[0]//2
print(point)

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
S=''
#for i in range(k):
#	S+=s
#print(s)
S = list(s)
#last_word=s[-1]
count=0
l_s=len(S)
code_list = 'abcdefghijklmnopqrstuvwxyz'
#print('step1')
if l_s>1 and S[0]==S[1]:
  flag=False
else:
  flag=True
for i in range(1,l_s):
  #print(S,count)
  if S[i]==S[i-1] and i<l_s-1:
    for j in code_list:
      if j != S[i+1] and j !=S[i-1]:
        S[i]=j
        count+=1
        break
  elif S[i]==S[i-1]:
    for j in code_list:
      if j != S[i-1] and j !=S[0]:
        S[i]=j
        count+=1
        break
if S[-1]==S[0] and flag:
  plu=k-1
else:
  plu= 0
print(count*k+plu)

Submission status: Wrong Answer

Solution code:
import sys
import os

S = input()
K = int(input())
if len(S) == 1:
    print(0)
    sys.exit()

cnt = 1
ans = 0
i = 0
while i < len(S)-1:
    for j in range(i+1,len(S)):
        if S[i] == S[j]:
            cnt +=1
        else:
            i = j
            break
    if cnt > 1:
        ans += cnt//2
        cnt = 1
        i = j
ans = K*ans
if S[0] == S[-1]:
    if S[0] != S[1] and S[-2] != S[-1]:
        ans += K-1
print(ans)

Submission status: Wrong Answer

Solution code:
# A - Connection and Disconnection
S = str(input())
K = int(input())
count =  0
tmp = 0
N = len(S)

if N == 1:
    if K >= 2:
        count = 1 * (K//2)
    else:
        count = 0
elif S[0] == S[N-1]:
    count += 1
else:
    pass

for i in range(1,N-1):
    if S[i-1] == S[i] == S[i+1] and tmp == 0:
        tmp = 1
    else:
        tmp = 0
    if S[i-1] == S[i] and tmp == 0:
        count += 1
    else:
        pass

if N == 1 and tmp == 0:
    print(count)
elif N == 3 and tmp == 0:
    print(count*(K-1))
elif N == 3 and tmp == 1:
    print(count + K)
else:
    ans = count * K
    print(ans)

Submission status: Wrong Answer

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

l = []
for i in range(101):
    l.append(i)


hoge = []
for i in n:
    hoge.append(i)


count = 0

if len(hoge) > 1 and hoge[0] == hoge[-1] and hoge[-1] == hoge[-2]:
    hoge[-1] = 'A'
    count += 1
elif len(hoge) > 1 and hoge[0] == hoge[-1]:
    hoge[-1] = 'B'
    count += 1

for i in range(len(hoge) -1):
    if hoge[i] == hoge[i+1]:
        hoge[i+1] = l[i]
        count +=1



ans = count * k

if len(hoge) == 1:
    ans = k // 2

#print(hoge)
#print(count)
print(ans)

Submission status: Wrong Answer

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

def main():
    s = list(input().rstrip())
    k = int(input())

    c = 0

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

        for i in range(len(s) - 1):
            if s[i] == s[i+1]:
                c += 1
                s[i+1] = '1'

        print(c*k)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

# now_s = S[0]
# for i in range(1, len(S)):
#     if now_s == S[i]:
#         cnt += 1
#     now_s = S[i]

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


cnt = 0
ruisekiwa = [1]
now_s = S[0]
for i in range(1, len(S)):
    if now_s == S[i]:
        ruisekiwa[-1] += 1
    else:
        ruisekiwa.append(1)
    now_s = S[i]

if len(ruisekiwa) == 1:
    if ruisekiwa[0] % 2 == 0:
        print(ruisekiwa[0]//2*K)
        exit()

    # 文字数が奇数
    if K % 2 == 0:
        cnt += (ruisekiwa[0]-2) * (K//2)
        cnt += (ruisekiwa[0]-1) * (K//2)
    else:
        cnt += (ruisekiwa[0]-2) * (K//2 + 1)
        cnt += (ruisekiwa[0]-1) * (K//2)
    print(cnt)
    exit()

# 文字種が1種類のパターンはおk

cnt = 0
for i in range(len(ruisekiwa)):
    cnt += ruisekiwa[i]//2

# 先頭と末尾が違うなら問題ない
if S[0] != S[-1]:
    print(cnt*K)
    exit()

# 先頭と末尾が同じ場合を考える

# 先頭が偶数回数続いていて, 末尾も奇数回数
if ruisekiwa[0] % 2 == 0 and ruisekiwa[-1] % 2 == 0:
    print(cnt * K)
    exit()

# 先頭が奇数回数続いていて, 末尾も奇数回数
if ruisekiwa[0] % 2 == 1 and ruisekiwa[-1] % 2 == 1:
    if K % 2 == 1:
        print(cnt * (K//2+1) + (cnt+1) * (K//2))
    else:
        print(cnt * (K//2) + (cnt+1) * (K//2))
    exit()

# 先頭が偶数, 末尾が奇数
if ruisekiwa[0] % 2 == 0 and ruisekiwa[-1] % 2 == 1:
    print(cnt * K)
    exit()

# 先頭が奇数, 末尾が偶数
if ruisekiwa[0] % 2 == 1 and ruisekiwa[-1] % 2 == 0:
    print(cnt * K)
    exit()



# 先頭と次が同じで、先頭と末尾が同じ, 先頭は奇数回数続いている
if S[0] == S[1] and S[0] == S[-1] and S[1] != S[2]:
    print(cnt * K)
    exit()

Submission status: Wrong Answer

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

moji = []

count = 1
for i in range(len(S)-1): 
  if(S[i] == S[i+1]):
    count += 1
  else:
    moji.append(count)
    count = 1
moji.append(count)
#print(moji)

ans = 0

if S[0] == S[len(S)-1]:
  for i in range(1, len(moji)-1):
    ans += (moji[i] // 2) * K
    
  ans += ((moji[0] + moji[len(moji)-1]) // 2) * (K-1)
  ans += moji[0] // 2
  ans += moji[len(moji)-1] // 2

else:
  for i in range(1, len(moji)-1):
    ans += (moji[i] // 2) * K
    
print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
d= [c  for c in s]
n=int(input())
l=len(d)
count=0

for i in range(l-2):
  if i==0 and d[0]==d[l-1] and d[i]==d[i+1]:
    d[0]="1"
    count+=1
  if d[i]==d[i+1]:
    if d[i]!=d[i+2]:
       d[i]="1"
       count+=1
    elif d[i]==d[i+2]:
      d[i+1]="1"
      count+=1
if d[l-2]==d[l-1]:
  count+=1
elif d[l-2]==d[l-1] and d[0]==d[l-1]:
  count+=0
print(count*n)

Submission status: Wrong Answer

Solution code:
#-------------------------------------------------------------------
import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
#-------------------------------------------------------------------
s = input()		# 文字列の入力  abcdefg...
k = int(input())						# 5


ans=0

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

if len(s)==2:
	if s[0]==s[-1]: ans=k
	print(ans)
	exit(0)

count=0
ans_s=0
for i in range(len(s)-1):
	
	if s[i]!=s[i+1]:
		count=0
		continue
	
	if count%2==0:
		ans_s+=1
	
	count+=1
	p(s[i], s[i+1], count, ans_s)

p(s[i], s[i+1], count, ans_s, ans)
if s[0]==s[-1]:
	if count%2==0:
		if s[0]!=s[1] and s[-1]!=s[-2]:
			ans+=k-1


ans+=(ans_s * k)
print(ans)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

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

  if S[Length-1]==S[Length-2]:
    pass
  else:
    if S[Length-1]==S[0]:
      change+=1

  ans=(K-1)*change+K*count
  print(ans)

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
flg=False
for s in S:
    if S[0] != s:
        flg=True
        break
if not flg:
    print(len(S)*K//2)
    exit()
ans = 0
i=0
while i<len(S)-1:
    renzoku=1
    x=i
    while S[x] == S[i+1]:
        renzoku+=1
        if i+2 < len(S):
            i=i+1
        else:
            break

    if renzoku >1:
        ans+=renzoku//2
    i+=1
    
print(ans*K)

Submission status: Wrong Answer

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

cnt=0
i=1
while i <len(s):
  if i>0:
    if s[i]==s[i-1]:
      cnt+=1
      i+=2
    else:
      i+=1
  
if k==1:
  print(cnt)
  exit()
  
ss=s+s
cntcnt=0
i=1
while i <len(ss):
  if i>0:
    if ss[i]==ss[i-1]:
      cntcnt+=1
      i+=2
    else:
      i+=1
if k==2:
  print(cntcnt)
  exit()
  
sss=s+s+s
cntcntcnt=0
i=1
while i <len(sss):
  if i>0:
    if sss[i]==sss[i-1]:
      cntcntcnt+=1
      i+=2
    else:
      i+=1
if k==3:
  print(cntcntcnt)
  exit()

if len(s)==1:
  print(k//2)
  exit()
  
ssss=s+s+s+s
cntcntcntcnt=0
i=1
while i <len(ssss):
  if i>0:
    if ssss[i]==ssss[i-1]:
      cntcntcntcnt+=1
      i+=2
    else:
      i+=1
if cntcntcntcnt-cntcntcnt==cntcntcnt-cntcnt:
  print(cntcnt+(cntcntcnt-cntcnt)*(k-2))
else:
  #print("hoge")
  print(cntcnt-1*(((cntcntcnt-cntcnt)*(2-k)//2))+(cntcntcntcnt-cntcntcnt)*((k-2)//2))

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
# 一番効率が良いのは3連続で同じ文字列が続くときに真ん中を別の文字に変換する場合
# 2連続で同じ文字が続く場合は後ろの文字を変える.次の文字からまた数え直す
# 最後の文字列については無視すれば良い

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


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

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

Submission status: Wrong Answer

Solution code:
import sys,queue,copy
sys.setrecursionlimit(10**8)
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
INF = float('inf')

S = list(input())
K = int(input())
i = 0
ans = 0

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

while i+1 < len(S):
    tempc= 1
    while S[i] == S[i+1]:
        tempc += 1
        if tempc == len(S):
            print(tempc * K // 2)
            exit()
        if i+2 < len(S):
            i += 1
        else:
            break



    ans += tempc //2
    if i+2 >= len(S):
        break
    i += 1

j=2
frag = 0
if S[0] == S[-1] and len(S) != 1 and K >1:
    tempc = 1
    while S[-1] == S[-j]:
        tempc += 1
        if (j+1) <= len(S):
            j += 1
        else:
            break

    frag += tempc%2

print((ans+frag)*K-frag)

Submission status: Wrong Answer

Solution code:
S = list(input())
K = int(input())
n = len(S)
s_pre = '_'
count = 0
last = 0
output = 0
if  len(S) == 1:
  output = K//2
else:
    for s in S:
        if s_pre == s:
            s = '_'
            count += 1
            s_pre = s
        if S[-1] == S[0]:
            S[-1] = '_'
            last = 1
        if last == 1:
            output -= 1
        output = K*count

print(output)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
count = 0
import copy
s1 = list(set(s))
if len(s1) == 1:
    count = int((k * len(s)) / 2)
elif k >= 3:
    t1 = s * 5
    for i in range(len(t1) - 1):
        if t1[i] == t1[i + 1]:
            t1.pop(i + 1)
            t1.insert(i + 1, "")
            count += 1
    if count % 5 == 0:
        count = (count / 5) * k
    elif (count + 2) % 5 == 0:
        count = ((count + 2) / 5) * k - 2
    else:
        count = ((count + 4) / 5) * k - 4
else:
    t1 = s * k
    for i in range(len(t1) - 1):
        if t1[i] == t1[i + 1]:
            t1.pop(i + 1)
            t1.insert(i + 1, "")
            count += 1
print(int(count))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys
import random
import itertools
import math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt


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


def LF(): return list(map(float, input().split()))


def LI_(): return list(map(lambda x: int(x)-1, input().split()))


def II(): return int(input())


def IF(): return float(input())


def LS(): return list(map(list, input().split()))


def S(): return list(input().rstrip())


def IR(n): return [II() for _ in range(n)]


def LIR(n): return [LI() for _ in range(n)]


def FR(n): return [IF() for _ in range(n)]


def LFR(n): return [LI() for _ in range(n)]


def LIR_(n): return [LI_() for _ in range(n)]


def SR(n): return [S() for _ in range(n)]


def LSR(n): return [LS() for _ in range(n)]


def gcd(x, y):
    if y == 0:
        return x
    return gcd(y, x % y)


def my_index_multi(l, x):
    return [i for i, _x in enumerate(l) if _x == x]


mod = 1000000007
inf = float('INF')


def A():
    S = input()
    
    K = II()

    i = 0
    ans = 0
    A = 1
    while(A > 0):
        count = 0
        while(S[i] == S[i+1]):
            count += 1
            i += 1
        if(S[i] != S[i+1]):
            i += 1
        if count > 0:
            ans += (count+1)//2

        if S[-1] == S[i]:
            A = -1
    if K >= 2 and S[0] == S[-2]:
        ans += 1
        print((ans*K)-1)
    else:
        print(ans*K)

    

    return

# B


def B():
    N, K = map(int, input().split())
    h = LI()
    count = 0

    for i in h:
        if i >= K:
            count += 1
        else:
            continue
    print(count)
    return

# C


def C():
    N = II()
    A = LI()
    J = [0] * N
    for i, n in enumerate(A):
        J[n-1] = i+1
    for s in J:
        print(s, end=" ")

    return

# D


def D():
    A, B = map(int, input().split())
    g = gcd(A, B)
    # go = g
    # myset = set([1])
    sq = int(sqrt(g)) + 2
    ans = 1

    for i in range(2, sq):
        if g % i == 0:
            ans += 1
        while g % i == 0:
            g /= i

            # myset.add(i)
        if g == 1:
            break
        if i == sq - 1:
            # myset.add(go)
            ans += 1

    print(ans)

    return

# E


def E():
    N, M = LI()
    bit_max = 1 << N
    dp = [[inf for i in range(bit_max)] for j in range(M+1)]
    dp[0][0] = 0

    for i in range(M):
        a, b = LI()

        c = LI()
        bit = 0

        for d in c:
            bit |= 1 << (d-1)

        for w in range(bit_max):

            t = w | bit
            cost = dp[i][w] + a

            dp[i+1][t] = min(dp[i+1][t], cost)

            dp[i+1][w] = min(dp[i][w], dp[i+1][w])

    print(-1) if dp[-1][-1] == inf else print(dp[-1][-1])

    return

# F


def F():
    N, M = LI()

    value = [0] * M
    power = [0] * M
    for i in range(M):
        a, b = LI()
        value[i] = a
        c = LI()
        L = ['0'] * N
        for k in c:
            L[k - 1] = '1'
        bint = ''.join(L)
        power[i] = int(bint, 2)
    bint = ''.join(list(['1'] * N))
    dp = [inf] * (int(bint, 2)+1)
    t = 0
    cost = 0

    dp[0] = 0
    for s in range(int(bint, 2) + 1):
        for i in range(M):
            t = s | power[i]
            cost = dp[s] + value[i]
            dp[t] = min(dp[t], cost)
    print(-1) if(dp[-1] == inf) else print(dp[-1])

    return

    # you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")


def solution(S):
    # write your code in Python 3.6
    import math

    vowels = "AIUEO"
    count = 0
    consonants = 0
    newC = []
    newV = []

    for i in S[:-1]:
        if i in vowels:
            count += 1
            newV.append(i)
        else:
            consonants += 1
            newC.append(i)

    if consonants < count or consonants - 2 > count:
        return 0

    a = math.factorial(len(set(newC))) * math.factorial(len(set(newV)))
    return a

    pass


# Solve
if __name__ == '__main__':
    A()

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
N = len(S)
cnt = 0
flg1 = False
flg2 = False
flg3 = False
flg4 = False
for i in range(N-1):
    if S[i] != S[i+1]:
        flg3 = True
        break
if (flg3 == False) and N % 2 == 1:
    print((N*K)//2)
    exit()
if (S[0] == S[N-1]) and K != 1:
    cnt += 1
    flg1 = True
if S[N-2] == S[N-1]:
    flg4 = True
for i in range(N-1):
    if flg2:
        flg2 = False
        continue
    if (i == N - 2) and flg1:
        break
    if S[i] == S[i+1]:
        cnt += 1
        flg2 = True
# print(cnt)
if flg1 and flg4 == False:
    print(cnt*K-1)
else:
    print(cnt*K)
# print(cnt*K)

Submission status: Wrong Answer

Solution code:
S = str(input())
K = int(input())
tmp = 1
cnt1 = 0
cnt2 = 0
for i in range(1, len(S)):
    if S[i-1] == S[i]:
        tmp += 1
    else:
        cnt1 += tmp // 2
        tmp = 1
if tmp > 1:
    cnt1 += tmp//2
S2 = S + S
tmp = 1
for i in range(1, len(S2)):
    if S2[i-1] == S2[i]:
        tmp += 1
    else:
        cnt2 += tmp // 2
        tmp = 1
if tmp > 1:
    cnt2 += tmp//2
if K == 1:
    print(cnt1)
else:
    print((cnt2 - cnt1) * (K-2) + cnt2)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
def unit(s):
    """sの両端を外した状態でいくつ変えるか
    """
    count = 0
    prev = s[0]
    for c in s[1:]:
        if c==prev:
            count += 1
            prev = None
        else:
            prev = c
    return count
if len(s)==2:
    if s[0]==s[1]:
        print(k)
    else:
        print(0)
elif k==1:
    print(unit(s))
elif len(s)==1:
    print(k//2)
elif not s:
    print(0)
elif s[0]!=s[-1]:
    print(k * unit(s))
#     if k==1:
#         print(unit(s))
#     else:
#         result = s2 + s0 * (k-2) + s1
#         print(result)
else:
    s0 = unit(s[1:-1])
    s1 = unit(s[1:])
    s2 = unit(s[:-1])
    s3 = unit(s)
    result = min(s1*(k-1)+s3, s2*(k-1)+s3)
#     if k%2==0:
#         result2 = min(s3*(k/2)+s0*(k/2-1)+s1, s2+s3*(k/2)+s0*(k/2-1))
#     else:
#         result2 = min(s3*(k//2+1)+s0*(k//2), s2+(s3*(k//2)+s0*(k//2-1))+s1)
    print(min(result, result) + (k-1))

Submission status: Wrong Answer

Solution code:
S = input()
K = input()
c = 0
if S[0] == S[-1]:
    S = S + S[0]

cnt= 0
flg = True
for i in range(len(S)-1):
  if S[i] == S[i+1] and flg:
    cnt += 1
    flg = False
  else:
    flg = True
    if i == len(S)-2:
      c = 0

print(int(cnt)*int(K) - c)

Submission status: Wrong Answer

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

if len(S_list) == 1:
    print(K//2)
else:
    S2_list = copy.copy(S_list)
    for i in range(1, len(S_list)):
        if S_list[i-1] == S_list[i]:
            S_list[i] = "change"

    if S_list[0] == S_list[-1]:
        S2_list[0] = "change"
        for i in range(1, len(S2_list)):
            if S2_list[i-1] == S2_list[i]:
                S2_list[i] = "change"
        print((K//2+1)*S_list.count('change') + (K//2)*S2_list.count('change'))
    else:
        print(K*S_list.count('change'))

Submission status: Wrong Answer

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

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

listS = list(S)
change = 0

for i, s in enumerate(listS[1:], start=1):
    if s == listS[i - 1] and listS[i - 1] != '.':
        change += 1
        listS[i] = '.'

ans = change * K

if S[0] == S[-1] and S[-1] != S[-2]:
    ans += K - 1

print(ans)

Submission status: Wrong Answer

Solution code:
# A - Connection and Disconnection
s = input()
k = (int)(input())

beforeStr = ''
count = 0

if len(s) == 1:
    print((int)(k / 2))
else:
    firstChar = s[0]
    endChar_ = s[len(s) - 2]
    endChar = s[len(s) -1]

    list_s = list(s)

    if endChar != endChar_ and firstChar == endChar:
        count += 1
        list_s[0] = 'Z'

    for i in range(len(s)):

        if beforeStr == list_s[i]:
            count += 1
            list_s[i] = 'Z'
        beforeStr = list_s[i]

    print(count * k)

Submission status: Wrong Answer

Solution code:
# coding:utf-8

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

if(len(s) == 1 and k > 1):
    print( int(k / 2) )
elif(len(s) == 1 and k == 1):
    print(0)
elif(k == 1):
    chenge_count = 0
    count_tmp = 0
    for i in range(1, len(s)):
        if( s[i] == s[i-1] ):
            count_tmp += 1
        elif(s[i] != s[i-1]):
            count_tmp += 1
            chenge_count += int(count_tmp / 2)
            count_tmp = 0
    count_tmp += 1
    chenge_count += int(count_tmp / 2)
    
    print(chenge_count)
else:
    chenge_count = 0
    count_tmp = 0
    s = s * 2
    for i in range(1, len(s)):
        if( s[i] == s[i-1] ):
            count_tmp += 1
        elif(s[i] != s[i-1]):
            count_tmp += 1
            chenge_count += int(count_tmp / 2)
            count_tmp = 0
    count_tmp += 1
    chenge_count += int(count_tmp / 2)
    
    if(k % 2 == 1):
        print(int(chenge_count * int( k / 2 ) + (chenge_count // 2)))
    else:
        print(chenge_count * int( k / 2 ))

Submission status: Wrong Answer

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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

s = input()
k = I()
n = len(s)
res = 0
i = 0
lst = deque()
while i < n:
    tmp = s[i]
    i += 1
    cnt = 1
    while i < n and tmp == s[i]:
        cnt += 1
        i += 1
    lst.append(cnt)
if len(lst) >= 2:
    if s[0] == s[-1]:
        x = lst.popleft()
        y = lst.pop()
        res = 0
        for i in lst:
            res += (i//2)*k
        res += (x//2)*(k-1) + (y//2)*(k-1)
        res += ((x+y)//2)*(k-1)
        print(res)
    else:
        res = 0
        for i in lst:
            res += i//2*k
        print(res)
else:
    print(n*k//2)

Submission status: Wrong Answer

Solution code:

import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
# A = [ int(input()) for _ in range(N) ]
##############################

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

T = S[:]

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

count = 0
for i in range(1, len(T)):
    if T[i-1] == T[i]:
        count += 1
        T[i] = '-'

count *= K

T = S[:]

if T[0] == T[-1]:
    if T[0] != T[1] and T[-1] != T[-2]:
        count += (K-1)

print(count)