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())
# l = len(s)
# a = [0]*l
# ans = 0
# for i in range(l):
#     if s[i] == s[(i-1)%l]: a[i] += 2
#     if s[i] == s[(i+1)%l]: a[i] += 1
# #print(a)
# for i in range(l):
#     if a[i] == 3:
#         ans += 1
#         a[i] = 0
#         if a[(i+1)%l] >= 2: a[(i+1)%l] -= 2
#         if a[(i-1)%l] == 1: a[(i-1)%l] = 0 
# #print(ans)  
# #print(a)
# ans += len([i for i in range(l) if a[i] == 1 and a[(i+1)%l] == 2])
# ans *= k
# if a[0] == 2 and a[-1] == 1: ans -= 1
# print(ans)

s = input()
k = int(input())
l = len(s)
ans = 0
s = s*min(3, k)
a = [0]*len(s)
for i in range(len(s)):
    if i > 0 and s[i] == s[(i-1)]: a[i] += 2
    if i < len(s)-1 and s[i] == s[(i+1)]: a[i] += 1
for i in range(len(s)):
    if a[i] == 3:
        a[i] = -1
        if a[i+1] >= 2: a[i+1] -= 2
        if a[i-1] == 1: a[i-1] = 0
for i in range(len(s)-1):
    if a[i] == 1 and a[i+1] == 2:
        a[i] = -1
        a[i+1] = 0
if k < 3: ans = len([i for i in range(len(s)) if a[i] == -1])
else:
    ans += len([i for i in range(l) if a[i] == -1])
    ans += len([i for i in range(2*l,3*l) if a[i] == -1])
    ans += len([i for i in range(l, 2*l) if a[i] == -1]) * (k-2)
print(ans)

Submission status: Wrong Answer

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


for s in S:
    if s.isupper():
        exit()
    try:
        float(s)
    except ValueError:
        pass
    else:
        exit()
try:
    float(K)
except ValueError:
    exit()
else:
    if not float(K).is_integer():
        exit()

if not (1 <= len(S) and len(S) <= 100):
    exit()
if not (1 <= int(K) and int(K) <= 10**9):
    exit()

L = []
tmp = []
pre = ""
for s in S:
    if len(tmp) == 0:
        pre = s
        tmp.append(s)
    elif pre == s:
        tmp.append(s)
    else:
        L.append(tmp)
        pre = s
        tmp = [s]
L.append(tmp)

count = 0
for l in L:
    count += int(len(l) / 2)

count *= int(K)
if len(L) == 1:
    print(count)
    exit()
if L[0][0] == L[len(L)-1][0]:
    if (len(L[0]) + len(L[len(L) - 1])) % 2 == 0:
        count = count + 1 * int(K) - 1
print(count)

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 count % 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 = input()
K = int(input())

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

Submission status: Wrong Answer

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

def makerawlist(S):
    sigma = 0
    counter = 1
    for i in range(len(S)-1):
        if S[i]==S[i+1]:
            counter += 1
        else:
            sigma += counter//2
            counter = 1
    sigma += counter//2
    return sigma

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
# import math
# import numpy as np
# from functools import lru_cache
import string

args = sys.argv
if len(args) == 2:
  from IPython import embed
  em = True
else:
  em = False

# sys.setrecursionlimit(10000)

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

def calc(S, K):
  if len(S) == 1:
    if K == 1:
      print(0)
      exit()
    else:
      S = S * K
      K = 1
  # a, b = map(int, input().split())
  # a = list(map(int, input().split()))
  # a = list(map(lambda x: int(x)*(-1), a))  # 各要素を-1倍


  # n = int(input())
  # t = [int(input()) for i in range(n)]
  if em: sys.stdin = open('/dev/tty')

  #### function
  # @lru_cache(maxsize=10000)

  #### process
  # if em: embed()
  last_chk = 0
  letters = string.ascii_lowercase
  score = 0
  for i in range(0, len(S) - 2):
    # print(S)
    if S[i] == S[i + 1]:
      S[i + 1] = letters.strip(S[i]).strip(S[i + 2])[0]
      score = score + 1

  if S[-2] == S[-1]:
    S[-1] = letters.strip(S[-2]).strip(S[0])[0]
    score = score + 1
    # last_chk = 1
  else:
    if S[-1] == S[0]:
      S[-1] = letters.strip(S[-2]).strip(S[0])[0]
      score = score + 1
      last_chk = 1
  return score, last_chk, S

if K % 2 == 0:
  S = S * 2
  K = int(K / 2)
  score, last_chk, S2 = calc(S,K)
  total = score * K
  if last_chk == 1: total = total - 1
  print(total)
else:
  if K == 1:
    score, last_chk, S2 = calc(S,K)
    total = score * K
    if last_chk == 1: total = total - 1
    print(total)
  else:
    S_orig = S
    S = S * 2
    K = int((K - 1) / 2)
    score, last_chk, S2 = calc(S,K)
    total = score * K
    # if last_chk == 1: total = total - 1
    if em: embed()
    S3 = [S2[-1]] + S_orig
    score3, last_chk3, S4 = calc(S3,1)
    if last_chk3 == 1: score3 = score3 - 1
    print(total + score3)


# score = 0
# i = 0
# last_chk = 0
# while True:
#   if S[i] == S[i + 1]:    # 次と同じなら
#     if i == len(S) - 2:    # 末尾だったらスコア増えない
#       score = score + 1
#       break
#     elif i == len(S) - 3:
#       if S[i + 1] == S[i + 2]:
#         score = score + 1
#         if S[0] == S[-1]:
#           score = score + 1
#         break
#     else:
#       score = score + 1
#       i = i + 2
#   else:
#     i = i + 1

# print(score*K)


## heapsort, O(logN)
# import heapq
# heapq.heapify(A)
# min = heapq.heappop(A)
# heapq.heappush(A, new_element)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
t = s[:]
c = 0

if len(t) == 1:
    print(k // 2)
else:
    t.append(s[0])
    # print(t)
    for i in range(1, len(t) - 1):
        if t[i - 1] == t[i]:
            c += 1
            t[i] = "A"
    # print(t)
    c *= k
    if t[-1] == t[-2]:
        c += k - 1
    print(c)

Submission status: Wrong Answer

Solution code:
from itertools import groupby

S = input()
K = int(input())
A = []
for s in S:
  A.append(s)

def cont(A):
  B = []
  b = 0
  for key, value in groupby(A):
    B.append([key, list(value)])
  for i in range(len(B)):
    b += len(B[i][1]) // 2
  return b

def plus1(A):
  plu1 = 0
  for i in range(1,len(A)):
    if A[0] == A[i]:
      plu1 += 1
    else:
      break
  return plu1

def plus2(A):
  plu2 = 0
  for j in range(1,len(A)):
    if A[0] == A[-j]:
      plu2 += 1
    else:
      break
  return plu2

X = len(S) - len(list(set(A)))
if len(list(set(A))) == 1:
  an = len(S)*K
  ans = an // 2
elif X == 0:
  ans = 0
elif A[0] != A[len(A)-1]:
  ans = cont(A) * K
else:
  if plus1(A)%2 == 1 and plus2(A)%2 == 1:
    ans = cont(A)*K + (K-1)
  else:
    ans = cont(A) * K
print(ans)

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):
    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)
else:
    print(sum_count*k)

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 + cnt1 * (K -1), cnt1 + cnt * (K - 1))
else:
    ans = cnt * K
print(ans)

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 = []
    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

    # 先端と末端の文字が同じ場合
    if s[0] == s[-1]:

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

        # どちらかが1文字の場合
        elif s[0] != s[1] or s[-2] != s[-1]:
            # 先頭が1文字の場合
            if s[0] != s[1]:

                result = sum([x // 2 for x in count_l[:-1]]) * k + (count_l[-1] + 1)//2 * (k-1) + count_l[-1]//2
            # 末端が1文字の場合
            else:
                result = sum([x // 2 for x in count_l[1:]]) * k + (count_l[0] + 1)//2 * (k-1) + count_l[0]//2

        # どちらも2文字以上連続する場合
        else:
            result = sum([x // 2 for x in count_l[1:-1]]) * k + (count_l[0] + count_l[1])//2 + count_l[0]//2 + count_l[-1]//2

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

    print(result)

Submission status: Wrong Answer

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

S1 = S + '00' 
i = 0
ans = 0

if len(S) == 1:
    print(K // 2)
else:
    while S1[i] != '0':
        if S1[i] == S1[i+1]:
            ans += 1
            i += 1
        i += 1

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

Submission status: Wrong Answer

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


# for s in S:
#     if s.isupper():
#         exit()
#     try:
#         float(s)
#     except ValueError:
#         pass
#     else:
#         exit()
# try:
#     float(K)
# except ValueError:
#     exit()
# else:
#     if not float(K).is_integer():
#         exit()

# if not (1 <= len(S) and len(S) <= 100):
#     exit()
# if not (1 <= int(K) and int(K) <= 10**9):
#     exit()
L = []
tmp = []
pre = ""
for s in S:
    if len(tmp) == 0:
        pre = s
        tmp.append(s)
    elif pre == s:
        tmp.append(s)
    else:
        L.append(tmp)
        pre = s
        tmp = [s]
L.append(tmp)
# print(L)

count = 0
for l in L:
    count += int(len(l) / 2)
    # print(l)
    # print(int(len(l) / 2))

count *= int(K)
if len(L) == 1:
    print(count)
    exit()
if L[0][0] == L[len(L)-1][0]:
    if (len(L[0]) + len(L[len(L) - 1])) % 2 == 0:
        count = count + 1 * int(K) - 1
print(count)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
m = 0
n = 0
j = 0
for i in range(len(s)):
    if s.count(s[0]) == len(s):
        print(len(s) * k // 2)
        exit()
    if len(s) <= j + 1:
        break
    if s[j] == s[j + 1]:
        if j == len(s) - 1:
            n += 1
        else:
            m += 1
        j += 1
    j += 1

print(m * k)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

flag = False

itr = 0
cnt = 1
len_cnt = []
for x in S :
  if itr == 0 :
    pre = x
    itr += 1
    continue
  now = x
  if pre == now :
    cnt += 1
    itr += 1
    if itr == len(S) :
      len_cnt.append(cnt)
    continue
  if pre != now :
    len_cnt.append(cnt)
    itr+=1
    if itr == len(S) and cnt == 1 :
      len_cnt.append(cnt)
    cnt = 1
    pre = x
    continue

if sum(len_cnt) != len(S) :
  len_cnt.append(1)

ans = 0
for x in len_cnt :
  if x == 1 :
    continue
  else :
    ans += int(x / 2)

# if S[0] == S[-1] and len_cnt[-1] > 1 and len_cnt[-1] % 2 == 1 and K > 1:
#   ans += 1
if S[0] == S[-1] and len_cnt[-1] % 2 == 1 and len_cnt[0] % 2 == 1 :
  ans += 1
  flag = True

ANS = K * ans
# print (ANS, len_cnt)
if flag :
  ANS -= 1

# if S[0] ==S[-1] and len_cnt[-1] % 2 == 1 and len_cnt[-1] != 1 :
#   ANS -= 1
print(ANS)

Submission status: Wrong Answer

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


a = S[0]
c = 0
S2 = copy.deepcopy(S)
for i in range(1, len(S)):
    if S2[i] == S2[i-1]:
        S2[i] = '1'
        c += 1

a = S[-1]
last = 0
for i in range(2, len(S)+1):
    last += 1
    if S[-i] != a:
        break

if all([e == S[0] for e in S[1:]]) == True:
    if len(S)%2==0:
        print(int(len(S)/2*K))
    else:
        print(int(len(S)//2*((K+1)//2)+(len(S)//2+1)*K//2))
elif S[0] == S[-1] and last % 2 == 1:
    c3 = 0
    S3 = copy.deepcopy(S)
    for i in range(2, len(S)):
        if S3[i] == S3[i-1]:
            S3[i] = '1'
            c3 += 1
    print(c + (c3+1)*(K-1))
else:
    print(c*K)

Submission status: Wrong Answer

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

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)
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)*(((b+1)/2)-1))
print(ans)

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):
    sum_count = int((len(s)/2)*k)
    k = 1
    flag = 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)
else:
    print(sum_count*k)

Submission status: Wrong Answer

Solution code:
slist=list(input())
k=int(input())
n=len(slist)
kaisu=0
i=1
shirushi=0
kotae=0
if n>1:

	while 1<=i<=n-1:
		if slist[i]==slist[i-1]:
			kaisu+=1
			if i==n-1:
				shirushi=1
			i+=2
		else:
			i+=1
	
	if k>1:
		if shirushi==0 and slist[0]==slist[n-1]: #このとき二回目以降は初項書き換え
			if slist[0]==slist[1]: #やばいパターン
				yabaikaisu=1
				i=2
				shirushi=0
			
				while 1<=i<=n-1:
					if slist[i]==slist[i-1]:
						yabaikaisu+=1
						if i==n-1:
							shirushi=1
						i+=2
					else:
						i+=1
					
				kotae=kaisu+(k-1)*yabaikaisu
			else: #やばくはないパターン
				kotae=k+kaisu*k-1
	
		else: #楽勝パターン
			kotae=kaisu*k
			
	else: #k=1のとき
		kotae=kaisu


else: #n=1のとき
	if k%2==0:
		kotae=k/2
	else:
		kotae=(k-1)/2

print(kotae)

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:
  if t[0]==t[-1] and t[-2]!=t[-1]:
    ans+=k-1
  
if t.count(t[0])==L:
  ans=(k*L)//2
print(ans)

Submission status: Wrong Answer

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


if __name__ == '__main__':
    S = input()
    K = int(input())
    cnt = 0
    if len(S) % 2 == 1 and K > 1:
        S2 = S * 2
        K2 = K // 2
        cnt = min(f(S2, K2), f(S2[::-1], K2))
        K = 1

    print(cnt + min(f(S, K), f(S[::-1], K)))

Submission status: Wrong Answer

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

flag = 1
temp = []
for i in range(0, len(S)):
    if i == len(S) - 1:
        temp.append(S[i] * flag)
        break
    if S[i] == S[i + 1]:
        flag += 1
    elif S[i] != S[i + 1]:
        temp.append(S[i] * flag)
        flag = 1


if len(temp) == 1:
    print(len(S) * K // 2)
elif K != 1 and temp[0][0] == temp[len(temp) - 1][0]:
    count = 0
    first = temp[0]
    last = temp[len(temp) - 1]
    temp[len(temp) - 1] += temp[0]
    for i in range(1, len(temp)):
        count += len(temp[i]) // 2
    print(count * K + len(first) // 2 + len(last) // 2 - 1)
else:
    count = 0
    for i in range(1, len(temp)):
        count += len(temp[i]) // 2
    print(count * K)

Submission status: Wrong Answer

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

buf=''
cnt = 1
total = 0
cnts = []


for s in S:
    #print(buf,s,cnt)
    if s == buf:
        cnt += 1       
    else:
        cnts.append(cnt)
        total += int(cnt/2)
        cnt = 1        
    buf = s
cnts.append(cnt)    
total += int(cnt/2)

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

elif S[0]==S[-1] and cnts[1] % 2 != 0:
    if (cnts[1]+cnts[-1]) % 2 == 0:
        print(total*K+K-1)
else:
    print(total*K)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
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):
    sum_count = int((len(s)/2)*k)
    k = 1
    flag = 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 flag == 0:
    sum_count += count//2

if flag == True:
    print((sum_count)*k+1)
else:
    print(sum_count*k)

Submission status: Wrong Answer

Solution code:
S = str(input())
N = int(input())
S = S
S_list=list(S)
count = 0
for i in range(len(S_list)-1):
    if i < len(S_list)-2 and S_list[i] == S_list[i+1] and S_list[i+1] == S_list[i+2]:
        count += 1
        S_list[i+1] = "1"
    elif S_list[i] == S_list[i+1]:
        count += 1
    elif S_list[i] == S_list[i+1] and i == len(S_list)-2:
        count_last+=1
print(count*N)

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

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

count_list.append(count)
ans = 0

if(S[0] != S[-1]):
  for i in range(len(count_list)):
    if count_list[i] > 0:
      ans += (count_list[i] + 1) // 2
      
else:
  lst = 0
  for i in range(len(count_list)):
    if count_list[i] > 0:
      lst += (count_list[i] + 1) // 2
  if count_list[0] % 2 == 1:
      lst -= 1
   
  b = count_list[0] - 1  
  pp = count_list.pop(0)
  if len(count_list) > 0:
    count_list[-1] += pp
  else: 
    count_list.append(pp)
  
  for i in range(len(count_list)):
    if count_list[i] > 0:
      ans += (count_list[i] + 1) // 2

  ans = ans * (K-1) + lst

print(ans)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
s = input()
k = int(input())
# print (s,k)
n=len(s)
# print (s[0],s[n-1])
p=0
q=0
if s[0] == s[n-1]:
    l=1 
    # Start 
    for i in range(1,n):
        #print("loop",i)
        if s[i] == s[i-1]:
            p +=1
        else:
            break
    if l < n:
        for i in range(n-2,0,-1):
            if s[i] == s[i+1]:
                q +=1
            else:
                break
    l = 1 + p + q
    # print("Ring ", l, p,q)
    op = (l // 2) * k  + (l % 2) * (k-1)
else: 
    op = 0     

# print("OP",op)

s2 = s[p:n-1-q] 
n=len(s2)
            

l = 0
for i in range(1,n):
    # print(s2[i],i,l,op)
    if s2[i-1] != s2[i]:
        op += (l // 2 + l % 2) * k
        l = 0 
    else:
        l += 1 
if l != 0:
    op += (l // 2 + l % 2) * k

        
print(op)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

def connect_cnt(S):
	cnt_tmp = 1
	cnt = []

	for i in range(len(S) - 1):

		if S[i] == S[i+1]:
			cnt_tmp += 1
		else:
			cnt.append(cnt_tmp)
			cnt_tmp = 1

		if i == len(S)-2:
			cnt.append(cnt_tmp)

	connect_cnt = sum(np.array(cnt) // 2)
	return connect_cnt


if K == 1:
	print(connect_cnt(S))
	exit(0)

if len(S) == 1:
	print(connect_cnt(S*K))
	exit(0)

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

	connect_cnt = connect_cnt(S)
	ans = connect_cnt * K
	print(ans)

else:

	S_dash = S + S
	connect_cnt_Sdash = connect_cnt(S_dash)
	connect_cnt_S = connect_cnt(S)

	if K > 2:
		ans = connect_cnt_Sdash * (K//2) + connect_cnt_S
		print(ans)
		exit(0)

	else:
		ans = connect_cnt_Sdash * (K//2)
		print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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 <= 2:
        print(count_changes(s, k))
    else:
        base_count = count_changes(s, 3)
        d, rest = divmod(k, 3)
        res = base_count * d
        if rest:
            res += count_changes(s, rest, s[-1])
        if s[0] == s[-1]:
            res += d - 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:
        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))