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:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math
import collections


def simple_count(S, changed=False):
    count = 0
    for i in range(len(S) - 1):
        if changed:
            changed = False
        else:
            if S[i] == S[i + 1]:
                changed = True
                count += 1
    return count, changed


def solve(S, K):
    if S[0] != S[-1]:
        return simple_count(S)[0] * K
    else:
        count2, changed = simple_count(S * 2)
        if changed:
            return count2 * (K // 2) + simple_count(S, changed=False)[0] * (K % 2)
        else:
            return count2 * (K // 2) + 1 + simple_count(S, changed=False)[0] * (K % 2)
        # return simple_count(S + S[0])[0] * K


def test(S, K):
    assert solve(S, K) == simple_count(S * K)[0], "{} {} = {} {}".format(S, K, solve(S, K), simple_count(S * K)[0])
assert solve("cooooooooonteeeeeeeeeest", 999993333) == 8999939997
assert solve("aqqq", 2) == simple_count("aqqq" * 2)[0]
test("qq", 81)
# print(solve("qqq", 2), simple_count("qqq" * 2)[0])
assert solve("qqq", 2) == simple_count("qqq" * 2)[0]
# print(solve("qqq", 3), simple_count("qqq" * 3)[0])
assert solve("qqq", 3) == simple_count("qqq" * 3)[0]
assert solve("issii", 2) == simple_count("issii" * 2)[0]




test("iqi", 3)
test("iqqqqiiii", 1)
test("iqqqqiiii", 6)
test("iqqqqiiii", 7)
test("qqqqqiiiii", 7)

S = sys.stdin.readline().rstrip()
K, = map(int, sys.stdin.readline().rstrip().split(" "))
print(solve(S, K))

exit(0)

Submission status: Wrong Answer

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

itr = 0
cnt = 1
len_cnt = []
for x in S :
  if itr == 0 :
    pre = x
    itr += 1
    continue
  now = x
  if pre == now :
    cnt += 1
    itr += 1
    if itr == len(S) :
      len_cnt.append(cnt)
    continue
  if pre != now :
    len_cnt.append(cnt)
    itr+=1
    if itr == len(S) and cnt == 1 :
      len_cnt.append(cnt)
    cnt = 1
    pre = x
    continue

if sum(len_cnt) != len(S) :
  len_cnt.append(1)

ans = 0
for x in len_cnt :
  if x == 1 :
    continue
  else :
    ans += int(x / 2)

if S[0] == S[-1] and len_cnt[-1] > 1 and len_cnt[-1] % 2 == 1 and K > 1:
  ans += 1

ANS = K * ans
# print (ANS, len_cnt)
if S[0] ==S[-1] and len_cnt[-1] % 2 == 1 and len_cnt[-1] != 1 :
  ANS -= 1
print(ANS)

Submission status: Wrong Answer

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

def func(ms):
    r = 0
    k = 0
    m = ms[0]
    for i in range(1, len(ms)):
        if ms[i] == ms[i-1]:
            k+=1
        else:
            r += int((k+1)/2)
            k = 0
        m = ms[i]
    r += int(k+1/2)
    return r            
    
if S[0] * len(S) == S:
    if len(S) == 1:
        print(int(K/2))
    else:
        print(int(len(S) * K / 2))
elif len(set(S)) == len(S):
    print(0)
elif S[0] == S[-1]:
    r = 0
    m = S[0]
    f = 0
    for c in S:
        if c == m:
            r += 1
            f += 1
        else:
            break   
    s = S[::-1]
    fe = 0
    for c in s:
        if c == m:
            r += 1
            fe += 1
        else:
            break
    S = S[f:-fe]
    tr = int(r/2) * (K-1)
    r = func(S) * K
    print(r+tr)
else:
    print(func(S)*K)

Submission status: Wrong Answer

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

if len(S)==1:
  if K==1:
    print(0)
    exit()
  else:
    print(K//2)
    exit()
for i in range(1,len(S)):
  if S[i]==S[i-1]:
    S[i]="!"
    c+=1
if S[0]!=S[-1]:
  print(c*K)
else:
  if S[0]==S[1]:
    for i in range(99999999999999999999):
      print(i*10**9999)
  else:
    print(c*K+K-1)

Submission status: Wrong Answer

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

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

def ans(s, k):
    flg = False
    pre_s = None
    dup_list = []
    cnt = 1
    _ans = 0
    for _s in s:
        if pre_s == _s:
            cnt += 1
        else:
            if cnt>1:
                dup_list.append(cnt)
            cnt = 1
        pre_s = _s
    for d in dup_list:
        _ans += d//2
        
    return _ans * k
  
print(ans(s, k))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

cnt = 0
p = ''
for i, s in enumerate(S):
    if p == s:
        cnt += 1
        p = ''
    else:
        p = s
cnt *= K
if p == S[0]:
    cnt += K - 1
print(cnt)

Submission status: Wrong Answer

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

import re


def nchars(s, n):
    """文字列 s に、同じ文字が n 個以上連続している部分文字列を見つける        
    """
    assert n > 0
    reg = re.compile("(.)\\1{%d,}" % (n - 1))
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]
lst = list(nchars(s,2))
count = 0
for i in lst:
    count += len(i)//2
count = count*k

if s[0]==s[-1]:

    first = s[0]
    f = 0
    e = 0
    end = s[-1]
    for i in s:
        if first == i:
            f += 1
        else:
            break
    for i in reversed(s):
        if end == i:
            e += 1
        else:
            break
    if f % 2 == 1 and e % 2 == 1:
        if f == len(s) and k > 2:
            count+=k-2
        else:
            count += k-1

print(count)

Submission status: Wrong Answer

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


def s_change(S):
    s_henkan = 0
    for i in range(1,len(S)):
#         print(S[i])
        if S[i] == S[i-1]:
#             print(S[i-1],S[i])
            S[i] = '1'
            s_henkan += 1
    return(S,s_henkan)

        
henkan = 0
    
if len(S) == 1:
    henkan = K // 2
elif len(S) == 2:
    if S[0] == S[1]:
        henkan = K
    else:
        henkan = 0
else:
    L = len(S) -1
    if S[0] == S[L]:
        if S[0] == S[1] and S[L-1] == S[L]:
            S[0] = '1'

#     print(S)
    SSSS = s_change(S)
    henS = SSSS[0]
    s_henkan = SSSS[1]

    
    if henS[0] == henS[len(henS)-1]:
        henkan = s_henkan * K + (K-1)
    else:
        henkan = s_henkan * K

print(henkan)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict as df
s=input().rstrip()
ini=len(s)
k=int(input())
s=[i for i in s]
if len(s)==2:
    if s[0]!=s[-1]:
        print(0)
    else:
        print((len(s)*k)//2)
    exit()
if len(s)==1:
    print(k//2)
    exit()
else:
    count=0
    temp=[]+s
    temp=[ i for i in temp]
    for i in range(1,len(s)):
        if temp[i]==temp[i-1]:
            temp[i]='randi'
            
            count+=1
    if s[0]!=s[-1]:
        print(count*k)
        exit()
    if s.count(s[0])==len(s):
        print((len(s)*k)//2)
        exit()
    if k==1:
        print(count)
        exit()
    if s[-1]==s[0] and s[0]!=s[1] and s[-1]!=s[-2]:
        print(count*k + k-1)
        exit()
    
    if s[-1]==s[0]==s[1]==s[-2]:
        print(count*k)
        exit()
    else:
        temp=temp+temp
        var=0
        
        
        print(count*k+k-1)

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()
if K!=1:
    S_old = S
    i=0
    while S_old[-1] == S_old[i]:
        S= S + S[i]
        i+=1
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


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

Submission status: Wrong Answer

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

S = input().strip()
K = int(input())
f = 0
if len(set(S)) == 1:
    print(len(S)*K//2)
else:
    c = 0
    first = 0
    _s = ''
    for s in S:
        if s != _s:
            first += c // 2
            c = 1
            _s = s
        else:
            c += 1
    c = 0
    middle = 0
    f = False
    _s = S[0]
    for s in S:
        if s != _s:
            if not f:
                f = True
                c = 0
            else:
                middle += c // 2
                c = 1
                _s = s
        else:
            c += 1
    c = 0
    bit = ''
    _s = ''
    for s in S:
        if _s == '':
            bit = s
            _s = s
        elif s == _s:
            bit += s
        else:
            break
    _s = ''
    for s in S[::-1]:
        if _s == '':
            bit += s
            _s = s
        elif s == _s:
            bit += s
        else:
            break
    if len(set(bit)) == 1:
        bit = len(bit) // 2
    else:
        bit = bit.count(bit[0]) // 2 + bit.count(bit[-1]) // 2
    c = 0
    last = 0
    _s = ''
    for s in S[::-1]:
        if s != _s:
            last += c // 2
            c = 1
            _s = s
        else:
            c += 1

    print(first + bit * (K - 1) + middle * (K - 2) + last)

Submission status: Wrong Answer

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

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

zcount=0
ju=0
for i in range(len(S)):
  if S[i]==S[0]:
    zcount+=1
    if i==len(S)-1:
      ju=1
  else:
    break

if ju==0:
  for i in reversed(range(len(S))):
    if S[i]==S[0]:
      zcount+=1
    else:
      break
  
if S[0]==S[1]:
  S[0]="#"
for i in range(1,len(S)):
  if S[i-1]==S[i]:
    S[i]="#"

ans=S.count("#")
if ju==1 and len(S)%2==1:
  print((len(S)*K)//2)
  exit()
  
if s[-1]==s[0] and s[1]!=s[0] and s[-2]!=s[0]:
  print(K*ans+(K-1))
  exit()

if S[0]=="#" and zcount>1 and zcount%2==1 and ju==0:
  ans-=1
  if S[-2]!=S[0] and s[0]==s[-1]:
    ans+=1

print(ans*K)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math
import collections


def simple_count(S, changed=False):
    count = 0
    for i in range(len(S) - 1):
        if changed:
            changed = False
        else:
            if S[i] == S[i + 1]:
                changed = True
                count += 1
    return count, changed


def solve(S, K):
    if S[0] != S[-1]:
        return simple_count(S)[0] * K
    else:
        count2, changed = simple_count(S * 2)
        if changed:
            return count2 * (K // 2) + simple_count(S, changed=False)[0] * (K % 2)
        else:
            return count2 * (K // 2) + 1 + simple_count(S, changed=False)[0] * (K % 2)
        # return simple_count(S + S[0])[0] * K


def test(S, K):
    assert solve(S, K) == simple_count(S * K)[0], "{} {} = {} {}".format(S, K, solve(S, K), simple_count(S * K)[0])
assert solve("cooooooooonteeeeeeeeeest", 999993333) == 8999939997
assert solve("aqqq", 2) == simple_count("aqqq" * 2)[0]
test("qq", 81)
# print(solve("qqq", 2), simple_count("qqq" * 2)[0])
assert solve("qqq", 2) == simple_count("qqq" * 2)[0]
# print(solve("qqq", 3), simple_count("qqq" * 3)[0])
assert solve("qqq", 3) == simple_count("qqq" * 3)[0]
assert solve("issii", 2) == simple_count("issii" * 2)[0]




test("iqi", 3)
test("iqqqqiiii", 1)
test("iqqqqiiii", 6)
test("iqqqqiiii", 7)
test("qqqqqiiiii", 7)

# S = sys.stdin.readline().rstrip()
# K, = map(int, sys.stdin.readline().rstrip().split(" "))
# print(solve(S, K))

exit(0)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
ans = 0
cnt = 0
ans_fin = 0
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        cnt = cnt +1
        if i == len(s)-2:
            if cnt%2 == 1:
                ans = ans + int((cnt+1)/2)
            else:
                ans = ans + int(cnt/2)
    else:
        if cnt%2 == 1:
            ans = ans + int((cnt+1)/2)
            cnt = 0
        elif (cnt%2 == 0 and not(cnt == 0)):
            ans = ans + int(cnt/2)
            cnt = 0
        else:
            pass
ans_fin = ans*k
#print(ans_fin)
if not(s[0] == s[len(s)-1]):
    pass
if len(s) == 1:
    if k%2 ==0:
        ans_fin = int(k/2)
    else:
        ans_fin = int((k-1)/2)
else:
    cnt2 = 0
    cnt3 = 0
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            cnt2 = cnt2 + 1
        else:
            break
    for i in range(len(s)-1):
        if s[-i] == s[-i-1]:
            cnt3 = cnt3 + 1
    
    if (cnt2%2 ==0 and cnt2 >0) and(cnt3%2 == 0 and cnt3>0):
        ans_fin = ans_fin + (k-1)
print(ans_fin)

Submission status: Wrong Answer

Solution code:
import sys
s=input()
k=int(input())
count=1
answer=0
lastChange=False
roopContinue=False
lastWord=""
for i in range(len(s)):
  if i==len(s)-1:
    print(len(s)*k//2)
    sys.exit()
  elif k==1:
    break
  elif s[i+1]==s[0]:
    lastWord+=s[0]
  else:
    break
s+=lastWord
for i in range(len(s)-1):
  if s[i]==s[i+1]:
    count+=1
    if i==len(s)-2:
      answer+=(count//2)
  else:
    answer+=(count//2)
    count=1
    if i==len(s)-2:
      answer+=(count//2)
print(answer*k)

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
p=s[0]
cnt=1
q=[]
for i in range(len(s)-1):
	if s[i+1]==p:
		cnt+=1
	else:
		q.append(cnt)
		cnt=1
		p=s[i+1]
q.append(cnt)
for i in range(len(s)):
	if p==s[i]:
		cnt+=1
	else:
		break
qqq=cnt
import copy
qq=copy.copy(q)
qq[-1]=qqq
r=copy.copy(qq)
r[0]=0
pq=copy.copy(q)
pq[0]=0
for i in range(len(qq)):
	qq[i]//=2
for i in range(len(q)):
	q[i]//=2
	pq[i]//=2
for i in range(len(r)):
	r[i]//=2
if k==1:
	print(sum(q))
	exit()
elif len(q)==1:
	print(len(s)*k//2)
	exit()
print(sum(qq)+sum(r)*(k-2)+sum(pq))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import copy
a=list(input())
b=int(input())
if len(a)==1:
  print(b//2)
else:
  c=copy.copy(a) 
  l=len(a)
  ans1=0
  ans2=0
  t=0
  for i in range(1,len(a)):
    if c[i]==c[i-1]:
      ans1+=1
      c[i]=str(i%10)
  c=a*2
  if c[l-1]==c[l-2]:
    c[l-1]=str(1)
  for i in range(l,len(c)):
    if c[i]==c[i-1]:
      ans2+=1
      c[i]=str(i%10)
      if i==len(c)-1:
        t=1
  if b==1:
    print(ans1)
  elif t:
    if b%2==0:
      print((ans1+ans2)*b//2)
    else:
      print((ans1+ans2)*(b//2)+ans1)
  else:
    print(ans1+ans2*(b-1))

Submission status: Wrong Answer

Solution code:
import numpy as np

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

def connect_cnt(S):
	cnt_tmp = 1
	cnt = []

	for i in range(len(S) - 1):

		if S[i] == S[i+1]:
			cnt_tmp += 1
		else:
			cnt.append(cnt_tmp)
			cnt_tmp = 1

		if i == len(S)-2:
			cnt.append(cnt_tmp)

	connect_cnt = sum(np.array(cnt) // 2)
	return connect_cnt

if len(S) == 1:
  print(connect_cnt(S*K))
  exit(0)

if K == 1:
  print(connect_cnt(S))
  exit(0)

if S[0] != S[-1]:

	connect_cnt = connect_cnt(S)
	ans = connect_cnt * K
	print(ans)

else:

	S_dash = S + S[0]
	connect_cnt_Sdash = connect_cnt(S_dash)
	connect_cnt_S = connect_cnt(S[1:])

	ans = connect_cnt_Sdash * (K - 1) + connect_cnt_S
	print(ans)

Submission status: Wrong Answer

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

cnt=0
if len(s)%2==1:
  s=s+s
  n=n-n//2
r=1
R=[]
for i in range(1,len(s)):
  if s[i]==s[i-1]:
    r+=1
    if i==len(s)-1:
      R.append(r)
  else:
    R.append(r)
    r=1
for i in R:
  cnt=cnt+i//2
print(cnt*n)

Submission status: Wrong Answer

Solution code:
S = input().replace(" ", "")
K = int(input())

str1 = S
str2 = S * 2
n1 = 0
n2 = 0
ln1 = []
ln2 = []

for c in str1:
  ln1.append(c)
for i in range(len(ln1)):
  if i == 0:
    pass
  elif ln1[i-1] == ln1[i]:
    n1 += 1
    ln1[i] = "A"
    
for c in str2:
  ln2.append(c)
for i in range(len(ln2)):
  if i == 0:
    pass
  elif ln2[i-1] == ln2[i]:
    n2 += 1
    ln2[i] = "A"
    
if n1 == n2:
  print(n1 * K)
else:
  print(n1 + (n2 - n1) * (K -1))

Submission status: Wrong Answer

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

for i in range(1, len(S)):
    if S[i] == S[i-1] and S[i] != None:
        ans += 1
        S[i] = None
ans *= K

if len(S) == 1:
    ans = K // 2
    print(ans)
    exit()
if S[-1] == S[0] and S[1] != None:
    ans += (K-1)
print(ans)

Submission status: Wrong Answer

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

if k == 1:
    cnt1 = 0
    cnt2 = 0
    for i in range(1, n-1):
        if i % 2 == 0:
            if s[i] == s[i-1] or s[i] == s[i+1]:
                cnt1 += 1
        else:
            if s[i] == s[i-1] or s[i] == s[i+1]:
                cnt2 += 1
    print(min(cnt1, cnt2))
else:
    s += 2*s
    s1 = ''
    s2 = ''
    for i in range(3*n):
        if i < 3*n - 1 and i > 0:
            if i % 2 == 0:
                s2 += s[i]
                if s[i] == s[i-1] or s[i] == s[i + 1]:
                    s1 += '0'
                else:
                    s1 += s[i]
            else:
                s1 += s[i]
                if s[i] == s[i-1] or s[i] == s[i + 1]:
                    s2 += '0'
                else:
                    s2 += s[i]
        elif i == 3*n - 1:
            if i % 2 == 0:
                s2 += s[i]
                if s[i] == s[i-1]:
                    s1 += '0'
                else:
                    s1 += s[i]
            else:
                s1 += s[i]
                if s[i] == s[i-1]:
                    s2 += '0'
                else:
                    s2 += s[i]
        else:
            if i % 2 == 0:
                s2 += s[i]
                if s[i] == s[i + 1]:
                    s1 += '0'
                else:
                    s1 += s[i]
            else:
                s1 += s[i]
                if s[i] == s[i + 1]:
                    s2 += '0'
                else:
                    s2 += s[i]
    
    s1_arr = [s1[:n].count('0'), s1[n:2*n].count('0'), s1[2*n:].count('0')]
    s2_arr = [s2[:n].count('0'), s1[n:2*n].count('0'), s2[2*n:].count('0')]
    if k == 2:
        print(min(s1_arr[0] + s1_arr[2], s2_arr[0] + s2_arr[2]))
    else:
        print(min(s1_arr[0] + s1_arr[1]*(k-2) + s1_arr[2], s2_arr[0] + s2_arr[1]*(k-2) + s2_arr[2]))

Submission status: Wrong Answer

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

T = S * 2
T2 = S

countA = 0
for i in range(len(S) - 1):
    if S[i] == S[i + 1]:
        countA += 1
        S[i + 1] = 'A'

# 2bai
countB = 0
for i in range(len(T) - 1):
    if T[i] == T[i + 1]:
        countB += 1
        T[i + 1] = 'A'
if T[-1] == T2[0]:
    C = T2.copy()
    C.insert(0, T[-1])
    countC = 0
    for i in range(len(C) - 1):
        if C[i] == C[i + 1]:
            countC += 1
            C[i + 1] = 'A'

if K > 1:
    if K % 2 == 1:
        if T[-1] == T2[0]:
            print(K//2*countB+countC)
        else:
            print(K//2*countB+countA)
    else:
        print(K//2*countB)
else:
    print(countA)

Submission status: Wrong Answer

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

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 and c==1:
      c = 0

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

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
c = 0
if len(s)==1:
    print(k//2)
elif len(s)==3:
    if s[0]==s[1] and s[0]!=s[2]:
        print(k)
    elif s[0]==s[1] and s[0]==s[2]:
        if k>1:
            print((3*k)//2)
        else:
            print(1)
    elif s[1]==s[2]:
        print(k)
    elif s[0]==s[2]:
        print(k-1)
    else:
        print(0)
else:
    i = 0
    q = False
    while i < len(s)-1:
        if s[i]==s[i+1]:
            c+=1
            i+=1
        i+=1
    if s[len(s)-1]==s[0]:
        if s[len(s)-1]!=s[len(s)-2]:
            c+=1
            q = True
    if q:
        if c==1:
            print(k-1)
        else:
            print((c-1)*k+k-1)
    else:
        print(c*k)

Submission status: Wrong Answer

Solution code:
def f():
    S = input()
    K = int(input())
    S = list(S)
    if K == 1 and len(S) == 1:
        print(0)
        return
    
    if K == 1 and len(S) != 1:
        ans = 0
        for i in range(len(S)-1):
            if S[i] == S[i+1]:
                S[i+1] = '1'
                ans += 1
        print(ans)
        return
    
    if K != 1 and len(S) == 1:
        ans = K // 2
        print(ans)
        return
    
    if K != 1 and len(S) != 1:
        ORG_S = S[:]
        ans_min = 10**12
        # A
        ans = 0
        if S[0] == S[-1]:
            S[-1] = 'X'
            ans += 1
        for i in range(len(S)-1):
            if S[i] == S[i+1]:
                S[i+1] = str(i)
                ans += 1
        ans = ans*(K-1)
        S2 = S[:] + ORG_S
        for i in range(len(S2)-1):
            if S2[i] == S2[i+1]:
                S2[i+1] = 'A'+str(i)
                ans += 1
        ans_min = min(ans, ans_min)
        # B
        ans = 0
        S = ORG_S[:]
        if S[0] == S[-1]:
            S[0] = 'X'
            ans += 1
        for i in range(len(S)-1):
            if S[i] == S[i+1]:
                S[i+1] = str(i)
                ans += 1
        ans = ans*(K-1)
        S2 = ORG_S + S[:]
        for i in range(len(S2)-1):
            if S2[i] == S2[i+1]:
                S2[i+1] = 'A'+str(i)
                ans += 1
        ans_min = min(ans, ans_min)         
        # B-rev
        ans = 0
        S = ORG_S[:]
        if S[0] == S[-1]:
            S[0] = 'X'
            ans += 1
        for i in reversed(range(len(S)-1)):
            if S[i] == S[i+1]:
                S[i] = str(i)
                ans += 1
        ans = ans*(K-1)
        S2 = ORG_S + S[:]
        for i in range(len(S2)-1):
            if S2[i] == S2[i+1]:
                S2[i] = 'A'+str(i)
                ans += 1
        ans_min = min(ans, ans_min)   

        
        print(ans_min)
        return

f()

Submission status: Wrong Answer

Solution code:
import math

def main():
    s = input()
    k = int(input())
    cnt = 0
    if (len(s) == 0):
        cnt = 0
    elif (len(s) == 1):
        cnt = math.floor(k/2)
    elif (len(s) == 2):
        if (s[0] == s[1]):
            cnt = k
        elif (s[0] != s[1]):
            cnt = 0
    else:
        l = list()
        prev = ''
        for c in s:
            if(c == prev):
                l[-1][1] += 1
            else:
                l.append([c, 1])
                prev = c
        if (len(l) == 1):
            cnt += l[0][-1] * k // 2
        elif (len(l) == 2):
            cnt += l[0][-1] // 2
            cnt += l[1][-1] // 2
            cnt *= k
        else:
            edge1 = l[0]
            edge2 = l[-1]
            mid = l[1:-1]
            for e in mid:
                cnt += e[1] // 2 * k
            if (edge1[0] == edge2[1]):
                cnt += (edge1[1] + edge[2]) // 2 * (k-1)
            else:
                cnt += (edge1[1]) // 2 * (k-1)
                cnt += (edge2[1]) // 2 * (k-1)
            cnt += edge1[1] // 2
            cnt += edge2[1] // 2

    print(cnt, end='')


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

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

before = ""
cnt = 0
h = S[0]
for s in S:
    if before == s:
        cnt+=1
        before = ""
    else:
        before = s
cnt *= K
j = 1
for s in S[1:]:
    if s == h:
        j+=1
    else:
        break
if len(S) == j:
    for s in reversed(S[1:]):
        if s == h:
            j+=1
        else:
            break
if j > 1 and j%2 == 1:
    cnt+= K-1


print(cnt)

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 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 collections import defaultdict as df
s=input().rstrip()
ini=len(s)
k=int(input())
s=[i for i in s]
if len(s)==2:
    if s[0]!=s[-1]:
        print(0)
    else:
        print((len(s)*k)//2)
    exit()
if len(s)==1:
    print(k//2)
    exit()
else:
    count=0
    temp=[]+s
    temp=[ i for i in temp]
    for i in range(1,len(s)):
        if temp[i]==temp[i-1]:
            temp[i]='randi'
            
            count+=1
    if s[0]!=s[-1]:
        print(count*k)
        exit()
    if s.count(s[0])==len(s):
        print((len(s)*k)//2)
        exit()
    if k==1:
        print(count)
        exit()
    if s[-1]==s[0] and s[0]!=s[1] and s[-1]!=s[-2]:
        print(count*k + k-1)
        exit()
    
    if s[-1]==s[0]==s[1]==s[-2]:
        print(count*k)
        exit()
    else:
        temp=temp+temp
        var=0
        
        for i in range(1,len(temp)):
            if temp[i]==temp[i-1]:
                var+=1
                temp[i]='gandu'
        var=var*(k-1)
        print(count*k+var)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
n = len(s) - 1
count = 1
output = 0
last = 0
tmp = 0

for i in range(n):
    if s[i] == s[i + 1]:
        count += 1

    else:
        output += count // 2
        count = 1

if s[0] == s[-1]:
    for i in range(n + 1):
        if s[i] == s[0]:
            count += 1

        else:
            tmp = i
            break

    if tmp == 0:
        print((len(s) * k) // 2)

else:
    output += count // 2
    count = 1

    for i in range(tmp, n):
        if s[i] == s[i + 1]:
            count += 1

        else:
            last += count // 2
            count = 1

    last += count // 2

    print(output * (k - 1) + last)

Submission status: Wrong Answer

Solution code:
a = input()
j = int(input())

# is0i0is0i0
# print(a, len(a))
i = 0
c = 0
prev = False
for x in a:
  # print(prev)
  if c > 0:
    # print(x, a[c-1], c, prev, x == a[c-1] and not prev)
    if x == a[c-1] and not prev:
      i += 1
      prev = True
      c += 1
      continue
  prev = False
  # print("c++", c)
  c +=1 

if a[0] == a[-1]:
  i += 1
print(i*j)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
result = 0
if S[0] != S[-1]:
    t = None
    for i in range(len(S)):

        if i == 0:
            t = S[i]
            continue

        if t == S[i]:
            result += 1
            t = None
            continue

        t = S[i]

    print(result * K)
    exit()

else:
    m = None
    for i in range(len(S)):

        if i == 0:
            m = S[i]
            continue

        if m == S[i]:
            result += 1
            m = None
            continue

        m = S[i]
    t = S[0]
    count_a = 1
    for i in range(len(S)):
        if i == 0:
            continue

        if t == S[i]:
            count_a += 1
        else:
            break
    if count_a == len(S):
        print((len(S) * K) // 2)
        exit()
    count_b = 0

    for i in range(len(S)):
        if t == S[-i-1]:
            count_b += 1
        else:
            break

    count = count_a + count_b
    change = count // 2
    change_a = count_a // 2
    change_b = count_b // 2
    if change == change_a + change_b:
        print(result * K)
    else:
        print(result * K - 1)

Submission status: Wrong Answer

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

n = len(s)
s_list = []
for i in range(n):
    s_list.append(s[i])

ans = 0
for i in range(n - 1):
    if s_list[i] == s_list[i + 1]:
        ans += 1
        s_list[i + 1] = str(i + 1)
ans *= k

if s_list[0] == s_list[n - 1]:
    ans += k - 1

print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin

S = stdin.readline().rstrip()
K = int(stdin.readline().rstrip())
ans_odd = 0
ans_even = 0
changed_odd = [False] * len(S)
changed_even = [False] * len(S)

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

for i in range(1, len(S)):
    if S[i] == S[i - 1] and not changed_odd[i - 1]:
        ans_odd += 1
        changed_odd[i] = True

if S[0] == S[len(S) - 1] and not changed_odd[len(S) - 1]:
    ans_even += 1
    changed_even[0] = True

for i in range(1, len(S)):
    if S[i] == S[i - 1] and not changed_even[i - 1]:
        ans_even += 1
        changed_even[i] = True

if S[0] == S[len(S) - 1] and not changed_even[len(S) - 1]:
    print(ans_even * K - 1)
else:
    if K % 2 == 0:
        print((ans_odd + ans_even) * (K // 2))
    else:
        print((ans_odd + ans_even) * (K // 2) + min(ans_even, ans_odd))

Submission status: Wrong Answer

Solution code:
import sys
s=input()
k=int(input())
count=1
answer=0
lastChange=False
roopContinue=False
lastWord=""
for i in range(len(s)):
  if k==1:
    break
  elif i==len(s)-1:
    print(len(s)*k//2)
    sys.exit()
  elif s[i+1]==s[0]:
    lastWord+=s[0]
  else:
    break
s+=lastWord
for i in range(len(s)-1):
  if s[i]==s[i+1]:
    count+=1
    if i==len(s)-2:
      answer+=(count//2)
  else:
    answer+=(count//2)
    count=1
    if i==len(s)-2:
      answer+=(count//2)
print(answer*k)

Submission status: Wrong Answer

Solution code:
S = input().replace(" ", "")
K = int(input())

str1 = S
str2 = S * 2
n1 = 0
n2 = 0
ln1 = []
ln2 = []

for c in str1:
  ln1.append(c)
for i in range(len(ln1)):
  if i == 0:
    pass
  elif ln1[i-1] == ln1[i]:
    n1 += 1
    ln1[i] = "A"
    
for c in str2:
  ln2.append(c)
for i in range(len(ln2)):
  if i == 0:
    pass
  elif ln2[i-1] == ln2[i]:
    n2 += 1