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:
# -*- coding: utf-8 -*-

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

another_c = ''
cnt = 0


t = list(s)
start = t[0]
for i, c in enumerate(t):
    prev_c = t[i-1]
    if i == len(t)-1:
        next_c = start
    else:
        next_c = t[i+1]
    if i > 0 and c == prev_c:
        another_c = chr(ord(c) + 1)
        if ord(another_c) > ord('z'):
            another_c = chr(ord(another_c)-26)
        if next_c == another_c:
            another_c = chr(ord(another_c) + 1)
        t[i] = another_c
        cnt = cnt + 1
    else:
        another_c = ''
    #print(i, prev_c, c, next_c, "->", another_c) 

#print("".join(t))
end = t[-1]

if k > 1:
    cnt2 = 0
    t = list(s)
    for i, c in enumerate(t):
        if i == 0:
            prev_c = end
        else:
            prev_c = t[i-1]
        if i == len(t)-1:
            next_c = start
        else:
            next_c = t[i+1]
        if c == prev_c:
            another_c = chr(ord(c) + 1)
            if ord(another_c) > ord('z'):
                another_c = chr(ord(another_c)-26)
            if next_c == another_c:
                another_c = chr(ord(another_c) + 1)
            t[i] = another_c
            cnt2 = cnt2 + 1
        else:
            another_c = ''
        #print(i, c, another_c)
    cnt = cnt + (cnt2 * (k-1))
        #print("".join(t))


print(cnt)

Submission status: Wrong Answer

Solution code:
tmp = input()
S = list(tmp.strip())
K = int(input())
#S = S * K
lens = len(S)


#print(S)
count = 0
for i in range(1, lens):
    if S[i-1] == S[i]:
        count += 1
        S[i] ='X'
if S[0] == S[lens-1]:
    count = (count+1)*(K-1)+count
else:
    count = count * K

print(count)

Submission status: Wrong Answer

Solution code:
s = list(input().strip())
t = int(input())
ans = 0
change = False
last = False
# 長さ1の時は回数割る2切り捨てだけ変える必要
if len(s) == 1:
    print(t // 2)
    exit()
before = s[0]
count = 1
first = True
for i in range(1, len(s)):
    if before == s[i]:
        count += 1
    else:
        before = s[i]
        # 最初の文字が何個続くか偶奇判定
        if first:
            first = False
            if count % 2 == 1:
                first_odd = True
            else:
                first_odd = False
        ans += count // 2
        count = 1
# 同じ文字のみの場合
if first:
    if count % 2 == 1:
        first_odd = True
    else:
        first_odd = False
# 最後の同じ文字列について
ans += count // 2
# 最後の文字が何個続くか偶奇判定
if count % 2 == 1:
    last_odd = True
else:
    last_odd = False

# 最初と最後が同じ文字かつ、最後が奇数の場合
if s[0] == s[-1] and first_odd and last_odd:
    print(ans + (ans + 1) * (t - 1))
else:
    print(ans * t)

Submission status: Wrong Answer

Solution code:
S = str(input())
K = int(input())
C = i = 0

if len(S) == 1:
    C = int(K / 2)
else:
    while i + 1 < len(S):
        if S[i] == S[i + 1]:
            C += 1
            i += 1
        i += 1
    if S[0] == S[-1]:
        S += S
        i = c = 0
        while i + 1 < len(S):
            if S[i] == S[i + 1]:
              c += 1
              i += 1
            i += 1
        if c == C * 2 + 1:
            C = C * K + K - 1
        else:
            C *= K
    else:
        C *= K

print(C)

Submission status: Wrong Answer

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

if not(1 <= len(S) and len(S) <= 100):
    exit()
if not (S.isalpha() and S.islower()):
    exit()
if not float(K).is_integer():
    exit()
if not (1 <= int(K) and int(K) <= 10 ** 9):
    exit()

L = []
tmp = []
pre = ""
for s in S:
    if len(tmp) == 0:
        pre = s
        tmp.append(s)
    elif pre == s:
        tmp.append(s)
    else:
        L.append(tmp)
        pre = s
        tmp = [s]
L.append(tmp)

count = 0
for l in L:
    count += int(len(l) / 2)
    # print(l)
count *= int(K)
if len(L) == 1:
    if len(L[0]) == 1:
        count = int(int(K)/2)
    
elif L[0][0] == L[len(L)-1][0]:
    if len(L[0]) % 2 == 1 and len(L[len(L)-1]) % 2 == 1:
        count = count + int(K) - 1
print(count)

Submission status: Wrong Answer

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

box = ""
count = 0
     
for mozi in S:
  if mozi == box:
    count += 1
    box = ""
  else:
    box = mozi
    
box = S[0]
count_sta = 0
for mozi in S:
  if box != mozi:
  	break
  count_sta +=1

box = S[len(S)-1]
count_fin = 0
for mozi in reversed(S):
  if box != mozi:
  	break
  count_fin +=1

if S[0] == S[len(S)-1] and count_fin%2 != 0 and count_sta%2 !=0:
  print((count+1)*K-1)
else:
  print(count*K)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
T = S * 2

once = list(S)
once.append('0')
twice = list(T)
twice.append('0')

once_replace = 0
twice_replace = 0
 
for i in range(0, len(S)-1):
  if i == len(S)-1:
    break
  elif once[i] == once[i+1] == once[i+2]:
    once[i+1] = str(i)
    once_replace += 1
    continue
  elif once[i] == once[i+1]:
    once[i+1] = str(i)
    once_replace += 1
    
for i in range(0, len(T)-1):
  if i == len(T)-1:
    break
  elif twice[i] == twice[i+1] == twice[i+2]:
    twice[i+1] = str(i)
    twice_replace += 1
    continue
  elif twice[i] == twice[i+1]:
    twice[i+1] = str(i)
    twice_replace += 1
    
answer = (twice_replace - once_replace)*K
    
print(answer)

Submission status: Wrong Answer

Solution code:
def count_changes(s, k, prev=None):
    count = 0
    for _ in range(k):
        for cur in s:
            if cur == prev:
                count += 1
                prev = None
            else:
                prev = cur
    return count, prev


if __name__ == '__main__':
    s = input()
    k = int(input())
    if k <= 3:
        print(count_changes(s, k)[0])
    else:
        base_count, prev = count_changes(s, 3)
        d, rest = divmod(k, 3)
        res = base_count * d
        if rest:
            res += count_changes(s, rest, prev)[0]
        if s[0] == prev:
            res += (d - 1)
        print(res)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
N = len(S)
counts = 0
count2 = 0
adder = 0
adder2 = 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(int(2 + 2*((K-1)/2) +(((K-1)/2)-1)))



elif K <= 3:
    S_arranged = list(S*K)
    for i in range(len(S_arranged)-2):
        if (S_arranged[i] == S_arranged[i+1]) and (S_arranged[i+1] == S_arranged[i+2]):
            S_arranged[i+1] = "0"
            counts += 1
        
    for i in range(len(S_arranged)-1):
        if S_arranged[i] == S_arranged[i+1]:
            S_arranged[i] = "1"
            counts += 1
    print(S_arranged)
    print(counts)

else:
    S_arranged = list(S*4)
    for i in range(len(S_arranged) - 2):
        if (S_arranged[i] == S_arranged[i+1]) and (S_arranged[i+1] == S_arranged[i+2]):
            S_arranged[i+1] = "0"
            if i+1 <= len(S)-1:
                adder += 1
            elif i+1 > len(S)*3 -1:
                adder2 += 1
            elif i+1 > len(S) - 1 and i+1 <= len(S)*2 - 1:
                counts +=1
            else:
                count2 +=1

    for i in range(len(S_arranged)-1):
        if S_arranged[i] == S_arranged[i+1]:
            S_arranged[i] = "1"
            if i <= len(S)-1:
                adder += 1
            elif i > len(S)*3 -1:
                adder2 += 1
            elif i > len(S)-1 and i+1 <= len(S)*2-1:
                counts +=1
            else:
                count2 +=1


    if K % 2 == 0:
        print(int(adder + (counts + count2) * ((K-2)/2) + adder2))

    else:
        print(int((adder + counts * ((K-1)/2) + count2 * ((K-1)/2 -1) +adder2)))

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 S[0]==S[-1]:
    if len(arr)>1 and arr[0]%2==1 and arr[-1]%2==1:
        print((ans+1)*K)
#         print('A')
    else:
        if len(arr)==1 and N%2==1:
            if N==1:
                print(K//2)
#                 print('B')
            else:    
                print((ans+1)*K-1)
#                 print('C')
        else:
            print((ans)*K)
#             print('D')
else:
    if len(arr)==1 and N%2==1:
        if N==1:
            print(K//2)
#             print('E')
        else:    
            print((ans+1)*K-1)
#             print('F')
    else:
        print((ans)*K)

Submission status: Wrong Answer

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

ans = 0
tmp = 0
head = 1
hip = 1
if S[0] == S[-1]: key = True
else: key = None
itr = 0
b = 0
for s1, s2 in zip(S[:-1], S[1:]):
    if s1 == s2:
        tmp += 1
    else:
        ans += (tmp+1)//2
        if (key == True) and (b == 0):
            head = tmp
            b+=1
        tmp = 0

    if (key == True) and (itr == len(S)-2):
        hip = tmp
    itr += 1
ans += (tmp+1)//2
ans = ans*K
if b==0:
    ans = len(S)*K//2
if (head%2 == 0) and (hip%2 == 0):
    ans += K-1
print(ans)

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:
  if S[-1] == S[0]:
    S[-1] = '+'
    output -= 1
    count += 1
  for s in S:
    if s_pre == s:
      s = '_'
      count += 1
    s_pre = s
  output += K*count
  

print(output)

Submission status: Wrong Answer

Solution code:
import copy
S=list(input())
SS=copy.deepcopy(S)
K=int(input())
c=0
cc=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:
  SS[0]="!"
  for i in range(1,len(S)):
    if SS[i]==SS[i-1]:
      SS[i]="!"
      cc+=1
  print(min(c+(K-1)*cc+K-1,c*K+K-1))

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
l = [c for c in s]
i=0
count=0
f_t = 0
n=len(l)
l.append(l[0])
while i < n:
  if l[i] == l[i+1]:
    i += 2
    count += 1
    if i == 0:
      l[n] == 0 #絶対被らない文字列
    elif i == n-1:
      f_t += 1
      
  else:
    i += 1

if f_t:
  ans = count*k - 1
else:
  ans = count*k
  
print(ans)

Submission status: Wrong Answer

Solution code:
s=str(input())
k=int(input())
t=str()
cnt=0

for i in range(k):
  t=t+s

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

print(cnt+k-1)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
c = 0
d = 0
a = []
s += "A"
for i in range(len(s)-1):
    if (s[i] == s[i+1]):
        c += 1
    else:
        a.append(c+1)
        d += 1
        c = 0
c = 0
if s[0] != s[len(s)-2]:
    for i in range(d):
        c += a[i]//2
    print(int(c*k))
elif d == 1:
    print(int(a[0]*k//2))
else:
    for i in range(1,d-1):
        c += a[i]//2
    c += (k-1)*((a[0]+a[d-1])//2) + a[0]//2 + a[d-1]//2
    print(int(c))

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
cnt = 0
flag = 0
for i in range(len(s)-1):
  if s[i] == s[i+1]:
    cnt += 1
    if i-1 >=0 and s[i-1] == s[i] and flag == 0:
      cnt -= 1
      flag = 1
    else:
      flag = 0
  else:
    flag = 0
tmp = 0
if len(s)%2 and s.count(s[0])%2 and s[0] == s[-1]:
  tmp = k//2
print(cnt*k + tmp)

Submission status: Wrong Answer

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

tail = s[0]
plus = 0
if tail == s[-1]:
  tmp_cnt1 = 0
  tmp_cnt2 = 0
  for i in s:
    if tail == i:
      tmp_cnt1 += 1
    else:
      break
  for i in s[::-1]:
    if tail == i:
      tmp_cnt2 += 1
    else:
      break
  if (tmp_cnt1 % 2) == 1 and (tmp_cnt2%2)==1:
    if tmp_cnt1 != len(s):
      plus = 1

cnt=0
if len(s)==1:
  ret = k
else:
  tmp=s[0]
  tmp_cnt=0
  for i in s[1:]:
    if tmp==i:
      tmp_cnt+=1
    else:
      cnt+=(tmp_cnt+1)//2
      tmp_cnt=0
    tmp=i
  cnt+=(tmp_cnt+1)//2
  ret=cnt*k
  if plus == 1:
    ret += k-1
print(ret)

Submission status: Wrong Answer

Solution code:
def count_changes(s, k, prev=None):
    count = 0
    for _ in range(k):
        for cur in s:
            if cur == prev:
                count += 1
                prev = None
            else:
                prev = cur
    return count, prev


if __name__ == '__main__':
    s = input()
    k = int(input())
    if k <= 3:
        print(count_changes(s, k)[0])
    else:
        base_count, prev = count_changes(s, 3)
        d, rest = divmod(k, 3)
        res = base_count * d
        if rest:
            res += count_changes(s, rest, prev)[0]
        if s[0] == prev:
            res += (d - 1)
        print(res)

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
    if st[-1] != st[-2]:
        li.append(1)
    return li

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

lin2 = cou(s)

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

for i in range(1,len(lin)):
    point3 += lin[i]//2

for w in range(1,len(lin2)):
    point2 += lin2[w]//2

if k == 2:
    point += (point2*2)
    point += lin2[0]//2
else:
    point += (point3*(k-1))
    point += point2
    point += lin2[0]//2

print(point)

Submission status: Wrong Answer

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

buf=''
cnt = 1
total = 0
cnts = []


for s in S:
    #print(buf,s,cnt)
    if s == buf:
        cnt += 1       
    else:
        cnts.append(cnt)
        total += int(cnt/2)
        cnt = 1        
    buf = s
cnts.append(cnt)    
total += int(cnt/2)

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

elif S[0]==S[-1] and cnts[1] % 2 != 0:
    if (cnts[1]+cnts[-1]) % 2 == 0:
        print(total*K+K-1)
    else:
        print(total*K)
else:
    print(total*K)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline
S = list(input())[: -1]
ls = S * 2
K = int(input())
left = 0
mid = 0
right = 0
last = 0
i = 0
while i < len(S):
  if ls[i] == ls[i + 1]:
    left += 1
    if i == len(S) - 1:
      last = 1
    i += 1
  i += 1
i = 0 + last
while i < len(S):
  if ls[i] == ls[i + 1]:
    mid += 1
    i += 1
  i += 1
i = 0 + last
while i < len(S) - 1:
  if ls[i] == ls[i + 1]:
    right += 1
    i += 1
  i += 1
print(left + mid * max(K - 2, 0) + right)

Submission status: Wrong Answer

Solution code:
S, K = open(0).read().split()

cGroups = []
tempList = []
for i, c in zip(range(len(S)), S):
    tempList.append(c)
    if (i == len(S) - 1) or c != S[i + 1]:
        cGroups.append(tempList)
        tempList = []

singleResult = 0
for g in cGroups:
    if len(g) > 1:
        singleResult += len(g) // 2

result = singleResult * int(K)

if cGroups[0][0] == cGroups[-1][0] and len(cGroups[0]) % 2 == 1 and len(cGroups[-1]) % 2 == 1:
    result += int(K) - 1

print(result)

Submission status: Wrong Answer

Solution code:
S = list(input()) * 2
K = int(input())
Len_S2 = len(S)
Len_S1 = Len_S2 // 2
count1 = 0
count2 = 0
for i in range(Len_S2-1):
    if S[i] == S[i+1]:
        if i < Len_S1 - 1:
            count1 += 1
        if i < Len_S2:
            count2 += 1
        S[i+1] = "1"
diff = count2 - count1
if all(c == S[0] for c in S[:Len_S1:]):
    print((count1 + diff * (K-1))+ (1 * K // 2))
else:
    print(count1 + diff * (K-1))

Submission status: Wrong Answer

Solution code:
s = input()
length = len(s)
k = int(input())
if(len(s)==1):
    print(int(k/2))
    exit()
start = 0
end = 0
t = 0
last = 0
count = 0
ans = 0
b4 = s[0]
for c in s:
    if(b4!=c):
        if(last==0):
            start=t
            t=1
            last = 1
        else:
            ans+=int(t/2)
            t=1
    else:
        t+=1
    count+=1
    b4=c
end = t
#print(start)
#print(end)
#print(ans)
if(ans==0 and start == 0 and end != 0):
    print(int(end*k/2))
    exit()
if(start==end):
    result = (ans * k) + int((start + end) / 2) * k + int(start / 2) + int(end / 2)
    print(result)
    exit()
if(s[0] == s[-1]):
    result = (ans*k)+int((start+end)/2)*k +int(start/2) +int(end/2)-1
    if(result<0):print(0)
    else:print(result)
else:print((int(end/2)+int(start/2)+ans)*k)

Submission status: Wrong Answer

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

# =============================================================================
# double = s*2
# arr = [-1]
# for i in range(len(double)):
#     if arr[-1] != i-1:
#         if double[i] == double[i-1]:
#             arr.append(i)
# arr.remove(-1)
# if k%2 == 0:
#     count = len(arr) * k // 2
# else:
#     count = len(arr) * (k // 2) + len(arr) // 2
# print(count)
# =============================================================================


if s[0] == s[-1]:
    arr = [-1]
    for i in range(len(s)):
        if arr[-1] != i-1:
            if s[i] == s[i-1]:
                arr.append(i)
    arr.remove(-1)
    if len(arr) != 0:
        if arr[-1] == len(s)-1:
            print(len(arr)*k)
        else:
            l = len(arr)
            arr = [0]
            for i in range(len(s)):
                if i == 0:
                    continue
                if arr[-1] != i-1:
                    if s[i] == s[i-1]:
                        arr.append(i)
            if arr[-1] != len(s)-1:
                print(l + len(arr)*(k-1))
            else:
                if k%2 == 0:
                    print((l+len(arr))*k//2)
                else:
                    print((l+len(arr))*(k//2)+l)
    else:
        print(k-1)
else:
    arr = [-1]
    for i in range(len(s)):
        if arr[-1] != i-1:
            if s[i] == s[i-1]:
                arr.append(i)
    arr.remove(-1)
    print(len(arr)*k)

Submission status: Wrong Answer

Solution code:
S = list(input())
#取得文字列
K = int(input())
#繰り返し回数

count = 0
befStr = ""


for i in range(1,len(S)):
  befStr = S[i - 1]
  if befStr == S[i]:
    count += 1
    S[i] = "B"
    
if K!=1 and S[0] == S[len(S)-1]:
  print(count*K + K - 1)
else:
  print(count*K)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
#print(type(S), type(K))
last_char = S[:1]
S_from1 = S[1:]
counter = 1
SumOfManip = 0
#文字列が連続する回数のリスト
NumOfSequenceCharList = []

for c in S_from1:
    if(last_char == c):
        counter += 1
    else:
        NumOfSequenceCharList.append(counter)
        counter = 1
    last_char = c

if(counter == len(S)):
    NumOfSequenceCharList.append(counter)
    AllNum = K * NumOfSequenceCharList[0]
    if AllNum % 2 == 0:
        AllManip = AllNum / 2
    else:
        AllManip = (AllNum - 1) / 2
    print(int(AllManip))

else:
    if(counter != 1):
        NumOfSequenceCharList.append(counter)
    #最初と最後の文字が同じなら
    if(S[:1] == S[-1]):
        #偶数ならその数の1/2が操作回数、奇数ならその数-1して1/2にした数が操作回数
        #最初と最後にれんぞくした文字数を足す
        NumFirstPlusLast = NumOfSequenceCharList[0] + NumOfSequenceCharList[-1]
        if NumFirstPlusLast % 2 == 0:
            ManipFirstLast = NumFirstPlusLast / 2
        else:
            ManipFirstLast = (NumFirstPlusLast - 1) / 2
        #最初と最後以外を処理
        for num in NumOfSequenceCharList[1:-2]:
            if num % 2 == 0:
                Manip = num / 2
            else:
                Manip = (num -1) /2
            SumOfManip += Manip
        if NumOfSequenceCharList[0] % 2 == 0:
            Manip0 = NumOfSequenceCharList[0] / 2
        else:
            Manip0 = (NumOfSequenceCharList[0] - 1) / 2
        if NumOfSequenceCharList[-1] % 2 == 0:
            ManipM1 = NumOfSequenceCharList[-1] / 2
        else:
            ManipM1 = (NumOfSequenceCharList[-1] - 1) / 2

        AllManip = Manip0 + (K - 1) * ManipFirstLast + K * SumOfManip +  ManipM1

    else:
        for num in NumOfSequenceCharList:
            if num % 2 == 0:
                Manip = num / 2
            else:
                Manip = (num -1) /2
            SumOfManip += Manip
        AllManip = K * SumOfManip

    #print(AllManip,NumOfSequenceCharList,NumFirstPlusLast,ManipFirstLast,Manip0,ManipFirstLast,SumOfManip,ManipM1)
    print(int(AllManip))

Submission status: Wrong Answer

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

str1 = S
str2 = S * 2
str3 = S * 3
str4 = S * 4
n1 = 0
n2 = 0
n3 = 0
n4 = 0
ln1 = []
ln2 = []
ln3 = []
ln4 = []

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"
    
for c in str3:
  ln3.append(c)
for i in range(len(ln3)):
  if i == 0:
    pass
  elif ln3[i-1] == ln3[i]:
    n3 += 1
    ln3[i] = "A"
    
for c in str4:
  ln4.append(c)
for i in range(len(ln4)):
  if i == 0:
    pass
  elif ln4[i-1] == ln4[i]:
    n1 += 1
    ln4[i] = "A"
  
if len(S) == 1:
  print(K//2)
elif n1 == (n2 - n1) and (n2 - n1) == (n3 - n2):
  print(int(n1 * K))
elif n1 != (n2 - n1) and (n2 - n1) == (n3 - n2) and n1 != (n4 - n3):
  if K >= 3:
    print(int(n1 + (n3 - n1) * (K - 1)//2 - 1))
  elif K == 2:
    print(int(n2))
  else:
    print(int(n1))
elif n1 != (n2 - n1) and (n2 - n1) == (n3 - n2):
  if K >= 3:
    print(int(n1 + (n3 - n1) * (K - 1)//2))
  elif K == 2:
    print(int(n2))
  else:
    print(int(n1))
else:
  if K % 2 == 1:
    print(int(n1 + (n3 - n1) * (K - 1)//2))
  else:
    print(int(n2 * K / 2))

Submission status: Wrong Answer

Solution code:
S = list(input())
#取得文字列
K = int(input())
#繰り返し回数

count = 0
befStr = ""


for i in range(1,len(S)):
  befStr = S[i - 1]
  if befStr == S[i]:
    count += 1
    S[i - 1] = "B"
    
if K!=1 and S[0] == S[len(S)-1]:
  print(count*K + K - 1)
else:
  print(count*K)

Submission status: Wrong Answer

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

s_cnt = 0
for i in range(len(s) - 1):
    if s[i] != s[i + 1]:
        s_cnt = i + 1
        break
else:
    s_cnt = len(s)
for i in range(len(s) - 1, 1, -1):
    if s[i] != s[i - 1]:
        e_cnt = len(s) - i
        break
else:
    e_cnt = len(s)
is_start = False
idx_list = []
ans = 0
for i in range(len(s) - 1):
    if s[i] == s[i + 1]:
        if not is_start:
            is_start = True
            s_idx = i
    else:
        if is_start:
            is_start = False
            e_idx = i
            ans += (e_idx - s_idx + 1) // 2
            idx_list.append((s_idx, e_idx))
else:
    if is_start:
        e_idx = len(s) - 1
        ans += (e_idx - s_idx + 1) // 2
ans *= k
if len(s) == 1:
    ans = k // 2
elif s_cnt == len(s) and e_cnt == len(s):
    if len(s) % 2 == 1:
        ans += 1
elif s_cnt % 2 == 1 and e_cnt % 2 == 1 and s[0] == s[-1]:
    ans += k - 1
print(ans)

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
SS=list(S)
n=0
nn=0
#モノマー内の重複を修正。文字数が偶数なら同じことを繰り返すだけ
if len(S)%2==0:
    for i in range(len(S)-1):
        if SS[i]==SS[i+1]:
            SS[i+1]="?"
            n+=1
    if SS[0]==SS[-1]:
        n+=1
        nn-=1
    print(n*K+nn)
else: #文字数が奇数の場合、偶数回目と奇数回目で伏せる文字を変えた方が効率的なことがある
    SSS=list(S+S)
    for i in range(len(SSS)-1):
        if SSS[i]==SSS[i+1]:
            SSS[i+1]="?"
            n+=1
    if K%2==0: #Kが偶数なら末端の処理は不要
        if SSS[0]==SSS[-1]:
            n+=1
            nn-=1
        print(n*K//2+nn)
    else: #Kが奇数なら最後の1ループは例外的に計算
        for i in range(len(S)-1):
            if SS[i]==SS[i+1]:
                SS[i+1]="?"
                nn+=1
        print(n*(K//2)+nn)

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
code_list = 'abcdefghijklmnopqrstuvwxyz'

def c1(s):#len(s)>1
  count1=0
  S = list(s)
  l_s=len(S)
  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
          count1+=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
          count1+=1
          break
  return count1,S[-1]

def c2(s,last):#k>2 and len(s)>1
  S=list(s)
  count2=0
  l_s=len(S)
  for i in range(0,l_s):
    #print(S,count)
    if i==0 and S[0]==last:
      for j in code_list:
        if j != S[i+1] and j !=last:
          S[i]=j
          count2+=1
          break
    elif 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
          count2+=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
          count2+=1
          break
  return count2

def c3(s,last):#k>1 len(s)>1
  S=list(s)
  count3=0
  l_s=len(S)
  for i in range(0,l_s):
    #print(S,count)
    if i==0:
      if S[0]==last:
        for j in code_list:
          if j != S[i+1] and j !=last:
            S[i]=j
            count3+=1
            break
    elif 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
          count3+=1
          break
    elif S[i]==S[i-1]:
      for j in code_list:
        if j != S[i-1]:
          S[i]=j
          count3+=1
          break
  return count3

if len(s)==1:
  print(k//2)
elif k==1:
  c,_=c1(s)
  print(c)
elif k==2:
  count1,last=c1(s)
  count3=c3(s,last)
  print(count1+count3)
else:
  count1,last=c1(s)
  count2=c3(s,last)
  count3=c3(s,last)
  #print(count1,count2,count3)
  print(count1+count2*(k-2)+count3)

Submission status: Wrong Answer

Solution code:
s = list(input())
s2= s+s
n = int(input())
c1=0
ss ='abc'
add1=0
if len(s)>1:
    if len(s)%2==1:
        if s[0] == s[-1]:
            add1 = 1
for i in range(1,len(s)):
    # print(s[i],s[i-1])
    if(i+1<len(s)):
        if s[i-1]==s[i] and s[i]==s[i+1]:
            c1 += 1
            for v in ss:
                if v!=s[i]:
                    s[i]=v
                    break
        elif s[i - 1] == s[i]:
            c1 += 1
            for v in ss:
                if v != s[i -1] and v!=s[i+1]:
                    s[i] = v
                    break
    else:
        if s[i - 1] == s[i]:
            c1 += 1
            for v in ss:
                if v != s[i -1]:
                    s[i] = v
                    break

c2=0
add2=0
# print(s2)
if s2[0] == s2[-1] and s2[-1]!=s2[-2]:
    add2 = 1
for i in range(1,len(s2)):
    if(i+1<len(s2)):
        if s2[i-1]==s2[i] and s2[i]==s2[i+1]:
            for v in ss:
                if v!=s2[i]:
                    s2[i]=v
                    break
            c2+=1
        elif s2[i - 1] == s2[i]:
            for v in ss:
                if v != s2[i -1] and v!=s2[i+1]:
                    s2[i] = v
                    break
            c2+=1
    else:
        if s2[i - 1] == s2[i]:
            for v in ss:
                if v != s2[i -1]:
                    s2[i] = v
                    break
            c2+=1
if n==1:
    print(c1)
    exit()
elif(n==2):
    print(c2)
    exit()
res = 0
add=0
# print(s)
# print(s2)
# print('c1:',c1,'c2',c2)
if len(s)&1:
    run = n-1
    if run%2==0:
        time = run//2
        res= c2*time
        res+= add2*(time-1)
        res+=c1+add1
    else:
        time = n//2
        res = c2*time
        res += (time-1)*add2
else:
    if(n%2==0):
        time = n//2
        res=time*c2
        res+=add2*(time-1)
    else:
        time = n//2
        res=c2*time
        res+=(time-1)*add2
        res+=c1+add2


print(res)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline
S = list(input())[: -1]
ls = S * 2
K = int(input())
if len(S) == 1:
  print(K // 2)
  exit(0)
left = 0
mid = 0
right = 0
last = 0
i = 0
while i < len(S):
  if ls[i] == ls[i + 1]:
    left += 1
    if i == len(S) - 1:
      last = 1
    i += 1
  i += 1
i = 0 + last
while i < len(S):
  if ls[i] == ls[i + 1]:
    mid += 1
    i += 1
  i += 1
i = 0 + last
while i < len(S) - 1:
  if ls[i] == ls[i + 1]:
    right += 1
    i += 1
  i += 1
print(left + mid * max(K - 2, 0) + right)

Submission status: Wrong Answer

Solution code:
def count_changes(s, k, prev=None):
    count = 0
    for _ in range(k):
        for cur in s:
            if cur == prev:
                count += 1
                prev = None
            else:
                prev = cur
    return count, prev


if __name__ == '__main__':
    s = input()
    k = int(input())
    if k <= 3:
        print(count_changes(s, k)[0])
    else:
        base_count, prev = count_changes(s, 3)
        d, rest = divmod(k, 3)
        res = base_count * d
        if rest:
            res += count_changes(s, rest, prev)[0]
        elif s[0] == prev:
            res += (d - 1)
        print(res)

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:
        if s[0]==s[1] or s[1]==s[2]:
            print(k)
        else:
            print(k-1)
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:
            chain_hajime=0
            chian_owa=0
            for i in range(len(s)):
                if s[0]==s[i]:
                    chain_hajime+=1
                else: break
            for i in range(len(s)):
                if s[-1]==s[-(i+1)]:
                    chian_owa+=1
                else: break
            a=list(nchars(s, 2))
            b=0
            for i in a:
                b+=len(i)//2
            if chian_owa%2==1 and chain_hajime%2==1:
                print(b*k+k-1)
            else:
                print(b*k)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
t = s
cnt = 0
loop = 0
t = [i for i in t]
# if t[::-1][0] == t[0] and t[::-1][1] != t[0]:
#     cnt += 1
b = 0
while b < len(t) - 1:
    if len(list(set(t[b:]))) == len(t[b:]):
        break
    if t[b] == t[b + 1]:
        t[b + 1] = '?'
    b += 1
# aa = cnt*(k-1)+t.count("?")*k
print(t.count("?")*k)