Problem:


Score : 300 points

Problem Statement

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

Constraints

1 \leq |S| \leq 100

S consists of lowercase English letters.

1 \leq K \leq 10^9

K is an integer.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the minimum number of operations required.

Sample Input 1

issii
2

Sample Output 1

4

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

Sample Input 2

qq
81

Sample Output 2

81

Sample Input 3

cooooooooonteeeeeeeeeest
999993333

Sample Output 3

8999939997



Sample input:
issii
2


Sample output:
4

Submission status: Wrong Answer

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



if len(s)==1:
    if k<2:
        ans = 0
    else:
        ans = k//2
elif len(s)==2:
    if s[0]==s[1]:
        ans = k
    else:
        ans = 0
else:
    sset = set()
    for i in range(len(s)):
        sset.add(s[i])
    if len(sset)==1:
        llen = len(s)*k
        ans = llen//2
        
    else:
        if k == 1:
            ans = 0
            bef = ""
            befchen = False
            trignum = 0
            for i in range(len(s)):
                now = s[i]
                if bef==now:
                    if befchen == False:
                        ans += 1
                        befchen = True
                        trignum = i
                if  befchen == True:
                    if trignum == i-1:
                        befchen = False
                bef = now

        elif k >=2:
            ans = 0
            bef = ""
            befchen = False
            trignum = 0
            for i in range(len(s)):
                now = s[i]
                if bef==now:
                    if befchen == False:
                        #print(now,i)
                        ans += 1
                        befchen = True
                        trignum = i
                if  befchen == True:
                    if trignum == i-1:
                        befchen = False
                bef = now

            if s[0]==s[-1]:
                if befchen == False:
                    ans += 1

            ans *= k

            if s[0]==s[-1]:
                if befchen == False:
                    ans -= 1

print(ans)

Submission status: Wrong Answer

Solution code:
X=input()
a=int(input())
count=0
ans=0
b=0
j=0
z=list(X)
Z=X+X
y=list(Z)
if len(X)==1:
    ans=int(a/2)
else:
    for i in range(len(z)-1):
        if z[i]==z[i+1]:
            z[i+1]=0
            count=count+1
            if z[0]==z[len(z)-1]:
                while z[j]!=z[len(z)-1]:
                    b=b+1
                ans=a*count+(a-1)*(b-1)
            else:
                ans=a*count
print(ans)

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:
        print(count*k)
        exit()

Submission status: Wrong Answer

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

N=len(S)
t=S[0]
cnt=0
arr=[]
for s in S[1:N]:
    if t==s:
        cnt+=1
    else:
        arr.append(cnt+1)
        cnt=0
    t=s
arr.append(cnt+1)
ans=sum(map(lambda x: x//2, arr))
if len(arr)>1 and S[0]==S[-1] and arr[0]%2==1 and arr[-1]%2==1:
    print((ans+1)*K-1)
#     print('A')
elif len(arr)==1 and N%2==1:
    print((ans+1)*K)
#     print('B')
else:
    print((ans)*K)

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
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   
ans += len([i for i in range(l) if a[i] == 1 and a[(i+1)%l] == 2])
ans *= k
if a[-1] == 1: ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
def getCount(target, index):
    count = 0
    key_char = target[index]
    
    for char in target[index::] :
        if (key_char == char) :
            count += 1
        else :
            break
    
    return count

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

head_seq = getCount(s, 0)

if head_seq == len(s) :
    print((len(s) * k) // 2)
else :
    target_str = s[head_seq::] + s[0:head_seq:]

    position = 0
    replace_count = 0
    while position < len(target_str) :
        temp_seq = getCount(target_str, position)
        replace_count += temp_seq // 2
        position += temp_seq

    tail_seq = getCount(target_str[::-1], 0)
    org_head_seq = getCount(s, 0)
    org_tail_seq = getCount(s[::-1], 0)

    print(replace_count * k - tail_seq // 2 + org_head_seq // 2 + org_tail_seq // 2)

Submission status: Wrong Answer

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

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

print(-1)
exit()

ans = 0
T = list(S)
b = 0

for i, t in enumerate(T[1:-1], start=1):
    if t == T[i - 1] and T[i - 1] != '.':
        b += 1
        T[i] = '.'

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

print(ans + b * K)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
from decimal import *
S = input()
K = Decimal(input())
if len(S) == 1:#文字列の長さが1のときは話が変わる
    ans = K // 2
    print(ans)
    exit()
fletter = 0 # 最初の文字の連続数を数える
lletter = 0 # 最後の文字の連続数を数える
cletter = 0 # 現在連続している文字の連続数を数える
cng = 0 # 文字変更の必要がある文字をカウント
for i in range(len(S)): # 最初の文字の連続数を数える
    if S[0] == S[i]:
        fletter += 1
    else:
        break
for i in range(len(S)):
    if S[-1] == S[-(i + 1)]:
        lletter += 1
    else:
        break
for i in range(len(S) - 1): # 1文字ずつ連続しているかを判定
    if S[i] == S[i + 1]:
        cletter += 1
    else:
        cng -= - cletter // 2
        cletter = 0
cng -= - cletter // 2 # 最終文字まで到達したら文字変更数の処理
if S[0] == S[-1] and fletter % 2 == 1 and lletter % 2 == 1:
    ans = Decimal((cng + 1) * K - 1)
else:
    ans = Decimal(cng * K)
print(ans) #解答の出力

Submission status: Wrong Answer

Solution code:
import collections
S =[i for i in input()]
K = int(input())
a = 1
b = []
c = []
result = 0
if S[0] != S[-1]:
  try:
    for j in range(len(S)):
      if S[j] == S[j+1]:
        a += 1
      else:
        b.append(a)
        a = 1
  except:
    None
  for k in b:
    result += k//2
  print(result*K)
elif len(collections.Counter(S)) == 1:
  print(len(S)*K//2)
else:
  for j in range(len(S)):
    if S[j] == S[j + 1]:
      a += 1
    else:
      c.append(a)
      break
  for j in range(len(S)):
    if S[-j-1] == S[-j-2]:
      a += 1
    else:
      c.append(a)
      break
  S = S+S
  S = S[c[0]:-c[1]]
  try:
    for j in range(len(S)):

      if S[j] == S[j+1]:
        a += 1
      else:
        b.append(a)
        a = 1
  except:
    b.append(a)
  for k in b:
    result += k//2
  print(result*(K-1)+c[0]//2+c[1]//2)

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)         
        
        # C
        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 reversed(range(len(S2)-1)):
            if S2[i] == S2[i+1]:
                S2[i+1] = 'A'+str(i)
                ans += 1
        ans_min = min(ans, ans_min)   
        
        print(ans_min)
        return

f()

Submission status: Wrong Answer

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

sl=list(s)

cnt=0

for i in range(len(sl)-1):
    if sl[i]==sl[i+1]:
        cnt+=1
        sl[i+1]="?"
ss=list("".join(sl)+s)

tmp=0
for i in range(len(sl)-1, len(ss)-1):
    if ss[i]==ss[i+1]:
        tmp+=1
        ss[i+1]="?"

print(cnt+tmp*(k-1))

Submission status: Wrong Answer

Solution code:
import itertools

S = input()
K = int(input())
totalx = 0
seq = [len(list(g)) for k, g in itertools.groupby(S)]
for i in range(len(seq)):
    if seq[i]%2==0:
        x = seq[i]
    else:
        x = seq[i]-1
    totalx+=int(x/2)
totaly = 0
if not K==1:
    st = S + S
    seq2 = [len(list(g)) for k, g in itertools.groupby(st)]
    for i in range(len(seq2)):
        if seq2[i]%2==0:
            x = seq2[i]
        else:
            x = seq2[i]-1
        totaly+=int(x/2)
#sub = totaly-totalx
if K%2==0:
    print(int(totaly*K/2))
else:
    print(int(totaly*(K-1)/2+totalx))

Submission status: Wrong Answer

Solution code:
s = list(input())
n = int(input())
c = 0
l = len(s)
while l >= 2:
    x = s.pop(0)
    l -= 1
    if s[0] == x:
        c += 1
        s.pop(0)
        l -= 1
print(c*n)

Submission status: Wrong Answer

Solution code:
from itertools import groupby
import math

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

cnt = 0

tttt = []
ttt = [[k, len(list(g))] for k, g in groupby(s)]
for tt in ttt:
  for i,v in enumerate(tt):
    if tt[i] == 1:
      next
    else:
      tttt.append(tt[1])

for tt in tttt:
    if tt % 2 == 0:
      cnt += tt / 2
    else:
      cnt += math.ceil(tt//2)

print(int(cnt/2*k))

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
dup = 0
tmp = ""
s2 = s * 2

for c1, c2 in zip(s2, s2[1:]):
    if c1 == c2:
        if tmp:
            tmp = ""
        else:
            dup += 1
            tmp = c1
    else:
        tmp = ""

answer = dup * k // 2
print(answer)

Submission status: Wrong Answer

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

if len(S) == 1:
    print(K // 2)
else :
    pre = S[0]
    flg = 0
    cnt = 0
    cnt_list =[]
    
    for i in range(1, len(S)):
        cnt += 1
        if flg == 1:
            flg = 0
            pre = S[i]
            continue
        if S[i] == pre:
            cnt_list.append(cnt)
            flg = 1
        pre = S[i]

    S = S[::-1]
    pre = S[0]
    flg = 0
    cnt = 0
    cnt_list2 =[]
    
    for i in range(1, len(S)):
        cnt += 1
        if flg == 1:
            flg = 0
            pre = S[i]
            continue
        if S[i] == pre:
            cnt_list2.append(cnt)
            flg = 1
        pre = S[i]
    
    if S[0] == S[-1] and (S[-1] == S[-2] or S[0] == S[1]):
        print(min(len(cnt_list), len(cnt_list2))*K)
    elif S[0] == S[-1]:
        print(min(len(cnt_list), len(cnt_list2))*K+K)
    else :
        print(min(len(cnt_list), len(cnt_list2))*K)

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[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:
import copy
S_list = list(input())
K = int(input())

if len(S_list) == 1:
    print(K//2)
elif len(S_list) == 2:
    if S_list[0]==S_list[1]:
        print(K)
    else:
        print(0)
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 = list(input())
K = int(input())
if S.count(S[0]) == len(S):
    print(len(S) * K // 2)
    exit()

cnt = 0
continuous = 0
chr = S[0]
for i in range(1,len(S)):
    if chr == S[i]:
        cnt += 1
    if chr != S[i] or i == len(S) - 1:
        continuous += (cnt + 1) // 2
        cnt = 0
    chr = S[i]
if continuous == 0 and S[0] == S[-1]:
    print(K - 1)
else:
    print(K * continuous)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
pre_character = ''
S_list=list(S)
s_len = len(S)
def get_abc(s1,s2):
    if s1 == 'a':
        if s2 == 'b':
            return 'c'
        else:
            return 'b'
    elif s1 == 'b':
        if s2 == 'a':
            return 'c'
        else:
            return 'a'
    else:
        if s2 == 'a':
            return 'b'
        else:
            return 'a'

count = 0
for i in range(s_len):
    if pre_character == S_list[i]:
        count += 1
        if (s_len - 1) != i:
            S_list[i] = get_abc(pre_character,S_list[i+1])
        else:
            S_list[i] = get_abc(pre_character,S[0])
    pre_character = S_list[i]
#print(S_list)
if S_list[0] == S_list[-1]:
    pre_character = S_list[-1]
    S_list=list(S)
    second_count = 0
    for i in range(s_len):
        if pre_character == S_list[i]:
            second_count += 1
            if (s_len - 1) != i:
                S_list[i] = get_abc(pre_character,S_list[i+1])
            else:
                S_list[i] = get_abc(pre_character,S[0])
        pre_character = S_list[i]
    #print(S_list)
    gusu = K//2
    kisu = K - gusu
    print((count*kisu)+ (second_count*gusu))
else:
    print(count*K)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
n = len(s)
count = 0
C = 0
change = 0
A =[]
for i in range(n-1):
  if s[i] == s[i+1]:
    count += 1
    C += 1
  else:
    if count == 1:
      change += 1
    elif count%2 == 0:
      change += count//2
    else:
      change += (count+1)//2
    A.append(count)
    count = 0
    C = 0 
if count == 1:
  change += 1
elif count%2 == 0:
  change += count//2
else:
  change += (count+1)//2

if count != 0:
  A.append(count)
A.append(1)
if C%2 == 0 and A[0]%2 == 0 and s[0] == s[n-1] and n != 1:
  print(change*k + k-1)
elif n==1:
  if k == 1:
    print(0)
  elif k%2 == 0:
      print(k//2)
  else:
    print((k-1)//2)
else:
  print(change*k)

Submission status: Wrong Answer

Solution code:
import random
import string

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

prev_c = S[0]
num_changes = 0
for i, c in enumerate(S[1:]):
    if c == prev_c:
        num_changes += 1
        prev_c = 'replaced'
    else:
        prev_c = c

total_changes = 0
if S[0] == prev_c:
    num_changes2 = 0
    for i, c in enumerate(S):
        if c == prev_c:
            num_changes2 += 1
            prev_c = 'replaced'
        else:
            prev_c = c
    total_changes = num_changes + num_changes2 * (K-1)
else:
    total_changes = num_changes * K

print(total_changes)

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:
s = list(input())
k = int(input())
count1 = 0
count2 = 1
f = s[0]
memo = [0]
for c1, c2 in zip(s[:], s[1:]):
    if not memo[-1] and c1 == c2:
        count1 += 1
        memo.append(1)
    else:
        memo.append(0)
s[0] = "#"
memo2 = [0]
for c1, c2 in zip(s[:], s[1:]):
    if not memo2[-1] and c1 == c2:
        count2 += 1
        memo2.append(1)
    else:
        memo2.append(0)
if f == s[-1]:
    if memo[-1]:
        print(k * count1)
    else:
        if memo2[-1]:
            print(count1 * (k - k // 2) + count2 * k // 2)
        else:
            print(count1 * k + count2 * (k-1))
else:
    print(k * count1)

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))  # カンマを取ると n 個ちょうどになる
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]
        
if len(s)==1:
	print(k//2)
elif len(s)==2:
	if len(set(s))==1:
		print(k)
	else:
		print(0)
elif len(s)==3:
	if len(set(s))==1:
		print(3*k//2)
	elif len(set(s))==2:
		print(k)
else:
    if s[0]!=s[-1]:
        a=list(nchars(s, 2))
        b=0
        for i in a:
            b+=len(i)//2
        print(b*k)
    else:
        if s[0]!=s[1] and s[-1]!=s[-2]:
            a=list(nchars(s, 2))
            b=0
            for i in a:
                b+=len(i)//2
            print(b*k+k-1)
        else:
            a=list(nchars(s, 2))
            b=0
            c=list(nchars(s+s, 2))
            l1_l2_sym_diff = set(a) ^ set(c)
            if len(l1_l2_sym_diff)%2==1:
                for i in a:
                    b+=len(i)//2
                print(b*k)
            else:
                for i in a:
                    b+=len(i)//2
                print(b*k+k-1)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
#true_k = list(s * k )

#s_1 = true_k[0::2]
#s_2 = true_k[1::2]
#s_1 = list(s * k )[0::2]
#s_2 = list(s * k )[1::2]
ans = 0
word = '0'
moji = []
for i in s:
    if  word == i:
      word = '0'
      moji.append(word)
      ans +=1
    else:
      word = i 
      moji.append(word)

if moji[0] == moji[-1]:
  ans +=1

print(ans*k)    
#print(moji)

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[0]
        y = lst[-1]
        res = 0
        m = len(lst)
        for i in range(1,m-1):
            res += lst[i]//2
        res *= 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
        print(res*k)
else:
    print(n*k//2)

Submission status: Wrong Answer

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

if len(S) == 1:
    print(K // 2)
elif len(S) == 2:
    if S[0] == S[1]:
        print(K)
    else:
        print(0)

elif len(S) == 3 and(S[0] == S[1]) and (S[1] == S[2]):
    if K % 2 == 0:
        print(int(3*K/2))
    elif K == 1:
        print(1)
    else:
        print(2 + 2*(K-2))


else:
    S_arranged = list(S)
    for i in range(N):
        if (S_arranged[i] == S_arranged[i-1]) and (S_arranged[i-1] == S_arranged[i-2]):
            S_arranged[i-1] = "0"
            if (i >= 2):
                counts +=1
            else:
                counts += 1

    for i in range(N):
        if S_arranged[i] == S_arranged[i-1]:
            S_arranged[i-1] = "1"
            if i >= 2:
                counts += 1
            else:
                counts += 1
    if (S[0] == S[-1]) and (S[0] != S[1]) and (S[-1] != S[-2]):
        print(counts*K + adder*(K-1) -1)
    else:
        print(counts*K + adder*(K-1))

Submission status: Wrong Answer

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


a = S[0]
c = 0
S2 = copy.deepcopy(S)
for i in range(1,len(S)):
    if S2[i] == S2[i-1]:
        S2[i] = '1'
        c += 1

a = S[-1]
last = 1
for i in range(2,len(S)+1):
    last += 1
    if S[-i] != a:
        break


if S[0] == S[-1] and last%2==1:
    c3 = 0
    S3 = copy.deepcopy(S)
    for i in range(2,len(S)):
        if S3[i] == S3[i-1]:
            S3[i] = '1'
            c3 += 1
    print(c + (c3+1)*(K-1))
else:
    print(c*K)

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
if len(S)==1:
    print(len(S)*K//2)
elif K==1:
    i=0
    count=0
    while i<len(S)-1:
        if S[i]==S[i+1]:
            count+=1
            i+=2
        else:
            i+=1
    print(count)

else:
    s=""
    s+=S*2
    i=0
    count=0
    while i<len(s):
        if i==len(s)-1:
            if s[i]==s[0]:
                count+=1
                if s[0]==s[1]:
                    count-=1
            i+=1
        elif s[i]==s[i+1]:
            count+=1
            if i==len(s)-2:
                if s[i+1]==s[0]:
                    count-=1
            i+=2

        else:
            i+=1
    if K%2==0:
        print(count*(K//2))
    else:
        i=0
        Count=0
        while i<len(S)-1:
            if S[i]==S[i+1]:
                Count+=1
                i+=2
            else:
                i+=1
        print(count*(K//2)+Count)

Submission status: Wrong Answer

Solution code:
# coding: utf-8

line = [x for x in input()]
num = int(input())

flag = True
for i in range(1, len(line)):
    if line[i] != line[i - 1]:
        flag = False
        break

counter = 0
for i in range(1, len(line)):
    if line[i] == line[i - 1]:
        counter += 1
        line[i] = "."

counter2 = 0
if line[0] == line[-1]:
    counter2 = 1

if flag and len(line) % 2 != 0:
    m = (num - 1) // 2
    m2 = num - m
    print(counter * m2 + (len(line) - len(line) // 2) * m)

elif len(line) == 1:
    print(int(num / 2))
else:
    print(counter * num + (num - 1) * counter2)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
if len(S) == 1:
  print(K//2)
  exit()
ans = 0
temp = 0
lst =[]
i = 0
while i < len(S)-1:
  if S[i] == S[i+1]:
    temp += 1
    i += 1
  else:
    lst.append(temp+1)
    temp = 0
    i += 1
lst.append(temp+1)
for n in lst:
  ans += n//2
if S[0] == S[-1]:
  if lst[-1] ==1:
    ans += 1
    ans = ans*K -1
  elif lst[-1]%2 == 1:
    ans += 1
    ans = ans*K -1
  else:
    ans = ans*K
else:
  ans = ans*K
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())

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

Submission status: Wrong Answer

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

l=len(s)
zero=0
first=0
second=0
last=0
S=s+s
LS=list(S)

for n in range(2*l):
  if n==0:
    if LS[0]==LS[-1]:
      zero+=1
  if 0<n<=l-1:
    if LS[n-1]==LS[n]:
      first+=1
      LS[n]=str(n)
  if l<=n<2*l-1:
    if LS[n-1]==LS[n]:
      second+=1
      LS[n]=str(n)
  if n==2*l-1:
    if LS[n-1]==LS[n]:
      last+=1
      LS[n]=str(n)
      zero-=1

if K%2==0:
  ans=(zero+first+second+last)*(K//2)
else:
  ans=(zero+first+second+last)*(K//2)+zero+first

print(ans)

Submission status: Wrong Answer

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


result = 0

# S内の重複
dup_c = 0
c_list = ['XXXXX']
for s in S:
    if c_list[0] == s:
        dup_c += 1
        c_list = ['XXXXX']
    else:
        c_list[0] = s
result = dup_c * K

# 間の重複
if len(S) > 1:
#    if S[0] == S[len(S)-1]:
#        if S[1] != S[0] and S[len(S)-2] != S[0]:
#            result += K - 1
    #後ろから同じ文字をカウント
    Sr = S[::-1]
    same_s = 0
    for i in range(len(Sr)):
        if Sr[0] == Sr[i]:
            same_s += 1
        else:
            break
    
    if same_s != 1:
        if same_s % 2 == 1:
            result += K - 1
    elif Sr[0] == Sr[len(Sr)-1]:
        result += K - 1

else:
    result += K//2

print(result)

Submission status: Wrong Answer

Solution code:
#coding: utf-8

S=list(input())
K=int(input())
n=len(S)*K

if len(S)==1:	
	print(n//2 if n%2==0 else (n-1)//2)
	exit()

c = 0

if S[0] == S[-1] and K>=2:
	c+=K-1
	S[0] = "!"


f=False
for i in range(1,len(S)):
	if f:
		continue
	else:
		f=False
	if S[i]==S[i-1]:
		c+=K
		S[i] = "?"
		f = True
#		print(S)

	
#print(S*K)
print(c)

Submission status: Wrong Answer

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

s = str(input()).replace('\n', '')
k = int(input())

s = s + s[0]

lst = []
last = ''
cnt = 0
for i in range(len(s)):
  if s[i] == last or last == '':
    cnt += 1
  else:
    lst.append(cnt)
    cnt = 1
  last = s[i]
lst.append(cnt)
    
n_lst = [i // 2 for i in lst]
answer = sum(n_lst)*k

print(answer)

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
 
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 flg and 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 not flg:
    print(ans*K)
elif 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:
s = input()
k = int(input())

prev = ""
count1 = 0
flag = 0
for i in s:
    if flag == 1:
        prev = i
        flag = 0
        continue
    if prev == i:
        count1 += 1
        flag = 1
    prev = i

prev = s[-1]
count2 = 0
flag = 0
for i in s:
    if flag == 1:
        prev = i
        flag = 0
        continue
    if prev == i:
        count2 += 1
        flag = 1
    prev = i

ret = count1 + (k-1) * count2

print(ret)

Submission status: Wrong Answer

Solution code:
s = raw_input()
k = raw_input()
c = 0
i = 0
out = 0
while( i < len(s)-1 ):
  if s[i] == s[i+1]:
    c += 1
    i += 1
  i += 1
if i == len(s)-1 and s[0] == s[len(s)-1]:
  out = out + 1
out = (int(k)-1)*out + int(k)*c
print(out)

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:
            saigo = True
        num = num + 1
        j = 1
    else:
        j = 0

num = num * k
if saigo:
    num = num - 1
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 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))
    else:
        result2 = min(s3*(k//2+1)+s0*(k//2), s2+(s3*(k//2)+s0*(k//2-1))+s1)
    print(min(result2, 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)
    
    if s[-1]==s[0]==s[1]==s[-2]:
        print(count*k)
    else:
        print(count*k)