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

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

S1 = list(S)
S2 = list(S) + list(S)
S3 = list(S) + list(S) + list(S)

def calc(T):
    L = T[::]
    ret = 0
    for i, t in enumerate(L[1:], start=1):
        if t == L[i - 1]:
            ret += 1
            L[i] = '.'
    return ret

change = [0, 0, 0]
for i, T in enumerate([S1, S2, S3]):
    change[i] = min(calc(T), calc(T[:: -1]))

diff1 = change[1] - change[0]
diff2 = change[2] - change[0]
same = diff1 * 2 - diff2

ans = diff1 * K - same * (K - 1)
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
from itertools import groupby

s = stdin.readline().rstrip()
k = int(stdin.readline().rstrip())

def cou(st):
    li = []
    point = 1
    for i in range(len(st)-1):
        if st[i] == st[i+1]:
            point += 1
            if i == len(st)-2:
                li.append(point)
        else:
            li.append(point)
            point = 1
    if st[-1] != st[-2]:
        li.append(1)
    return li

if len(s) == 1:
    lin = [1]
else:
    lin = cou(s)
if len(lin) == 1:
    print(lin[0]*k//2)
    exit()

lin2 = lin

if k > 1:
    if s[0] == s[-1]:
        lin[-1] += lin[0]
else:
    if len(s) == 1:
        print(0)
        exit()
point = 0
point2 = 0
point3 = 0
for i in range(1,len(lin)):
    point3 += lin[i]//2

for w in range(1,len(lin2)):
    point2 += lin2[w]//2

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

Submission status: Wrong Answer

Solution code:
T = input()
# T = "xaaxxxx"
K = int(input())
# S = T*K
S_ = T*2
N = len(T)
last = T[-1]
mod = 10**9+7
"""
後ろを前につなげて、一番長くなるのはいくつ？
cooooooooonteeeeeeeeeest
3
"""
def check(S):
    S += "1"
    cnt = 1
    pre = "-1"
    flg = False
    ret = 0
    for i, s in enumerate(S):
        if pre == s:
            cnt += 1
            flg = True
        else:
            if flg:
                ret += cnt // 2
                flg = False
            cnt = 1
        pre = s
    return ret

s = 0
for i in range(N):
    if last != T[i]:
        s = i
        break
e = 0 
for i in range(N):
    if last != T[-i]:
        e = -i
        break
# 一回
if K == 1:
    print(check(T))
    exit()
# 同じ文字の連続
if len(set(T)) == 1:
    ans = check(T*2)*(K//2)+check(T)*(K%2)
    print(ans)
    exit()
# 最初と最後が違う
if s == e:
    print(check(T)*K)
    exit()


# その他
pre = T[:s]
mid = S_[e+N-1:s+N]
end = T[e-1:]
a = check(pre)    
b = check(mid)    
c = check(end)
print(a+b*(K-1)+c)

Submission status: Wrong Answer

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

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 len(s) == 1:
    ans = k // 2
elif s_cnt == len(s) and e_cnt == len(s) and len(s) % 2 == 1 and len(s) != 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:
S = input()
K = int(input())
 
c = S[0]
isSameCahr = True
for i in range(0, len(S)):
    if c != S[i]:
        isSameCahr = False
        break

if isSameCahr:
    print(int(len(S) * K / 2))
else:    
    count = 0
    bef = ""
    isbefchange = False
    for i in range(0, len(S)):
        if(isbefchange == False and S[i] == bef):
            count += 1
            isbefchange = True
            bef = S[i]
            continue
        bef = S[i]
        isbefchange = False
    count *= K
    if(isbefchange == False and S[len(S)-1] == S[0] and S[1] != S[0]):
        count += (K-1)
    print(count)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
N = len(S)
counts = 0
count2 = 0
adder = 0
adder2 = 0

if len(S) == 1:
    print(K // 2)
elif len(S) == 2:
    if S[0] == S[1]:
        print(K)
    else:
        print(0)

elif len(S) == 3 and(S[0] == S[1]) and (S[1] == S[2]):
    if K % 2 == 0:
        print(int(3*K/2))
    elif K == 1:
        print(1)
    else:
        print(2 + 2*(K-2))

elif K == 3:
    S_arranged = list(S*3)
    for i in range(len(S_arranged)-2):
        if (S_arranged[i] == S_arranged[i+1]) and (S_arranged[i+1] == S_arranged[i+2]):
            S_arranged[i+1] = "0"
            counts += 1
        
    for i in range(len(S_arranged)-1):
        if S_arranged[i] == S_arranged[i+1]:
            S_arranged[i] = "1"
            counts += 1
            
    print(counts)

else:
    S_arranged = list(S*4)
    for i in range(len(S_arranged) - 2):
        if (S_arranged[i] == S_arranged[i+1]) and (S_arranged[i+1] == S_arranged[i+2]):
            S_arranged[i+1] = "0"
            if i+1 <= len(S)-1:
                adder += 1
            elif i+1 > len(S)*3 -1:
                adder2 += 1
            elif i+1 > len(S) - 1 and i+1 <= len(S)*2 - 1:
                counts +=1
            else:
                count2 +=1

    for i in range(len(S_arranged)-1):
        if S_arranged[i] == S_arranged[i+1]:
            S_arranged[i] = "1"
            if i <= len(S)-1:
                adder += 1
            elif i > len(S)*3 -1:
                adder2 += 1
            elif i > len(S)-1 and i+1 <= len(S)*2-1:
                counts +=1
            else:
                count2 +=1


    if K % 2 == 0:
        print(int(adder + (counts + count2) * ((K-2)/2) + adder2))

    else:
        print(int((adder + counts * ((K-1)/2) + count2 * ((K-1)/2 -1) +adder2)))

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
a = []
ans = 0
count = 0
for i in range(len(S)-1):
    if S[i] == S[i+1] :
        count += 1
    else:
        if count!=0:
            ans += (count+1)//2
            a.append(count+1)
            count = 0
if count !=0:
    ans += (count+1)//2 
ans = ans*K
a.append(0)
if len(S) == 1:
    ans = K//2
else:    
    
    if (count+1)%2==1 and a[0]%2 == 1 and S[0]== S[1] and S[0] == S[len(S)-1]:
    
        ans+=K-1
    if S[0]!= S[1] and (count+1)%2 ==1 and  S[0] == S[len(S)-1]:
        ans+=K-1
    if count+1 == len(S):
        ans+=K-1
 
print(ans)

Submission status: Wrong Answer

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

import re
def nchars(s, n):
    """文字列 s に、同じ文字が n 個以上連続している部分文字列を見つける
    """

    assert n > 0
    reg = re.compile("(.)\\1{%d,}" % (n - 1))  # カンマを取ると 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:
            chain_hajime=0
            chian_owa=0
            for i in range(len(s)):
                if s[0]==s[i]:
                    chain_hajime+=1
                else: break
            for i in range(len(s)):
                if s[-1]==s[-(i+1)]:
                    chian_owa+=1
                else: break
            a=list(nchars(s, 2))
            b=0
            for i in a:
                b+=len(i)//2
            if chian_owa%2==1 and chain_hajime%2==1:
                print(b*k+k-1)
            else:
                print(b*k)

Submission status: Wrong Answer

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

if len(S) == 1:
    print(K // 2)
else :
    pre = S[0]
    flg = 0
    cnt = 0
    cnt_list =[]
    
    for i in range(1, len(S)):
        cnt += 1
        if flg == 1:
            flg = 0
            pre = S[i]
            continue
        if S[i] == pre:
            cnt_list.append(cnt)
            flg = 1
        pre = S[i]
    
    S = S[::-1]
    pre = S[0]
    flg = 0
    cnt = 0
    cnt_list2 =[]
    
    for i in range(1, len(S)):
        cnt += 1
        if flg == 1:
            flg = 0
            pre = S[i]
            continue
        if S[i] == pre:
            cnt_list2.append(cnt)
            flg = 1
        pre = S[i]
    
    if S[0] == S[-1] and (S[-1] == S[-2] and len(S)-1 in cnt_list):
        print(len(cnt_list)*K)
    elif S[0] == S[-1] and (S[0] == S[1] and 0 in cnt_list2):
        print(len(cnt_list2)*K)
    elif S[0] == S[-1]:
        print(len(cnt_list)*K+K)
    else :
        print(len(cnt_list)*K)

Submission status: Wrong Answer

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

if flag == True:
    print((sum_count)*k+1)
elif len(s) == 1:
    print(k//2)
else:
    print(sum_count*k)

Submission status: Wrong Answer

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

def counter(string):
    count = 0
    answer = 0
    prev = ""
    for index,sss in enumerate(string):
        if prev == "" or sss == prev:
            count+=1
            if count == 2: 
                answer+=1
                count=0
                string[index] = "1"
        else:
            count=1
        prev = sss
    return answer,string

kk = k//2

if k%2 == 0: 
    answer, string = counter(ss)
    if string[0] == string[-1]:
        print(answer*kk+kk-1)
    else:
        print(answer*kk)
else:
    answer, string = counter(s)
    answer2, string2 = counter(ss)

    if string2[0] == string2[-1]:
        print(answer2*kk + answer + kk)
    else:
        print(answer2*kk + answer)

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
p=s[0]
cnt=1
q=[]
for i in range(len(s)-1):
	if s[i+1]==p:
		cnt+=1
	else:
		q.append(cnt)
		cnt=1
		p=s[i+1]
q.append(cnt)
if q[-1]%2==0:
	cnt=True
else:
	cnt=False
ans=0
if cnt==True:
	for i in range(len(q)):
		ans+=q[i]//2
	print(ans*k)
	exit()
if len(q)==1:
	print(len(s)*k//2)
	exit()
for i in range(len(q)-1):
	ans+=q[i+1]//2
ans*=k
ans+=q[0]//2
if s[0]==s[-1]:
	ans+=(q[0]+q[-1])//2*(k-1)
ans+=q[-1]//2
print(ans)

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) & (T[-1] == T2[0]):
        if K % 2 == 1:
            print(countA*K+K-1)
    else:
        if K % 2 == 1:
            print(K//2*countB+countA)
        else:
            print(K//2*countB)
else:
    print(countA)

Submission status: Wrong Answer

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

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

cnt=1
ans1=0
c=[]
for i in range(1,LS-1):
    if S[i-1]==S[i]:
        cnt+=1
    elif cnt!=1:
        ans1+=cnt//2
        c.append(cnt)
        cnt=1

if K==1:
    if S[-2]==S[-1]:
        cnt+=1
        ans1+=cnt//2
else:
    if S[-2]==S[-1]:
        cnt+=1
        c.append(cnt)
        if cnt%2==0:
            if ans1!=0:
                ans1+=cnt//2
                ans1=ans1*K
            else:
                ans1=K
        elif cnt%2==1 and S[0]==S[-1]:
            if ans1!=0:
                if c[0]%2==0:
                    ans1+=cnt//2
                    ans1=ans1*K
                else:
                    ans1+=cnt//2+1
                    ans1=ans1*K-1
            else:
                ans1=(cnt//2)*K+K-1
        elif cnt%2==1 and S[0]!=S[-1]:
            ans1+=cnt//2
            ans1=ans1*K
    else:
        ans1+=cnt//2
        c.append(cnt)
        c.append(1)
        if S[0]==S[-1]:
            if ans1!=0:
                ans1=ans1*K+K-1
            else:
                ans1=K-1
        else:
            ans1=ans1*K
print(ans1)

Submission status: Wrong Answer

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



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
	elif s[0] == s[len(s)-1]:
		count += 1
print(count*k)

Submission status: Wrong Answer

Solution code:
S_list = [s for s in input()]
K = int(input())
count = 0
if len(S_list) % 2 == 1 and len(list(set(S_list))) == 1:
    print((len(S_list) * K) // 2)
elif K > 1:
    S_list.append(S_list[0])
    for idx, s in enumerate(S_list):
        if idx == len(S_list) - 1:
            break
        if S_list[idx + 1] == s:
            count += 1
            S_list[idx + 1] = "*"
    print(count * K)
else:
    for idx, s in enumerate(S_list):
        if idx == len(S_list) - 1:
            break
        if S_list[idx + 1] == s:
            count += 1
            S_list[idx + 1] = "*"
    print(count * K)

Submission status: Wrong Answer

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

def main():
    s = list(input().rstrip())
    k = int(input())


    if len(s) == 1:
        print(k//2)
    else:
        sa = s[:]

        c1 = 0
        c2 = 0

        for i in range(len(s) - 1):
            if sa[i] == sa[i+1]:
                c1 += 1
                sa[i+1] = '1'

        if sa[0] == sa[-1]:
            s[0] = '2'
            c2 += 1

            for i in range(len(s) - 1):
                if s[i] == s[i+1]:
                    c2 += 1
                    s[i+1] = '2'


            if k % 2 == 0:
                x = k // 2
                print(c1*x + c2*x)
            else:
                x = k // 2
                print(c1*(x+1) + c2*x)
        else:
            print(c1*k)

if __name__ == '__main__':
    main()

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] and S[0] != S[1]:
        ans += 1
        print((ans*K)-1)
    elif 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 = list(input())
l = int(len(S))      ##Sの長さ
K = int(input())
counter_mae = 0            ##最初からカウント             
counter_ato = 0
count = 0
Frag = False         ##偶奇フラグ(Trueは影響なし)
Frag_z = False       ##全部同じかどうか
if S[0] == S[l - 1]:        ##先頭と末尾が同じとき
    for i in range(1,l):        ##先頭からの何文字目まで同じか
        if S[i] == S[0]:
            counter_mae += 1
        else:
            break
    if counter_mae == l-1:        ##全文字同じ場合 
        Frag_z = True
    else:                       ##全文字同じではないとき
        for i in range(l - 1):
            if S[0] == S[l - i - 1]:        ##後ろから何文字まで同じか
                counter_ato += 1

            else:
                break

        if counter_ato % 2 == 0:
            Frag = True
        else:
            if counter_mae % 2 == 0:
                Frag = True

for i in range(len(S) - 1):     ##Sの中の書き換え操作回数
    if S[i] == S[i + 1]:
        count += 1
        S[i + 1] = "0"


if Frag_z == True:
    if l % 2 == 0:
        print(K * count)
        
    else:
        if K % 2 == 0:
            print((K // 2) *(2 * count + 1))
            
        else:
            
            print((K // 2) * (count + 1) + (K // 2 + 1) * (count))
else:
    if Frag == True:
        print(K * count)

    else:
        print(count + (K - 1) * count)

Submission status: Wrong Answer

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

def func(ms):
    r = 0
    k = 0
    m = ms[0]
    for i in range(1, len(ms)):
        if ms[i] == ms[i-1]:
            k+=1
        else:
            r += int((k+1)/2)
            k = 0
        m = ms[i]
    r += int((k+1)/2)
    return r            

m = S[0]
f = True
for i in range(1, len(S)):
        if S[i]  != S[i-1]:
            f = False
            
if f == True:
    if len(S) == 1:
        print(int(K/2))
    else:
        print(int(len(S) * K / 2))
elif len(set(S)) == len(S):
    print(0)
elif S[0] == S[-1]:
    r = 0
    m = S[0]
    f = 0
    for c in S:
        if c == m:
            r += 1
            f += 1
        else:
            break   
    s = S[::-1]
    fe = 0
    for c in s:
        if c == m:
            r += 1
            fe += 1
        else:
            break
    S = S[f:-fe]
    tr = int(r/2) * (K-1)
    r = func(S) * K
    print(r+tr)
else:
    r = 0
    k = 0
    m = S[0]
    for i in range(1, len(S)):
        if S[i] == S[i-1]:
            k+=1
        else:
            r += int((k+1)/2)
            k = 0
        m = S[i]
    r += int((k+1)/2)
    print(r*K)

Submission status: Wrong Answer

Solution code:
a=list(input())
k=int(input())
ans=0
if len(a)==1:
  print(k//2)
elif len(list(set(a)))==1:
  print((len(a)*k)//2)
else:  
  for i in range(len(a)-1):
    if a[i]==a[i+1]:
      a[i+1]='1'
      ans+=1
  if a[0]==a[-1]:
    print(ans*k+k-1)
  else:
    print(ans*k)

Submission status: Wrong Answer

Solution code:
word_line=input().rstrip()
#print(word_line)
numK=int(input())

char_list=list(word_line)
#print(char_list)

if(char_list[0]!=char_list[len(char_list)-1]):
    s=0
    for i in range(1,len(char_list)-1):
       if(char_list[i-1]==char_list[i]):
           char_list[i]="wa"
           s+=1
    s=s*numK    

else:
    allsame=0
    for i in range(len(char_list)-1):
        if(char_list[i]==char_list[i+1]):
            allsame+=1
    if(allsame==len(char_list)-1)or(len(char_list)==1):
        s=(len(char_list)*numK)//2
    
    else:
        count_a=1
        i=0
        while (char_list[i]==char_list[i+1])and(i<len(char_list)-2):
            count_a+=1
            i+=1
        
        count_b=1
        i=1
        while(char_list[len(char_list)-i]==char_list[len(char_list)-i-1])and(i<len(char_list)-2):
            count_b+=1
            i+=1
            
        #print(count_a)
        #print(count_b)
        
        s=0
        #print(len(char_list)-count_b-1)
        #for i in range(2,3):
        #    print("aaa")
        if(count_a+count_b+1==len(char_list)):
            s=0
        else:
            for i in range(count_a+1,len(char_list)-count_b-1):
                if(char_list[i-1]==char_list[i]):
                    char_list[i]="wa"
                    s+=1
            
        #print(s)
        s=s*numK
        #print(s)
        s+=(count_a//2+count_b//2+(count_a+count_b)//2*(numK-1))
    
print(s)

Submission status: Wrong Answer

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

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

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

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

print(scr*K+x)

Submission status: Wrong Answer

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

A=[]
D=1
for i in range(len(S)-1):
  if S[i]==S[i+1]:
    D+=1
  else:
    A.append(D)
    D=1
A.append(D)
B=[i for i in A if not i==1]
C=[(i//2) for i in B]
Scount=(sum(C))

E=[]
H=1
for j in range(len(T)-1):
  if T[j]==T[j+1]:
    H+=1
  else:
    E.append(H)
    H=1
E.append(H)
F=[j for j in E if not j==1]
G=[(j//2) for j in F]
Tcount=(sum(G))
ans=(Tcount-Scount)*(K-1)+Scount
print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
ans=0
cnt=1
add=0
lead=1
if len(s)==1:
  print(k//2)
  exit()
for i in range(1,len(s)):
  if s[i-1] == s[i]:
    lead+=1
  else:
    break
for i in range(1,len(s)):
  if s[i-1] == s[i]:
    cnt+=1
  else:
    ans+=cnt//2
    cnt=1
  if s[0] == s[-1] and lead%2!=0 and i == (len(s)-1) and cnt%2==1:
    ans+=cnt//2
    add=1*(k-1)
  elif i == (len(s)-1) and cnt>=2:
    ans+=cnt//2
print(ans*k+add)

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
        # print(i, S2[i])

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

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

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

    print(cnt)

Submission status: Wrong Answer

Solution code:
s =  input()
num = int(input())
out = 0

cha = s[0]
countf = 1
countl = 0
for i in range((len(s)-1)//2):
  if s[i+1] == cha:
    countf += 1
  else:
    break
for i in range((len(s)+1)//2):
  if s[-i-1] == cha:
    countl += 1
  else:
    break

fs = s[:-countl]
ms = s[countf:-countl]
ls = s[countf:]

if countf == 1 and countl == 0:
  fs = s
  ms = s
  ls = s

count = 1
for i in range(len(fs)-1):
  if fs[i] == fs[i+1]:
    count += 1
  else:
    out += count//2
    count = 1
out += count//2
  
count = 1
for i in range(len(ls)-1):
  if ls[i] == ls[i+1]:
    count += 1
  else:
    out += count//2
    count = 1
out += count//2    

count = 1
tout = 0
for i in range(len(ms)-1):
  if ms[i] == ms[i+1]:
    count += 1
  else:
    tout += count//2
    count = 1
tout += count//2
out += tout*(num-2)

out += ((countf+countl)//2) * (num-1)

if s.count(s[0]) == len(s):
  out = (len(s)*num)//2

print(out)

Submission status: Wrong Answer

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

K = int(K)
ans = 0
S = [c for c in S]

if S[0] == S[len(S)-1]:
    ans += K

cnt = 0
for i in range(len(S)-2):
    if S[i] == S[i+1]:
        cnt += 1
    else:
        ans += cnt//2 * K
        cnt = 1

print(ans)

Submission status: Wrong Answer

Solution code:
import copy

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

if len(S_list) == 1:
    print(K//2)
else:
    for i in range(1, len(S_list)):
        if S_list[i-1] == S_list[i]:
            S_list[i] = "change"
    S1_count = S_list.count("change")

    if S_list[0] == S_list[-1]:
        S2_list = list(S)
        S2_list[0] = "change"
        for i in range(1, len(S2_list)):
            if S2_list[i-1] == S2_list[i]:
                S2_list[i] = "change"
        if S2_list.count("change") > S1_count+1:
            print(K*(S1_count+1)-1)
        else:
            S2_count = S2_list.count("change")
            print((((K//2)+1)*S1_count) + ((K//2)*S2_count))
    else:
        print(K * S1_count)

Submission status: Wrong Answer

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

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

if K==1:
  cnt = 0
  for i in range(len(S)-1):
    if S[i]==S[i+1]:
      S[i+1]=0
      cnt+=1
  print(cnt)
  exit()
    
SW=S*2
cnt1=0
p,q=divmod(K,2)
for i in range(len(SW)-1):
  if SW[i]==SW[i+1]:
    SW[i+1]=0
    cnt1+=1

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

ans = cnt1*p + cnt2*q
print(ans)

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
  if c != len(x):
    prev = False
  # print("c++", c)
  c +=1 

#prev and
if a[0] == a[-1]:
  if (i==0) and j>1:
    # print("|1|")
    print(int((j+j%2)/2))
  elif not prev:
    print(i*j+(j-1))
  else: 
    # print("|2|", i, j)
    print(i*j)
else:
  # print("|3|")
  print(i*j)

Submission status: Wrong Answer

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

sl = list(s)
cnt = 1
i = 0
su = 0
tmp = "0"
f = 0
ff =0
fst =0
lst = 0

for i in range(len(s)):
  if sl[i] == tmp:
    cnt += 1
  else:
    su += int(cnt / 2)
    if ff == 1:
      fst = cnt
    ff += 1
    cnt = 1
    tmp = sl[i]

if cnt != 1:
  su += int(cnt / 2)
  lst = cnt

if sl[0] == sl[-1]:
  if (fst == 0) or (fst % 2 != 0):
    if lst % 2 != 0:
      f = k / 2

t = k * su + f

if len(s) == 1:
  ath = k / 2
  print(int(ath))
else:  
  print(int(t))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
S = list(input())
#取得文字列
K = int(input())
#繰り返し回数

count = 0
befStr = ""

for i in range(1,len(S)):
  befStr = S[i - 1]
  if befStr == S[i]:
    count += 1
    S[i] = ""
    

if S[len(S) -1 ] == S[0]:
  count += 1
print(count*K)