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:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[]]*num
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))

#################

S = str(input())
n = len(S)
K = I()

num = 0
flag = False
for i in range(1,n):
    if S[i]==S[i-1] and flag==False:
        num += 1
        flag = True
    else:
        flag = False

if flag==False and S[0]==S[-1]:
    num2 = 1
    flag2 = False
    for i in range(2,n):
        if S[i]==S[i-1] and flag2==False:
            num2 += 1
            flag2 = True
        else:
            flag2 = False
    if flag2==False:
        print(num+num2*(K-1))
        exit()
    else:
        print(min(num2*(K//2) + num*(K-K//2), num*(K//2) + num2*(K-K//2)))
        exit()
else:
    print(num*K)

Submission status: Wrong Answer

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

d, l, r = s[0], 0, 0

if d == s[-1]:
    for i in range(1, sl):
        if s[i] != d:
            l = i
            break
    
    for i, c in enumerate(s[::-1]):
        if c != d:
            r = i
            break

ans = l//2 + r//2 + (l+r) // 2 * (k-1)
s = s[l:sl-r]
cnt = 0

for i in range(1, len(s)):
    if s[i] == s[i-1]:
        s[i] = ""
        cnt += 1

ans += cnt * k

print(ans)

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()
    S.rstrip('\n')
    K = II()

    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 > 0:
            ans += (count+1)//2

        if S[-1] == S[i]:
            A = -1

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

s = input()
k = int(input())
flag = 1
lenth = 1
ans = 0
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        lenth += 1
    else :
        flag = 0
    if flag == 0:
       ans += lenth//2
       lenth = 1
       flag = 1
ans += lenth//2
lenth = 1
flag = 1

ji = 0
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        lenth += 1
    else :
        flag = 0
    if flag == 0:
       # print(lenth)
       if lenth%2 == 1:
            ji += 1
       break
if lenth%2 == 1 and flag ==1:
    ji += 1
lenth = 1
flag = 1
for i in range(len(s)-1):

    if s[len(s)-i-1] == s[len(s)-1-(i+1)]:
        lenth += 1
    else :
        flag = 0
    if flag == 0:
       # print(lenth)
       if lenth%2 == 1:
            ji += 1
       break
if lenth%2 == 1 and flag == 1:
    ji += 1
ans = ans*k
# print(ans, ji)
if ji == 2 and s[0] == s[len(s)-1]:
    ans = ans+k-1
if len(s) == 1:
    if k == 1:
        print(0)
    elif k == 2:
        print(1)
    else:
        print(k//2)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
S=list(input())
K=int(input())
c=0
f=1
if len(S) == 1:
    ans=K//2
else:
    for a in range(len(S)-1):
        if S[a] == S[a+1]:
            c+=1
            if a != len(S)-2 and S[a+1] == S[a+2]:
                f+=1
            else:
                c-=f//2
                f=1
    ans=c*K
    m=1
    u=1
    if S[0] == S[len(S)-1]:
        for i in range(len(S)-1):
            if S[i]==S[i+1]:
                m+=1
            else:
                break
        for i in reversed(range(len(S)-1)):
            if S[i]==S[i+1]:
                u+=1
            else:
                break
        if m%2 == 1 and u%2==1:
            ans+=K-1
print(ans)

Submission status: Wrong Answer

Solution code:
S = list(input())
K = int(input())
S2 = S[:] * 7

if len(S) == 1:
    print(K//2)
    exit()
cons = 0
for i in range(len(S)-1):
    if S[i] == S[i+1]:
        cons += 1
        S[i+1] = '_'
cons2 = int(S[0] == S[-1])
if K == 1:
    print(cons)
    exit()
cons3 = 0
for i in range(len(S2) - 1):
    if S2[i] == S2[i+1]:
        cons3 += 1
        S2[i+1] = '_'
if cons3 != cons * 2 + cons2:
    minus = 1
else:
    minus = 0
print(cons3 * (K // 7) + cons * (K%7))

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import Counter
S=input().strip()
K=int(input())
C=Counter(S)

if S[0]!=S[-1]:
    A=0

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

    print(A*K)

elif len(C)==1:
    print(len(S)*K//2)

else:
    front=1
    back=1

    for i in range(1,len(S)):
        if S[i]==S[i-1]:
            front+=1
        else:
            frd=i
            break

    for i in range(len(S)-2,-1,-1):
        if S[i]==S[i+1]:
            back+=1
        else:
            bcd=i
            break

    #print(frd,bcd,front,back)

    ANS=(front+back)*(K-1)//2+front//2+back//2

    ind=frd+1
    A=0

    while ind<=bcd:
        if S[ind]==S[ind-1]:
            A+=1
            ind+=2
        else:
            ind+=1

    #print(A)

    print(ANS+A*K)

Submission status: Wrong Answer

Solution code:
def f():
    S = input()
    K = int(input())
    S = list(S)
    if K == 1 and len(S) == 1:
        print(0)
        return
    
    if K == 1 and len(S) != 1:
        ans = 0
        for i in range(len(S)-1):
            if S[i] == S[i+1]:
                S[i+1] = '1'
                ans += 1
        print(ans)
        return
    
    if K != 1 and len(S) == 1:
        ans = K // 2
        print(ans)
        return
    
    if K != 1 and len(S) != 1:
        ORG_S = S[:]
        ans_min = 10**12
        # A
        ans = 0
        if S[0] == S[-1]:
            S[-1] = 'X'
            ans += 1
        for i in range(len(S)-1):
            if S[i] == S[i+1]:
                S[i+1] = str(i)
                ans += 1
        ans = ans*(K-1)
        S2 = S[:] + ORG_S
        for i in range(len(S2)-1):
            if S2[i] == S2[i+1]:
                S2[i+1] = 'A'+str(i)
                ans += 1
        ans_min = min(ans, ans_min)
        # B
        ans = 0
        S = ORG_S[:]
        if S[0] == S[-1]:
            S[0] = 'X'
            ans += 1
        for i in range(len(S)-1):
            if S[i] == S[i+1]:
                S[i+1] = str(i)
                ans += 1
        ans = ans*(K-1)
        S2 = ORG_S + S[:]
        for i in range(len(S2)-1):
            if S2[i] == S2[i+1]:
                S2[i+1] = 'A'+str(i)
                ans += 1
        ans_min = min(ans, ans_min)         
        print(ans_min)
        return

f()

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
flag = False
count = 0
for i in range(len(S)-1):
    if not flag:
        if S[i] == S[i+1]:
            count += 1
            flag = True
    else:
        flag = False
ans = 0
if len(S) == 1:
    ans = K // 2
elif S[0] == S[-1] and not flag:
    ans += count
    count = 0
    for i in range(1, len(S)-1):
        if not flag:
            if S[i] == S[i+1]:
                count += 1
                flag = True
        else:
            flag = False
    ans += (count + 1)*(K-1)

else:
    ans = count * K
print(ans)

Submission status: Wrong Answer

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

len_s=len(s)
ans=0

if len(set(s))==1:
	ans=k*len_s//2
		


else:	
	for i in range(len_s-1):
		if s[i]==s[i+1]:
			ans=ans+1
			s=list(s)
			s[i+1]="0"
			s="".join(s)

	ans=ans*k

	if s[0]==s[len_s-1]:
		ans=ans+k-1


print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
count = 0
count2 = 1
count3 = 1
flag = 0
flag2 = 0
if len(S) == 1:
    print(K // 2)
else:
    for i in range(len(S)-1):
        if S[i] == S[i+1] and flag == 0:
            count += 1
            flag = 1
        else:
            flag = 0
        if S[i] != S[i+1]:
            flag2 = 1
    num = count * K
    if flag2 == 0:
        print(len(S) * K //2)
    else:

        if S[0] == S[-1]:
            if len(S) == 2:
                print(num)
            else:
                for i in range(len(S)-1):
                    if S[i] == S[i+1]:
                        count2 += 1
                for i in range(len(S)-1, 0, -1):
                    if S[i] == S[i-1]:
                        count3 += 1
                if count2 % 2 != 0 and count3 % 2 != 0:
                    print(num + K - 1)
                else:
                    print(num)
        else:
            print(num)

Submission status: Wrong Answer

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

ops = 0
i = 1

while i < len(s):
    if s[i-1] == s[i]:
        ops += 1
        i += 1
    i += 1

i = 0

ops2 = 0

ss = s + s

while i < len(ss):
    if ss[i-1] == ss[i]:
        ops2 += 1
        i += 1
    i += 1

diff = ops2 - 2 * ops

ops = k * ops + (k-1) * diff

print(ops)

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:
  count = 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:  
  count_len = 1
  for i in range(len(s)-1):
    if s[-1-i]==s[-2-i]:
      count_len += 1
    else:
      break
  if count_len%2 ==1 and len(s)>2:
    join_n = k//2
  
print(count*k + join_n)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# A - Connection and Disconnection
S = str(input())
K = int(input())
count =  0
tmp = 0
N = len(S)

if N == 1:
    if K >= 2:
        count = 1 * (K//2)
    else:
        count = 0
elif S[0] == S[N-1]:
    count += 1
else:
    pass

for i in range(1,N-1):
    if S[i-1] == S[i] == S[i+1] and tmp == 0:
        tmp = 1
    else:
        tmp = 0
    if S[i-1] == S[i] and tmp == 0:
        count += 1
    else:
        pass

if N == 1:
    print(count)
else:
    ans = count * K
    print(ans)

Submission status: Wrong Answer

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

if not (1 <= len(S) and len(S) <= 100):
    exit()
if not (1 <= int(K) and int(K) <= 10 ** 9):
    exit()
for s in S:
    if s.isupper():
        exit()

pre = ""
count = 0
first = S[0]
flag = False
for i in range(len(S)):
    if i == (len(S) - 1):
        if pre != S[i] and first == S[i]:
            flag = True
    if pre != S[i]:
        pre = S[i]

    elif pre == S[i]:
        count += 1
        pre = ""


count *= int(K)
if flag:
    count = count + 1*int(K) -1 
print(count)

Submission status: Wrong Answer

Solution code:
import copy

s=list(str(input()))
n=int(input())
s_base = copy.deepcopy(s)
i0 = s[0]
count = 0
if len(s) == 1:
    ans = n//2
else:
    #r1 = n//2 + n%2
    #s

    for i, name in enumerate(s[1:]):
        if name == i0:
            i0 = -1
            count += 1
            s[i+1] = i0
        else:
            i0 = name
    if s[0] == s[-1]:
        al = n-1
        if s[1] == -1:
            s2 = copy.deepcopy(s)
            for i in s_base:
                s2.append(i)
            i0 = s2[0]
            count2 = copy.deepcopy(count)
            for i, name in enumerate(s2[1:]):
                if name == i0:
                    i0 = -1
                    count2 += 1
                    s2[i+1] = i0
                else:
                    i0 = name
            al = (count2 - count*2)*(n//2)
    else:
        al = 0
    ans = count*n + al
print(ans)

Submission status: Wrong Answer

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

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

for i in range(1,len(S)):
    #print(i)
    #print(S)
    if S[i] == S[i-1]:
        #print("change")
        result = result + 1
        S = S[:i] + '.' + S[i+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)

#aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
#1000000000

Submission status: Wrong Answer

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

S_double = S + S
for s_doublei in range(1, len(S_double)):
    if S_double[s_doublei] == S_double[s_doublei - 1]:
        S_double = S_double[: s_doublei] + "?" + S_double[s_doublei + 1:]
        res += 1

print(res // 2 * K)

Submission status: Wrong Answer

Solution code:
s=list(input())
k=int(input())
if k>1:
	s.append(s[0])
ans=0

last=1
for n in range(len(s)-1):


	if s[n]==s[n+1]:

		last+=1
		if last%2==0:
			ans+=1

	else:
		last=1


print(ans*k)

Submission status: Wrong Answer

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

S += "1"
l = len(S)
pair = []
current = S[0]
seq = 1
for i in range(1, l):
    if(i == l):
        pair.append(seq)
    elif(S[i] == current):
        seq += 1
    else:
        pair.append(seq)
        seq = 1
        current = S[i]

ans = 0
#print(pair)
if(len(pair) == 1):
    ans += (K * pair[0])//2
elif(S[0] != S[l-1]):
    for i in range(len(pair)):
        ans += K * (pair[i]//2)
else:
    for i in range(len(pair)-1):
        ans += K * i
    ans += ((pair[i+1]+1)//2)*(K-1) + (pair[i+1]//2)

print(ans)

Submission status: Wrong Answer

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

length = len(S)
if length == 1:
    print(K)
    exit(0)
if length == 2:
    if S[0] == S[-1]:
        print(K)
    else:
        print(0)
    exit(0)

count = 0
start_end_same = S[0] == S[-1]
i = 2
while i < length:
    if S[i-2] == S[i-1] == S[i]:
        S[i-1] = '-'
        count += 1
        i += 2
    elif S[i-2] == S[i-1]:
        S[i-1] = '-'
        count += 1
        i += 1
    elif S[i-1] == S[i]:
        if i == length-1:
            count += 1
            S[i-1] = '-'
        i += 1
    else:
        i = min(i+2, length)
ans = count*K
if start_end_same and S[1] != '-' and S[-2] != '-':
    ans += (K-1)
print(ans)

Submission status: Wrong Answer

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

T = list(S)
cnt = 0

for i in range(len(T)):
  if i == 0:
    continue
  if T[i] == T[i-1]:
    T[i] = '_'
    cnt += 1
  else:
    if i == len(T)-1:
      if T[i] == T[0]:
        T[i] = '_'
        cnt += 1

cnt = cnt * K
print(cnt)

Submission status: Wrong Answer

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

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

for i in range(1,len(S)):
    #print(i)
    #print(S)
    if S[i] == S[i-1]:
        #print("change")
        result = result + 1
        S = S[:i] + '.' + S[i+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 = input()
k = int(input())

# sが1種類の文字の繰り返しの場合
if len(set(s)) == 1:
    print((len(s) * k) // 2)

# 複数種の場合
else :
    # 同じ文字の連続する箇所をカウント
    before_mozi = ''
    count_l = [0]
    same_f = False
    for mozi in s:
        if mozi == before_mozi:
            if not same_f:
                count_l.append(1)
            count_l[-1] += 1
            same_f = True
        else:
            same_f = False
        before_mozi = mozi

    result = sum([x // 2 for x in count_l]) * k

    # 先端と末端の文字が同じで，
    # かつお互いが2文字以上連続していないとき
    if s[0] == s[-1] and s[0] != s[1] and s[-1] != s[-2]:
        result += k-1

    print(result)

Submission status: Wrong Answer

Solution code:
# A - Connection and Disconnection

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

lengthS = len(s)
replaceFlag = [0] * lengthS

cnt = 0
before = "0"
for i in range(lengthS):
    if before == s[i]:
        replaceFlag[i] = 1
        cnt += 1
        before = "0"
    else:
        before = s[i]
ans = cnt * k
if s[0] == s[lengthS-1]:
    if replaceFlag[lengthS-1] == 0:
        ans += (k-1)
print(ans)

Submission status: Wrong Answer

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

n = len(s)
cnt = [0]

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

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

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

ans = ren * k + b - 1

print(ans)

Submission status: Wrong Answer

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



if len(s)==1:
    if k<2:
        ans = 0
    else:
        ans = k//2
elif len(s)==2:
    if s[0]==s[1]:
        ans = k
    else:
        ans = 0
else:
    if k == 1:
        ans = 0
        bef = ""
        befchen = False
        trignum = 0
        for i in range(len(s)):
            now = s[i]
            if bef==now:
                if befchen == False:
                    ans += 1
                    befchen = True
                    trignum = i
            if  befchen == True:
                if trignum == i-1:
                    befchen = False
            bef = now

    elif k >=2:
        ans = 0
        bef = ""
        befchen = False
        trignum = 0
        for i in range(len(s)):
            now = s[i]
            if bef==now:
                if befchen == False:
                    #print(now)
                    ans += 1
                    befchen = True
                    trignum = i
            if  befchen == True:
                if trignum == i-1:
                    befchen = False
            bef = now
        
        if s[0]==s[-1]:
            if befchen == False:
                ans += 1
                
        ans *= k
        
        if s[0]==s[-1]:
            if befchen == False:
                ans -= 1

print(ans)

Submission status: Wrong Answer

Solution code:
s=list(input())
s2=s*2
k=int(input())
an1=0
an2=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

print(an1+(an2-an1)*(k-1))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
S=input()
K=int(input())
d=defaultdict(int)
first_last=0
memo=[0]*len(S)
count=0
for i in range(len(S)):
  if i>=1:
    if (S[i]==S[i-1])and(memo[i-1]==0):
      count+=1
      memo[i]=1
      
      
memo2=[0]*len(S)
count2=0
if (S[0]==S[-1]) and (memo[-1]==0):
  count2+=1
  memo2[0]=1
  first_last+=1
for i in range(1,len(S)):
  if (S[i]==S[i-1])and(memo[i-1]==0):
      count2+=1
      memo2[i]=1

if first_last==1:
  print(count+count2*(K-1))
else:
  print(count*K)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
n = 0
j = s[0]
l = 1
p = 0
if len(set(list(s))) == 1:
  print(len(s)*k//2)
else:
  for i in range(1,len(s)):
    if s[i] == j:
      l += 1
    else:
      n += l//2
      l = 1
      j = s[i]
  n += l//2
  if s[0] == s[-1] and s[0] != s[1] and s[0] != s[-2]:
    p = k-1
  print(n*k+p)

Submission status: Wrong Answer

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

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

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

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

Submission status: Wrong Answer

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

count = 0
cont = 0

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

#すべて同じ文字の場合の処理
if len(S) == S.count(S[0]):
    count += (len(S) * K) // 2
    print(count)
    exit()

#最初のブロックの連続数える
firstword = 0
first_index = 0
if S[0] == S[-1]:
    for i in range(len(S) - 1):
        if S[i] == S[i+1]:
            cont += 1
        else:
            firstword = cont
            cont = 0
            first_index = i
            break 

#末尾からの連続を数える。
lastword = 0
last_index = 0
for i in reversed(range(len(S))):
    if i == 0:
        break
    if S[i] == S[i-1]:
        cont += 1
    else:
        lastword = cont
        cont = 0
        last_index = i
        break

#中間部分を数える。
middleword = 0
for i in range(first_index, last_index):
    if S[i] == S[i+1]:
        cont += 1
    else:
        middleword += (cont + 1) // 2
        cont = 0


if S[0] == S[-1]:
    count += middleword * K
    count += ((firstword + lastword + 2) // 2 ) * (K-1)
    count += (firstword + 1) // 2 + (lastword + 1) // 2
else:
    count += middleword * K
    count += ((firstword + 1) // 2 + (lastword + 1) // 2) * K

print(count)

Submission status: Wrong Answer

Solution code:
from sys import stdin


def change(S, K):
    c = 0

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

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

    if i == len(S) and S[-1] == S[0] and S[-1] != S[-2]:
        c += 1

    return K * c


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

    print(change(S, K))

Submission status: Wrong Answer

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

if len(s)== 1:
    ans += k//2
else:
    if s[0] == s[1] == s[-1]:
        ans += k
        s[0] = "0"
    for i in range(0,len(s)-1):
        if s[i]==s[i+1]:
            ans += k
            s[i+1] = "0"
    if s[0] == s[-1]:
        ans += k-1
print(ans)

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
SS=list(S)
n=0
#モノマー内の重複を修正(偶数文字)
if len(S)%2==0:
    for i in range(len(S)-1):
        if SS[i]==SS[i+1]:
            SS[i+1]="A"
            n+=1
    if SS[0]==SS[-1]:
        n+=1
    print(n*K)
else: #文字数が奇数の場合、偶数回目と奇数回目で伏せる文字を変えた方が効率的なことがある
    SSS=list(S+S)
    for i in range(len(SSS)-1):
        if SSS[i]==SSS[i+1]:
            SSS[i+1]="A"
            n+=1
    if K%2==0:
        if SSS[0]==SSS[-1]:
            n+=1
        print(n*K//2)
    else:
        if SSS[0]==SSS[-1]:
            n+=1
        print(n*K//2+n//2+1)

Submission status: Wrong Answer

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

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

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

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


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

print(ans)

Submission status: Wrong Answer

Solution code:

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

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

    print(total_count)

hoge()

Submission status: Wrong Answer

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

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

if L==1:
  ans=k//2
print(ans)

Submission status: Wrong Answer

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

n = len(s)
cnt = [0]

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

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

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

ans = ren * k + b + 1

print(ans)