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 k % 2 == 1:

    tmp = s + s + s

    c = [0]
    for i in range(1, len(tmp)):
        if tmp[i] == tmp[i-1] and c[-1] == 0:
            c.append(1)
        else:
            c.append(0)

    ans = 0
    ans += sum(c[:len(s)])
    ans += sum(c[2*len(s):])
    ans += sum(c[len(s):2*len(s)]) * (k - 2)
    print(ans)

if k % 2 == 0:

    tmp = s + s + s + s

    c = [0]
    for i in range(1, len(tmp)):
        if tmp[i] == tmp[i-1] and c[-1] == 0:
            c.append(1)
        else:
            c.append(0)

    ans = 0
    ans += sum(c[:len(s)])
    ans += sum(c[3*len(s):])
    ans += sum(c[len(s):3*len(s)]) * (k - 2)
    print(ans)

Submission status: Wrong Answer

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

pre_c = ''
flg = 0
count = 1

f_c = S[0]
f_e = S[-1]
t = []

for c in S:
  if c == pre_c:
    count += 1
  else:
    t.append(count)
    count = 1
  pre_c = c

t.append(count)

count_1 = 0
for i in t:
  count_1 += i // 2
    
if f_c == f_e:
  t[0] = t[0] + t[-1]
  t.pop(-1)
  
count_2 = 0
for i in t:
  count_2 += i // 2
  
print(count_1 + count_2 * (K - 1))

Submission status: Wrong Answer

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

a_li = []
b = len(S)
i = 0
j = 0
S.append(0)
x = 0

while i != b:
    a = 1
    while S[j] == S[j+1]:
        i += 1
        j += 1
        a += 1
    i += 1
    j += 1
    a_li.append(a)

if S[0] == S[-2]:
    if a_li[0]%2 == 1:
        if a_li[0]%2 == 1:
            x = K-1
scr = 0
for n in range(len(a_li)):
    scr += a_li[n] // 2

if b == 1:
    scr = K//2
    print(scr)
else:
    print(scr*K+x)

Submission status: Wrong Answer

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

tmp = s + s + s

c = [0]
for i in range(1, len(tmp)):
    if tmp[i] == tmp[i-1] and c[-1] == 0:
        c.append(1)
    else:
        c.append(0)

ans = 0
ans += sum(c[:len(s)])
ans += sum(c[2*len(s):])
ans += sum(c[len(s):2*len(s)]) * (k - 2)
print(ans)

Submission status: Wrong Answer

Solution code:
S = raw_input()
K = int(raw_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            

m = S[0]
f = True
for i in range(1, len(S)):
        if S[i]  != S[i-1]:
            f = False
            
if f == True:
    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:
    r = 0
    k = 0
    m = S[0]
    for i in range(1, len(S)):
        if S[i] == S[i-1]:
            k+=1
        else:
            r += int((k+1)/2)
            k = 0
        m = S[i]
    r += int((k+1)/2)
    print(r*K)

Submission status: Wrong Answer

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

lens = len(s)

cnt = 0
ans = 0

sig = lens % 2

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

ans = cnt * k

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

if lens == 1:
  ans = k//2

print(ans)

Submission status: Wrong Answer

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

l = len(s)
flag = 0
for i in range(1,l):
    if(s[i]!=s[i-1]):
        flag=1


flag2 = 0
cnt = 0

if(flag==0 and l%2==1):
    a = (l*k)//2
    print(a)
else:
    if(l==1):
        print(k//2)
    else:
        i=1
        while(i<l):
            if((s[i]==s[i-1])):
                cnt+=1
                i+=2
            else:
                i+=1
        
        if(s[0]==s[l-1] and s[l-1]!=s[l-2] and s[0]!=s[1]):
            flag=1


        if(flag2==1):
            if(k==1):
                print(cnt)
            else:
                print(cnt*k+k-1)
        else:
            print(cnt*k)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
cnt = 0
lenS = len(s)
ans = 0
if lenS == 1:
    ans = int(k//2)
else:
    for i in range(0, lenS - 1):
        if s[i] == s[i + 1]:
            s[i + 1] = "1"
            ans += 1
    ans *= k
    if s[0] == s[lenS-1]:
        ans += k - 1
print(ans)

Submission status: Wrong Answer

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

a = 0
c = 1
for i in range(1,len(S)) :
    if S[i] == S[i-1] :
        c += 1
    else :
        a += c//2
        c = 1

if S[0] == S[-1] :
    a += 1
print(a*N)

Submission status: Wrong Answer

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

groups = []
char = ''
i=0
j=0
while i<len(S) and j<len(S)-1:
    j+=1
    if S[i]==S[j]:
        char = S[i]
    else:
        if j==i+1:
            i=j
            continue
        groups.append((i,j-1))
        char=''
        i = j
if j!=i:
    groups.append((i,j))

ans = 0
temp = 0
temp2 = []
for j in range(len(groups)):
    first = groups[j][0]
    end = groups[j][1]
    if first==0 or end==len(S)-1:
        temp += end-first+1
        temp2.append((first,end))
    else:
        ans += (end-first+1)//2*K

for j in range(len(temp2)):
    first = temp2[j][0]
    end = temp2[j][1]
    ans += (end-first+1)//2

ans += temp//2*(K-1)

print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
cnt = 0
lenS = len(s)
ans = 0
if lenS == 1:
    ans = int(k/2)
else:
    for i in range(0, lenS - 1):
        if s[i] == s[i + 1]:
            s[i + 1] = "1"
            ans += 1
    ans *= k
    if s[0] == s[lenS-1]:
        ans += k - 1
print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
m=0
j=0
if len(set(list(s)))==1:print((len(s)*k)//2);exit()
for i in range(len(s)):
 if len(s)<=j+1:
  a=m*k
  if s[0]==s[len(s)//3]:print(a+k-1)
  else:print(a)
  break
 if s[j]==s[j+1]:
  m+=1
  j+=1
 j+=1

Submission status: Wrong Answer

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

def len_checker(S,r):
    count = 1
    if r == 0:
        icon = S[0]
        for j in range(1,len(S)):
            if S[j] == icon:
                count += 1
            else:
                break
    elif r == 1:
        icon = S[len(S)-1]
        for j in range(1,len(S)):
            jj = len(S) - j -1
            if S[jj] == icon:
                count += 1
            else:
                break
#     print(S,r,count)
    return(count)

def s_change(S,K):
    s_henkan = 0
    L = len(S) -1
#     print(S,S[0],S[L])
    if K == 1:
        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
    else:
        if S[0] == S[L]:
            cf = len_checker(S,0)
            rf = len_checker(S,1)
    #         print("cf:",cf,"rf:",rf)
            if cf % 2 == 1 and rf % 2 == 0:
                S[L] = "2"
                s_henkan += 1
    #             pass
            else:
                S[0] = '2'
                s_henkan += 1
    #     print("mit:",S)
        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:
    SSSS = s_change(S,K)
#     print("SSSS: ",SSSS)
    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:
S = list(input())
K = int(input())

a_li = []
b = len(S)
i = 0
j = 0
S.append(0)
x = 0

while i != b:
    a = 1
    while S[j] == S[j+1]:
        i += 1
        j += 1
        a += 1
    i += 1
    j += 1
    a_li.append(a)

if S[0] == S[-2]:
    if a_li[0] >= 2 and (a_li[0]%2 == 1):
        if a_li[0] >= 2 and (a_li[0]%2 == 1):
            x = K-1
scr = 0
for n in range(len(a_li)):
    scr += a_li[n] // 2

if b == 1:
    scr = K//2
    print(scr)
else:
    print(scr*K+x)

Submission status: Wrong Answer

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

last = ''
cnt_diff = 0
for s in S:
    if(s==last):
        cnt_diff+=1
        last='0'
    else:
        last=s
if(S[0]==S[-1] and last!='0'):
    if(K==1):
        print(cnt_diff)
    elif(K==2):
        print(cnt_diff*2+1)
    else:
        print(cnt_diff*2+(cnt_diff+1)*(K-2))
else:
    print(cnt_diff*K)

Submission status: Wrong Answer

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

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

# If the first and last characters are the same
if a[0] == a[-1]:
  # If the string repeats but does not contain any consecutive characters, e.g aba*2
  if (i==0) and j>1: 
    print(j-1)
  # If the string repeats and contains consecutive characters e.g aabba
  else: 
    print(i*j+(j-1))
# If the first and last characters are different e.g ab, abc, abba
else:
  print(i*j)


#aAaAaA

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]

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

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

print(point)

Submission status: Wrong Answer

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

c = []
tmp = 1
pre = s[0]
for si in s[1:]:
    if pre != si:
        c.append(tmp)
        pre = si
        tmp = 1
    else:
        tmp += 1
c.append(tmp)
ans = 0
for ci in (ci for ci in c if ci > 1):
    ans += ci // 2

print(ans * k)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
i= 0
list1 = []
while True:
    j = i+1
    cout = 1
    while True:
        if j > len(s)-1:
            break
        elif s[i] == s[j]:
            cout += 1
            j += 1
        else:
            i += cout
            break
    list1.append(cout)
    cout = 0
    if j > len(s)-1:
        break
mid = 0
if len(list1)==1:
    if list1[0] % 2 == 0:
        print(list1[0]//2*k)
        exit()
    else:
        if k == 1:
            print(list1[0]//2*k)
            exit()
        print(list1[0]//2*k+1)
        exit()
if s[0] == s[-1]:
    kasanari = (list1[0]+list1[-1])//2*(k-1)
    first = list1[0]//2
    del list1[0]
    last = list1[-1]//2
    del list1[-1]
    for i in range(len(list1)):
        if list1[i] >= 2:
            mid += list1[i]//2
    mid = mid*k
    print(kasanari+first+last+mid)

else:
    for i in range(len(list1)):
        if list1[i] >= 2:
            mid += list1[i]//2
    mid = mid*k
    print(mid)

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]

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

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

print(str(point-1))

Submission status: Wrong Answer

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

last = ''
cnt_diff = 0
for s in S:
    if(s==last):
        cnt_diff+=1
        last='0'
    else:
        last=s
if(S[0]==S[-1] and last!='0'):
    print(cnt_diff+(cnt_diff+1)*(K-1))
else:
    print(cnt_diff*K)

Submission status: Wrong Answer

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

if len(S) == 1:
    result = K // 2 
    print(result)
else:
    count = 0
    S = S + "0"

    i = 0
    while i < len(S)-2:
        if S[i] == S[i+1]:
            count += 1
            i += 2
            """
            if S[i] == S[i+2]:
                i += 2
            else:
                i += 2
            """
        else:
            i += 1
    
    S = S[:-1] #最後の0を削除
    
    if len(S) == 2:
        result = count * K
    else: #S[0]からの連続文字列数とS[-1]からの連続文字列数がどちらも奇数ならば「+ 1 × (K-1)」が必要
        if S[0] == S[-1]:
            len_first = 1
            len_last = 1
            i = 0
            flag = 0
            while S[i] == S[i+1]:
                len_first += 1
                i += 1
                if i == len(S)-1:
                    flag = 1
                    len_last = len_first
                    break
                
            if flag == 0: 
                len_last = 1
                i = -1
                while S[i] == S[i-1]:
                    len_last += 1
                    i -= 1
            
            #print(len_first)
            #print(len_last)

            if len_first % 2 == 1 and len_last % 2 == 1:
                result = count * K + (K-1)
            else:
                result = count * K
        else:
            result = count * K
    
    
    print(result)

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 len(set(s)) == 1:
      print(k // 2)
      exit()
    if k <= 10 ** 8:
        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[-1]==s[-(i+1)]:
            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:
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 <= 10 ** 8:
        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())
 
ans = 0
length = len(s)
 
if length == 1:
  ans = k // 2
elif length == 2:
  if s[0] == s[1]:
    ans = k
else:
  sep = 0
  i = 0
  last = 0
  firstchar = s[0]
  is_one_char = True
  while i < length-1:
    if firstchar != s[i]:
      is_one_char = False
    
    if s[i] == s[i+1]:
      sep += 1
      last = i
      i += 1
    i += 1
  
  if is_one_char:
    ans = (length // 2) * k
    if length % 2 != 0:
      ans += k // 2
  else:
    ans = sep * k
    if last != length-2:
      lastchar = s[length-1]
      lastchar_count = 0
      j = 0
      while j < length:
        if lastchar == s[j]:
          lastchar_count += 1
          j += 1
        break
      if lastchar_count % 2 == 1:
          ans += k-1
 
print(ans)

Submission status: Wrong Answer

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

last = ''
cnt_diff = 0
for s in S:
    if(s==last):
        cnt_diff+=1
        last='0'
    else:
        last=s
print(cnt_diff*K)

Submission status: Wrong Answer

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

if len(S) == 1:
    result = K // 2 
else:
    count = 0
    S = S + "0"

    i = 0
    while i < len(S)-2:
        if S[i] == S[i+1]:
            count += 1
            i += 2
            """
            if S[i] == S[i+2]:
                i += 2
            else:
                i += 2
            """
        else:
            i += 1
    
    S = S[:-1] #最後の0を削除
    
    if len(S) == 2:
        result = count * K
    else: #S[0]からの連続文字列数とS[-1]からの連続文字列数がどちらも奇数ならば「+ 1 × (K-1)」が必要
        if S[0] == S[-1]:
            len_first = 1
            len_last = 1
            i = 0
            flag = 0
            while S[i] == S[i+1]:
                len_first += 1
                i += 1
                if i == len(S)-1:
                    flag = 1
                    len_last = len_first
                    break
                
            if flag == 0: 
                len_last = 1
                i = -1
                while S[i] == S[i-1]:
                    len_last += 1
                    i -= 1
            
            #print(len_first)
            #print(len_last)

            if len_first % 2 == 1 and len_last % 2 == 1:
                result = count * K + (K-1)
            else:
                result = count * K
        else:
            result = count * K
    
    
    print(result)

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 len(set(list(s))) == 1:
      print((len(s) * k) // 2)
      exit()
    if k <= 10 ** 8:
        print(count_changes(s, k)[0] + 1)
    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 = list(input())
K = int(input())
L = len(S)

sum = 0
x = 1
if L == 1:
  print(K//2)
elif L == 2 and S[0] == S[1]:
  print(K)
elif L == 2:
  print(K*2)
elif S[0] != S[-1]:
  for i in range(0, L-1):
    if S[i] == S[i+1]:
      x += 1
    else:
      sum += (x // 2)*K
      x = 1
  sum += (x // 2)*K
  print(sum)
else:
  for i in range(0, L-1):
    if S[i] == S[i+1]:
      x += 1
    else:
      sum += (x // 2)
    break
  y = x
  x = 1
  for i in range(y, L-1):
    if S[i] == S[i+1]:
      x += 1
    else:
      sum += (x // 2)*K
      x = 1
  sum += (x // 2)
  sum += ((x+y) // 2) * (K-1)
  print(sum)

Submission status: Wrong Answer

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

def solve():
    S = input()
    l = len(S)
    K = int(input())
    SK = S*min(2,K)
    swap_count = [0,0]
    T =  ''
    if l == 1:
        res = K//2
    elif l == 2 and K == 1:
        res = int(S[0]==S[1])
    else:
        s1, s2, s3 = '-', S[0], S[1]
        for i,s in enumerate(SK[2:]+'--', start=1):
            s1, s2, s3 = s2, s3, s
            if s3 and s1==s2:
                s2 = '='
                swap_count[i//l] += 1
            T += s1
        res = swap_count[0] + max(0, K-1)*swap_count[1]
    if l <= 2:
        return str(res)
    else:
        print(T)
    

if __name__ == '__main__':
    print(solve())

Submission status: Wrong Answer

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

def solve():
    S = input()
    l = len(S)
    K = int(input())
    SK = S*min(2,K)
    swap_count = [0,0]
    T =  ''
    if l == 1:
        res = K//2
    elif l == 2 and K == 1:
        res = int(S[0]==S[1])
    else:
        s1, s2, s3 = '-', S[0], S[1]
        for i,s in enumerate(SK[2:]+'--', start=1):
            s1, s2, s3 = s2, s3, s
            if s3 and s1==s2:
                s2 = '='
                swap_count[i//l] += 1
            T += s1
        res = swap_count[0] + max(0, K-1)*swap_count[1]
    if l >= 2:
        return str(res)
    else:
        print(T)
    

if __name__ == '__main__':
    print(solve())

Submission status: Wrong Answer

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

count = 0
S = S + "0"


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

S = S[:-1] #最後の0を削除

if len(S) == 1:
    result = K // 2 
elif len(S) == 2:
    result = count * K
else: #S[0]からの連続文字列数とS[-1]からの連続文字列数がどちらも奇数ならば「+ 1 × (K-1)」が必要
    if S[0] == S[-1]:
        len_first = 1
        len_last = 1
        i = 0
        flag = 0
        while S[i] == S[i+1]:
            len_first += 1
            i += 1
            if i == len(S)-1:
                flag = 1
                len_last = len_first
                break

        if flag == 0: 
            len_last = 1
            i = -1
            while S[i] == S[i-1]:
                len_last += 1
                i -= 1
        
        #print(len_first)
        #print(len_last)
        
        
        if len_first % 2 == 1 and len_last % 2 == 1:
            result = count * K + (K-1)
        else:
            result = count * K
    else:
        result = count * K


print(result)

Submission status: Wrong Answer

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

S = S[::-1]

S = list(S)

S_one = copy(S)

c_one = 0

for i in range(1,len(S_one)):
    if S_one[i-1] == S_one[i]:
        S_one[i] = '-'
        c_one += 1


S_two = copy(S) + copy(S)


c_two = 0

for i in range(1,len(S_two)):
    if S_two[i-1] == S_two[i]:
        S_two[i] = '-'
        c_two += 1




if S_two[len(S_two)-1] == S_two[0] and K%2 == 0:
    print(c_two*math.floor(K/2) + math.floor(K/2) - 1)

elif S_two[len(S_two)-1] == S_two[0] and K%2 == 1:
    print(c_two*math.floor(K/2) + math.floor(K/2) + c_one)

else:
    if K%2 == 0:
        print(c_two*math.floor(K/2))
    else:
        print(c_two*math.floor(K/2) + c_one)

Submission status: Wrong Answer

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

sum = 0
x = 1
if L == 1:
  print(K//2)
elif L == 2 and S[0] == S[1]:
  print(K)
elif S[0] != S[-1]:
  for i in range(0, L-1):
    if S[i] == S[i+1]:
      x += 1
    else:
      sum += (x // 2)*K
      x = 1
  sum += (x // 2)*K
  print(sum)
else:
  for i in range(0, L-1):
    if S[i] == S[i+1]:
      x += 1
    else:
      sum += (x // 2)
    break
  y = x
  x = 1
  for i in range(y, L-1):
    if S[i] == S[i+1]:
      x += 1
    else:
      sum += (x // 2)*K
      x = 1
  sum += (x // 2)
  sum += ((x+y) // 2) * (K-1)
  print(sum)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
i= 0
list1 = []
while True:
    j = i+1
    cout = 1
    while True:
        if j > len(s)-1:
            break
        elif s[i] == s[j]:
            cout += 1
            j += 1
        else:
            i += cout
            break
    list1.append(cout)
    cout = 0
    if j > len(s)-1:
        break
mid = 0
if len(list1)==1:
    if list1[0] % 2 == 0:
        print(list1[0]//2*k)
        exit()
    else:
        print(list1[0]//2*k+1)
        exit()
if s[0] == s[-1]:
    kasanari = (list1[0]+list1[-1])//2*(k-1)
    first = list1[0]//2
    del list1[0]
    last = list1[-1]//2
    del list1[-1]
    for i in range(len(list1)):
        if list1[i] >= 2:
            mid += list1[i]//2
    mid = mid*k
    print(kasanari+first+last+mid)

else:
    for i in range(len(list1)):
        if list1[i] >= 2:
            mid += list1[i]//2
    mid = mid*k
    print(mid)

Submission status: Wrong Answer

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

def solve():
    S = input()
    l = len(S)
    K = int(input())
    SK = S*min(2,K)
    swap_count = [0,0]
    T =  ''
    if l == 1:
        res = K//2
    elif l == 2 and K == 1:
        res = int(S[0]==S[1])
    else:
        s1, s2, s3 = '-', S[0], S[1]
        for i,s in enumerate(SK[2:]+'--', start=1):
            s1, s2, s3 = s2, s3, s
            if s3 and s1==s2:
                s2 = '='
                swap_count[i//l] += 1
            T += s1
        print(T)
        res = swap_count[0] + max(0, K-1)*swap_count[1]
    return str(res)
    

if __name__ == '__main__':
    print(solve())

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
s = list(input())
k = int(input())
n = len(s)
t = s * 3
for i in range(n * 3 - 1):
    if t[i + 1] == 'X':
        continue
    if t[i] == t[i + 1]:
        t[i + 1] = 'X'
ans = t[0:n].count('X') + t[n:2 * n].count('X') * (k - 2) + t[2 * n:3 * n].count('X')
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
k = input()
w = s * 2
t = s * 3

s = list(s)
w = list(w)
t = list(t)
ans_s = 0
ans_w = 0
ans_t = 0
for i in range(0, len(s)-1):
    if s[i] == s[i+1]:
        ans_s += 1
        s[i+1] = "0"

for i in range(0, len(w)-1):
    if w[i] == w[i+1]:
        ans_w += 1
        w[i+1] = "0"

for i in range(0, len(t)-1):
    if t[i] == t[i+1]:
        ans_t += 1
        t[i+1] = "0"

if len(s) == 1:
    ans_s = int(k) // 2
elif (ans_s*2) == ans_w:
    ans_s *= int(k)
elif (ans_s*3 + 2) != ans_t:
    ans_s = ans_s*int(k) + int(k) - 2
else:
    ans_s = ans_s*int(k) + int(k) - 1


print(ans_s)

Submission status: Wrong Answer

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

if l == 1:
    print(K // 2)
    exit()

if l == 2:
    if S[0] == S[1]:
        print(K)
    else:
        print(0)
    exit()



else:
    ans = 0
    prev = None
    edge = False
    for i, v in enumerate(S):
        if prev == v:
            ans += 1
            if i == 0 or i == l - 1:
                edge = True
            prev = None
        else:
            prev = v

    if S[0] != S[-1] or edge:
        print(ans * K)
    else:
        ans2 = 1
        prev = None
        edge2 = False
        for i, v in enumerate(S):
            if i == 0:
                continue
            elif prev == v:
                ans2 += 1
                if i == l - 1:
                    edge2 = True
                prev = None
            else:
                prev = v

        ans3 = 1
        prev = None
        edge3 = False
        for i, v in enumerate(S[::-1]):
            if i == 0:
                continue
            elif prev == v:
                ans3 += 1
                if i == l - 1:
                    edge3 = True
                prev = None
            else:
                prev = v

        if ans2 == ans3:
            print((ans + ans2) * (K // 2) + min(ans, ans2) * (K % 2))
        else:
            print('type3')
            print(min(ans2 * K, ans3 * K))

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
first = s[0]
last = s[-1]
first_num = 0
last_num = 0
now_num = 0
now = s[0]
ans = 0
for i in s:
  if i == first:
    first_num += 1
  else:
    break
for i in reversed(s):
  if i != last:
    break
  last_num += 1
for i in s:
  if i == now:
    now_num += 1
  else:
    ans += now_num // 2
    now_num = 1
  now = i
ans += now_num // 2
ans *= k
if first == last:
  if first_num % 2 and last_num % 2:
    ans += k - 1
print(ans)

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 <= 10 ** 8:
        print(count_changes(s, k)[0] + 1)
    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())
d=[[0 for _ in range(400)] for _ in range(4)]
ans = [0,0,0,0]
l = len(S)
odd_diff = 0
even_diff = 0
if l == 1:
    print(int(K//2))
if l > 1:
    for i in range(4):
        s = S * (i + 1)
        l = len(s)
        for j in range(l-1):
            
            if j == 0 and s[j] == s[j+1]:
                ans[i] += 1
                d[i][0] = 1
            if j > 0 and s[j] == s[j+1] and d[i][j-1] == 0:
                ans[i] += 1
                d[i][j] = 1
    if K <= 4:
        print(int(ans[K-1]))
    elif K > 4 and K % 2 == 0:
        even_diff = ans[3] - ans[1]
        print(int(ans[1]+(K/2)*even_diff))
    elif K > 4 and K % 2 == 1:
        odd_diff = ans[2] - ans[0]
        print(int(ans[0]+(K//2)*odd_diff))