Problem:


Score : 300 points

Problem Statement

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

Constraints

1 \leq |S| \leq 100

S consists of lowercase English letters.

1 \leq K \leq 10^9

K is an integer.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the minimum number of operations required.

Sample Input 1

issii
2

Sample Output 1

4

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

Sample Input 2

qq
81

Sample Output 2

81

Sample Input 3

cooooooooonteeeeeeeeeest
999993333

Sample Output 3

8999939997



Sample input:
issii
2


Sample output:
4

Submission status: Wrong Answer

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

import sys

debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        # for line in sys.stdin:
        #     lines.append(line)
        lines.append(input())
        lines.append(input())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]

    s = lines[0]
    k = int(lines[1])

    return (s, k)


def solve(s, k):

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

    target_1 = s[0]
    index_1 = 0
    for i in range(len(s)):
        if s[i] != target_1:
            index_1 = i
            break
    
    target_3 = s[-1]
    index_3 = len(s) - 1
    for i in range(len(s) - 1, -1, -1):
        if s[i] != target_3:
            index_3 = i
            break
    
    s_head = s[0:index_1]
    s_body = s[index_1:index_3 + 1]
    s_tail = s[index_3 + 1:]
    if debug:
        log("s_head=%s" % s_head)
        log("s_body=%s" % s_body)
        log("s_tail=%s" % s_tail)

    total_replace_count = 0

    last_c = None
    count = 1
    replace_count = 0
    for c in s_body:
        if c == last_c:
            count = count + 1    
        else:
            replace_count = replace_count + count // 2
            count = 1
        last_c = c
    replace_count = replace_count + count // 2

    total_replace_count = total_replace_count + replace_count * k
    if debug:
        log("total_replace_count=%d" % total_replace_count)

    if s[0] == s[-1]:
        s_h_t = s_head + s_tail
        total_replace_count = total_replace_count + len(s_h_t) // 2 * (k-1)
        if debug:
            log("total_replace_count=%d" % total_replace_count)

    
    total_replace_count = total_replace_count + len(s_head) // 2
    if debug:
        log("total_replace_count=%d" % total_replace_count)
    total_replace_count = total_replace_count + len(s_tail) // 2
    if debug:
        log("total_replace_count=%d" % total_replace_count)

    return total_replace_count


def main():
    # 出力
    result = solve(*parse_input())
    if isinstance(result, list):
        for r in result:
            print("%s" % r, sep='')
    else:
        print("%s" % result, sep='')

if __name__ == '__main__':

    main()

Submission status: Wrong Answer

Solution code:
s=list(input())
s2=s*2
s3=s*3

k=int(input())
an=0
an1=0
an2=0
an3=0
skip=0

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

for i in range(len(s)-1):
    if skip==1:
        skip=0
        continue
    
    if s[i]==s[i+1]:
        an1+=1
        skip=1

skip=0

for i in range(len(s2)-1):
    if skip==1:
        skip=0
        continue
    
    if s2[i]==s2[i+1]:
        an2+=1
        skip=1

skip=0

for i in range(len(s3)-1):
    if skip==1:
        skip=0
        continue
    
    if s3[i]==s3[i+1]:
        an3+=1
        skip=1
        
if k%2==0:
    print(an2*(k//2))
else:
    print(an1+(an3-an1)*(k//2))

Submission status: Wrong Answer

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

count = 0
i=0
join_flag =0
join_n = 0

if len(s)==1:
  print(k//2)
    
elif len(s)==2:
  if s[0]==s[1]:
    print(k)
  else:
    print(0)
  
elif len(set(s))==1:
  print(len(s)*k //2)
  
else:
  while i<len(s)-1:
    if s[i] == s[i+1]:
      count+=1
      i+=2
    else:
      i+=1
  
  if (s[-1] == s[0]) :
    join_flag =1
    
  if join_flag:  
    
    left_len = 1
    right_len = 1

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

    for i in range(len(s)-1):    
      if s[-1-i]==s[-2-i]:
        right_len +=1
      else:
        break            
    
    if (left_len + right_len) %2 ==0:        
      join_n = k-1
    else:
      join_n = k//2

  print(count*k + join_n)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
s = list(s)
s.append(s[0])
h = len(s)
saigo = False
j = 0
num = 0

if h == 2:
    num = k // 2
else:
    for i in range(1,h):
        if s[i] == s[i-1] and j == 0:
            if i == h-1:
                saigo = True
            num = num + 1
            j = 1
        else:
            j = 0

    num = num * k
    if saigo:
        num = num - 1




print(num)

Submission status: Wrong Answer

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

moji = []

count = 1
for i in range(len(S)-1): 
  if(S[i] == S[i+1]):
    count += 1
  else:
    moji.append(count)
    count = 1
moji.append(count)
print(moji)

ans = 0
if len(moji) == 1:
  ans = (moji[len(moji)-1] * K) //2
elif len(moji) != 1 and S[0] == S[len(S)-1]:
  for i in range(1, len(moji)-1):
    ans += (moji[i] // 2) * K
    
  ans += ((moji[0] + moji[len(moji)-1]) // 2) * (K-1)
  ans += moji[0] // 2
  ans += moji[len(moji)-1] // 2

else:
  for i in range(len(moji)):
    ans += (moji[i] // 2) * K
    
print(ans)

Submission status: Wrong Answer

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

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

before = ""
cnt = 0
h = S[0]
for s in S:
    if before == s:
        cnt+=1
        before = ""
    else:
        before = s
cnt *= K
j = 1
for s in S[1:]:
    if s == h:
        j+=1
    else:
        break
if len(S) != j:
    for s in reversed(S[1:]):
        if s == h:
            j+=1
        else:
            break
if j > 3 and j%2 == 1:
    cnt+= K-1

print(cnt)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#l=list(map(int, input().split()))
#n,m=map(int, input().split())
#l=list(map(int, input().split()))
#n = int(input())
def map_all(s):
    temp = s[0]
    
    for i in range(1,len(s)-1):
        if (temp != s[i]):
            return False

    return True

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

if map_all(s):
    temp = k * len(s)
    print(temp//2)
    exit()

le = len(s) -1
ans = 0
if (s[0] == s[-1]):
    ans += k
    if (k>1):
        le -= 1


for i in range(le):
    if s[i] == s[i+1]:
        ans += k

print(ans)

Submission status: Wrong Answer

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

prev = S[0]
cnt = 1
ans = 0

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

else:
    for c in S[1:]:
        if c == prev:
            cnt += 1
        elif cnt >= 2:
            ans += cnt // 2
            cnt = 1
        prev = c
    else:
        flag = False
        if S[0] == S[-1]:
            cnt += 1
            if cnt % 2 == 0: 
                flag = True
        if cnt >= 2:
            ans += cnt // 2
        
    if flag:
        print(ans * K - 1)
    else:
        print(ans * K)

Submission status: Wrong Answer

Solution code:

from sys import stdin

S = str(stdin.readline().rstrip())
K = int(stdin.readline().rstrip())

lenS = len(S)
ans = 0

for i in range(lenS):
    mo = S[0]*lenS
    if mo == S:
        ans = lenS*K//2
        print(ans)
        exit()
    else:
        pass

if S[0] == S[lenS-1]:
    S2 = S*2
    a = 0#位置
    while a < lenS*2-1:
        ans2 = 0
        if S2[a] == S2[a+1]:
            b = 2
            x = S2[a]
            for i in range(1,lenS*2-a):
                try:
                    if x == S2[a+1+i]:
                        b += 1
                    else:
                        break
                except:
                    b = 2
            ans2 += b//2
            a += 1+b
        else:
            a += 1
    a = 0#位置
    while a < lenS-1:
        ans1 = 0
        if S[a] == S[a+1]:
            b = 2
            x = S[a]
            for i in range(1,lenS-a):
                try:
                    if x == S[a+1+i]:
                        b += 1
                    else:
                        break
                except:
                    b = 2
            ans1 += b//2
            a += 1+b
        else:
            a += 1
    differ = ans2-ans1
    ans = differ*(K-1) + ans1*K
else:
    a = 0#位置
    while a < lenS-1:
        if S[a] == S[a+1]:
            b = 2
            x = S[a]
            for i in range(1,lenS-a):
                try:
                    if x == S[a+1+i]:
                        b += 1
                    else:
                        break
                except:
                    b = 2
            ans += b//2
            a += 1+b
        else:
            a += 1
    ans = ans*K


print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
t =  s + s
ans = 0
i = 1
while True:
    c = 0
    while True:
        if i>=len(t):
            break
        elif t[i-1] == t[i]:
            i +=2
            c+= 1
        else:
            break
    ans += c
    if i>=len(t):
        break
    i+=1
c1 = 0
for i in range(1, len(s)):
    c1 +=1
    if len(set(s[-i:]))!=1:
        break
c2 = 0
for i in range(1, len(s)):
    c2 +=1
    if len(set(s[-i:]))!=1:
        break
c = max(c1, c2)
if c%2 ==0:
    print(((ans* k)+1)//2)
else:
    print(((ans* k))//2)

Submission status: Wrong Answer

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

if len(S) == 1:
    print(K // 2)
    exit()
res, flag = 0, False
for i in range(1, len(S)):
    if i > 1 and S[i] == S[i-1] == S[i-2] and flag:
        flag = False
    elif S[i] == S[i-1]:
        res += 1
        flag = True
res *= K
if S[0] == S[-1] and not flag:
    res += K - 1
print(res)

Submission status: Wrong Answer

Solution code:
import sys
s=input()
k=int(input())
c=0
count=1
answer=0
lastChange=False
roopContinue=False
lastWord=""
if s[0]==s[-1]:
  for i in range(len(s)):
    if i==len(s)-1:
      print(len(s)*k//2)
      sys.exit()
    elif k==1:
      break
    elif s[i+1]==s[0]:
      lastWord+=s[0]
      c+=1
    else:
      break
s+=lastWord
for i in range(len(s)-1):
  if s[i]==s[i+1]:
    count+=1
    if i==len(s)-2:
      answer+=(count-c+1//2)
  else:
    answer+=(count//2)
    count=1
    if i==len(s)-2:
      answer+=(count-c+1//2)
print(answer*k)

Submission status: Wrong Answer

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

L = len(S)

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

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

S += S[0]

cnt = 0
L = len(S)

acc = [0] * L
tmp = 0
cnt = 0
last = False
for i in range(1, L):
    if S[i-1] == S[i]:
        tmp += 1
        if tmp % 2 != 0:
            cnt += 1
            if i == L - 1:
                last = True
    else:
        tmp = 0
    acc[i] = tmp

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

Submission status: Wrong Answer

Solution code:
import math

S = input()
K = int(input())
cnt = 0
tmp = 1
if K == 1:
    for i in range(1, len(S)):
        if S[i - 1] == S[i]:
            tmp += 1
        elif tmp >= 2:
            cnt += tmp // 2
            tmp = 1
        else:
            tmp = 1
    if tmp >= 2:
        cnt += tmp // 2
else:
    g1 = 0
    g2 = 0
    jlst = S + S
    for i in range(1, len(S) * 2):
        if jlst[i - 1] == jlst[i]:
            tmp += 1
        elif tmp >= 2:
            cnt += tmp // 2
            tmp = 1
        else:
            tmp = 1

    if tmp >= 2:
        cnt += tmp // 2

    if cnt % 2 == 0:
        cnt = K * (cnt // 2)
    else:
        g1 = cnt // 2
        g2 = cnt - g1
        v = math.ceil(K / 2)
        if tmp >= 3 and tmp % 2 != 0 and S[0] == S[-1]:
            cnt = (g1 * (K - v)) + (g2 * v)
        else:
            cnt = (g1 * v) + (g2 * (K - v))

print(cnt)

Submission status: Wrong Answer

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

leng = len(S)
result = 0
flg = 0
skp=0

for i in range(1,len(S)):
    if skp==1:
        skp=0
        continue
    #print(i)
    #print(S)
    #print(S[i])
    #print(S[i-1])
    if S[i] == S[i-1]:
        result = result + 1
        S = S[:i-1] +"."+ S[i:]
        skp = 1
        
#print(S)

if S[0] == S[leng-1]:
    result = result + 1
    flg = 1

result = result * K
if flg == 1 :
    result = result - 1
print(result)

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 :
    if (len(S)*K)%2==1:
        print((len(S)*K)//2)
    elif len(S)%2==1 and (len(S)*K)%2==0:
        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:
def p(st):
    l=len(st)
    tc=1
    ac=0
    p=s[0]
    for i in range(1,l):
        if st[i]==p:
            tc+=1
        else:
            ac+=tc//2
            tc=1
            p=st[i]
    else:
        ac+=tc//2
    return ac


s=input()
k=int(input())
n=len(s)
if n==1:
    print(k//2)
    exit()
if n==2:
    if s[0]==s[1]:
        print(k)
    else:
        print(0)
    exit()
ac=p(s)
if k>1:
    ac2=p(s+s)
    print(ac2*k//2)
else:
    print(ac)

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

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

S = input()
N = len(S)
K = INT()

A = []
cur = S[0]
cnt = 1
for i in range(1, N):
    if S[i-1] == S[i]:
        cnt += 1
    else:
        A.append((S[i-1], cnt))
        cnt = 1
A.append((S[N-1], cnt))

change = 0
for s, cnt in A:
    change += cnt // 2

if S[0] == S[N-1] and A[-1][1] % 2 == 1:
    print((change+1)*K-1)
else:
    print(change*K)

Submission status: Wrong Answer

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

import math

C = 0
if S[0]==S[-1]:
    C = 1
if all([True if S[0]==S[i] else False for i in range(len(S))]):
    C = 2

f = '!'
count = 0
if C == 0:
    #print('違う時')
    for i in range(len(S)):
        if S[i]==f:
            count += 1
            f = '!'
        else:
            f = S[i]
    print(K*count)
elif C==1:
    #print('同じ時')
    count = 0
    #前何文字同じかチェック
    Fflag = 0
    for i in range(len(S)):
        if S[i]==S[0]:
            Fflag += 1
        else:
            break
    Bflag = 0
    for j in range(len(S))[::-1]:
        if S[j]==S[-1]:
            Bflag +=1
        else:
            break
    f = '!'
    for k in range(Fflag,Bflag+1):
        if S[k]==f:
            count += 1
            f = '!'
        else:
            f = S[k]
    
    print(math.floor(Fflag/2) + math.floor(Bflag/2)+ count*K + math.floor((Fflag+Bflag)/2)*(K-1))
elif C == 2:
    print(math.floor(len(S)*K/2))

Submission status: Wrong Answer

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



if s[0] != s[len(s)-1]:
	while i < len(s)-2:
		if s[i] == s[i+1]:
			if i == len(s)-2:
				count +=1
				i += 1
			if s[i+1] == s[i+2]:
				count += 1
				i += 2
			else: 
				count += 1
				i += 1
		else:
			i += 1
	if i == len(s)-2:
		if s[i] == s[i+1]:
			count +=1
	print(count*k)

elif s[0] == s[len(s)-1]:
	for i in range(k):
		t += s
	while i < len(t)-2:
		if t[i] == t[i+1]:
			if i == len(t)-2:
				count +=1
				i += 1
			if t[i+1] == t[i+2]:
				count += 1
				i += 2
			else: 
				count += 1
				i += 1
		else:
			i += 1
	if i == len(t)-2:
		if t[i] == t[i+1]:
			count +=1

	print(count)

Submission status: Wrong Answer

Solution code:
from itertools import groupby

s = input()
k = int(input())
a = [sum(1 for _ in g) for _, g in groupby(s)]
if len(a) == 1:
    print(len(s) * k // 2)
else:
    r = sum(x // 2 for x in a) * k
    if s[0] == s[-1]:
        r += ((a[0] + a[-1]) // 2 - a[0] // 2 - a[1] // 2) * (k - 1)
    print(r)

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
cnt=[1]*len(s)
l=0
ans=0
p=0
for i in range(len(s)-1):
    if s[i]==s[i+1]:
        cnt[l]+=1
    elif cnt[l]!=1:
        l+=1
    ans=0
while cnt[p]!=1:
    ans+=cnt[p]//2
    p+=1
print(ans*k)

Submission status: Wrong Answer

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

a = len(S)
b = len(set(S))

ans = 0
cnt = 0
for i in range(1, len(S)):
    #print(i)
    if S[i-1] == S[i] :
        ans += 1          
        if i != len(S)-1 and S[i] == S[i+1]:
            cnt += 1

cnt = cnt // 2
if cnt % 2 == 1:
    cnt += 1
ans = ans - cnt

print(ans*K)

Submission status: Wrong Answer

Solution code:
S = list(input())
N = len(S)
K = int(input())
T = S.copy()
X = S.copy()
S += T
ans = 0
cnt = 0

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

for i in range(1, N):
    if S[i - 1] == S[i]:
        cnt += 1
        S[i] = "-1"
if T[0] == T[-1]:
    cnt2 = 0
    for i in range(N):
        if T[i] == T[i - 1]:
            cnt2 += 1
            T[i] = "-1"
    ans = cnt + cnt2 * (K - 1)
    cnt1 = 0
    if X[0] == X[1]:
        cnt1 += 1

    for i in range(1, N-1):
        if X[i + 1] == X[i]:
            cnt1 += 1
            X[i + 1] = "-1"
    ans = min(ans, cnt1 + cnt * (K - 1))
else:
    ans = cnt * K
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

S = list(map(str,input().split()))
S = [c for c in S[0]]
K = int(input())

count = 0
S2 = S[1:] + [S[0]]

countflag = True
tmp = 0
for i in range(len(S2)):

    if S[i] == S2[i]:
        if tmp == 1:
            tmp = 0
            countflag = True
            
        if countflag:
            count += 1
        else:
            tmp += 1
        
        countflag = False
    else:
        countflag = True

count *= K

print(count)

Submission status: Wrong Answer

Solution code:
import re

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

isAllSameChar = re.search(r'^(.)\1+$', s)
if isAllSameChar:
    print(int(k * len(s) / 2))
else:
    series = [1,]
    for i in range(0, len(s)-1):
        if s[i] == s[i+1]:
            series[-1] += 1
        else:
            series.append(1)
    # print(series)
    changeCharCount = 0
    for sameCharBlockLen in series:
        changeCharCount += int(sameCharBlockLen / 2)
    changeCharCount *= k
    if s[0] == s[-1]:
        if (series[0] % 2 == 1) and (series[-1] % 2 == 1):
            changeCharCount += (k - 1)
    print(changeCharCount)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
n = len(S)
S = S + S[0]
samecount = 1
count = 0
pre = ''
for s in S:
    if s == pre:
        samecount += 1
    else:
        count += samecount//2
        samecount = 1
    pre = s

ans = count * K

if samecount == 1:
    print(ans)
else:
    samecount -= 1
    right = samecount
    if samecount == n:
        if samecount % 2 == 0:
            ans += K*(samecount//2)
        else:
            ans += K*(samecount//2)
            ans += K//2
    else:
        left = 1
        pre = S[0]
        for s in S[1:-1]:
            if s == pre:
                left += 1
            else:
                break
        if right % 2 == 0 or left % 2 == 0:
            ans += K*(right//2)

        else:
            ans += K*(right//2)
            ans += K-1

print(ans)

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()


if S[0] != S[-1]:
    ans_i = 0
    ans_j = 0
    i = 2
    j = 1

    if l % 2 == 0:
        while j < l - 1:
            if S[j - 1] != S[j] and S[j] != S[j + 1]:
                j += 2
            else:
                ans_j += 1
                j += 2
        if S[-2] == S[-1] or S[-1] == S[0]:
            ans_j += 1

        while i < l:
            if S[i - 1] != S[i] and S[i] != S[i + 1]:
                i += 2
            else:
                ans_i += 1
                i += 2
        ans = min(ans_i, ans_j)
        print(ans * K)

    else:
        while j < l:
            if S[j - 1] != S[j] and S[j] != S[j + 1]:
                j += 2
            else:
                ans_j += 1
                j += 2

        while i < l - 1:
            if S[i - 1] != S[i] and S[i] != S[i + 1]:
                i += 2
            else:
                ans_i += 1
                i += 2
        if S[-2] == S[-1] or S[-1] == S[0]:
            ans_i += 1
        ans = min(ans_i, ans_j)
        print(ans * K)

else:
    ans_i = 0
    ans_j = 0
    i = 2
    j = 1
    K_is_odd = False
    l_is_odd = False

    if l % 2 == 1:
        T = S + S
        if K % 2 == 1:
            K_is_odd = True
        K //= 2
        if l % 2 == 1:
            l_is_odd = True
        l *= 2
    else:
        T = S

    #print('modified:T={}, K={}'.format(T, K))

    while j < l - 1:
        if T[j - 1] != T[j] and T[j] != T[j + 1]:
            j += 2
        else:
            ans_j += 1
            j += 2
    if T[-2] == T[-1] or T[-1] == T[0]:
        ans_j += 1

    while i < l:
        if T[i - 1] != T[i] and T[i] != T[i + 1]:
            i += 2
        else:
            ans_i += 1
            i += 2
    if T[-1] == T[0] or T[0] == T[1]:
        ans_i += 1
    ans = min(ans_i, ans_j)
    #print('ans_i={}, ans_j={}'.format(ans_i, ans_j))

    if l_is_odd and K_is_odd:
        remain_i = 0
        remain_j = 0
        T = S
        #print('l and K are odd! T={}'.format(T))
        l = len(S)

        i = 2
        j = 1

        if l % 2 == 0:
            while j < l - 1:
                if T[j - 1] != T[j] and T[j] != T[j + 1]:
                    j += 2
                else:
                    remain_j += 1
                    j += 2
            if T[-2] == T[-1] or T[-1] == T[0]:
                remain_j += 1

            while i < l:
                if T[i - 1] != T[i] and T[i] != T[i + 1]:
                    i += 2
                else:
                    remain_i += 1
                    i += 2
            if T[-1] == T[0] or T[0] == T[1]:
                remain_i += 1

        else:
            while j < l - 1:
                if T[j - 1] != T[j] and T[j] != T[j + 1]:
                    j += 2
                else:
                    remain_j += 1
                    j += 2


            while i < l:
                if T[i - 1] != T[i] and T[i] != T[i + 1]:
                    i += 2
                else:
                    remain_i += 1
                    i += 2
            if T[-1] == T[0] or T[0] == T[1]:
                remain_i += 1


        #print('remain_i={}, remain_j={}'.format(remain_i, remain_j))
        if ans_i < ans_j:
            print(ans * K + remain_i)
        elif ans_i > ans_j:
            print(ans * K + remain_j)
        else:
            print(ans * K + min(remain_i, remain_j))


    else:
        print(ans * 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 i%2 == 1 and s[0] == s[-1]:
  ans += k-1


print(ans)

Submission status: Wrong Answer

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


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


if __name__ == '__main__':
    s = input()
    k = int(input())
    if k <= 2:
        print(count_changes(s, k))
    else:
        base_count = count_changes(s, 3)
        d, rest = divmod(k, 3)
        res = base_count * d
        if rest:
            res += count_changes(s, rest)
        print(res)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math
import collections


def simple_count(S, changed=False):
    count = 0
    for i in range(len(S) - 1):
        if changed:
            changed = False
        else:
            if S[i] == S[i + 1]:
                changed = True
                count += 1
    return count, changed


def solve(S, K):
    if K == 1:
        return simple_count(S, K)[0]
    if S[0] != S[-1]:
        return simple_count(S)[0] * K
    else:
        count2, changed = simple_count(S * 2)
        if changed:
            return count2 * (K // 2) + simple_count(S, changed=False)[0] * (K % 2)
        else:
            return (count2+1) * (K // 2) + simple_count(S, changed=True)[0] * (K % 2)
        # return simple_count(S + S[0])[0] * K


def test(S, K):
    assert solve(S, K) == simple_count(S * K)[0], "{} {} = {} {}".format(S, K, solve(S, K), simple_count(S * K)[0])
assert solve("cooooooooonteeeeeeeeeest", 999993333) == 8999939997
assert solve("aqqq", 2) == simple_count("aqqq" * 2)[0]
test("qq", 81)
# print(solve("qqq", 2), simple_count("qqq" * 2)[0])
assert solve("qqq", 2) == simple_count("qqq" * 2)[0]
# print(solve("qqq", 3), simple_count("qqq" * 3)[0])
assert solve("qqq", 3) == simple_count("qqq" * 3)[0]
assert solve("issii", 2) == simple_count("issii" * 2)[0]




test("iqi", 3)
test("iqqqqiiii", 1)
test("iqqqqiiii", 6)
test("iqqqqiiii", 7)
test("qqqqqiiiii", 7)
test("q", 7)
test("qiq", 1)
test("qiq", 3)
test("qiq", 7)

S = sys.stdin.readline().rstrip()
K, = map(int, sys.stdin.readline().rstrip().split(" "))
print(solve(S, K))

exit(0)

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 + (len_s >= 3 and s[0] == s[-1] and k & 1)
print(solve2())

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
ans = 0
prev = S[0]
left = 0
cnt = 1
for i in range(1, len(S)):
    if prev!=S[i]:
        prev = S[i]
        if cnt!=1:
            ans += cnt//2
        cnt = 1
    else:
        cnt += 1
if cnt!=1:
    if cnt==len(S):
        ans = (len(S)*K)//2
        print(ans)
    else:
        ans += cnt//2
        print(ans*K)
else:
    ans += cnt//2
    print(ans*K)