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 copy import deepcopy
from collections import deque, defaultdict
from operator import mul
from functools import reduce
sys.setrecursionlimit(20000000)

# input = sys.stdin.readline


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

    ans = 0
    first = False

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

            if i == 0:
                first = True

    if s[0] == s[-1] and not first:
        ans += 1

    print(ans * k)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import math
from copy import deepcopy
from collections import deque, defaultdict
from operator import mul
from functools import reduce
sys.setrecursionlimit(20000000)

# input = sys.stdin.readline


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

    ans = 0

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

    print(ans * k)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def d_c(s):
  count = 0
  x = ''
  for i in range(len(s)):
    if x == s[0]:
      count += 1
      x = ''
    else:
      x = s[0]
    s = s[1:]
  return count
 
def num_con(s):
  x = s[0]
  s = s[1:]
  count = 1
  for i in range(len(s)):
    if s[0] == x:
      count += 1
      x = s[0]
      s = s[1:]
    else:
      break
  return count
      
    
s = input()
k = int(input())
 
if len(s)==1:
  print(k//2)
elif s[0]==s[-1]:
  f = num_con(s)
  m = num_con(s[::-1])
  dis = (f//2 + m//2 - (f+m)//2)*(k-1)
  print(d_c(s)*k-dis)
else:
  print(d_c(s)*k)

Submission status: Wrong Answer

Solution code:
def d_c(s):
  count = 0
  x = ''
  for i in range(len(s)):
    if x == s[0]:
      count += 1
      x = ''
    else:
      x = s[0]
    s = s[1:]
  return count
 
def num_con(s):
  x = s[0]
  s = s[1:]
  count = 1
  for i in range(len(s)):
    if s[0] == x:
      count += 1
      x = s[0]
      s = s[1:]
    else:
      break
  return count
      
    
s = input()
k = int(input())
 
if len(s)==1:
  print(k//2)
elif s[0]==s[-1]:
  f = num_con(s)
  m = num_con(s[::-1])
  dis = (f//2 + m//2 - (f+m)//2)*(k-1)
  print(dis)
  print(d_c(s)*k-dis)
else:
  print(d_c(s)*k)

Submission status: Wrong Answer

Solution code:
def d_c(s):
  count = 0
  x = ''
  for i in range(len(s)):
    if x == s[0]:
      count += 1
      x = ''
    else:
      x = s[0]
    s = s[1:]
  return count
 
def num_con(s):
  x = s[0]
  s = s[1:]
  count = 1
  for i in range(len(s)):
    if s[0] == x:
      count += 1
      x = s[0]
      s = s[1:]
    else:
      break
  return count
      
    
s = input()
k = int(input())
 
if len(s)==1:
  print(k//2)
elif s[0]==s[-1]:
  f = num_con(s)
  m = num_con(s[::-1])
  dis = (f//2 + m//2 - (f+m)//2)*(k-1)
  print(d_c(s)-dis)
else:
  print(d_c(s)*k)

Submission status: Wrong Answer

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

chrs = []
prev = 0
cnt = 0
for c in s:
    if c==prev: cnt+=1
    else: chrs.append(cnt);cnt=1
    prev = c
chrs.append(cnt)

ans = 0
if s[0]==s[-1]: ans = (chrs[1]%2 and chrs[-1]%2) * (k-1)
for i in chrs: ans += i//2
ans *= k

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

print(ans)

Submission status: Wrong Answer

Solution code:
S = list(input())
K = int(input())
A = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
num = 0
TTL_num = 0
first = 0

if len(S)==1:
	num += 1
	first += 1
elif len(S)==2:
	if S[0] == S[1]:
		num += 1
else:
	for i in range(len(S)):
		if i <= (len(S)-2):
			if S[i] == S[i+1]:
				if i <= (len(S)-3):
					for j in range(len(A)):
						if S[i] != A[j] and S[i+2] != A[j]:
							S[i+1] = A[j]
							num += 1
							break
				elif i == (len(S)-2):
					for k in range(len(A)):
						if S[i] != A[k] and S[0] != A[k]:
							S[i+1] = A[k]
							num += 1
							break
		if i == (len(S)-1) and S[i] == S[0]:
			for l in range(len(A)):
				if S[0] != A[l]:
					num += 1
					first += 1
					break

if first == 1:
	TTL_num = num*K -1
else:
	TTL_num = num*K

print(TTL_num)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

focus_c = S[0]
consecutive_s = S[0]
focus_s_array = []
for s in S[1:]:
    if s == focus_c:
        consecutive_s += s
    else:
        focus_s_array.append(consecutive_s)
        focus_c = s
        consecutive_s = s
else:
    focus_s_array.append(consecutive_s)


cost = 0
for fsa in focus_s_array:
    cost += len(fsa) // 2
ans = cost * K

if len(focus_s_array) == 1:
    if len(focus_s_array[0]) % 2 == 1:
        ans += K - 1
elif (focus_s_array[0][0] == focus_s_array[-1][0]) and (len(focus_s_array[0]) + len(focus_s_array[-1])) % 2 == 0:
    ans += K - 1

print(ans)

Submission status: Wrong Answer

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

focus_c = S[0]
consecutive_s = S[0]
focus_s_array = []
for s in S[1:]:
    if s == focus_c:
        consecutive_s += s
    else:
        focus_s_array.append(consecutive_s)
        focus_c = s
        consecutive_s = s
else:
    focus_s_array.append(consecutive_s)


cost = 0
if focus_s_array[0][0] == focus_s_array[-1][0] and len(focus_s_array) > 1:
    focus_s_array[0] += focus_s_array[-1]
    focus_s_array = focus_s_array[:-1]
else:
    print((len(S) * K) // 2)
    exit()

for fsa in focus_s_array:
    cost += len(fsa) // 2
ans = cost * K

print(ans)

Submission status: Wrong Answer

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

focus_c = S[0]
consecutive_s = S[0]
focus_s_array = []
for s in S[1:]:
    if s == focus_c:
        consecutive_s += s
    else:
        focus_s_array.append(consecutive_s)
        focus_c = s
        consecutive_s = s
else:
    focus_s_array.append(consecutive_s)


cost = 0
if focus_s_array[0][0] == focus_s_array[-1][0] and len(focus_s_array) > 1:
    focus_s_array[0] += focus_s_array[-1]
    focus_s_array = focus_s_array[:-1]

for fsa in focus_s_array:
    cost += len(fsa) // 2
ans = cost * K

print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
c = 0
d = 0
a = []
s += "A"
for i in range(len(s)-1):
    if (s[i] == s[i+1]):
        c += 1
    else:
        a.append(c+1)
        d += 1
        c = 0
c = 0
if s[0] != s[len(s)-2]:
    for i in range(d):
        c += int(a[i]/2)
    print(int(c*k))
elif d == 1:
    print(int(a[0]*k/2))
else:
    for i in range(1,d):
        c += int(a[i]/2)
    c += (k-1)*int((a[0]+a[d-1])/2) + int(a[0]/2) + int(a[d-1]/2)
    print(int(c))

Submission status: Wrong Answer

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

REPLACED = '_'


def replace(s):
    characters = list(s)
    s_length = len(s)
    s_types = len(set(s))

    if s_length == s_types:
        return s

    i = 0
    while i < s_length - 1:
        chunk = characters[i:i + 3]

        if REPLACED in chunk:
            i += 1
            continue

        chunk_length = len(chunk)
        chunk_types = len(set(chunk))
        if chunk_length == 1:
            break
        elif chunk_length == 2:
            if chunk_types == 2:
                break
            else:
                characters[i + 1] = REPLACED
                break
        elif chunk_length == 3:
            if chunk_types == 3:
                i += 2
                continue
            elif chunk_types == 2:
                if chunk[0] == chunk[1]:
                    characters[i] = REPLACED
                    i += 2
                    continue
                elif chunk[1] == chunk[2]:
                    characters[i + 2] = REPLACED
                    i += 3
                    continue
                else:
                    i += 2
                    continue
            else:
                characters[i + 1] = REPLACED
                i += 2
                continue
        else:
            raise Exception

    return ''.join(characters)


def check(s, k):
    s_length = len(s)
    s_types = len(set(s))
    if (s_types == 1) or (s_length == 1):
        return s_length * k // 2
    elif s_types == s_length:
        return 0
    else:
        replaced_s = replace(s)
        replaced_count = replaced_s.count(REPLACED)
        if replaced_s[0] == replaced_s[-1] and replaced_s[0] != REPLACED:
            return (replaced_count * k) + (k - 1)
        else:
            return replaced_count * k


print(check(S, K))

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
c = 0
d = 0
a = []
s += "A"
for i in range(len(s)-1):
    if (s[i] == s[i+1]):
        c += 1
    else:
        a.append(c+1)
        d += 1
        c = 0
c = 0
if s[0] != s[len(s)-2]:
    for i in range(d):
        c += a[i]//2
    print(int(c*k))
elif d == 1:
    print(int(a[0]*k//2))
else:
    for i in range(1,d):
        c += a[i]//2
    c += (k-1)*((a[0]+a[d-1])//2) + a[0]//2 + a[d-1]//2
    print(int(c))

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
n = len(s) - 1
count = 1
output = 0
last = 0
tmp = 0

for i in range(n):
    if s[i] == s[i + 1]:
        count += 1

    else:
        output += count // 2
        count = 1

if s[0] == s[-1]:
    for i in range(n + 1):
        if s[i] == s[0]:
            count += 1

        else:
            tmp = i
            break

    if tmp == 0:
        print((len(s) * k) // 2)
        
    else:
        output += count // 2
        count = 1
 
        for i in range(tmp, n):
            if s[i] == s[i + 1]:
                count += 1
 
            else:
                last += count // 2
                count = 1
 
        last += count // 2
 
        print(output * (k - 1) + last)
else:
    output += count // 2
    count = 1

    for i in range(tmp, n):
        if s[i] == s[i + 1]:
            count += 1

        else:
            last += count // 2
            count = 1

    last += count // 2

    print(output * (k - 1) + last)

Submission status: Wrong Answer

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

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

left = 0
right = 1

while left < N and S[left] == S[0]:
    left += 1
while right <= N and S[-right] == S[-1]:
    right += 1

change = 0
isChanged = False
T = list(S[left: N - right + 1])

for i, t in enumerate(T[1:], start=1):
    if t == T[i - 1]:
        if not isChanged:
            change += 1
            isChanged = True
            continue
    isChanged = False

ans = change * K
if S[0] == S[-1]:
    ans += left // 2 + (left + right - 1) // 2 * (K - 1) + (right - 1) // 2
print(ans)

Submission status: Wrong Answer

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

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

left = 0
right = N - 1

while left < N and S[left] == S[0]:
    left += 1
while right >= 0 and S[right] == S[-1]:
    right -= 1

change = 0
for i, s in enumerate(S[left:right + 2], start=left):
    if s == S[i - 1]:
        if not isChanged:
            change += 1
            isChanged = True
            continue
    isChanged = False

ans = change * K
if S[0] == S[-1]:
    ans += left // 2 + (left + (N - right - 1)) // 2 * (K - 1) + (N - right - 1) // 2
print(ans)

Submission status: Wrong Answer

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

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

if count == len(s)-1:#すべて同じ場合
	print((len(s)*k//2))

elif s[0] != s[-1]:#先頭と末尾が異なる場合
	print((count)*k)

elif s[0] == s[-1]:#先頭と末尾が同じ場合
	for i in range(len(s)):# start
		if s[0] == s[i]: 
			start += 1
		else:
			break
	if start == 1:# 1は消す
		start = 0

	for i in reversed(range(len(s))):#end 
		if s[-1] == s[i]:
			end += 1
		else:
			break
	if end == 1:#1は消す
		end = 0

	print (k*((count))-\
			((k-1)*(start//2)+\
				(end//2)-\
					(start+end)//2))

Submission status: Wrong Answer

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

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

if count == len(s)-1:#すべて同じ場合
	print((len(s)*k//2))

elif s[0] != s[-1]:#先頭と末尾が異なる場合
	print((count//2)*k)

elif s[0] == s[-1]:#先頭と末尾が同じ場合
	for i in range(len(s)):
		if s[0] == s[i]: 
			start += 1
			print(start)
		else:
			break
	if start == 1:
		start = 0
	for i in reversed(range(len(s))):
		if s[-1] == s[i]:
			end += 1
		else:
			break
	if end == 1:
		end = 0

	print (k*((count//2))-\
			((k-1)*(((start//2)+\
				((end//2))-\
					(start+end)//2))))

Submission status: Wrong Answer

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

def main():
    s = input().strip()
    k = int(input())

    d = [[s[0], 1]]
    for i in range(1, len(s)):
        if s[i] == d[-1][0]:
            d[-1][1] += 1
        else:
            d.append([s[i], 1])
    
    if len(d) == 1:
        print((len(s) * k) // 2)
    else:
        ans = 0
        for c, count in d:
            ans += count // 2
        ans *= k
        if d[0][0] == d[-1][0]:
            a = d[0][1]
            b = d[-1][1]
            ans -= (a//2) + (b//2) - ((a+b)//2)
        print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

t = S[0]

if S.count(t) == len(S) :
    ans = (len(S)//2)*N
elif t != S[-1] :
    ans = 0
    c = 1
    for i in range(1,len(S)) :
        if S[i] == S[i-1] :
            c += 1
        else :
            ans += c//2
            c = 1
    ans *= N
else :
    a = 0
    b = 0
    c = 0
    d = 1
    for s in S :
        if t == s :
            a += 1
        else :
            break
    for s in S[::-1] :
        if t == s :
            b += 1
        else :
            break
    for i in range(a+1,len(S)-b+1) :
        if S[i] == S[i-1] :
            d += 1
        else :
            c += d//2
            d = 1
    ans = a//2 + c*N + (((b+a)//2)*(N-1)) + b//2

print(ans)

Submission status: Wrong Answer

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

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

if count == len(s)-1:#すべて同じ場合
	print(-(-count//2)*k)

elif s[0] != s[-1]:#先頭と末尾が異なる場合
	print(-(-count//2)*k)

elif s[0] == s[-1]:#先頭と末尾が同じ場合
	for i in range(len(s)):
		if s[0] == s[i]: 
			start += 1
			print(start)
		else:
			break
	if start == 1:
		start = 0
	for i in reversed(range(len(s))):
		if s[-1] == s[i]:
			end += 1
		else:
			break
	if end == 1:
		end = 0
	print (k*(-(-count//2))-\
			((k-1)*((-(-start//2)+\
				(-(-end//2))-\
					(start+end)//2))))

Submission status: Wrong Answer

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

if count == len(s)-1:#すべて同じ場合
	print(-(-count//2)*k)

elif s[0] != s[-1]:#先頭と末尾が異なる場合
	print(-(-count//2)*k)

elif s[0] == s[-1]:#先頭と末尾が同じ場合
	for i in range(len(s)):
		if s[0] == s[i]: 
			start += 1
		else:
			break
	for i in reversed(range(len(s))):
		if s[-1] == s[i]:
			end += 1
		else:
			break
	print (k*(-(-count//2))-\
			((k-1)*((-(-start//2)+\
				(-(-end//2))-\
					(start+end)//2))))

Submission status: Wrong Answer

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

if count == len(s)-1:#すべて同じ場合
	print(-(-count//2)*k)

elif s[0] != s[-1]:#先頭と末尾が異なる場合
	print(-(-count//2)*k)

elif s[0] == s[-1]:#先頭と末尾が同じ場合
	for i in range(len(s)):
		if s[0] == s[i+1]: 
			start += 1
		else:
			break
	for i in range(len(s)-1,0,-1):
		if s[-1] == s[i-1]:
			end += 1
		else:
			break
	print (k*(-(-count//2)+(-start//2)+(-end//2))+\
		(-(-start//2)+(-(-end//2))+(k-1)*(-(-(start+end)//2))))

Submission status: Wrong Answer

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

if count == len(s)-1:#すべて同じ場合
	print(-(-count//2)*k)

elif s[0] != s[-1]:#先頭と末尾が異なる場合
	print(-(-count//2)*k)

elif s[0] == s[-1]:#先頭と末尾が同じ場合
	for i in range(len(s)):
		if s[0] == s[i+1]: 
			start += 1
		else:
			break
	for i in range(len(s)-1,0,-1):
		if s[-1] == s[i-1]:
			end += 1
		else:
			break
	print (-(-start//2)+(-(-end//2))+(-(-(start+end)//2)))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

s=input()
K=int(input())
S=list(s) #文字を置き換えるためリストに変換

def monomer(A): #文字列内の置換回数を数える関数。引数がリスト。
    N=0
    for i in range(len(A)-1):
        if A[i]==A[i+1]:
            A[i+1]="?"
            N+=1
    return N

n=monomer(S) #文字列S単体内での隣接重複文字、重複を置き換える
if S[0]!=S[-1] and S[-1]!="?": #モノマー内の置き換え後に最初と最後が同じでなければそのまま掛け算でOK
    print(n*K)

#置き換え後も最初と最後が同じ場合は接続ダブりを考慮
else:
    WS=list(s*2) #qqqのような、偶数次と奇数次で置換文字が交互に変わるパターン対策
    WS.insert(0,WS[-1]) #ppapのような2次以降に最初の文字を置換した方がいいパターン対策
    wn=monomer(WS)
    SS=list(s*2)
    SS.insert(0,SS[-1])
    nn=monomer(SS)
    print(n+wn*((K-1)//2)+nn*((K-1)%2)) #最初のループ、途中のループ、最後に余ったループでの置き換え回数

Submission status: Wrong Answer

Solution code:
s=input()
K=int(input())
S=list(s) #文字を置き換えるためリストに変換

def monomer(A): #文字列内の置換回数を数える関数。引数がリスト。
    N=0
    for i in range(len(A)-1):
        if A[i]==A[i+1]:
            A[i+1]="?"
            N+=1
    return N

n=monomer(S) #文字列S単体内での隣接重複文字、重複を置き換える
if S[0]!=S[-1] and S[-1]!="?": #モノマー内の置き換え後に最初と最後が同じでなければそのまま掛け算でOK
    print(n*K)

#置き換え後も最初と最後が同じ場合は接続ダブりを考慮
else:
    if S[-1]=="?":
        S.insert(0,S[-1])
        nn=monomer(S)
        print(n+nn*(K-1))
    else:
        WS=S
        WS=list(s*2) #qqqのような、偶数次と奇数次で置換文字が交互に変わるパターン対策
        WS.insert(0,WS[-1]) #ppapのような2次以降に最初の文字を置換した方がいいパターン対策
        wn=monomer(WS)
        print(n+wn*((K-1)//2)+wn*((K-1)%2)) #最初のループ、途中のループ、最後に余ったループでの置き換え回数

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

s=input()
K=int(input())
S=list(s) #文字を置き換えるためリストに変換

def monomer(A): #文字列内の置換回数を数える関数。引数がリスト。
    N=0
    for i in range(len(A)-1):
        if A[i]==A[i+1]:
            A[i+1]="?"
            N+=1
    return N

n=monomer(S) #文字列S単体内での隣接重複文字、重複を置き換える
if S[0]!=S[-1]: #モノマー内の置き換え後に最初と最後が同じでなければそのまま掛け算でOK
    print(n*K)

else: #置き換え後も最初と最後が同じ場合は接続ダブりを考慮
    WS=list(s*2) #qqqのような、偶数次と奇数次で置換文字が交互に変わるパターン対策
    WS.insert(0,WS[-1]) #
    nn=monomer(WS)
    print(n+nn*((K-1)//2)+nn*((K-1)%2)) #最初のループ、途中のループ、最後に余ったループでの置き換え回数

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

s=input()
K=int(input())
S=list(s) #文字を置き換えるためリストに変換

def monomer(A): #文字列内の置換回数を数える関数。引数がリスト。
    N=0
    for i in range(len(A)-1):
        if A[i]==A[i+1]:
            A[i+1]="?"
            N+=1
    return N

n=monomer(S) #文字列S単体内での隣接重複文字、重複を置き換える
if S[0]!=S[-1]: #モノマー内の置き換え後に最初と最後が同じでなければそのまま掛け算でOK
    print(n*K)

else: #置き換え後も最初と最後が同じ場合は接続ダブりを考慮
    WS=list(s*2)
    WS.insert(0,WS[-1])
    del WS[-1]
    nn=monomer(WS)
    print(n+nn*((K-1)//2)+nn*((K-1)%2)) #最初のループ、途中のループ、最後に余ったループでの置き換え回数

Submission status: Wrong Answer

Solution code:
s=input()
K=int(input())
S=list(s) #文字を置き換えるためリストに変換

def monomer(A): #文字列内の置換回数を数える関数。引数がリスト。
    N=0
    for i in range(len(A)-1):
        if A[i]==A[i+1]:
            A[i+1]="?"
            N+=1
    return N

n=monomer(S) #文字列S単体内での隣接重複文字、重複を置き換える
if S[0]!=S[-1]: #モノマー内の置き換え後に最初と最後が同じでなければそのまま掛け算でOK
    print(n*K)

Submission status: Wrong Answer

Solution code:
s=input()
K=int(input())
S=list(s) #文字を置き換えるためリストに変換

def monomer(A): #文字列内の置換回数を数える関数。引数がリスト。
    N=0
    for i in range(len(A)-1):
        if A[i]==A[i+1]:
            A[i+1]="?"
            N+=1
    return N

n=monomer(S) #文字列S単体内での隣接重複文字、重複を置き換える
if S[0]!=S[-1]: #モノマー内の置き換え後に最初と最後が同じでなければそのまま掛け算でOK
    print(n*K)

else: #置き換え後も最初と最後が同じ場合を考慮
    WS=list(s*2)
    WS.insert(0,WS[-1])
    del WS[-1]
    nn=monomer(WS)
    print(nn*(K//2)+n*(K%2))

Submission status: Wrong Answer

Solution code:
S=list(str(input()))
K=int(input())
ans=ans2=0

for i in range(1,len(S)):
    if S[i] == S[i-1]:
        ans += 1
        S[i] = '_'

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

print(ans*K+ans2*(K-1))

Submission status: Wrong Answer

Solution code:
s=input()
K=int(input())
S=list(s) #文字を置き換えるためリストに変換

def monomer(A): #文字列内の置換回数を数える関数。引数がリスト。
    N=0
    for i in range(len(A)-1):
        if A[i]==A[i+1]:
            A[i+1]="?"
            N+=1
    return N

n=monomer(S) #文字列S単体内での隣接重複文字、重複を置き換える
if S[0]!=S[-1]: #モノマー内の置き換え後に最初と最後が同じでなければそのまま掛け算でOK
    print(n*K)
    
else: #置き換え後も最初と最後が同じ場合を考慮
    SS=list(s)
    SS.insert(0,SS[-1])
    del SS[-1]
    nn=monomer(SS)
    print(n+nn*(K-1))

Submission status: Wrong Answer

Solution code:
s=input()
t=int(input())
b=0
c=0
for i in range(c,len(s)-1):
  a=1
  for j in range(c+1,len(s)):
    if s[c]==s[j]:
      a+=1
    else:
      c=j
      break
  c=j
  b+=(a//2)
b=b*t
if s[0]==s[len(s)-1]:
  a=1
  c=1
  for k in range(len(s)-1):
    if s[k]==s[k+1]:
      c+=1
    else:
      break
  u=s[::-1]
  for l in range(len(s)-1):
    if u[l]==u[l+1]:
      a+=1
    else:
      break
  if a%2==1 and c%2==1:
    b+=t-1
w=s[::-1]
if w==s:
  b=t*len(w)//2
print(b)

Submission status: Wrong Answer

Solution code:
S = input()

#K = 1
K = int(input())

mycnt = 0
mynum = 1
for i in range(len(S)-1):
    if S[i] == S[i+1]:
        mynum += 1
        
    else: 
        mycnt += mynum//2
        mynum = 1
    #print('mycnt=', mycnt)

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

elif S[0] != S[-1]:
    
    print(mycnt * K)

else:
    myleft = 1
    myright = 0
    mys = S[0]
    i = 1
    while S[i] == mys:
        myleft += 1
        i += 1
    
    myright = 0
    i = -1
    while S[i] == mys:
        myright += 1
        i -= 1
    print( (mycnt * K) - ( myleft//2 + myright//2 - (myright + myleft)//2 ) * ( K - 1 ) )

Submission status: Wrong Answer

Solution code:
#O(2n)程度を見込む解答
s=input()
k=int(input())
ss=s+s
n=len(s)

counts=0
i=0
while i<n-1:
	if s[i]!=s[i+1]:
		i+=1
	else:
		i+=2
		counts+=1
		
countss=0
i=0
while i<n*2-1:
	if ss[i]!=ss[i+1]:
		i+=1
	else:
		i+=2
		countss+=1
		
if countss==counts*2:
	print(counts*k)
else:
	print(counts*k+k-1)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
n = len(s)
if n == 1:
    if k == 1:
        print(0)
    else:
        print(k//2)
else:
    ans = []
    ss = 0
    i = 0
    while True:
        if i == n:
            break
        curr = s[i]
        temp = 1
        i += 1
        if i == n :
            break
        while s[i] == curr:
            temp += 1
            i += 1
            if i == n:
                break
        ans.append(temp)
    print(ans)
    if len(ans) > 1:
        for i in ans:
            ss += (i//2)*k
        if s[0] == s[-1] and ans[0]%2 == ans[-1]%2 == 1:
            ss += k-1
    else:
        ss = (n//2)*k
    print(ss)

Submission status: Wrong Answer

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

if count == len(s)-1:#すべて同じ場合
	print((count//2)*k)

elif s[0] != s[len(s)-1]:#先頭と末尾が異なる場合
	print((count//2)*k)

elif s[0] == s[len(s)-1]:#先頭と末尾が同じ場合
	for i in range(len(s)):
		if s[0] == s[i+1]: 
			start += 1
		else:
			break
	for i in range(len(s)-1,0,-1):
		if s[len(s)-1] == s[i-1]:
			end += 1
		else:
			break
	print(start,end,count)
	print (((count)//2*k)-(k-1)*(((-(-start//2))+(-(-end//2)+((-(start+end))//2)))))

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
n = len(s)
if n == 1:
    if k == 1:
        print(0)
    else:
        print(k//2)
else:
    ans = []
    ss = 0
    i = 0
    while True:
        if i == n:
            break
        curr = s[i]
        temp = 1
        i += 1
        if i == n :
            ans.append(temp)
            break
        while s[i] == curr:
            temp += 1
            i += 1
            if i == n:
                break
        ans.append(temp)
    print(ans)
    if len(ans) > 1:
        for i in ans:
            ss += (i//2)*k
        if s[0] == s[-1] and ans[0]%2 == ans[-1]%2 == 1:
            ss += k-1
    else:
        ss = (n//2)*k
    print(ss)

Submission status: Wrong Answer

Solution code:

def main():
    s = input()
    k = int(input())
    print(solve(s, k))

def solve(s, k):
    ans = 0
    if len(set(s)) == 1:
        ans += replace_count(len(s)*k)
    else:
        # 接続部分
        start = 1
        end = 0
        i = 1
        # issii
        while (s[0] == s[i]):
            i +=1
            start +=1
        j = -1
        while (s[0] == s[j]):
            j -=1
            end +=1
        ans += replace_count(start+end) * (k-1)
        ans += replace_count(start) 
        ans += replace_count(end) 

        # 単語内部
        same = 1
        for idx in range(i, len(s)+j+1):
            if idx >= len(s)-1:
                break
            if s[idx] == s[idx+1]:
                same += 1
            else:
                ans += replace_count(same) * k
                same = 1
    return ans


def replace_count(n):
    return n//2

main()

Submission status: Wrong Answer

Solution code:
def main():
    S = input()
    K = int(input())
    a = list(S)
    cnt = 0
    if len(set(a))==1:
        cnt = (len(a)*K)//2
        print(cnt)
    else:
        temp = 1
        temp_2 = 1
        i = 0
        while a[i] == a[i+1]:
            temp += 1
            i += 1
        j = len(a) - 1
        while a[j] == a[j-1]:
            temp_2 += 1
            j -= 1
        for k in range(i+1,j-1):
            if a[k+1] == a[k]:
                a[k+1] = str(k)
                cnt += 1
        cnt *= K
        cnt += temp//2
        cnt += temp_2//2
        
        if a[0] == a[-1]:
            temp_3 = (temp + temp_2)//2
            cnt += temp_3 * (K-1)
        else:

            temp_4 = 0
            if temp > 1:
                temp_4 += temp//2
            elif temp_2 > 2:
                temp_4 += temp_2//2
            cnt += temp_4 * (K-1)


        print(cnt)

    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from itertools import*
s=input();k=int(input());x=0
g=[len(list(v))for k,v in groupby(s)]
for c in g:x+=c//2
x*=k
if s[-1]==s[0] and g[0]>1<g[-1]:
	if len(s)==g[0]:x+=k//2
	else:x+=k-1
print(x)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
n = len(s)
if n == 1:
    if k == 1:
        print(0)
    else:
        print(k//2)
else:
    ans = []
    ss = 0
    i = 0
    while True:
        if i == n:
            break
        curr = s[i]
        temp = 1
        i += 1
        if i == n :
            break
        while s[i] == curr:
            temp += 1
            i += 1
            if i == n:
                break
        ans.append(temp)
    if len(ans) > 1:
        for i in ans:
            ss += (i//2)*k
        if s[0] == s[-1] and ans[0]%2 == ans[-1]%2 == 1:
            ss += k-1
    else:
        ss = (n//2)*k
    print(ss)

Submission status: Wrong Answer

Solution code:
from sys import stdin
from itertools import groupby

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

li = [len(tuple(s)) for _ , s in groupby(s)]

if s[0] == s[-1] and k>1:
    if len(li) == 1:
        print((li[0]//2)*k)
        exit()
    else:
        co = 0
        for l in range(1,len(li)-1):
            co += li[l]//2
        co = co*k
        co += li[0]//2+li[-1]//2+((li[0]+li[-1])//2)*(k-1)
        print(co)
        exit()

else:
    co = 0
    for j in range(len(li)):
        co += li[j]//2
    print(co*k)
    exit()

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:
        if cnt[0] % 2 == 0:
            b += k - 1

ans = ren * k + b

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

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

li = [len(tuple(s)) for _ , s in groupby(s)]


if s[0] == s[-1]:
    if len(li) == 1:
        print((li[0]//2)*k)
        exit()
    else:
        co = 0
        for l in range(1,len(li)-1):
            co += li[l]//2
        co = co*k
        co += li[0]//2+li[-1]//2+((li[0]+li[-1])//2)*(k-1)
        print(co)
        exit()

else:
    co = 0
    for j in range(len(li)):
        co += li[j]//2
    print(co*k)
    exit()

Submission status: Wrong Answer

Solution code:
from sys import stdin
from itertools import groupby

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

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

li = [len(tuple(s)) for _ , s in groupby(s)]


if li == None:
    print(k//2)
    exit()

if s[0] == s[-1]:
    if len(li) == 1:
        print((li[0]//2)*k)
        exit()
    else:
        co = 0
        for l in range(1,len(li)-1):
            co += li[l]//2
        co = co*k
        co += li[0]//2+li[-1]//2+((li[0]+li[-1])//2)*(k-1)
        print(co)
        exit()

else:
    co = 0
    for j in range(len(li)):
        co += li[j]//2
    print(co*k)
    exit()

Submission status: Wrong Answer

Solution code:
from itertools import*
s,k=open(0);k=int(k);x=0
g=[len(list(v))for k,v in groupby(s)]
for c in g:x+=c//2
x*=k
if s[-1]==s[0]and(g[0]%2 or g[-1]%2):
	if len(s)==g[0]:x+=k//2
	else:x+=k-1
print(x)

Submission status: Wrong Answer

Solution code:
from itertools import*
s,k=open(0);k=int(k);x=0
g=[len(list(v))for k,v in groupby(s)]
for c in g:x+=c//2
x*=k
if s[-1]==s[0]and(g[0]%2 or g[-1]%2):
	if len(s)==g[0]:y+=k//2
	else:x+=k-1
print(x)

Submission status: Wrong Answer

Solution code:
from sys import stdin
from itertools import groupby

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

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

li = [len(tuple(s)) for _ , s in groupby(s)]


if li == None:
    print(k//2)
    exit()

if len(li) == 1:
    print((li[0]//2)*k)
    exit()

if s[0] == s[-1]:
    co = 0
    for l in range(1,len(li)-1):
        co += li[l]//2
    co = co*k
    co += li[0]//2+li[-1]//2+((li[0]+li[-1])//2)*(k-1)
    print(co)
    exit()

else:
    co = 0
    for j in range(len(li)):
        co += li[j]//2
    print(co*k)
    exit()