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:
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    S = input()
    K = int(input())

    s = S + S
    num = 0
    l, prev = 1, ''
    for si in s:
        if si == prev:
            l += 1
        else:
            num += l // 2
            l = 1
        prev = si
    num += l // 2

    n1 = num // 2
    n2 = (num + 1) // 2
    print(n1 * ((K + 1) // 2) + n2 * (K // 2))


main()

Submission status: Wrong Answer

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


if __name__ == '__main__':
    s = input()
    k = int(input())
    if k <= 3:
        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, s[-1])
        print(res)

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:
        if s[0] == s[-1] and s[-1]!=s[-2] and s[0]!=s[1]:
            n += 2
        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)

Submission status: Wrong Answer

Solution code:
S = list(input())
N = len(S)
K = int(input())
T = S.copy()
X = S.copy()
X += T
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[N - 1]:
    # print(cnt)
    # cnt2 = 0
    # for i in range(1, N):
    #     if T[i] == T[i - 1]:
    #         cnt2 += 1
    #         T[i] = "-1"
    # ans = cnt + cnt2 * (K - 1)
    # print(cnt2)
    cnt1 = 0
    skip = set()
    for i in range(N):
        if i in skip:
            continue
        if X[i + 1] == X[i]:
            cnt1 += 1
            skip.add(i + 1)
    ans = min(cnt * (K // 2) + cnt1 * ((K + 1) // 2), cnt1 * ((K + 1) // 2) + cnt * (K // 2))
    ans = min(ans, cnt * K)
    ans = min(ans, cnt1 * K)
else:
    ans = cnt * K
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:
        if s[0] == s[-1] and s[-1]!=s[-2] and s[0]!=s[1]:
            n += 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)

Submission status: Wrong Answer

Solution code:
from sys import stdin

S = stdin.readline().rstrip()
K = int(stdin.readline().rstrip())
ans_odd = 0
ans_even = 0
changed_odd = [False] * len(S)
changed_even = [False] * len(S)

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

cnt = 1
ans = 0
i = 0
begin = 0
end = 0

start = 0
for i in range(1, len(S)):
    if S[i - 1] != S[i]:
        if start == 0:
            begin += cnt
        else:
            if cnt >= 2:
                ans += cnt // 2
        cnt = 1
        start = i
    else:
        cnt += 1
    if i == len(S) - 1:
        end += cnt
        break

if S[0] != S[-1]:
    print(ans * K + (begin // 2) * K + (end // 2) * K)
else:
    print(ans * K + begin // 2 + end // 2 + ((begin + end) // 2) * (K - 1))

Submission status: Wrong Answer

Solution code:
import sys
import os
S = input()
K = int(input())
if len(S) == 1:
    print(0)
    sys.exit()
cnt0 = 1
cntL = 1
cnt = 1
ans = 0
i = 0
while i < len(S)-1:
    for j in range(i+1,len(S)):
        if S[i] == S[j]:
            cnt +=1
        else:
            if i == 0:
                cnt0 = cnt
            i = j
            break
    if cnt > 1:
        ans += cnt//2
        cnt = 1
        i = j

for i in range(len(S)-1,0,-1):
    if S[i] == S[i-1]:
        cntL +=1
    else:
        break
ans = K*ans
if S[0] == S[-1] and cnt0%2 != 0 and cntL %2 != 0:
    ans += K-1

print(ans)

Submission status: Wrong Answer

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


if __name__ == '__main__':
    s = input()
    k = int(input())
    if k <= 3:
        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, s[-1])
        elif s[0] == s[-1]:
            res += (d - 1)
        print(res)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
s = s * 2
count = 0
i = 0
while i <= len(s):
    tmp = 1
    for j in range(i + 1, len(s)):
        if s[i] == s[j]:
            tmp += 1
        else:
            break
    i += tmp
    tmp //= 2
    count += tmp

print(count * k // 2)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys
import random
import itertools
import math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt


def LI(): return list(map(int, input().split()))


def LF(): return list(map(float, input().split()))


def LI_(): return list(map(lambda x: int(x)-1, input().split()))


def II(): return int(input())


def IF(): return float(input())


def LS(): return list(map(list, input().split()))


def S(): return list(input().rstrip())


def IR(n): return [II() for _ in range(n)]


def LIR(n): return [LI() for _ in range(n)]


def FR(n): return [IF() for _ in range(n)]


def LFR(n): return [LI() for _ in range(n)]


def LIR_(n): return [LI_() for _ in range(n)]


def SR(n): return [S() for _ in range(n)]


def LSR(n): return [LS() for _ in range(n)]


def gcd(x, y):
    if y == 0:
        return x
    return gcd(y, x % y)


def my_index_multi(l, x):
    return [i for i, _x in enumerate(l) if _x == x]


mod = 1000000007
inf = float('INF')


def A():
    S = input()

    K = II()

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

    i = 0
    ans = 0
    A = 1
    while(A > 0):
        count = 0
        while(S[i] == S[i+1]):
            count += 1
            i += 1
        if(S[i] != S[i+1]):
            i += 1
        if count == len(S)-2:
            print(((len(S)-1)*K)//2)
            return

        if count > 0:
            ans += (count+1)//2

        if S[-1] == S[i]:
            A = -1
    if K >= 2 and S[-2] == S[0] and (S[-3] != S[-2] or S[0] != S[1]):
        ans += 1
        print((ans*K)-1)
    else:
        print(ans*K)

    return

# B


def B():
    N, K = map(int, input().split())
    h = LI()
    count = 0

    for i in h:
        if i >= K:
            count += 1
        else:
            continue
    print(count)
    return

# C


def C():
    N = II()
    A = LI()
    J = [0] * N
    for i, n in enumerate(A):
        J[n-1] = i+1
    for s in J:
        print(s, end=" ")

    return

# D


def D():
    A, B = map(int, input().split())
    g = gcd(A, B)
    # go = g
    # myset = set([1])
    sq = int(sqrt(g)) + 2
    ans = 1

    for i in range(2, sq):
        if g % i == 0:
            ans += 1
        while g % i == 0:
            g /= i

            # myset.add(i)
        if g == 1:
            break
        if i == sq - 1:
            # myset.add(go)
            ans += 1

    print(ans)

    return

# E


def E():
    N, M = LI()
    bit_max = 1 << N
    dp = [[inf for i in range(bit_max)] for j in range(M+1)]
    dp[0][0] = 0

    for i in range(M):
        a, b = LI()

        c = LI()
        bit = 0

        for d in c:
            bit |= 1 << (d-1)

        for w in range(bit_max):

            t = w | bit
            cost = dp[i][w] + a

            dp[i+1][t] = min(dp[i+1][t], cost)

            dp[i+1][w] = min(dp[i][w], dp[i+1][w])

    print(-1) if dp[-1][-1] == inf else print(dp[-1][-1])

    return

# F


def F():
    N, M = LI()

    value = [0] * M
    power = [0] * M
    for i in range(M):
        a, b = LI()
        value[i] = a
        c = LI()
        L = ['0'] * N
        for k in c:
            L[k - 1] = '1'
        bint = ''.join(L)
        power[i] = int(bint, 2)
    bint = ''.join(list(['1'] * N))
    dp = [inf] * (int(bint, 2)+1)
    t = 0
    cost = 0

    dp[0] = 0
    for s in range(int(bint, 2) + 1):
        for i in range(M):
            t = s | power[i]
            cost = dp[s] + value[i]
            dp[t] = min(dp[t], cost)
    print(-1) if(dp[-1] == inf) else print(dp[-1])

    return

    # you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")


def solution(S):
    # write your code in Python 3.6
    import math

    vowels = "AIUEO"
    count = 0
    consonants = 0
    newC = []
    newV = []

    for i in S[:-1]:
        if i in vowels:
            count += 1
            newV.append(i)
        else:
            consonants += 1
            newC.append(i)

    if consonants < count or consonants - 2 > count:
        return 0

    a = math.factorial(len(set(newC))) * math.factorial(len(set(newV)))
    return a

    pass


# Solve
if __name__ == '__main__':
    A()

Submission status: Wrong Answer

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

def c1(s):#len(s)>1
  count1=0
  S = list(s)
  l_s=len(S)
  for i in range(1,l_s):
    #print(S,count)
    if S[i]==S[i-1] and i<l_s-1:
      for j in code_list:
        if j != S[i+1] and j !=S[i-1]:
          S[i]=j
          count1+=1
          break
    elif S[i]==S[i-1]:
      for j in code_list:
        if j != S[i-1] and j !=S[0]:
          S[i]=j
          count1+=1
          break
  return count1,S[-1]

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

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

Submission status: Wrong Answer

Solution code:
a = input()
c = int(input())
if len(a) == 1:
  print(c//2)
else:
  d = []
  for i in range(len(a)):
    d.append(a[i])
  b = 0
  for i in range(len(d)-2):
    if d[i] == d[i+1]:
      d[i+1] = 'S'
      b += 1
  if d[0] == d[-1]:
    b += 1
    print(b*c-1)
  else:
    print(b*c)

Submission status: Wrong Answer

Solution code:
X=input()
a=int(input())
count=0
ans=0
z=list(X)
b=0
if len(z)==1:
    ans=int(a/2)
elif z[0]!=z[len(z)-1]:
    for i in range(len(z)-1):
        if z[i]==z[i+1]:
            z[i+1]=0
            count=count+1
        ans=count*a
else:
     for k in range(len(z)-1):
        if z[k]==z[len(z)-1]:
            b+=1
     for i in range(len(z)-1):
        if z[i]==z[i+1]:
            z[i+1]=0
            count=count+1
     ans=int(a*count+(a-1)*int((((b+1)/2)-1)))

print(ans)

Submission status: Wrong Answer

Solution code:
S=list(input())
K=int(input())
c=0
for i in range(1,len(S)):
  if S[i]==S[i-1]:
    S[i]="!"
    c+=1
if S[0]!=S[-1]:  
  print(c*K)
else:
  print(c*K+K-2)

Submission status: Wrong Answer

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

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

if S[0] != S[-1]:
    change = 0
    T = list(S)
    for i, t in enumerate(T[1:-1], start=1):
        if t == T[i - 1] and T[i - 1] != '.':
            change += 1
            T[i] = '.'
    print(change * K)
else:
    T1 = list(S)

    change1 = 0
    for i, t in enumerate(T1[2:], start=2):
        if t == T1[i - 1]:
            change1 += 1
            T1[i] = '.'

    T2 = list(S)
    change2 = 0
    for i, t in enumerate(T2[1:-1], start=1):
        if t == T2[i - 1]:
            change2 += 1
            T2[i] = '.'

    ans = min(
        change1 * K + K - (1 if S[0] != S[1] else 0),
        change2 * K + K - (1 if S[-2] != S[-1] else 0)
    )

    print(ans)

Submission status: Wrong Answer

Solution code:
#-------------------------------------------------------------------
import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
#-------------------------------------------------------------------
s = input()		# 文字列の入力  abcdefg...
k = int(input())						# 5


ans=0

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

if len(s)==2:
	if s[0]==s[-1]: ans=k
	print(ans)
	exit(0)



# グループ作成
#「groupby」は、「キーとグループ」を返すようなイテレータを作成します
from itertools import groupby
#s='AAABBCABBB'

#「groupby」は、キーとグループを返すようなイテレータを作成します
m=[]
for key, g in groupby(s):
	#print( key, list(g) )
	m.append([key,len(list(g))])

p(m)

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

	for i in range(1,len(m)-1):
		ans+=(m[i][1]//2)

	ans+=(m[0][1]//2)
	ans+=(m[-1][1]//2)

	ans+=((m[0][1]+m[-1][1])//2)*(k-1)

else:
	for a,i in m:
		#p(a,i)
		ans+=(i//2)*k


print(ans)

Submission status: Wrong Answer

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

for i in range(K-1):
    S.extend(S)

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)

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:
        if s[0] == s[-1] and s[0]!=s[1]:
            n += 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:
a=input()
b=int(input())
s="A"
zn=0
n=0
nn=0
i=0
aa=[]
ab=[]
abb=0

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 len(ab)>2:
    if ab[-1]!=1:
        abb=ab[-1]
        del ab[-1]
    s=aa[0]
    i=0
    for x in aa:
        if aa[i]==s:
            nn+=1
        else:
            break
        i+=1
    i=1
    for x in aa:
        if aa[-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((b*ab[0])//2)
else:
    print(zn*b+(nn//2)*(b-1)+abb//2)

Submission status: Wrong Answer

Solution code:
from sys import stdin

S = stdin.readline().rstrip()
K = int(stdin.readline().rstrip())
ans_odd = 0
ans_even = 0
changed_odd = [False] * len(S)
changed_even = [False] * len(S)

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

cnt = 1
ans = 0
i = 0
begin = 0
end = 0

start = 0
for i in range(1, len(S)):
    if S[i - 1] != S[i]:
        if start == 0:
            begin += cnt
        else:
            if cnt >= 2:
                print(cnt)
                ans += cnt // 2
        cnt = 1
        start = i
    else:
        cnt += 1
    if i == len(S) - 1:
        end += cnt
        break

if S[0] != S[-1]:
    print(ans * K + (begin // 2) * K + (end // 2) * K)
else:
    print(ans * K + begin // 2 + end // 2 + ((begin + end) // 2) * (K - 1))

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
# 一番効率が良いのは3連続で同じ文字列が続くときに真ん中を別の文字に変換する場合
# 2連続で同じ文字が続く場合は後ろの文字を変える.次の文字からまた数え直す
# 最後の文字列については無視すれば良い

# S=''.join([S for _ in range(K)])
same=False
count = 0
flag=False
flag_odd=False
for i in range(1,len(S)):
    if not same:
        if S[i]==S[i-1]:
            same=True
            count+=1
        else:
            pass
    else:
        same=False
    if not same and i==K and S[0] == S[-1]:
        flag=True
        

same=False
count_odd = 0
for i in range(1,len(S)):
    if not same:
        if S[-i]==S[-i-1]:
            same=True
            count_odd+=1
        else:
            pass
    else:
        same=False
    if not same and i==K and S[0] == S[-1]:
        flag_odd=True
    # 文字列の最後尾が変更されずに、かつ前の文字列と同じであった場合は最後尾もしくは、最前を変える必要がある
min_count = min(count*K+flag*(K-1), count_odd*K+flag_odd*(K-1))
print(min_count)

# countは一つの文字列中に存在する必要変換回数.ただし、(文末が変化しない場合)文末の文字が文頭のものとかぶる場合があるので、その場合については、
# 文頭を変化させる

Submission status: Wrong Answer

Solution code:
S=list(input())
K=int(input())
c=0
for i in range(1,len(S)):
  if S[i]==S[i-1]:
    S[i]="!"
    c+=1
if S[0]!=S[-1]:  
  print(c*K)
else:
  print(c*K+K-1)

Submission status: Wrong Answer

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

l=len(s)
first=0
second=0
last=0
S=s+s
LS=list(S)

for n in range(2*l):
  if n==0:
    pass
  if 0<n<=l-1:
    if LS[n-1]==LS[n]:
      first+=1
      LS[n]=str(n)
  if l<=n<2*l-1:
    if LS[n-1]==LS[n]:
      second+=1
      LS[n]=str(n)
  if n==2*l-1:
    if LS[n-1]==LS[n] or LS[n]==LS[0]:
      last+=1
      LS[n]=str(n)

if K%2==0:
  ans=(first+second)*(K//2)+last*((K//2)-1)
else:
  ans=(first+second)*(K//2)+first+last*((K//2)-1)

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

if S[0] != S[-1]:
    change = 0
    T = list(S)
    for i, t in enumerate(T[1:-1], start=1):
        if t == T[i - 1] and T[i - 1] != '.':
            change += 1
            T[i] = '.'
    print(change * K)
else:
    T1 = list(S)

    change1 = 0
    for i, t in enumerate(T1[2:], start=2):
        if t == T1[i - 1]:
            change1 += 1
            T1[i] = '.'

    T2 = list(S)
    change2 = 0
    for i, t in enumerate(T2[1:-1], start=1):
        if t == T2[i - 1]:
            change2 += 1
            T2[i] = '.'

    ans = min(
        change1 * K + K - (1 if S[0] != S[1] else 0),
        change2 * K + K - (1 if T2[-2] != T2[-1] else 0)
    )

    print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
n=int(input())
i = 0
replace = 0
first = 0
last = 0
while i < len(s):
    c = s[i]
    count=0
    for c1 in s[i+1:len(s)]:
        if c != c1:
            break
        count += 1
    if count > 0:
        replace += int((count + 1)/2)
        firstCheck = False
        if i == 0:
            first = count
            firstCheck=True
        i += count +1
        if i >= len(s) and not(firstCheck):
            last = count
    else:
        i += 1
if s[0] != s[-1]:
    print(int(replace * n))
else:
    print(int((replace ) * n + (int((first + last + 1)/2) - int((first+1)/2) - int((last+1)/2)) * (n - 1)))

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+2):
        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())
# s = 'si'
# k = 3

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

Submission status: Wrong Answer

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

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

another_c = ''
cnt = 0


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:
        another_c = chr(ord(c) + 1)
        if ord(another_c) > ord('z'):
            another_c = chr(ord(another_c)-26)
        if next_c == another_c:
            another_c = chr(ord(another_c) + 1)
        t[i] = another_c
        cnt = cnt + 1
    else:
        another_c = ''
    #print(i, prev_c, c, next_c, "->", another_c) 

#print("".join(t))

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

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

    #print("".join(t))

print(cnt)

Submission status: Wrong Answer

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

N=len(S)
t=S[0]
cnt=0
arr=[]
for s in S[1:N]:
    if t==s:
        cnt+=1
    else:
        arr.append(cnt+1)
        cnt=0
    t=s
arr.append(cnt+1)
ans=sum(map(lambda x: x//2, arr))
if len(arr)>1 and S[0]==S[-1] and arr[0]%2==1 and arr[-1]%2==1:
    print((ans+1)*K-1)
elif len(arr)==1 and N%2==1:
    if N==1:
        print(K//2)
    else:    
        print((ans+1)*K)
else:
    print((ans)*K)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
print(1)

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 *= 3
    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]
                    
    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:
#code
s = input()
k = int(input())
l = 1
m = 0
for i in range(0, len(s) - 1):
    if s[i] == s[i + 1]:
        l += 1
    else:
        l = int(l / 2)
        m += l
        l = 1
if l > 1:
    l = int(l / 2)
    m += l
print(m * k)

Submission status: Wrong Answer

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

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


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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

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

def check_duplicate(s):
    pre_s = None
    cnt_list = []
    cnt = 1
    for _s in s:
        if pre_s == _s:
            cnt += 1
        else:
            if cnt>1:
                cnt_list.append(cnt)
            cnt = 1
        # print(pre_s, _s, cnt, cnt_list)
        pre_s = _s
    if cnt>1:
        cnt_list.append(cnt)
    if len(cnt_list)==0:
        cnt_list.append(cnt)
    return cnt_list

def calc_ans(cnt_list, k):
    _ans = 0
    for c in cnt_list:
        _ans += c//2
    return(_ans * k)


if len(s)==1:
    ans = k//2
    print(ans)
else:
    cnt_list = check_duplicate(s)
    if len(cnt_list)>1:
        print(calc_ans(cnt_list, k))
    elif len(cnt_list)==1:
        if cnt_list[0]%2==0:
            ans = cnt_list[0]//2 * k
            print(ans)
        else:
            ans = cnt_list[0] * k //2
            print(ans)
    else:
        if s[0]==s[-1]:
            ans = k//2
            print(ans)

Submission status: Wrong Answer

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

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

if S[0] != S[-1]:
    change = 0
    T = list(S)
    for i, t in enumerate(T[1:-1], start=1):
        if t == T[i - 1] and T[i - 1] != '.':
            change += 1
            T[i] = '.'
    print(change * K)
else:
    T1 = list(S)

    change1 = 0
    for i, t in enumerate(T1[2:], start=2):
        if t == T1[i - 1] and T1[i - 1] != '.':
            change1 += 1
            T1[i] = '.'

    T2 = list(S)
    change2 = 0
    for i, t in enumerate(T2[1:-1], start=1):
        if t == T2[i - 1] and T2[i - 1] != '.':
            change2 += 1
            T2[i] = '.'

    ans = min(
        change1 * K + K - (1 if S[0] != S[1] else 0),
        change2 * K + K - (1 if T2[-2] != T2[-1] else 0)
    )

    print(ans)