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())
def get_kyotsu(st):
    kyotsu = []
    tmp = 1
    for s1, s2 in zip(st[:-1], st[1:]):
        if s1 == s2:
            tmp += 1
        if s1 != s2 and tmp != 1:
            kyotsu.append(tmp)
            tmp = 1
    if tmp != 1:
        kyotsu.append(tmp)
    res = 0
    for k in kyotsu:
        res += k//2
    return res

sento = S[0]
for s1, s2 in zip(S[:-1], S[1:]):
    if s1 == s2:
        sento+=s2
    if s1 != s2:
        break
matsubi = S[-1]
for s1, s2 in zip(S[::-1][:-1], S[::-1][1:]):
    if s1 == s2:
        matsubi+=s2
    if s1 != s2:
        break

conn = matsubi + sento 

print(K * get_kyotsu(S) - (get_kyotsu(sento) + get_kyotsu(matsubi) - get_kyotsu(conn)) *(K-1))

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
def get_kyotsu(st):
    kyotsu = []
    tmp = 1
    for s1, s2 in zip(st[:-1], st[1:]):
        if s1 == s2:
            tmp += 1
        if s1 != s2 and tmp != 1:
            kyotsu.append(tmp)
            tmp =1
    res = 0
    for k in kyotsu:
        res += k//2
    return res

sento = S[0]
for s1, s2 in zip(S[:-1], S[1:]):
    if s1 == s2:
        sento+=s2
    if s1 != s2:
        break
matsubi = S[-1]
for s1, s2 in zip(S[::-1][:-1], S[::-1][1:]):
    if s1 == s2:
        matsubi+=s2
    if s1 != s2:
        break

conn = matsubi + sento 

print(K * get_kyotsu(S) - (get_kyotsu(sento) + get_kyotsu(matsubi) - get_kyotsu(conn)) *(K-1))

Submission status: Wrong Answer

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

    def f(s):
        prev = "A"
        cnt = 0
        for e in s:
            if e == prev:
                cnt += 1
                prev = "A"
            else:
                prev = e

        return cnt

    li = [f(s*i) for i in range(6)]

    if k < 6:
        ans = li[k]

    else:
        d = li[5] - li[4]
        ans = li[5] + d * (k - 5)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# At GrandContest 039 A
def count(s):
    n = len(s)
    s = list(s)
    m = n
    if n % 2 == 1:
        n -= 1
    icount = 0
    for i in range(0, n - 1):
        if s[i] == s[i + 1]:
            s[i + 1] = '_'
            icount += 1
#    if n != m and s[0] == s[-1]:
#        icount += 1
    return icount

assert count("aa") == 1
assert count("aaa") == 1
assert count("aba") == 0
assert count("aaaaaa") == 3
assert count("aaaaaaaaa") == 4
assert count("issiiissii") == 4
assert count("aaaa") == 2

def f(s, k):
    # min num of operations required to make s*k any two adj chars different
    unit = len(s)

    # unit is even
    if unit % 2 == 0:
        return count(s) * k
    else: # unit is odd
        if k % 2 == 0: # reps is even
            return count(s + s) * (k // 2)
        else: # unit is odd, k is odd (eg 3 x 5)
            if k == 1:
                return count(s)
            two = count(s + s)
            three = count(s + s + s)
#            print("two", two, "three", three, k)
            return two * (k // 2) + (three - two)

assert f("aba", 1) == 0
assert f("aaa", 3) == 4 # a_a_a_a_a
assert f("aaa", 4) == 6 # a_a_a_a_a_a_
assert f("aaa", 5) == 7 # a_a_a_a_a_a_a_a
assert f("aa", 1) == 1 # a_
assert f("aa", 2) == 2 # a_a_
assert f("aa", 3) == 3 # a_a_a_
assert f("aa", 4) == 4 # a_a_a_a_
assert f("aaa", 1) == 1 # a_a
assert f("aaa", 2) == 3 # a_a_a_
assert f("aaaa", 1) == 2 # a_a_
assert f("aaaa", 2) == 4 # a_a_a_a_
assert f("aaaa", 3) == 6 # a_a_a_a_a_a_
assert f("issii", 2) == 4 # is_i_is_i_
assert f("qq", 81) == 81
assert f("cooooooooonteeeeeeeeeest", 999993333) == 8999939997

s = input()
k = int(input())
ans = f(s, k)
print(ans)

Submission status: Wrong Answer

Solution code:
# At GrandContest 039 A
def count(s):
    n = len(s)
    s = list(s)
    m = n
    if n % 2 == 1:
        n -= 1
    icount = 0
    for i in range(0, n - 1):
        if s[i] == s[i + 1]:
            s[i + 1] = '_'
            icount += 1
#    if n != m and s[0] == s[-1]:
#        icount += 1
    return icount

assert count("aa") == 1
#assert count("aaa") == 2
assert count("aaaaaa") == 3
assert count("aaaaaaaaa") == 4
assert count("issiiissii") == 4

def f(s, k):
    # min num of operations required to make s*k any two adj chars different
    unit = len(s)

    # unit is even
    if unit % 2 == 0:
        return count(s) * k
    else: # unit is odd
        if k % 2 == 0: # reps is even
            return count(s + s) * (k // 2)
        else: # unit is odd, k is odd (eg 3 x 5)
            two = count(s + s)
            three = count(s + s + s)
#            print("two", two, "three", three, k)
            return two * (k // 2) + (three - two)

assert f("aaa", 3) == 4 # a_a_a_a_a
assert f("aaa", 5) == 7 # a_a_a_a_a_a_a_a
assert f("aa", 1) == 1 # a_
assert f("aa", 2) == 2 # a_a_
assert f("aa", 3) == 3 # a_a_a_
assert f("aa", 4) == 4 # a_a_a_a_
assert f("aaa", 1) == 1 # a_a
assert f("aaa", 2) == 3 # a_a_a_
assert f("aaaa", 1) == 2 # a_a_
assert f("issii", 2) == 4 # is_i_is_i_
assert f("qq", 81) == 81
assert f("cooooooooonteeeeeeeeeest", 999993333) == 8999939997

s = input()
k = int(input())
ans = f(s, k)
print(ans)

Submission status: Wrong Answer

Solution code:
# At GrandContest 039 A
def f(s, k):
    # min num of operations required to make s*k any two adj chars different
    n = len(s)
    s = list(s)
    icount, ecount = 0, 0
    for i in range(n - 1):
        if s[i] == s[i + 1]:
            s[i + 1] = '_'
            icount += 1
    if s[0] == s[-1] and s[0] != '_':
        ecount += 1
    icount *= k
    if k > 1:
        ecount *= (k - 1)
    return icount + ecount

assert f("a", 2) == 1 # a_
assert f("a", 3) == 2 # a__
assert f("aa", 1) == 1 # a_
assert f("aa", 2) == 2 # a_a_
assert f("aa", 3) == 3 # a_a_a_
assert f("aaa", 1) == 2
assert f("aaaa", 1) == 2 # _a_a
assert f("issii", 2) == 4 # is_i_is_i_
assert f("qq", 81) == 81
assert f("cooooooooonteeeeeeeeeest", 999993333) == 8999939997

s = input()
k = int(input())
ans = f(s, k)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
s = input()

k = int(input())

n = len(s)

res = 0

i = 1
l = 1

R = []

while i < n:
    if s[i]==s[i-1] :
        l += 1
        if i==n-1 :
            R.append(l)

    else :
        R.append(l)
        l = 1
        if i==n-1 :
            R.append(l)
    i = i+1

m = len(R)
for i in range(m) :
    if i==0 :
        if s[0]==s[n-1] :
            res += R[0]//2
        else :
            res += R[0]//2*k;
    elif i==m-1:
        if s[0]==s[m-1] :
            res += (R[m-1]+R[0])//2*(k-1)
        else :
            res += R[m-1]//2*k
    else :
        res += R[i]//2*k

if len(R)==1 :
    print(n*k//2)
else:
    print(res)

Submission status: Wrong Answer

Solution code:
s,k=open(0)
a,k=0,int(k)
b=t=s[0]
for c in s[1:-1]:
  a+=c==b
  b=c*(c!=b)
print(a*k+~-k)

Submission status: Wrong Answer

Solution code:
s,k=open(0)
a,k=0,int(k)
b=t=s[0]
for c in s[1:-1]:
  a+=c==b
  b=c*(c!=b)
print(a*k)

Submission status: Wrong Answer

Solution code:
s,k=open(0)
a,k=0,int(k)
b=t=s[0]
for c in s[1:-1]:
  a+=c==b
  b=c*(c!=b)
print(a*k+(b==t)*id(b)%4%2*~-k)

Submission status: Wrong Answer

Solution code:
s,k=open(0)
a,k=0,int(k)
b=t=s[0]
for c in s[1:-1]:
  f=c!=b
  a+=1^f
  b=c*f
print(a*k+(b==t)*id(b)%5%2*~-k)

Submission status: Wrong Answer

Solution code:
s,k=open(0)
k=int(k)
b=t=s[0]
a=0
for c in s[1:-1]:
  f=c!=b
  a+=1^f
  b=c*f
print(a*k+(b==t)*id(b)%3%2*~-k)

Submission status: Wrong Answer

Solution code:
s,k=open(0)
k=int(k)
l=len(s)
if l<3:
  print(k//2)
  exit()
b=t=s[0]
a=0
for c in s[1:-1]:
  f=c!=b
  a+=1^f
  b=c*f
print(a*k+(b==t)*~-k)

Submission status: Wrong Answer

Solution code:
s,k=open(0)
k=int(k)
b=t=s[0]
a=0
for c in s[1:-1]:
  f=c!=b
  a+=1^f
  b=c*f
print(a*k+(b==t)*~-k)

Submission status: Wrong Answer

Solution code:
s,k=open(0)
b=t=s[0]
a=0
for c in s[1:-1]:
  f=c!=b
  a+=1^f
  b=c*f
print((a+(b==t))*int(k))

Submission status: Wrong Answer

Solution code:
s,k=open(0)
b=t=s[0]
a=0
for c in s[1:-1]:
  f=c!=b
  a+=1^f
  b=c*f
print((a+(b==k))*int(k))

Submission status: Wrong Answer

Solution code:
import sys
s = input()
k = int(input())
if len(set(list(s))) == 1:
	print((len(s)*k)//2)
	sys.exit()
tmp = s[0]
cnt = 1
base = 0
is_first = True
for i in range(1, len(s)):
	if s[i] == tmp:
		cnt += 1
	else:
		if is_first:
			is_first = False
			first = cnt//2
		else:
			base += cnt//2
		tmp = s[i]
		cnt = 1

if s[0] != tmp:
	base += cnt//2
	print((first+base)*k)
else:
	i = 0
	last = cnt//2
	while s[i] == tmp:
		cnt += 1
		i += 1
	base += cnt//2
	#print(first, base, last)
	print(first + base*(k-1) + last)

Submission status: Wrong Answer

Solution code:
import sys
s = input()
k = int(input())
if len(set(list(s))) == 1:
	print((len(s)*k)//2)
	sys.exit()
s += s[0]
tmp = s[0]
cnt = 1
ans = 0
for i in range(1, len(s)):
	if s[i] == tmp:
		cnt += 1
	else:
		ans += cnt//2
		tmp = s[i]
		cnt = 1
ans += cnt//2
ans *= k
if len(s) >= 3 and s[-1] == s[-2] and s[-2] != s[-3]:
	ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
s = input()
k = int(input())
if len(set(list(s))) == 1:
	print((len(s)*k)//2)
	sys.exit()
s += s[0]
tmp = s[0]
cnt = 1
ans = 0
for i in range(1, len(s)):
	if s[i] == tmp:
		cnt += 1
	else:
		ans += cnt//2
		tmp = s[i]
		cnt = 1
ans += cnt//2
ans *= k
if s[-1] == s[-2]:
	ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
s = input()
k = int(input())
if len(set(list(s))) == 1:
	print((len(s)*k)//2)
	sys.exit()
s += s[0]
tmp = s[0]
cnt = 1
ans = 0
for i in range(1, len(s)):
	if s[i] == tmp:
		cnt += 1
	else:
		ans += cnt//2
		tmp = s[i]
		cnt = 1
ans += cnt//2
ans *= k
print(ans)

Submission status: Wrong Answer

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

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

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

	return cnt[0], cnt[-1]



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:

	connect_cnt_p = connect_cnt(S)
	a, b = connect_cnt_ab(S)
	ans = connect_cnt_p*K - (a//2 + b//2 - (a+b)//2)*(K-1)
	# a : 最初の何文字連続 b : 最後の何文字連続
	# 全体から最初と最後の重なりでの増減分を引く
	print(ans)

Submission status: Wrong Answer

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

last = ''
cnt_diff = 0
for s in S:
    if(s==last):
        cnt_diff+=1
        last='0'
    else:
        last=s
if(len(S)==S.count(S[0])):
    print(len(S)*K//2)
elif(S[0]==S[-1] and last!='0'):
    print(cnt_diff+(cnt_diff+1)*(K-1))
else:
    print(cnt_diff*K)

Submission status: Wrong Answer

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

def ranlength(A):
    num = list(A)
    count = 1
    result = ''
    table = []
    for i in range(len(num)):
        try:
            if num[i] == num[i+1]:
               count = count + 1
            else:
                result += str(num[i]) + str(count)
                table.append(count)
                count = 1
        except:
            result += str(num[i]) + str(count)
            table.append(count)
    return(table)

def one(m):
    return(sum([i//2 for i in ranlength(m)]))

def two(m):
    M=m+m
    return(one(M))

print(one(S)*K + (two(S)-one(S)*2)*(K-1))

Submission status: Wrong Answer

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

def ranlength(A):
    num = list(A)
    count = 1
    result = ''
    table = []
    for i in range(len(num)):
        try:
            if num[i] == num[i+1]:
               count = count + 1
            else:
                result += str(num[i]) + str(count)
                table.append(count)
                count = 1
        except:
            result += str(num[i]) + str(count)
            table.append(count)
    return(table)

def one(m):
    return(sum([i//2 for i in ranlength(m)]))

def two(m):
    M=m+m
    return(one(M))

reigai = 0
if (ranlength(S)[-1]*ranlength(S)[0])%2 == 1 and S[-1] == S[0]:
    reigai += 1

print(two(S)*(K//2) + one(S)*(K%2) + reigai)

Submission status: Wrong Answer

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

s_list = list(S)

modified = "mdf"

def solve():
    # その単語に1種類の文字しかなければ、合計の文字数を2で割捨てした数
    if len(set(s_list)) == 1:
        return (len(s_list)*K)//2
    
    cnt = 1
    cnt_list = []
    for i in range(len(s_list)-1):
        if s_list[i] == s_list[i+1]:
            cnt += 1
        else:
            cnt_list.append(cnt)
            cnt = 1
    
    # 最後の対応
    # cnt = 1の時、最後は1文字だけ
    # cnt >= 2の時、最後は同じ文字が続いてる
    cnt_list.append(cnt)
    
    # 最初と最後が同じ時は最初と最後を足したやつを作る必要。
    head = cnt_list[0]
    tail = cnt_list[-1]
    body = cnt_list[1:-1]
    
    if s_list[0] == s_list[-1]:
        head_tail = cnt_list[0] + cnt_list[-1]
        return head//2 + tail//2 + (sum(body)//2)*K + (head_tail//2)*(K-1)
    else:
        return (head//2 + tail//2 + sum(body)//2) * K

result = solve()

print(result)

Submission status: Wrong Answer

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

def ranlength(A):
    num = list(A)
    count = 1
    result = ''
    table = []
    for i in range(len(num)):
        try:
            if num[i] == num[i+1]:
               count = count + 1
            else:
                result += str(num[i]) + str(count)
                table.append(count)
                count = 1
        except:
            result += str(num[i]) + str(count)
            table.append(count)
    return(table)

def one(m):
    return(sum([i//2 for i in ranlength(m)]))

def two(m):
    M=m+m
    return(one(M))

reigai = 0
if ranlength(S)[-1]%2 == 1 and S[-1] == S[0]:
    reigai += 1

print(two(S)*(K//2) + one(S)*(K%2) + reigai)

Submission status: Wrong Answer

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

def ranlength(A):
    num = list(A)
    count = 1
    result = ''
    table = []
    for i in range(len(num)):
        try:
            if num[i] == num[i+1]:
               count = count + 1
            else:
                result += str(num[i]) + str(count)
                table.append(count)
                count = 1
        except:
            result += str(num[i]) + str(count)
            table.append(count)
    return(table)

def one(m):
    return(sum([i//2 for i in ranlength(m)]))

def two(m):
    M=m+m
    return(one(M))

print(two(S)*(K//2) + one(S)*(K%2))

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
s_2 = s
if len(set(s)) == 1:
	print(len(s) * k // 2) 
else:
	count = 0
	for i in range(1, len(s)):
		if s[i] == s[i-1]:
			s[i] = "_"
			count += 1
	if s[0] != s[-1]:
		print(count * k)
	else:
		s = s_2 * 2
		count_2 = 0
		for j in range(1, len(s)):
			if s[j] == s[j-1]:
				s[j] = "_"
				count_2 += 1
		diff = count_2 - count
		print(diff*(k-1)+count)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
cnt = 0
cnt_1 = 0
flow = 0
flow_1 = 0

if len(s) != 1:

    for i in range(len(s)):
        if s[flow_1] == s[flow_1+1]:
            cnt_1 += 1
            flow_1 += 2
        else:
            flow_1 += 1

        if flow_1 >= len(s)-1:
            break

    if k != 1:
        for i in range(len(s)):
            s.append(s[i])

    for i in range(len(s)):
        if s[flow] == s[flow+1]:
            cnt += 1
            flow += 2
        else:
            flow += 1

        if flow >= len(s)-1:
            break
    if k % 2 == 0:
        print(int(cnt * (k / 2)))
    elif s[0] != s[-1]:
        print(int(cnt * ((k - 1) / 2) + cnt_1))
    else:
        print(int(cnt * ((k-1) / 2) + cnt_1 + 1))

else:
    print(len(s)*k//2)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
cnt = 0
cnt_1 = 0
flow = 0
flow_1 = 0

if len(s) != 1:

    for i in range(len(s)):
        if s[flow_1] == s[flow_1+1]:
            cnt_1 += 1
            flow_1 += 2
        else:
            flow_1 += 1

        if flow_1 >= len(s)-1:
            break

    if k != 1:
        for i in range(len(s)):
            s.append(s[i])

    for i in range(len(s)):
        if s[flow] == s[flow+1]:
            cnt += 1
            flow += 2
        else:
            flow += 1

        if flow >= len(s)-1:
            break
    if k % 2 == 0:
        print(int(cnt * (k / 2)))
    else:
        print(int(cnt * ((k-1) / 2) + cnt_1 + 1))

else:
    print(len(s)*k//2)

Submission status: Wrong Answer

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

F=0
for i in range(1,len(S)):
    if S[i]!=S[i-1]:
        F=1
        break
if F==0:
    print((len(S)*K)//2)
elif K==1:
    ans=0
    for i in range(1,len(S)):
        if S[i]==S[i-1]:
            ans+=1
            S[i]="#"
    print(ans)
else:
    ans1=0
    for i in range(1,len(S)):
        if S[i]==S[i-1]:
            ans1+=1
            S[i]="#"
    ans2=0
    S2=S*2
    for i in range(1,len(S2)):
        if S2[i]==S2[i-1]:
            ans2+=1
            S2[i]="#"
    print(ans1+(ans2-ans1)*(K-1))

Submission status: Wrong Answer

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

s_list = list(S)

modified = "mdf"

def solve():
    if len(s_list) == 1:
        return K//2
    
    result = 0
    double_cnt = 0
    tmp_s = s_list[0]
    for s in s_list[1:]:
        if tmp_s == s:
            double_cnt += 1
            tmp_s = modified
        else:
            tmp_s = s
            
    result += double_cnt * K
    
    # 最後の文字と最初の文字が一緒なら、K-1回足す
    if tmp_s == s_list[0]:
        result += (K-1)
        
    return result

result = solve()

print(result)

Submission status: Wrong Answer

Solution code:
def oc(s):
	c = 0
	i = 0
	li = len(s)-1
	while i < li:
		if s[i] == s[i+1]:
			s[i+1] = '*'
			i += 1
		i += 1
	return s
	
	
s = list(input())
k = int(input())

c = 0
i = 0
if k <= 3:
	s *= k
	print(oc(s).count('*'))
else:
	sl = len(s)
	s *= 3
	s = oc(s)
	a = list(zip(*[iter(s)]*sl))
	cl = [x.count('*') for x in a]
	cl[1] *= k-2
	print(sum(cl))

Submission status: Wrong Answer

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

if len(s) != 1:
    if s[0] == s[-1] and s.count(s[0]) % 2 == 1:
        s[0] = 'A'
    if k != 1:
        for i in range(len(s)):
            s.append(s[i])

    for i in range(len(s)):
        if s[flow] == s[flow+1]:
            cnt += 1
            flow += 2
        else:
            flow += 1

        if flow >= len(s)-1:
            break
    if s[0] != 'A':
        print(int(cnt * (k / 2)))
    else:
        print(int(cnt * (k / 2) + (k - 1)))

else:
    print(len(s)*k//2)

Submission status: Wrong Answer

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

F=0
for i in range(1,len(S)):
    if S[i]!=S[i-1]:
        F=1
        break
if F==0:
    print((len(S)*K)//2)
else:
    A=0
    for i in range(1,len(S)):
        if S[i]==S[i-1]:
            S[i]="#"
            A+=1
    
    B=0
    if S[0]==S[-1]:
        B+=1
    
    print(A*K+B*(K-1))

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
cnt = 0
flow = 0
dummy = 'A'
if len(s) != 1:
    if s[0] == s[-1]:
        s[0] = dummy
    
    if k != 1:
        for i in range(len(s)):
            s.append(s[i])

    for i in range(len(s)):
        if s[flow] == s[flow+1]:
            cnt += 1
            flow += 2
        else:
            flow += 1

        if flow >= len(s)-1:
            break
    if s[0] != dummy:
        print(int(cnt * (k / 2)))
    else:
        print(int(cnt * (k / 2)+(k-1)))
else:
    print(len(s)*k//2)

Submission status: Wrong Answer

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

if len(S)==1:
    print(len(S)//2)
else:
    A=0
    for i in range(1,len(S)):
        if S[i]==S[i-1]:
            S[i]="#"
            A+=1
    
    B=0
    if S[0]==S[-1]:
        B+=1
    
    print(A*K+B*(K-1))

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
cnt = 0
flow = 0
if len(s) != 1:
    if k != 1:
        for i in range(len(s)):
            s.append(s[i])

    for i in range(len(s)):
        if s[flow] == s[flow+1]:
            cnt += 1
            flow += 2
        else:
            flow += 1

        if flow >= len(s)-1:
            break
    print(int(cnt * (k / 2)))
else:
    print(len(s)*k//2)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
cnt = 0
flow = 0
if len(s) != 1:
    for i in range(len(s)):
        s.append(s[i])

    for i in range(len(s)):
        if s[flow] == s[flow+1]:
            cnt += 1
            flow += 2
        else:
            flow += 1

        if flow >= len(s)-1:
            break

    print(int(cnt * (k / 2)))
else:
    print(len(s)*k//2)

Submission status: Wrong Answer

Solution code:
s = input()
n = int(input())
lens = len(s)
cnt = 0
i = 1
if lens == 1:
  print(n//2)
  exit()

while i < lens:
  if s[i] == s[i-1]:
    i += 1
    cnt += 1
  i += 1
if s[0] == s[-1] and s[0] != s[1] and s[-1] != s[-2]:
  print(cnt*n+n-1)
else:
  print(cnt*n)

Submission status: Wrong Answer

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

for i in range(len(s)):
    s.append(s[i])

for i in range(len(s)):
    if s[flow] == s[flow+1]:
        cnt += 1
        flow += 2
    else:
        flow += 1

    if flow >= len(s)-1:
        break

print(int(cnt*(k/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)):# 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//2))-\
			((k-1)*(start//2)+\
				(end//2)-\
					(start+end)//2))

Submission status: Wrong Answer

Solution code:
s = raw_input().strip()
k = input()

n = len(s)

ans = 0

if n == 1:
    print k - 1
    exit()

if n == 2:
    if s[0] != s[1]:
        print 0
    else:
        print k

    exit()

# case 1: s[0] = s[n - 1]
if s[0] == s[n - 1]:
    prev = n - 1

    suff = 0
    ridx = 0
    for i in xrange(n - 1, -1, -1):
        if s[i] == s[prev]: suff += 1
        else: break

        prev = i
        ridx = i

    if suff == n:
        print (n * k) // 2
        exit()

    pref = 0
    prev = 0
    lidx = 0
    for i in xrange(n):
        if s[i] == s[prev]: pref += 1
        else: break

        prev = i
        lidx = i

    left = pref
    mid = pref + suff
    right = suff

    ans = (left // 2) + (mid // 2) * (k - 1) + (right // 2)

    prev = lidx + 1
    sz = 0
    for i in xrange(lidx + 1, ridx):
        if s[i] == s[prev]: sz += 1
        else:
            ans += ((sz // 2) * k)
            sz = 1

        prev = i

    if (sz > 1):
        ans += ((sz // 2) * k)
        sz = 0

    print ans
    exit()
else:
    ans = 0
    sz = 0
    prev = 0
    for i in xrange(n):
        if s[i] == s[prev]:
            sz += 1
        else:
            ans += (sz // 2)
            sz = 1
            
        prev = i

    ans += (sz // 2)
    sz = 0

    ans *= k

    print ans
    exit()

Submission status: Wrong Answer

Solution code:
s = raw_input().strip()
k = input()

n = len(s)

ans = 0

if n == 1:
    print k - 1
    exit()

if n == 2:
    if s[0] != s[1]:
        print 0
    else:
        print k

    exit()

# case 1: s[0] = s[n - 1]
if s[0] == s[n - 1]:
    prev = n - 1

    suff = 0
    ridx = 0
    for i in xrange(n - 1, -1, -1):
        if s[i] == s[prev]: suff += 1
        else: break

        prev = i
        ridx = i

    if suff == n:
        print (n * k) // 2
        exit()

    pref = 0
    prev = 0
    lidx = 0
    for i in xrange(n):
        if s[i] == s[prev]: pref += 1
        else: break

        prev = i
        lidx = i

    left = pref
    mid = pref + suff
    right = suff

    ans = (left // 2) + (mid // 2) * (k - 1) + (right // 2)

    prev = lidx + 1
    sz = 0
    for i in xrange(lidx + 1, ridx):
        if s[i] == s[prev]: sz += 1
        else:
            ans += ((sz // 2) * k)
            sz = 1

        prev = i

    if (sz > 1):
        ans += ((sz // 2) * k)
        sz = 0

    print ans
    exit()
else:
    sz = 0
    prev = 0
    for i in xrange(n):
        if s[i] == s[prev]:
            sz += 1
        else:
            ans += (sz // 2)
            sz = 1
            
        prev = i

    ans += (sz // 2)
    sz = 0

    ans *= k

    print ans
    exit()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *
import itertools
import math
INF = 10**9
MOD = 10**9+7

S, K = input(), int(input())
N = len(S)
if S[0] == S[-1]:
    i = 1
    while i < N and S[i-1] == S[i]:
        i += 1
    j = -1
    while j >= 0 and S[j] == S[j-1]:
        j -= 1
    if N+j <= i:
        print(N*K//2)
    else:
        ans = K*((i-j)//2)
        S = S[i:j]
        l, r = 0, 0
        n = len(S)
        lst = []
        while r < n:
            while r+1 < n and S[r] == S[r+1]:
                r += 1
            r += 1
            lst.append(r-l)
            l = r
        for x in lst:
            ans += K*(x//2)
        print(ans)
else:
    l, r = 0, 0
    lst = []
    while r < N:
        while r+1 < N and S[r] == S[r+1]:
            r += 1
        r += 1
        lst.append(r-l)
        l = r
    ans = 0
    for x in lst:
        ans += K*(x//2)
    print(ans)

Submission status: Wrong Answer

Solution code:
s = raw_input().strip()
k = input()

n = len(s)

ans = 0

if n == 1:
    print k - 1
    exit()

if n == 2:
    if s[0] != s[1]:
        print 0
    else:
        print k

    exit()

# case 1: s[0] = s[n - 1]
if s[0] == s[n - 1]:
    prev = n - 1

    suff = 0
    ridx = 0
    for i in xrange(n - 1, -1, -1):
        if s[i] == s[prev]: suff += 1
        else: break

        prev = i
        ridx = i

    if suff == n:
        print (n * k) // 2
        exit()

    pref = 0
    prev = 0
    lidx = 0
    for i in xrange(n):
        if s[i] == s[prev]: pref += 1
        else: break

        prev = i
        lidx = i

    left = pref
    mid = pref + suff
    right = suff

    ans = (left >> 1) + (mid >> 1) * (k - 1) + (right >> 1)

    prev = lidx + 1
    sz = 0
    for i in xrange(lidx + 1, ridx):
        if s[i] == s[prev]: sz += 1
        else:
            ans += ((sz // 2) * k)
            sz = 1

        prev = i

    if (sz > 1):
        ans += ((sz // 2) * k)
        sz = 0
else:
    sz = 0
    prev = 0
    for i in xrange(n):
        if s[i] == s[prev]:
            sz += 1
        else:
            ans += (sz // 2)
            sz = 1
            
        prev = i

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

    ans *= k

print ans

Submission status: Wrong Answer

Solution code:
s = raw_input().strip()
k = input()

n = len(s)

ans = 0

if n == 1:
    print k - 1
    exit()

if n == 2:
    if s[0] != s[1]:
        print 0
    else:
        print k

    exit()

# case 1: s[0] = s[n - 1]
if s[0] == s[n - 1]:
    prev = n - 1

    suff = 0
    ridx = 0
    for i in xrange(n - 1, -1, -1):
        if s[i] == s[prev]: suff += 1
        else: break

        prev = i
        ridx = i

    if suff == n:
        print (n * k) // 2
        exit()

    pref = 0
    prev = 0
    lidx = 0
    for i in xrange(n):
        if s[i] == s[prev]: pref += 1
        else: break

        prev = i
        lidx = i

    left = pref + 1
    right = n - suff

    mid = left + right
    
    ans = (left >> 1) + (mid >> 1) * (k - 1) + (right >> 1)

    prev = lidx + 1
    sz = 0
    for i in xrange(lidx + 1, ridx):
        if s[i] == s[prev]: sz += 1
        else:
            ans += (sz // 2)
            sz = 1

        prev = i

    if (sz > 1):
        ans += (sz // 2)
        sz = 0
        
else:
    sz = 0
    prev = 0
    for i in xrange(n):
        if s[i] == s[prev]:
            sz += 1
        else:
            ans += (sz // 2)
            sz = 1
            
        prev = i

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

    ans *= k

print ans

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
s=[]
cnt=0
a=0
b=1

if len(S)==1:
  print(K//2)
else:
  for i in range(len(S)):
    s.append(S[i])
  if s[0]==s[-1]:
    cnt+=1
    s[-1]='#'
  
  for i in range(len(S)-1):
    if s[i]==s[i+1]:
      s[i+1]='@'
      cnt+=1
    if S[i]==S[i+1]:
      b+=1
    else:
      b=1

  if S[0]!=S[-1]:
    b=0
  print(cnt*K-b%2)

Submission status: Wrong Answer

Solution code:
s = raw_input().strip()
k = input()

n = len(s)

ans = 0

if n == 1:
    print k - 1
    exit()

if n == 2:
    if s[0] != s[1]:
        print 0
    else:
        print k

    exit()

# case 1: s[0] = s[n - 1]
if s[0] == s[n - 1]:
    prev = n - 1

    suff = 0
    ridx = 0
    for i in xrange(n - 1, -1, -1):
        if s[i] == s[prev]: suff += 1
        else: break

        prev = i
        ridx = i

    if suff == n:
        print (n * k) // 2
        exit()

    pref = 0
    prev = 0
    lidx = 0
    for i in xrange(n):
        if s[i] == s[prev]: pref += 1
        else: break

        prev = i
        lidx = i

    left = pref
    mid = pref + suff
    right = suff

    ans = (left >> 1) + (mid >> 1) * (k - 1) + (right >> 1)

    prev = lidx + 1
    sz = 0
    for i in xrange(lidx + 1, ridx):
        if s[i] == s[prev]: sz += 1
        else:
            ans += (sz // 2)
            sz = 1

        prev = i

    if (sz > 1):
        ans += (sz // 2)
        sz = 0
        
else:
    sz = 0
    prev = 0
    for i in xrange(n):
        if s[i] == s[prev]:
            sz += 1
        else:
            ans += (sz // 2)
            sz = 1
            
        prev = i

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

    ans *= k

print ans