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:
def slove():
    import sys
    input = sys.stdin.readline
    ss = str(input().rstrip('\n'))
    nn = int(input().rstrip('\n'))
    s = 0
    sb = True
    e = 0
    eb = True
    t = 0
    cnt = 0
    if len(ss) == 1:
        print(len(ss) * nn // 2)
        exit()
    else:
        if ss[0] == ss[-1]:
            for i in range(len(ss)):
                if ss[i] == ss[0]:
                    if sb:
                        s += 1
                else:
                    sb = False
                if ss[-i-1] == ss[0]:
                    if eb:
                        e += 1
                else:
                    eb = False
        else:
            sb = False
            eb = False
        if sb and eb:
            if s % 2 == 0:
                cnt += ((s + e) // 2 // 2)
                s = 0
                e = 0
            else:
                print(s * nn // 2)
                exit()
        else:
            for i in range(max(s, 1), min(len(ss) - e + 1, len(ss))):
                if ss[i] == ss[i-1]:
                    t += 1
                else:
                    t += 1
                    cnt += (t // 2)
                    t = 0
            cnt += (t // 2)
    print(cnt * nn + s // 2 + e // 2 + (s + e) // 2 * (nn - 1))


if __name__ == '__main__':
    slove()

Submission status: Wrong Answer

Solution code:

def hoge():
    rawstring = input()
    string = rawstring + rawstring[0] + '0'
    num = int(input())
    count = 0
    total_count = 0
    for i in range(len(string) - 1):
        if string[i] == string[i+1]:
            count += 1
        else:
            if count != 0:
                total_count += (count + 1) // 2
                count = 0

    # 先頭と末尾が同じ、かつ末尾が奇数個同じな場合は、1回減らせる
    if rawstring[0] == rawstring[-1]:
        for i in range(len(rawstring) - 1, 1, -1):
            if rawstring[i] == rawstring[i-1]:
                count += 1
            else:
                break
    if (count + 1) % 2 == 1:
        total_count = total_count * num - 1
    else:
        total_count = total_count * num

    print(total_count)

hoge()

Submission status: Wrong Answer

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

mycnt = 0
myflag = 0

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

else:
    for i in range(len(S)-1):
        if S[i] == S[i+1] and myflag == 0:
            mycnt += 1
            myflag = 1
        else: myflag = 0
        
    mycnt *= K
    
    if S[0] == S[-1] and S[0] != S[1]:
        if myflag == 0:
            mycnt += (K-1)
    
    #if S[0] == S[-1] and myflag ==0:
    #    mycnt += (K-1)

    print(mycnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
    S = input()
    K = int(input())
    a = list(S)
    cnt = 0
    if len(set(a))==1 and len(a) != 1:
        cnt = (len(a)*K)//2
    elif len(a) ==1:
        cnt += K//2
    
    elif a[0]==a[1] and a[0]==a[-1]:
        a[0] = "tkyg"
        cnt += 1
        for i in range(len(a)-1):
            if a[i+1] == a[i]:
                a[i+1] = str(i)
                cnt += 1
        cnt *= K
        if a[0] == a[-1] and len(a) > 1:
            cnt += K-1

    
    
    else:

        for i in range(len(a)-1):
            if a[i+1] == a[i]:
                a[i+1] = str(i)
                cnt += 1
        cnt *= K
        if a[0] == a[-1] and len(a) > 1:
            cnt += K-1
    print(cnt)

    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

ans = 0
now = 0
flag = 1

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

for i in range(len(s)):
    #print(i,now)
    if now >= len(s) - 1:
        break
    if s[now] == s[now+1]:
       ans += 1
       now += 2
       if now+1 == len(s) -1:
           flag = 0
    else:
        now += 1

if len(s) == 2:
    print(ans*k)
    exit()

if flag == 1:
    if s[0] == s[-1]:
        if s[0] == s[1]:
            flag = 2
        else:
            flag = 1
    else:
        flag = 0
        
if flag != 2:
    print(ans*k+flag*(k-1))
else:
    print(ans*k+k//2)
#print(ans,ans*k,ans*k+flag*(k-1))

Submission status: Wrong Answer

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

n = len(s)
cnt = [0]

for i in range(0,n-1):
    if s[i] == s[i+1]:
        cnt[-1] += 1
    else:
        if cnt[-1] != 1:
            cnt.append(1)

ren = 0
for i in cnt:
    ren += int(i // 2)

b=0
if s[0] == s[-1]:
    if (cnt[-1] % 2) == 0:
        b += k - 1

ans = ren * k + b

print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
n = len(s)
K = int(input())
t = list(s*4)
s = list(s)
m = len(t)
cnt = 0
c = 0
for i in range(1, m):
    if t[i] == t[i-1]:
        t[i] = 'A'
        cnt+=1
for i in range(1, n):
    if s[i] == s[i-1]:
        s[i] = 'A'
        c+=1
res = c+(cnt-c)*(K-1)//3
if n == 1:
    res = K//2
print(res)

Submission status: Wrong Answer

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

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

flag = False
if s[0] == s[-1]:
#    print("flag")
    flag = True
else:
    pass

#sの中だけ考える
count = 0
dp = ""
for i in range(len(s)):
    if dp == s[i]:
        count += 1
        dp = ""
    else:
        dp = s[i]
ans = count * k


if flag:
    try:
        #もし後ろ３つが同じなら
#        print("here")
        if len(set([s[-1],s[-2],s[-3]])) == 1:
            if set([s[0],s[1]]) == 1:
                pass
            else:
                ans = ans + (k-1)
    except IndexError:
        pass
else:
    pass

print(ans)

Submission status: Wrong Answer

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

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

before = ""
cnt = 0
idx = 0
flg = False
for s in S:
    if before == s:
        cnt+=1
        before = ""
    else:
        before = s
    idx += 1
cnt *= K
if len(S) >= 3 and S[0] == S[-1] == S[-2] == S[-3]:
    cnt += K -1


print(cnt)

Submission status: Wrong Answer

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

if len(set(S)) == 1:
    print((len(S) * K) // 2)
else:
    cnt = 0
    last = 0
    for i, s in enumerate(S):
        if i == 0 and S[-1] == s != S[-2]:
            S[i] = '_'
            cnt += 1
        elif S[(i + 1) % len(S)] == S[(i + 2) % len(S)] == s:
            S[(i + 1) % len(S)] = '_'
            cnt += 1
        elif S[(i + 1) % len(S)] == s:
            S[i] = '_'
            cnt += 1
            if i == len(S) - 1:
                last = 1
    print(cnt * K - last)

Submission status: Wrong Answer

Solution code:
import string


def count_changes(s, k, pool, prev=None):
    if k == 0:
        return 0, None, None
    t = s * k
    count = 0
    i = 0
    for cur in t:
        if cur == prev:
            count += 1
            prev = pool[i]
            i = (i + 1) % length
        else:
            prev = cur
    return count, i, prev


if __name__ == '__main__':
    s = input()
    k = int(input())
    pool = list(set(string.ascii_lowercase) - set(s))
    length = len(pool)
    if k <= 2:
        print(count_changes(s, k, pool)[0])
    else:
        base_count, i, prev = count_changes(s, 3, pool)
        d, rest = divmod(k, 3)
        print(base_count * d + count_changes(s, rest, pool, prev)[0])

Submission status: Wrong Answer

Solution code:
def count(S):
    ans = 0
    i = 1
    while i < len(S):
        if S[i-1] == S[i]:
            i += 1
            ans += 1
        i += 1
    return ans

def main():
    S = input()
    K = int(input())

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

    if K == 1:
        print(count(S))
        exit()

    ans = count(S*2)
    ans *= K // 2
    if K % 2 == 1:
        ans += count(S)

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
sum_count = 0
count = 1
key = s[0]
flag = True
if s[0] == s[len(s)-1] and s[len(s)-2] != s[len(s)-1]:
    flag = True
    for i in range(1,len(s)):
        if s[i] == key:
            count += 1
        else:
            sum_count += count//2
            key = s[i]
            count = 1
else:
    flag = False
    for i in range(1,len(s)):
        if s[i] == key:
            count += 1
        else:
            sum_count += count//2
            key = s[i]
            count = 1

sum_count += count//2

if flag == True:
    print((sum_count)*k+1)
else:
    print(sum_count*k)

Submission status: Wrong Answer

Solution code:
from itertools import groupby

S = input()
N = int(input())

if len(S) == 1:
    print(N-1)
    exit()

if len(S) == 2 and S[0] == S[1]:
    print(N)
    exit()

check = S
check_num = sum(len(list(v))//2 for k, v in groupby(check))
check2 = S * 2
check2_num = sum(len(list(v))//2 for k, v in groupby(check2))
check3 = S * 3
check3_num = sum(len(list(v))//2 for k, v in groupby(check3))

ans = check_num*N + (check2_num-2*check_num)*(N-1)
ans3 = check_num*3 + (check2_num-2*check_num)*(3-1)
if check3_num == ans3:
    print(ans)
else:
    print(round(N/2)*check_num + N//2*(check2_num-check_num))

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.readline
S = [ord(s)-97 for s in readline().strip()]
K = int(readline())

if len(S) == 1:
    Ans = K//2
else:
    dp = [1]*26
    dp[S[0]] = 0
    inf = 10**9
    
    for s in S[1:]:
        dp2 = [inf]*26
        for i in range(26):
            for j in range(26):
                if i == j:
                    continue
                dp2[i] = min(dp2[i], 1+dp[j])
        
        for j in range(26):
            if s == j:
                continue
            dp2[s] = min(dp2[s], dp[j])
        
        dp = dp2[:]
    
    ans = inf
    for i in range(26):
        if i == S[0]:
            continue
        ans = min(ans, dp[i])
    
    ma = min(dp)
    if ma == ans:
        Ans = ans*K
    else:
        Ans = ma*(-(-K//2)) + ans*(K//2)

print(Ans)

Submission status: Wrong Answer

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

import re


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

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

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
m = 0
n = 0
j = 0
for i in range(len(s)):
    if s.count(s[0])==len(s):
        print(len(s)*k//2)
        exit()
    if len(s) <= j:
        break
    if s[j] == s[j - 1]:
        if j == 0:
            n += 1
            j += 1
        else:
            if len(s) > 2:
                m += 1
                j += 1
    j += 1
print(m * k + n * (k - 1))

Submission status: Wrong Answer

Solution code:
def check_subseq(s):
    subseq_list = []
    tmp_length = 1
    for i in range(1, len(s)):
        if s[i - 1] == s[i]:
            tmp_length += 1
        else:
            if tmp_length >= 2:
                subseq_list.append(tmp_length)
            tmp_length = 1
        
    if tmp_length >= 2:
        subseq_list.append(tmp_length)

    return subseq_list


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

if s[0] == s[-1]:
    if len(set(s)) == 1:
        # print(len(s) * k // 2)
        exit()

    i = 1
    while True:
        if s[i] != s[-1]:
            first_subseq_length = i
            break
        i += 1

    i = 1
    while True:
        if s[-(i + 1)] != s[0]:
            last_subseq_length = i
            break
        i += 1

    # print(first_subseq_length, last_subseq_length)
    s = s[first_subseq_length:] + s[:first_subseq_length]
    subseq_list = check_subseq(s)
    ans = sum(map(lambda x: x // 2, subseq_list)) * k
    ans += first_subseq_length // 2 + last_subseq_length // 2 - subseq_list[-1] // 2
else:
    subseq_list = check_subseq(s)
    ans = sum(map(lambda x: x // 2, subseq_list)) * k
print(ans)

Submission status: Wrong Answer

Solution code:
def d_c(s):
  count = 0
  x = ''
  for i in range(len(s)):
    if x == s[0]:
      count += 1
      x = ''
    else:
      x = s[0]
    s = s[1:]
  return count

def num_con(s):
  x = s[0]
  s = s[1:]
  count = 1
  for i in range(len(s)):
    if s[0] == x:
      count += 1
      x = s[0]
      s = s[1:]
    else:
      break
  return count
      
    
s = input()
k = int(input())

if len(s)==1:
  print(k//2)
elif s[0]==s[-1]:
  f = num_con(s)
  m = num_con(s[::-1])
  if f%2==1 and m%2==1:
    print(d_c(s)*k + k - 1)
  else:
    print(d_c(s)*k)
else:
  print(d_c(s)*k)

Submission status: Wrong Answer

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

n = len(S)
s = S[0]
c = 0

if S.count(s) == n:
  print((n*K)//2)
  exit()
else:
  head = 1
  for i in range(1, n):
    if S[i] == s:
      head += 1
    else:
      S = S[i:]
      break
  if S[-1] == s:
    tail = 1
    for i in range(-2, -n, -1):
      if S[i] == s:
        tail += 1
      else:
        S = S[:i+1]
        break
    c += K*((head + tail)//2)
  start = 0
  s = S[start]
  S += 'X'
  for i in range(1, len(S)):
    if S[i] != s:
      c += K*((i - start)//2)
      start = i
      s = S[start]
print(c)

Submission status: Wrong Answer

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

count = 0

if len(s) == 2 and s[0] == s[1]:
    count = k
elif len(s) == 3 and s[0] == s[1] and s[2] == s[1]:
 	count = k + 1
else:
    for i in range(int(len(s)/2)):
        tmp = s[i*2:i*2+3]
        if len(tmp) == 2:
        	if tmp[0] == tmp[1]:
	            count+=1
        elif tmp[0] == tmp[1] or tmp[2] == tmp[1]:
            count+=1

    if len(s)%2==1:
        count = count*k
    elif len(s)%2==0:
        count = (count-1)*k

print(count)

Submission status: Wrong Answer

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

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

for i in range(len(S)):
    if S[i]==S[i-1]:
        if i%2==0:
            S[i]="?"
        else:
            S[i]="!"
        count+=1
if S[-1]==S[0]:
    print("a")
    print(count*K-2)
else:
    if K==1:
        print(count*K-1)
    else:
        print(count*K)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
count = 0
answer = 0
prev = ""
for index,sss in enumerate(s):
    if prev == "" or sss == prev:
        count+=1
        if count == 2: 
            answer+=1
            count=0
            s[index] = "○"
    else:
        count=1
    prev = sss

if s[0] == s[-1]:
    print(answer*k+k-1)
else:
    print(answer*k)

Submission status: Wrong Answer

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

count=0
end=True
first_count=1

if k==1:
  for i in range(1,len(s)):
    if s[i-1]==s[i]:
      s[i]=1
      count+=1
  print(count)
else:
  if s[0]==s[-1]:
    for i in range(1,len(s)):
      if s[i-1]==s[i]:
        first_count+=1
      else:
        break     
  
  for i in range(1,len(s)):
    if s[i-1]==s[i]:
      s[i]=1
      count+=1
      if i==len(s)-1:
        end=False

  if end and s[0]==s[-1]:
    if first_count%2 != 0:
    	count+=1
    	print(k*count-1)
    else:
      print(k*count)
  else:
    print(k*count)

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]

    next = S[1]
    flg = 0
    cnt = 0
    cnt_list2 =[]
    for i in range(0, len(S)-2):
        cnt += 1
        if flg == 1:
            flg = 0
            next = S[i+2]
            continue
        if S[i] == next:
            cnt_list2.append(cnt)
            flg = 1
        next = S[i+2]

    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=input()
t=s*2
k=int(input())
c,d=1,0
if len(s)==1:
    print(k//2)
else:
    for i in range(1,len(s)):
        if s[i-1]==s[i]:
            c+=1
        else:
            d+=c//2
            c=1
    d+=c//2
    l,r,p,q=t[0],t[-1],0,0
    while s[0]==l:
        s=s[1:]
        p+=1
        if len(s)==0:
            break
    if len(s)!=0:
        while s[-1]==r:
            s=s[:-1]
            q+=1
            if len(s)==0:
                break
    if t[0]==t[-1]:
        print((k-1)*((p+q)//2)+d*k-(k-1)*(p//2+q//2))
    else:
        print(d*k)

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
p=s[0]
cnt=1
q=[]
for i in range(len(s)-1):
	if s[i+1]==p:
		cnt+=1
	else:
		q.append(cnt//2)
		cnt=1
		p=s[i+1]
q.append(cnt//2)
import copy
cn=0
qq=copy.copy(q)
for i in range(len(s)):
	if s[i]==p:
		cnt+=1
		cn+=1
	else:
		qq[-1]=(cnt//2)
		cnt=0
		break
if cnt>0:
	qq[-1]=cnt//2
l=0
if cnt%2==0 and cn%2!=0:
	l=k-2
ans=0
ans+=(k-1)*sum(qq)
ans+=sum(q)
print(ans-(k-1)*(cn//2)-(l+1)//2)

Submission status: Wrong Answer

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

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

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

while i+1 < len(S):
    tempc= 1
    while S[i] == S[i+1]:
        tempc += 1
        if i+2 < len(S):
            i += 1
        else:
            break

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

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

    frag += tempc%2

print((ans+frag)*K)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from heapq import heappush, heappop, heapreplace

alpha = {'a', 'b', 'c'}
def solve():
    S = input()
    l = len(S)
    K = int(input())
    SK = S*min(3,K)
    s1 = s2 = s3 = ''
    swap_count = [0,0,0]
    for i,s in enumerate(SK+'--', start=-1):
        if s3:
            s1, s2, s3 = s2, s3, s
        elif s2:
            s3 = s
        elif s1:
            s2 = s
        else:
            s1 = s

        if s3 and len({s1,s2})==1:
            s2 = (alpha - {s1,s3}).pop()
            swap_count[i//l] += 1

    res = sum(swap_count) + max(0, K-3)*swap_count[1]
    return str(res)
    

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

n = len(s)
cnt = [0]

for i in range(0,n-1):
    if s[i] == s[i+1]:
        cnt[-1] += 1
    else:
        if cnt[-1] != 0:
            cnt.append(1)

ren = 0
for i in cnt:
    ren += int((i) // 2)

b=0
if s[0] == s[-1]:
    if (cnt[-1] % 2) == 0:
        b += k - 1

ans = ren * k + b

print(ans)

Submission status: Wrong Answer

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

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

flag = False
if s[0] == s[-1]:
#    print("flag")
    flag = True
else:
    pass

#sの中だけ考える
count = 0
dp = ""
for i in range(len(s)):
    if dp == s[i]:
        count += 1
        dp = ""
    else:
        dp = s[i]
ans = count * k


if flag:
    try:
        #もし後ろ３つが同じなら
#        print("here")
        if len(set([s[-1],s[-2],s[-3]])) == 1:
            if set([s[-1],s[-2]]) == 1:
                pass
            else:
                ans = ans + (k-1)
    except IndexError:
        pass
else:
    pass

print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
S = str(input())
K = int(input())

if len(S) == 1:
    print(K // 2)
else:
    before_s = ""
    dup_list = []
    dup_num = 0
#     文字列そのままの変換数
    for s in S:
        if s == before_s:
            dup_num += 1
            if dup_num == 1:
                dup_list.append(dup_num)
            else:
                dup_list[-1] = dup_num
        else:
            before_s = s
            dup_num = 0
    single_ans = 0
    for dnum in dup_list:
        single_ans += (dnum + 1) // 2
# 文字列2回重ねた変換数
    before_s = ""
    dup_list = []
    dup_num = 0
    for s in S * 2:
        if s == before_s:
            dup_num += 1
            if dup_num == 1:
                dup_list.append(dup_num)
            else:
                dup_list[-1] = dup_num
        else:
            before_s = s
            dup_num = 0
    double_ans = 0
    for dnum in dup_list:
        double_ans += (dnum + 1) // 2
    
    ans = single_ans * K + (double_ans - 2 * single_ans) * (K - 1)
    print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
same = []
tmp = 1
ans = 0
tsunagi = 0
for i in range(len(s)-1):
         if s[i] == s[i+1]:
                  tmp += 1
         else:
                  same.append(tmp)
                  tmp = 1
same.append(tmp)
if s[0] == s[len(s)-1] and same[0] ==1 and same [len(same)-1] == 1:
         tsunagi = 1
if tmp == len(s):
         print((len(s)*k)//2)
else:
         for i in range(len(same)):
                  ans += (same[i]//2)*k
         ans += (tsunagi)*(k-1)
         print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

cnt = 0
L = len(S)

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

changed = False
Flg = False
for i in range(L-1):
    if not changed:
        if i == L - 2:
            if S[i] == S[i+1]:
                cnt += 1
                changed = True
            else:
                if K > 1:
                    if S[0] == S[i+1]:
                        cnt += 1
                        changed = True
                        Flg = True
        else:
            if S[i] == S[i+1]:
                cnt += 1
                changed = True
    else:
        if i == L - 2:
            if K > 1:
                if S[0] == S[i + 1]:
                    cnt += 1
                    changed = True
                    Flg = True
        else:
            changed = False

cnt *= K
if Flg:
    cnt -= 1
print(cnt)

Submission status: Wrong Answer

Solution code:
#import pysnooper
#import numpy
#import os,re,sys,operator
#from collections import Counter,deque
#from operator import itemgetter
#from itertools import accumulate,combinations,groupby,combinations_with_replacement,permutations
from sys import stdin,setrecursionlimit
#from copy import deepcopy
#import heapq
#import math
#import string

setrecursionlimit(10**6)
input=stdin.readline

s=input().rstrip()
k=int(input().rstrip())
ran=[]
now=s[0]
cnt=1
for i in range(1,len(s)):
    if s[i]!=now:
        ran.append((now,cnt))
        now=s[i]
        cnt=1
    else:
        cnt+=1
ran.append((now,cnt))
ans=0
#print(ran)
for i in range(1,len(ran)-1):
    ns=ran[i][1]
    if ns>1:
        ans+=ns//2
ans*=k
#print(ans)
if len(ran)>1:
    if ran[0][1]>1:
        ans+=ran[0][1]//2
    if ran[-1][1]>1:
        ans+=ran[-1][1]//2
#print(ans)
if len(ran)==1 and len(s)>1:
    ans+=(ran[0][1]*k)//2
elif ran[0][0]==ran[-1][0] and k>1:
    ans+=((ran[0][1]+ran[-1][1])//2)*(k-1)
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
cnt = 0
i = 0
c0 = 0
c1 = 0
key = 0
endkey = 0
while i < len(s) - 1:
    if s[i] == s[i + 1]:
        start = i
        end = i
        for j in range(i + 1, len(s)):
            end = j
            if s[j] != s[i]:
                break

        if end == len(s) - 1: #もし最後の一文字が連続に含まれるとき
            if (end - start) % 2 == 0 and s[-1] == s[0]: #最後の一文字が連続でかつ最初の文字と同じでかつ奇数の時
                end += 1
                endkey = 1
            elif s[-1] == s[0]:
                endkey = 1
            end += 1
        cnt += (end - start) // 2 * k

        if start == 0 and end == len(s) and len(s) % 2 == 1:
            key = 1
            cnt += (end - start) * k // 2
            if k % 2 == 1:
                cnt += (end - start) // 2
        i = end - 1
        # print(start, end)
    i += 1

if len(s) == 1:
    cnt += k // 2
if not endkey and s[0] == s[-1]:
    cnt += k - 1
if endkey:
    cnt -= 1
print(cnt)

Submission status: Wrong Answer

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

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

sigma = makerawlist(S)
print(sigma*K)

Submission status: Wrong Answer

Solution code:
import string

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

end = len(S) -1
j = 0

for i in range(1, len(S)-1):
    if S[i-1] == S[i]:
        chars = list(string.ascii_lowercase)
        pre = S[i-1]
        back = S[i+1]
        if pre != back:
            chars.pop(chars.index(pre))
            chars.pop(chars.index(back))
        else:
            chars.pop(chars.index(pre))
        S[i] = chars[0]
        j += 1
    else:
        continue

if S[end] == S[0]:
    chars = list(string.ascii_lowercase)
    pre = S[end -1]
    back = S[0]
    if pre != back:
        chars.pop(chars.index(pre))
        chars.pop(chars.index(back))
    else:
        chars.pop(chars.index(pre))
    S[end] = chars[0]
    j += 1
    print(j*K-1)
else:
    print(j*K)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
count = 0
t = ""

if s[0] != s[len(s)-1]:
    t = list(s + "1" + "2")
    print(t)
    for i in range(len(t)-2):
        if t[i] == t[i+1]:
            if t[i] != t[i+2]:
                t[i] = "3"
                count += 1
            if t[i] == t[i+2]:
                t[i+1] = "3"
                count += 1
    print(count*k)
            
else:
    for i in range(k):
        t += s
    t = list(t + "1" + "2")

    for i in range(len(t)-2):
        if t[i] == t[i+1]:
            if t[i] != t[i+2]:
                t[i] = "3"
                count += 1
            if t[i] == t[i+2]:
                t[i+1] = "3"
                count += 1
    print(count)

Submission status: Wrong Answer

Solution code:
from sys import stdin


def change(S, K):
    c = 0

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

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

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

    return K * c


if __name__ == "__main__":
    S = stdin.readline().strip()
    K = int(stdin.readline())

    print(change(S, K))

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]:
    result = k * unit(s[1:-1])
    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+(k-1), s2*(k-1)+s3+(k-1))
    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, s2+(s3*(k//2)+s0*(k//2-1))+s1)
    print(min(result2, result) + (k-1))

Submission status: Wrong Answer

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

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

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

        t = S[i]

    print(result * K)
    exit()

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

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

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

        t = S[i]

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

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

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

    print(result * K)

Submission status: Wrong Answer

Solution code:
from itertools import groupby
import math

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

cnt = 0
  
ttt = [[k, len(list(g))] for k, g in groupby(s)]

for tt in ttt:
  for i,v in enumerate(tt):
    if tt[1] % 2 == 0:
      cnt += tt[1] / 2
      break
    else:
      cnt += math.ceil(tt[1]//2)
      break
      


print(int(cnt)*k)