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=input()
k=int(input())

cnt=0
i=1
while i <len(s):
  if i>0:
    if s[i]==s[i-1]:
      cnt+=1
      i+=2
    else:
      i+=1
  
if k==1:
  print(cnt)
  exit()
  
ss=s+s
cntcnt=0
i=1
while i <len(ss):
  if i>0:
    if ss[i]==ss[i-1]:
      cntcnt+=1
      i+=2
    else:
      i+=1
if k==2:
  print(cntcnt)
  exit()
  
sss=s+s+s
cntcntcnt=0
i=1
while i <len(sss):
  if i>0:
    if sss[i]==sss[i-1]:
      cntcntcnt+=1
      i+=2
    else:
      i+=1
if k==3:
  print(cntcntcnt)
  exit()


  
ssss=s+s+s+s
cntcntcntcnt=0
i=1
while i <len(ssss):
  if i>0:
    if ssss[i]==ssss[i-1]:
      cntcntcntcnt+=1
      i+=2
    else:
      i+=1

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

print(cnt-(cntcnt-cnt)*((1-k)//2)+(cntcntcnt-cntcnt)*(k-1)//2)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
s = 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 + 1:
        break
    if s[j] == s[j + 1]:
        if j == len(s) - 1:
            n += 1
        else:
            m += 1
        j += 1
    j += 1
print(m * k + n * (k - 1))

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
S_2=S*2+"A"
S_3=S*3+"A"
cnt_2=0
cnt_3=0
import queue
q=queue.Queue()
for i in S_2:
    if not q.empty():
        if not i == q.get():
            n=1
            while not q.empty():
                q.get()
                n+=1
            cnt_2+=n//2
            q.put(i)
        else:
            q.put(i)
            q.put(i)         
    else:
        q.put(i)
q=queue.Queue()
for i in S_3:
    if not q.empty():
        if not i == q.get():
            n=1
            while not q.empty():
                q.get()
                n+=1
            cnt_3+=n//2
            q.put(i)
        else:
            q.put(i)
            q.put(i)         
    else:
        q.put(i)

print(cnt_2+(cnt_3-cnt_2)*(K-2))

Submission status: Wrong Answer

Solution code:
S = input()
N = int(input())
c = 0
s = S+S
j = [True]*len(s)
for i in range(0,len(s)-1):
  if s[i]==s[i+1] and j[i]==True:
    #print(i,s[i],s[i+1])
    #print(s)
    j[i+1]=False
    c+=1
a = int(N*c/2)
k=0
if s[0]==s[-1] and j[-1]==True and not(s[0]==s[1]):
  k += (N-1)//2
  #print(k)

print(a+k)

Submission status: Wrong Answer

Solution code:
S, K = input(), int(input())
ans = 0
S_ = S + S[0]
i = 1
while(i<len(S_)):
    try:
        if (S_[i-1] == S_[i]) and (S_[i] == S_[i+1]):
            ans += 1
            i += 2
        elif S_[i] == S_[i+1]:
            ans += 1
            i += 2
        else:
            i += 1
    except IndexError:
        break
print(ans*K)

Submission status: Wrong Answer

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

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

another_c = ''
cnt = 0

for n in range(k):
    t = list(s)
    for i, c in enumerate(t):
        prev_c = t[i-1]
        next_c = t[(i+1)%len(t)]
        if i > 0 and c == prev_c:
            if i != len(t)-1 and next_c == chr(ord(c) + 1): 
                another_c = chr(ord(c) + 2)
            else:
                another_c = chr(ord(c) + 1)
            if ord(another_c) > ord('z'):
                another_c = 'a'
            t[i] = another_c
            cnt = cnt + 1
        #else:
        #    another_c = ''
        #print(i, c, another_c)

    #print("".join(t))

print(cnt)

Submission status: Wrong Answer

Solution code:
import math


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

if K == 1:
    cnt = 0
    S2 = list(S)
    for i in range(1, len(S2)):
        if S2[i-1] == S2[i]:
            S2[i] = "A"
            cnt += 1

    cnt = cnt * K

    if S2[0] == S2[-1]:
        cnt += K-1

    print(cnt)

else:
    S2 = list(S)
    S2 = S2 + S2
    cnt1 = 0
    for i in range(1, int(len(S2)/2)):
        if S2[i-1] == S2[i]:
            S2[i] = "A"
            cnt1 += 1

    cnt2 = 0
    for i in range(int(len(S2)/2), len(S2)):
        if S2[i-1] == S2[i]:
            S2[i] = "A"
            cnt2 += 1

    cnt = cnt1 * math.ceil(K/2)
    cnt += cnt2 * K//2

    if S[0] == S[-1]:
        if K > 2:
            cnt += math.ceil(K/2) - 1

    print(cnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

def counter_func(string,x):
  try:
    if string[x] == string[x+1]:
      return 1
    else:
      return 0
  except:
    return 0
  
counter = []
count = 0
j = 0
add = 0
while j < length:
  if counter_func(s,j) == 1:
    count += 1
  else:
    counter.append(count)
    count = 0
  j += 1
if s[-1] == s[0] and k > 1:
  counter[-1] += 1
  add = 1
sum = 0
if len(counter) != 1:
  for i in range(len(counter)-1):
    if counter[i] != 0 :
      sum += int((counter[i]+1)/2)*k
  if counter[-1] == 1 and add == 1:
    sum += int((counter[-1]+1)/2)*(k-1)
  elif counter[-1] != 0:
    sum += int((counter[-1]+1)/2)*k
else:
  sum = int((length*k)/2)
print(sum)

Submission status: Wrong Answer

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



counter = 0
ans_L = []

s = [i for i in j]
s.append("0")
renzoku = 1

for val in range(0,len(s)-1):
    tmp = s[val+1]
    if s[val] == tmp:
        renzoku += 1
    elif renzoku > 1 and s[val] != tmp:
        ans_L.append(renzoku//2)
        renzoku = 1



# 繋がっている場合
# kbk
renzoku = 0
if s[0] == [-2]:
    for val in range(len(s)+1):
        if s[val+1] == s[0]:
            renzoku += 1
        else:
            break

if len(s) == 2:
    print(k//2)
    import sys
    sys.exit()
    
    

print(sum(ans_L)*k + renzoku%2)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
n = len(s)
p = ['']*(4*n)

for i in range(n):
    p[i] = s[i]
    p[i+n] = s[i]
    p[i+2*n] = s[i]
    p[i+3*n] = s[i]

a = [0,0,0,0]

judge = 0
for i in range(1,4*n):
    if p[i] == p[i-1]:
        a[i//n] += 1
        p[i] = ''
        if i == n:
            judge = 1
        if judge == 1 and i == 2*n:
            judge = 2

if judge == 0:
    print(a[0]*k)
elif judge == 1:
    if k == 1:
        print(a[0])
    else:
        print(a[0]+a[1]*(k-1))
else:
    res = (a[0]+a[1])*(k//2)
    if k%2 == 1:
        res += a[0]
    print(res)

Submission status: Wrong Answer

Solution code:
import math
S=list(input())
K=int(input())
count = 0
ans = 0
flag = False
firstCount = 0
firstChart = ""
for i in range(1,len(S)):
  if S[i-1] == S[i]:
    count += 1
  else:
    if flag == False:
      firstCount = count
      firstChart = S[i-1]
      ans += math.ceil(count/2)
    else:
      ans += math.ceil(count/2)*K
    flag = True
    count = 0

if flag:
  ans += math.ceil(count/2)
  if firstChart == S[len(S)-1]:
    ans += (firstCount + count + 2) // 2 * (K-1)
else:
  ans += (count+1)*K//2
if len(S) == 1:
  if K == 1:
    print("0")
  else:
    print(math.ceil(K//2))
else:
  print(ans)

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 sig == 1 and s[0] == s[-1]:
  ans += k-1


print(ans)

Submission status: Wrong Answer

Solution code:

# import sys
# input = sys.stdin.readline
from collections import Counter
import string


def main():
    # while True:
        S = input()
        K = int(input())

        count_1st = 0
        S1 = list(S * 1)
        for i in range(len(S) - 1):
            if S1[i] == S1[i+1]:
                for c in string.ascii_lowercase:
                    if i == 0:
                        if c != S1[i+1]:
                            count_1st += 1
                            S1[i+1] = c
                            break
                    else:
                        if c != S1[i-1] and c != S1[i+1]:
                            count_1st += 1
                            S1[i+1] = c
                            break

        count_2nd = 0
        S2 = list(S * 2)
        for i in range(len(S) * 2 - 1):
            if S2[i] == S2[i+1]:
                for c in string.ascii_lowercase:
                    if i == 0:
                        if c != S2[i+1]:
                            count_2nd += 1
                            S2[i+1] = c
                            break
                    else:
                        if c != S2[i-1] and c != S2[i+1]:
                            count_2nd += 1
                            S2[i+1] = c
                            break

        count_3rd = 0
        S3 = list(S * 3)
        for i in range(len(S) * 3 - 1):
            if S3[i] == S3[i+1]:
                for c in string.ascii_lowercase:
                    if i == 0:
                        if c != S3[i+1]:
                            count_3rd += 1
                            S3[i+1] = c
                            break
                    else:
                        if c != S3[i-1] and c != S3[i+1]:
                            count_3rd += 1
                            S3[i+1] = c
                            break

        # count_k = 0
        # SK = list(S * K)
        # for i in range(len(S) * K - 1):
        #     if SK[i] == SK[i+1]:
        #         for c in string.ascii_lowercase:
        #             if i == 0:
        #                 if c != SK[i+1]:
        #                     count_k += 1
        #                     SK[i+1] = c
        #                     break
        #             else:
        #                 if c != SK[i-1] and c != SK[i+1]:
        #                     count_k += 1
        #                     SK[i+1] = c
        #                     break

        d = count_2nd - count_1st
        last_d = count_3rd - count_2nd
        # print(K, d, count_1st)
        # print(count_1st, count_2nd, count_3rd)
        # print(d * K)
        # print(count_1st + d * (K-1))
        print(count_1st + d * (K-2) + last_d)
        # print(count_k)
        # print(*SK)
        # print(*S1)
        # print(*S2)
        # print(*S3)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import math


def solve(*args: str) -> str:
    s = args[0]
    k = int(args[1])

    if len(s) == 1:
        return str(int(len(s) * k / 2))

    return str(
        min(count_adjacent_dup(s), count_adjacent_dup(s[::-1])) * k
    )


def count_adjacent_dup(s: str) -> int:
    ret = 0
    cnt = 0

    for i in range(len(s)):
        if s[i] == s[(i+1) % len(s)]:
            cnt += 1
        else:
            ret += math.ceil(cnt/2)
            cnt = 0

    if 0 < cnt:
        ret += math.ceil(cnt/2)

    return ret


if __name__ == "__main__":
    print(solve(*[input() for _ in range(2)]))

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)]
tttt = [tt[1] for tt in ttt for i,v in enumerate(tt) if tt[i] != 1]

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

print(int(cnt/2*k))

Submission status: Wrong Answer

Solution code:
def solve2():
    s = input()
    len_s = len(s)
    k = int(input())
    s += s
    cnt = 0
    flg = False
    for i in range(0, len_s * 2 - 1):
        if flg:
            flg = False
        elif s[i] == s[i + 1]:
            flg = True
            cnt += 1
    return cnt * k // 2
print(solve2())

Submission status: Wrong Answer

Solution code:
s=input()
l=len(s)
k=int(input())
ans=0
head=0
body=-1
tail=l-1
for i in range(l-1):
  if s[i]==s[i+1]:
    head=i+1
  else:
    break
for i in range(l-1,0,-1):
  if s[i]==s[i-1]:
    tail=i-1
  else:
    break
head_chars=head+1
tail_chars=l-tail
if tail-head<0:
  print((l*k)//2)
elif tail-head==1:
  print((head_chars//2+tail_chars//2)*k)
else:
  body_text=s[head+1:tail]
  ans=0
  tmp=0
  cnt=1
  for i in range(1,len(body_text)):
    if body_text[i]==body_text[i-1]:
      cnt+=1
    else:
      tmp+=cnt//2
      cnt=1
  tmp+=cnt//2
  ans+=tmp*k
  headtail_text=s[tail:]+s[:head+1]
  tmp=0
  cnt=1
  for i in range(1,len(headtail_text)):
    if headtail_text[i]==headtail_text[i-1]:
      cnt+=1
    else:
      tmp+=cnt//2
      cnt=1
  tmp+=cnt//2
  ans+=tmp*(k-1)
  ans+=head_chars//2
  ans+=tail_chars//2
  print(ans)

Submission status: Wrong Answer

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

s *= 2
first_char = s[0]
pre_char = ''
char = ''
ans = 0
last_char_change = False
single_cost = 0

for i in range(len(s)):
    char = s[i]
    if pre_char == char:
        ans += 1
        pre_char = ''
    else:
        pre_char = char
        if i == len(s) - 1:
            if first_char == char:
                ans += 1
                last_char_change = True
    
    if i == (len(s) // 2) - 1:
        single_cost = ans
    
if n % 2 == 1:
    ans = max(0, ans * ((n // 2) + 1))
    ans -= single_cost
    if last_char_change:
        ans = max(ans - 2, 0)
else:
    ans *= n // 2
    if last_char_change:
        ans = max(ans - 1, 0)



print(ans)

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 S[0]==S[-1]:
    if (cnts[1]+cnts[-1]) % 2 == 0:
        print(total*K+K-1)
else:
    print(total*K)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
if len(s)==1:
    print(k//2)
    exit()
import re


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

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

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

print(count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
S_2=S*2+"A"
S_3=S*3+"A"
cnt_2=0
cnt_3=0
import queue
q=queue.Queue()
for i in S_2:
    if not q.empty():
        if not i == q.get():
            n=1
            while not q.empty():
                q.get()
                n+=1
            cnt_2+=n//2
            q.put(i)
        else:
            q.put(i)
            q.put(i)         
    else:
        q.put(i)
for i in S_3:
    if not q.empty():
        if not i == q.get():
            n=1
            while not q.empty():
                q.get()
                n+=1
            cnt_3+=n//2
            q.put(i)
        else:
            q.put(i)
            q.put(i)         
    else:
        q.put(i)

print(cnt_2+(cnt_3-cnt_2)*(K-2))

Submission status: Wrong Answer

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

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

listS = list(S)
change = 0

for i, s in enumerate(listS[1:], start=1):
    if s == listS[i - 1] and listS[i - 1] != '.':
        change += 1
        listS[i] = '.'

listS = list(S[:: -1])
changeR = 0

for i, s in enumerate(listS[1:], start=1):
    if s == listS[i - 1] and listS[i - 1] != '.':
        changeR += 1
        listS[i] = '.'

c = min(change, changeR)
ans = c * K

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

print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
S = (stdin.readline().rstrip())
K = int(stdin.readline().rstrip())
ans = 0
N=len(S)
cnt = 1
G = S+"@"
flag = False
for i in range(0,N):
    if G[i] == G[i+1]:
        cnt += 1
        flag = True
    else:
        if cnt != 1:
            #print(cnt)
            ans += (cnt//2)
            cnt = 1
        #print(ans,flag)
if flag != True:
  ans = 0
for_eq = 1
rev_eq = 1
revS = S[::-1]
for j in range(0,N-1):
    if S[j] == S[j+1]:
        for_eq += 1
    else:
        break
for k in range(0,N-1):
    if revS[k] == revS[k+1]:
        rev_eq += 1
    else:
        break
#print(ans)
ans = ans*K
#print(rev_eq,for_eq)
if K != 1:
  if S[0] == S[-1]:
      if (rev_eq % 2 != 0) and (for_eq % 2 != 0):
          ans += ((K+1)//2)
      if (flag != True) and K == 1:
        ans = 0
if len(set(S))==1:
    ans = (len(S)*K)//2

print(ans)

Submission status: Wrong Answer

Solution code:

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


if __name__ == '__main__':
    S = input()
    K = int(input())
    print(min(f(S,K), f(S[::-1],K)))

Submission status: Wrong Answer

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

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

# "abababa"
# "abababaabababaabababa"

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

Submission status: Wrong Answer

Solution code:
a=input()
b=int(input())
s="A"
zn=0
n=0
nn=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)
if aa[0]==aa[-1] and ab[-1]==1:
    s=a[0]
    i=0
    for x in aa:
        if a[i]==s:
            nn+=1
        else:
            break
        i+=1
    i=1
    for x in aa:
        if a[-i]==s:
            nn+=1
        else:
            break
        i+=1
for x in ab:
    zn+=(x//2)
if len(a)==1:
    print(b//2)
elif len(ab)!=1:
    print(zn*b+(nn//2)*(b-1))
else:
    print((b*ab[0])//2)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#AGC39 A

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

s = input()
k = I()
n = len(s)
res = 0
i = 0
lst = deque()
while i < n:
    tmp = s[i]
    i += 1
    cnt = 1
    while i < n and tmp == s[i]:
        cnt += 1
        i += 1
    lst.append(cnt)
if len(lst) >= 2:
    if s[0] == s[-1] and k >= 2:
        x = lst.popleft()
        y = lst.pop()
        res = 0
        for i in lst:
            res += (i//2)*k
        res += (x//2)*(k-1) + (y//2)*(k-1)
        res += ((x+y)//2)*(k-1)
        print(res)
    else:
        res = 0
        for i in lst:
            res += (i//2)*k
        print(res)
else:
    print((n*k)//2)

Submission status: Wrong Answer

Solution code:
s=input()
d= [c  for c in s]
n=int(input())
l=len(d)
count=0

for i in range(l-2):
  if i==0 and d[0]==d[l-1] and d[i]==d[i+1]:
    d[0]="1"
    count+=1
  if d[i]==d[i+1]:
    if d[i]!=d[i+2]:
       d[i]="1"
       count+=1
    elif d[i]==d[i+2]:
      d[i+1]="1"
      count+=1
if d[l-2]==d[l-1]:
  count+=1
elif d[l-2]==d[l-1] and d[0]==d[l-1]:
  count+=0
if n%2==0:
  print(count*n)
else:
  print(count*n-1)

Submission status: Wrong Answer

Solution code:
import math
s=list(input())
k=int(input())
s=s*1000
ans=0
for i in range(1,len(s)):
    if s[i-1]==s[i]:
        ans+=1
        s[i]="A"
print(math.ceil(ans*(k/1000)))

Submission status: Wrong Answer

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

S2 = S * 2

singleCnt = 0
doubleCnt = 0
for i in range(len(S)-1):
  if S[i] == S[i+1]:
    S[i+1] = "1" # かぶらない文字に変換
    singleCnt += 1
if S[0] != S[-1]:
  ans = singleCnt * K
  
elif S[0] == S[-1]:
  for i in range(len(S2)-1):
    if S2[i] == S2[i+1]:
      S2[i+1] = "1" # かぶらない文字に変換
      doubleCnt += 1
  if S2[0] != S2[-1]:
    a, b = divmod(K, 2)
    if b == 0:
      ans = doubleCnt * K // 2
    else:  
      ans = doubleCnt * K // 2 + singleCnt
  else:
    ans = (doubleCnt - singleCnt) * (K - 1) + singleCnt

print(ans)

Submission status: Wrong Answer

Solution code:
s = list(map(str, input()))
k = int(input())
basic = 0
repeat = 0
counter = 0
s_a = s*2
for i in range(1,len(s)):
    if s[i] == s[i-1]:
        counter += 1
        basic += (counter % 2)
    else:
        counter = 0
counter = 0
for i in range(1,len(s_a)):
    if s_a[i] == s_a[i-1]:
        counter += 1
        repeat += (counter % 2)
    else:
        counter = 0
repeat -= basic
print(basic + repeat * (k - 1))

Submission status: Wrong Answer

Solution code:
t=list(input())
k=int(input())
count=0
flag=0
if len(t)==1:
    print(k//2)
    flag=1
if flag==0:
    for i in range(len(t)-1):
        if t[i]==t[i+1]:
            t[i+1]='*'
            count+=1
    count*=k
    if t[0]==t[len(t)-1]:count+=k-1
    print(count)

Submission status: Wrong Answer

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

T = S * 2
T2 = S

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

# 2bai
countB = 0

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

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

Submission status: Wrong Answer

Solution code:
t=str(input())
k=int(input())
L = len(t)
tmp = t[0]
n = 0 #change
flag = 0
for i in range(1,L):
  if flag==1:
    flag=0
  elif t[i]==tmp:# and flag==0:
    n+=1
    flag=1  
  tmp=t[i]
ans=k*n

if L>2 and k>1:
  if t[0]==t[-1] and flag==0:
    if k%2==1 and t[0]==t[1]:
      ans+=k-2
    else:
      ans+=k-1

if t.count(t[0])==L:
  ans-=1
if L==1:
  ans=k//2
print(ans)

Submission status: Wrong Answer

Solution code:
#coding: utf-8
strings = str(input())
k = int(input())
s = strings + strings
# print(s)
count = 0

for i in range(len(s)-1):
	# print('strj'+str(j))
	if s[i] == s[i+1]:
		# print(str('s[i]=') + s[i] + str(' s[i+1]=') + s[i+1])
		count += 1
		# s[i] = str(i)
		s = s[:i+1] + str('A') + s[i+2:]
		# print(s)

# print(count)
ans = count*k//2

# if k % 2 == 0:
# 	count = count * k // 2
# else:
# 	print(count*k//2)
# 	print(count//2)
# 	count = count * k // 2 - (count // 2)
if strings[0] == strings[len(strings)-1] and strings[len(strings)-1] != strings[len(strings)-2]:
	ans += (k - 1) // 2

print(ans)

Submission status: Wrong Answer

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

if len(s)==1:
  print(k//2)
else:
  
  if s[0]==s[-1]:
    p+=1

  l=[1]
  for i in range(len(s)-1):
    if s[i]!=s[i+1]:
      l.append(1)
    else:
      l[-1]+=1
  x=[i//2 for i in l]
  y=sum(x)

  if l[-1]!=1:
    print(y*k)
  else:
    print(y*k+p*(k-1))

Submission status: Wrong Answer

Solution code:
s=list(input())
k=int(input())
l=len(s)
if l%2 and len(set(s))==1:
    print(l*k//2)
    exit()
if s[-1]==s[0]==s[1]: s[0]='.'
for i in range(1,l):
    if s[i]==s[i-1]: s[i]='.'
ans=s.count('.')*k
if s[0]==s[-1] and s[0]!='.': ans+=k-1 
print(ans)

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 + 1:
        break
    if s[j] == s[j + 1]:
        if j == len(s) - 1:
            n += 1
            if s[j - 1] == s[j - 2] or s[j] == s[j + 1]:
                # n -= 1
                print(n)
        else:
            m += 1
        j += 1
    j += 1
print(m * k + n * (k - 1))

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
import re
import sys
import copy

def get_count(s):
  count02=0
  pattern = "a+|b+|c+|d+|e+|f+|g+|h+|i+|j+|k+|l+|m+|n+|o+|p+|q+|r+|s+|t+|u+|v+|w+|x+|y+|z+"
  result = re.finditer(pattern, s)
  for res in result:
    print(res.group())
    l = res.end()-res.start()
    if l>1:
      #切り捨て
      count02 += l//2
  return count02

s = input()
k = int(input())
count01=0
count02=0

#case01
if len(s)==1:
  print(k//2)
  sys.exit(0)
  
#case02
tmp = s[0]
flag=True
for i in range(len(s)):
  if s[i]!=tmp:
    flag=False
if flag==True:
  print((len(s)*k)//2)
  sys.exit(0)

#case03
s_org = copy.deepcopy(s)
s2 = copy.deepcopy(s)
if s[0]==s[-1]:
  tmp = s[0]

  offset01=0
  while s[0]==tmp:
    offset01+=1
    s = s[1:] + s[0]
    #print(s)
  offset02=0
  while s2[-1]==tmp:
    offset02+=1
    s2 = s2[-1] + s2[:-1]
    #print(s2)
  k -= 1
  print(">", s_org[:offset01])
  print(">", s, k)
  print(">", s_org[offset01:])
  print(get_count(s_org[:offset01]) + get_count(s)*k + get_count(s_org[offset01:]))
  sys.exit(0)

  
#case04
print(get_count(s)*k)

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)
if sigma == len(S)//2 and (len(S)*K)%2 == 1:
    print((len(S)*K)//2)
elif S[0]==S[len(S)-1] and S[0]!=S[1] and S[len(S)-2]!=S[len(S)-1]:
    print(sigma*K+K-1)
else:
    print(sigma*K)

Submission status: Wrong Answer

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

T = S * 2
T2 = S

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

# 2bai
countB = 0

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

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

Submission status: Wrong Answer

Solution code:
from math import ceil
from itertools import tee


def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)


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

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