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: Accepted

Solution code:
s = list(input())
n = int(input())

pre = ''
count = 0
for i in s:
    if pre == i:
        count+=1
        pre = ''
    else:
        pre = i

if n == 1:
    print(count)
    exit()

count2 = 0
for i in s:
    if pre == i:
        count2+=1
        pre = ''
    else:
        pre = i

if n == 2:
    print(count + (n-1) * count2)
    exit()

count3 = 0
for i in s:
    if pre == i:
        count3+=1
        pre = ''
    else:
        pre = i

if count2 == count3:
    print(count + (n-1) * count2)
else:
    print(count + ((n-1)//2 + (n-1)%2) * count2 + ((n-1)//2)*count3)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

import sys
def input():
	return sys.stdin.readline().strip()

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

mozi = list(S)
num = 0

for i in range(1, len(S)):
	if mozi[i] == mozi[i - 1]:
		mozi[i] = "A"
		num += 1

if mozi[0] != mozi[-1]:
	print(num*K)
	exit()

mozi2 = list(S)
mozi2[0] = "A"
num2 = 1
for i in range(1, len(S)):
	if mozi2[i] == mozi2[i - 1]:
		mozi2[i] = "A"
		num2 += 1

if mozi[0] != mozi2[-1]:
	n2 = K//2
	n1 = K - n2
	print(num*n1 + num2*n2)
else:
	print(num + (K - 1)*num2)

Submission status: Accepted

Solution code:
S = input()
K = int(input())
def calc(s):
    a = 0
    i = 1
    while i < len(s):
        if s[i] == s[i-1]:
            a += 1
            i += 2
        else:
            i += 1
    return a

A = calc(S)
B = calc(S*2)
C = calc(S*3)
D = calc(S*4)
if K <= 10:
    print(calc(S*K))
elif K % 2 == 1:
    print(A + (C-A) * (K-1) // 2)
else:
    print(B + (D-B) * (K-2) // 2)

Submission status: Accepted

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

w = None
N = []
for c in S:
    if c!=w:
        w=c
        N.append(1)
    else:
        N[-1]+=1

ans = 0
if len(N)==1:
    ans = (N[0]*K)//2
elif S[0]!=S[-1]:
    for n in N:
        ans += n//2
    ans *= K
else:
    ans += N[0]//2 + N[-1]//2
    ans += ((N[0]+N[-1])//2)*(K-1)
    for n in N[1:-1]:
        ans += (n//2)*K

print(ans)

Submission status: Accepted

Solution code:
from itertools import groupby
S = input()
K = int(input())
a1 = 0
for v, g in groupby(S):
    n = len(list(g))
    a1 += n // 2

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

a2 = 0
for v, g in groupby(S+S):
    n = len(list(g))
    a2 += n // 2

print(a1 + (a2-a1)*(K-1))

Submission status: Accepted

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

if len(set(s)) == 1:
    print len(s) * k / 2
else:
    n = len(s)
    a = [1]
    for i in xrange(1, n):
        if s[i] == s[i - 1]:
            a[-1] += 1
        else:
            a.append(1)
    ans = 0
    if s[0] == s[-1]:
        ans += a[0] / 2 + a[-1] / 2 + (a[0] + a[-1]) / 2 * (k - 1)
        a = a[1:-1]
    ans += sum(x / 2 for x in a) * k
    print ans

Submission status: Accepted

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

Submission status: Accepted

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

pre_s = ""
ls = []
temp = 1
for s in S:
    if s == pre_s:
        temp += 1
    else:
        ls.append((pre_s,temp))
        temp = 1
    pre_s = s
ls.append((pre_s,temp))

if len(ls)==2:
    s,l = ls[1]
    print((l*K)//2)
    exit()
ans = 0
if S[0] != S[-1]:
    for s,l in ls[1:]:
        ans += l//2
    ans *= K
else:
    for s,l in ls[2:-1]:
        ans += l//2
    ans *= K
    s0,l0 = ls[1]
    s1,l1 = ls[-1]
    ans += l0//2
    ans += l1//2
    ans += (K-1)*((l0+l1)//2)
print(ans)

Submission status: Accepted

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

L = len(S)

a = 1
B = []
for i in range(L-1):
    if S[i] == S[i+1]:
        a += 1
    else:
        B.append(a)
        a = 1
B.append(a)

ans = 0
if S[0] != S[-1]:
    for b in B:
        ans += b // 2
    ans *= K
elif len(B) == 1:
    ans = L*K//2
else:
    for i in range(1, len(B)-1):
        ans += B[i]//2
    ans *= K
    ans += (B[0]+B[-1])//2 * (K-1)
    ans += B[0] // 2
    ans += B[-1] // 2

print(ans)

Submission status: Accepted

Solution code:
s = input()
k = int(input())
prev1 = ""
prev2 = s[0]
a = 0
b = 0
flag = False
flag2 = False
for i, c in enumerate(s):
    if c == prev1:
        a += 1
        prev1 = ""
    else:
        prev1 = c
        if i == len(s)-1 and c == s[0]:
            flag = True
    if c == prev2:
        b += 1
        prev2 = ""
    else:
        prev2 = c
        if i == len(s)-1 and c == s[0]:
            flag2 = True

ans = 0
if flag:
    if flag2:
        ans = a + b*(k-1)
    else:
        ans = a*((k+1)//2) + b*(k-(k+1)//2)
else:
    ans = k*a
print(ans)

Submission status: Accepted

Solution code:
def main():
    S = input()
    K = int(input())
    seq = []
    prev = S[0]
    cnt = 1
    for c in S[1:]:
        if c == prev:
            cnt += 1
        else:
            seq.append(cnt)
            cnt = 1
            prev = c
    seq.append(cnt)
    ans = 0
    for s in seq[1:len(seq)-1]:
        ans += s//2 * K
    if len(seq) == 1:
        ans = seq[0] * K // 2
    elif S[0] == S[-1]:
        ans += (seq[0] + seq[-1])//2 * (K-1)
        ans += seq[0]//2 + seq[-1]//2
    else:
        ans += seq[0]//2 * K
        ans += seq[-1]//2 * K
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

counter = [1]
before = s[0]
for i in range(1, len(s)):
    if s[i] == before:
        counter[-1] += 1
    else:
        counter.append(1)
    before = s[i]


if s[0] == s[-1] and len(counter) >= 2:
    c_tmp = counter[0] + counter[-1]
    btw_sum = c_tmp // 2
    tot = 0
    for i in range(len(counter)):
        if i != 0 and i != len(counter) - 1:
            tot += (counter[i] // 2) * k
        else:
            tot += counter[i] // 2
    tot += btw_sum * (k - 1)

elif s[0] == s[-1] and len(counter) == 1:
    tot = (len(s) * k) // 2
else:
    tot = 0
    for i in range(len(counter)):
        tot += (counter[i] // 2) * k

print(tot)

Submission status: Accepted

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

ans1 = 0
prev = '#'
for i in range(len(S)):
    if S[i] == prev:
        ans1 += 1
        prev = '#'
    else:
        prev = S[i]

if prev != S[0]:
    print(ans1 * K)
    exit()

ans2 = 0
for i in range(len(S)):
    if S[i] == prev:
        ans2 += 1
        prev = '#'
    else:
        prev = S[i]

if prev == S[0]:
    print(ans1 + ans2 * (K-1))
else:
    print(ans1 * (K - K // 2) + ans2 * (K // 2))

Submission status: Accepted

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

L = []
c = 0
for i in range(len(S)+1):
  if i==len(S):
    L += [c]
    continue
  if i==0:
    c = 1
    continue
  if S[i]==S[i-1]:
    c += 1
  else:
    L += [c]
    c = 1

if len(L)==1:
  print len(S)*K/2
elif S[0]==S[-1]:
  print L[0]/2+L[-1]/2+sum(l/2 for l in L[1:-1])*K+(L[0]+L[-1])/2*(K-1)
else:
  print sum(l/2 for l in L)*K

Submission status: Accepted

Solution code:
import sys
from fractions import gcd
from itertools import groupby as gb
from itertools import permutations as perm
from itertools import combinations as comb
from collections import Counter as C
from collections import defaultdict as dd
sys.setrecursionlimit(10**5)
def y(f):
    if f:
        print('Yes')
    else:
        print('No')
def Y(f):
    if f:
        print('YES')
    else:
        print('NO')

s = input()
n = int(input())

if s[0] != s[-1]:
    g = gb(list(s))
    res = 0
    for k, v in g:
        a = len(list(v))
        res += a // 2
    print(res * n)
else:
    l = []
    g = gb(list(s))
    for k, v in g:
        a = len(list(v))
        l.append(a)
    if len(l) == 1:
        print((l[0] * n) // 2)
    else:
        rst = 0
        for i in range(1, len(l) - 1):
            rst += (l[i]) // 2
        sm = (l[0] + l[-1]) // 2
        pp = (l[0] // 2) + (l[-1] // 2)
        print(rst * n + sm * (n - 1) + pp)

Submission status: Accepted

Solution code:
s=input()
k=int(input())
if len(set(list(s)))==1:print((len(s)*k)//2);exit()
t=[1]
for i in range(1,len(s)):
  if s[i-1]==s[i]:t[-1]+=1
  else:t.append(1)
a=0
for i in t:a+=(i//2)*k
if s[0]==s[-1]:
  if t[0]%2==t[-1]%2==1:a+=k-1
print(a)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3

#import
#import math
#import numpy as np
#= int(input())
S = input()
K = int(input())
ls = len(S)
t1 = []

t = 1
for i in range(1, ls):
    if S[i - 1] == S[i]:
        t += 1
    else:
        if t > 1:
            t1.append(t)
        t = 1

if t > 1:
    t1.append(t)

ans = 0
for t in t1:
    ans += t // 2

if len(t1) == 1:
    if t1[0] == ls:
        print(ls * K // 2)

else:
    if S[0] != S[-1]:
        print(ans * K)
    
    else:
        ta = t1[0]
        tb = t1[-1]
        print(ans * K - (ta // 2 + tb // 2 - (ta + tb) // 2) * (K - 1))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3

#import
#import math
#import numpy as np
#= int(input())
S = input()
K = int(input())
ls = len(S)
t1 = []

t = 1
for i in range(1, ls):
    if S[i - 1] == S[i]:
        t += 1
    else:
        if t > 1:
            t1.append(t)
        t = 1

if t > 1:
    t1.append(t)

ans = 0
for t in t1:
    ans += t // 2

if len(t1) == 1:
    if t1[0] == ls:
        print(ls * K // 2)

else:
    if S[0] != S[1]:
        print(ans * K)
    
    else:
        ta = t1[0]
        tb = t1[-1]
        print(ans * K - (ta // 2 + tb // 2 - (ta + tb) // 2) * (K - 1))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3

#import
#import math
#import numpy as np
#= int(input())
S = input()
K = int(input())

t1 = []

t = 1
for i in range(1, len(S)):
    if S[i - 1] == S[i]:
        t += 1
    else:
        if t > 1:
            t1.append(t)
        t = 1

if t > 1:
    t1.append(t)

ans1 = 0
for t in t1:
    ans1 += t // 2

if K == 1:
    print(ans1)
    exit()

if len(t1) == 1:
    if t1[0] == len(S):
        print(t1[0] * K // 2)
        exit()

ans2 = 0
if S[0] == S[-1]:
    tt = t1.pop(0)
    ttt = t1.pop(len(t1) - 1)
    t = tt + ttt
    t1.append(t)
    for t in t1:
        ans2 += t // 2

else:
    ans2 = ans1 * (K - 1)

print(ans1 + ans2)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3

#import
#import math
#import numpy as np
#= int(input())
S = input()
K = int(input())

t1 = []

t = 1
for i in range(1, len(S)):
    if S[i - 1] == S[i]:
        t += 1
    else:
        if t > 1:
            t1.append(t)
        t = 1

if t > 1:
    t1.append(t)

ans1 = 0
for t in t1:
    ans1 += t // 2

if K == 1:
    print(ans1)
    exit()

if len(t1) == 1:
    if t1[0] == len(S):
        print(t1[0] * K // 2)
        exit()

ans2 = 0
if S[0] == S[-1]:
    t = t1.pop(0) + t1.pop(len(t1) - 1)
    t1.append(t)
    for t in t1:
        ans2 += t // 2

else:
    ans2 = ans1 * (K - 1)

print(ans1 + ans2)

Submission status: Runtime Error

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

    count_l = []
    i = 0
    counter = 1
    while True:

        if s[i] == s[i+1]:
            counter += 1
        else:
            count_l.append(counter)
            counter = 1
        i += 1
        if i == len(s)-1:
            if counter == 1:
                break
            else:
                count_l.append(counter)
                break

    answer = 0
    if len(count_l) >= 3 and s[0] == s[:-1]:
        sub_l = count_l[0]+count_l[-1] + count_l[1:-2]
        if k >= 2:
            for i in range(len(sub_l)):
                answer += (sub_l[i] // 2) * (k - 1)
            for j in range(len(count_l[:-2])):
                answer += count_l[j] // 2
            answer += count_l[:-1] // 2
    elif len(count_l) == 1:
        answer += (count_l[0] * k) // 2
    else:
        for i in range(len(count_l)):
            answer += (count_l[i] // 2) * k

    print(answer)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
s=input()
k=int(input())
data=[0]
res=1
for i in range(1,len(s)):
  if s[i]==s[i-1] and data[i-1]==0:
    data.append(1)
  else:
    data.append(0)
if s[0]==s[-1] and data[-1]==0:
  res=2
  data.append(1)
  for i in range(1,len(s)):
    if s[i]==s[i-1] and data[i-1]==0:
      data.append(1)
    else:
      data.append(0)
if k%2==0:
  print(sum(data)*k//res)
else:
  if res==1:
    print(sum(data)*k)
  else:
    print(sum(data)*k//2+data[:len(s)])

Submission status: Runtime Error

Solution code:
# A - Connection and Disconnection
S = input()
K = int(input())
S = S + '1'# 番兵

# 連続する文字数をカウント
cnt = []
conti = 1
for i in range(1,len(S)):
    if S[i-1] == S[i]:
        conti += 1
    else:
        cnt.append(conti)
        conti = 1

# 最初と最後の文字が同じとき
if S[0] == S[-2]:
    # 奇数個 + 奇数個のとき
    if cnt[0]%2 == 1 and cnt[-1]%2 == 1:
        comp = K - 1
        
ans = [a//2 for a in cnt if a >= 2]
ans = sum(ans)*K + comp
print(ans)

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())
ans = 0
soui = 0
moji = s[0]

for i in range(1,len(s)):
    if s[i] == s[i-1] and soui == 0:
        ans += 1
        soui = 1
    elif soui == 1:
        soui = 0

flag = 0

if s[0] == s[len(s)-1] and soui == 0:
    ans += 1
    flag = 1

for i in range(len(s)):
    if moji != s[i]:
        break
    elif i == len(s)-1:
        flag = 2

if flag == 1:
    print((k*ans)-((k-1)*((a//2)+(b//2)-((a+b)//2))))
elif flag == 2:
    print((k*len(s))//2)
else:
    print(k*ans)

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())
ans = 0
soui = 0
moji = s[1]

for i in range(1,len(s)):
    if s[i] == s[i-1] and soui == 0:
        ans += 1
        soui = 1
    elif soui == 1:
        soui = 0

flag = 0

if s[0] == s[len(s)-1] and soui == 0:
    ans += 1
    flag = 1

for i in range(len(s)):
    if moji != s[i]:
        break
    elif i == len(s)-1:
        flag = 2

if flag == 1:
    print((k*ans)-1)
elif flag == 2:
    print((k*len(s))//2)
else:
    print(k*ans)

Submission status: Runtime Error

Solution code:
    import math
    S = list(input())
    K = int(input())
    s = S * 2
    c1 = 0
    c2 = 0
    for i in range(len(s)-1):
        if s[i+1] == s[i]:
            if i < len(S)-1:
                c1 += 1
            c2 += 1
            s[i+1] = 0
    c = S[0]
    for s in S:
        if s != c:
            print(c1+(c2-c1)*(K-1))
            exit()
    print(math.floor(len(S)*K/2))

import math
S = list(input())
K = int(input())
s = S * 2
c1 = 0
c2 = 0
for i in range(len(s)-1):
    if s[i+1] == s[i]:
        if i < len(S)-1:
            c1 += 1
        c2 += 1
        s[i+1] = 0
c = S[0]
for s in S:
    if s != c:
        print(c1+(c2-c1)*(K-1))
        exit()
print(math.floor(len(S)*K/2))

Submission status: Runtime Error

Solution code:


def main():
    s = list(input())
    k = int(input())
    lst = []
    cnt = 1
    flg = 0
    ans = 0
    prev = s[0]
    for i in range(1, len(s)):
        if prev == s[i]:
            cnt += 1
        else:
            lst.append(cnt)
            cnt = 1
            prev = s[i]
            flg = 1
    lst.append(cnt)
    if len(lst) == 1:
        ans = s * k // 2
    else:
        # ans += sum(list(map(lambda x: x // 2, lst[1:len(lst)-1]))) * k
        for l in lst[1: len(lst) - 1]:
            ans += l // 2 * k
        if s[-1] == s[0]:
            ans += (lst[0] + lst[-1]) // 2 * (k - 1)
            ans += lst[0] // 2 + lst[-1] // 2
        else:
            ans += (lst[0] // 2 + lst[-1] // 2) * k
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import math
from collections import deque


def main():
    s = list(input())
    k = int(input())
    lst = []
    cnt = 1
    flg = 0
    ans = 0
    prev = s[0]
    for i in range(1, len(s)):
        if prev == s[i]:
            cnt += 1
        else:
            lst.append(cnt)
            cnt = 1
            prev = s[i]
            flg = 1
    lst.append(cnt)
    if len(lst) == 1:
        ans = s * k // 2
    else:
        # ans += sum(list(map(lambda x: x // 2, lst[1:len(lst)-1]))) * k
        for l in lst[1: len(lst) - 1]:
            ans += l // 2 * k
        if s[-1] == s[0]:
            ans += (lst[0] + lst[-1]) // 2 * (k - 1)
            ans += lst[0] // 2 + lst[-1] // 2
        else:
            ans += (lst[0] // 2 + lst[-1] // 2) * k
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import math
from collections import deque


def main():
    s = list(input())
    k = int(input())
    lst = []
    cnt = 1
    flg = 0
    ans = 0
    prev = s[0]
    for i in range(1, len(s)):
        if prev == s[i]:
            cnt += 1
        else:
            lst.append(cnt)
            cnt = 1
            prev = s[i]
            flg = 1
    lst.append(cnt)
    if flg == 0:
        ans = s * k // 2
    else:
        ans += sum(list(map(lambda x: x // 2, lst[1:len(lst)-1])))*k
        if s[-1] == s[0]:
            ans += (lst[0] + lst[-1]) // 2 * (k - 1)
            ans += lst[0] // 2 + lst[-1] // 2
        else:
            ans += (lst[0] // 2 + lst[-1] // 2) * k
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import math
from collections import deque


def main():
    s = list(input())
    k = int(input())
    lst = []
    cnt = 1
    flg = 0
    ans = 0
    prev = s[0]
    for i in range(1, len(s)):
        if prev == s[i]:
            cnt += 1
        else:
            lst.append(cnt)
            cnt = 1
            prev = s[i]
            flg = 1
    lst.append(cnt)
    if flg == 0:
        print(s * k // 2)
        return
    ans += sum(list(map(lambda x: x // 2, lst[1:len(lst)-1])))*k
    if s[-1] == s[0]:
        ans += (lst[0] + lst[-1]) // 2 * (k - 1)
        ans += lst[0] // 2 + lst[-1] // 2
    else:
        ans += (lst[0] // 2 + lst[-1] // 2) * k
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import math
from collections import deque


def main():
    s = list(input())
    k = int(input())
    lst = []
    cnt = 1
    flg = 0
    ans = 0
    prev = s[0]
    for i in range(1, len(s)):
        if prev == s[i]:
            cnt += 1
        else:
            lst.append(cnt)
            cnt = 1
            prev = s[i]
            flg = 1
    if flg == 0:
        print(s * k // 2)
        return
    ans += sum(map(lambda x: x // 2, lst[1:len(lst)-1]))*k
    if s[-1] == s[0]:
        ans += (lst[0] + lst[-1]) // 2 * (k - 1)
        ans += lst[0] // 2 + lst[-1] // 2
    else:
        ans += (lst[0] // 2 + lst[-1] // 2) * k
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import math
from collections import deque


def main():
    s = list(input())
    k = int(input())
    lst = []
    cnt = 1
    flg = 0
    ans = 0

    for i in range(len(s)-1):
        if s[i + 1] == s[i]:
            cnt += 1
        else:
            lst.append(cnt)
            cnt = 1
            flg = 1
    if flg == 0:
        print(s * k // 2)
        return
    ans += sum(map(lambda x: x // 2, lst[1:len(lst)-1]))*k
    if s[-1] == s[0]:
        ans += (lst[0] + lst[-1]) // 2 * (k - 1)
        ans += lst[0] // 2 + lst[-1] // 2
    else:
        ans += (lst[0] // 2 + lst[-1] // 2) * k
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
def solver(s, k):
    res = 0
    
    for i in range(1, len(s)):
        if s[i] == s[i - 1]:
            s[i] = 'A'
            res += k
    
    if s[0] == s[-1]:
        res += k - 1
        if s[0] == s[1]:
            res -= 1
    
    return res

# def solver2(s, k):
    t = []
    for _ in range(k):
        t += s
    res = 0
    for i in range(1, len(t)):
        if t[i] == t[i - 1]:
            t[i] = 'A'
            res += 1
    return res, t

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

# ss = s[:]
# kk = k
print(solver(s, k))
# print(solver2(ss, kk))

Submission status: Runtime Error

Solution code:
def solver(s, k):
    res = 0
    
    for i in range(1, len(s)):
        if s[i] == s[i - 1]:
            s[i] = 'A'
            res += k
    
    if s[0] == s[-1]:
        res += k - 1
        if s[0] == s[1]:
            res -= 1
    
    return res

def solver2(s, k):
    t = []
    for _ in range(k):
        t += s
    res = 0
    for i in range(1, len(t)):
        if t[i] == t[i - 1]:
            t[i] = 'A'
            res += 1
    return res, t

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

ss = s[:]
kk = k
print(solver(s, k))
# print(solver2(ss, kk))

Submission status: Runtime Error

Solution code:
s = list(input())
k = int(input())
ans = 0
for i in range(1, len(s)):
    if s[i] == s[i -1]:
        ans += 1
        s[i] = ""
j = 0
if s[-1] == s[0]:
    if s[-1] != s[-2]:
        j = 1

print(ans*k - j*(k - 1))

Submission status: Runtime Error

Solution code:
import sys

# A - Connection and Disconnection
import string

S = input()
K = int(input())
T = S + S[0]

lower = string.ascii_lowercase
ans = 0
is_specila_case = False

for c in lower:
	key = c * 2
	ans += T.count(key)

	if T.endswith(key):
		is_specila_case = True

ans *= K

if is_specila_case:
	print(ans - 1)
else:
	print(ans)import sys

# A - Connection and Disconnection
import string

S = input()
K = int(input())
T = S + S[0]

lower = string.ascii_lowercase
ans = 0
is_specila_case = False

for c in lower:
	key = c * 2
	ans += T.count(key)

	if T.endswith(key):
		is_specila_case = True

ans *= K

if is_specila_case:
	print(ans - 1)
else:
	print(ans)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;

#define lint long long
#define all(it) it.begin(), it.end()
#define FOR(i, n) for(int i = 0; i < n; i++)
#define FORU(i, a, b, s) for(int i = a; i < b; i += s)
#define FORD(i, a, b, s) for(int i = a; i > b; i -= s)
#define endl "\n"
lint INF = 2e18;
lint MOD = 1e9 + 7;

lint i, j, k, t;

int main() {
    string S;
    lint K;

    cin >> S >> K;

    S += S[0];

    lint cnt = 0;
    regex re(R"(([a-z])\1{0,})");

    for(sregex_iterator it(all(S), re), end; it != end; ++it) {
        auto &&m = *it;
        cnt += m.length() / 2;
    }

    cout << cnt * K << endl;
}

Submission status: Runtime Error

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

S = list(input())
K = int(input())
leng = len(S)
if len(set(S))==1:
    print(S*K//2)
    exit()
if S[0]!=S[-1] or K==1:
    last = S[0]
    cnt = 0
    for s in S[1:]:
        if s==last:
            cnt += 1
            last = '.'
        else:
            last = s
    print(cnt*K)
else:
    S += ['.']
    cnt = 1
    lis = []
    last = S[0]
    for s in S[1:]:
        if s==last:
            cnt += 1
        else:
            lis.append(cnt)
            last = s
            cnt = 1
    first = lis.pop(0)
    end = lis.pop()
    others = sum([i//2 for i in lis])
    ans = first//2 + others*K + (first+end)//2*(K-1)+end//2
    print(ans)

Submission status: Runtime Error

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

sw1 = 0
for i in range(26):
    if s.count(chr(ord("a")+i)) == len(s):
        sw1 = 1
        break

sw2 = 0
if s[0] == s[-1]:
    sw2 = 1
    
d = 0
for i in range(1,n):
    if s[0] == s[i]:
        d += 1
    else:
        break
            
e = 0
for i in range(1,n):
    if s[-1] == s[-i-i]:
        e += 1
    else:
        break        

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

if sw1 == 1:
    print(len(s)*k//2)
else:
    if sw2 == 0:
        print(ans*k)
    else:
        print(ans*k - d//2 - e//2 + (d+e)//2)

Submission status: Runtime Error

Solution code:
S = list(input())
N = len(S)
K = int(input())
if N == 1:
    print(K//2)
    exit()

cnt = 0
tmpS = S[:]
for i in range(N-1):
    if tmpS[i] == tmpS[i+1]:
        cnt += 1
        tmpS[i+1] = '!'

if S[N-1] == S[0]:
    a = 1
    while S[a-1] == S[a] and a <= N-1:
        a += 1
    b = 1
    while S[N-b] == S[N-b-1] and b <= N-1:
        b += 1
    print(cnt*K+a//2+b//2 - (a+b)//2 * (K-1))
else:
    print(cnt*K)

Submission status: Runtime Error

Solution code:
S = list(input())
flag = False
if S[0] == S[1]:
    flag = True
N = len(S)
K = int(input())
cnt = 0

for i in range(N-1):
    if S[i] == S[i+1]:
        cnt += 1
        S[i+1] = '!'

if S[N-1] == S[0]:
    if flag:
        print(cnt*K)
    else:
        print(cnt*K+(K-1))
else:
    print(cnt*K)

Submission status: Runtime Error

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

N = len(S)
p = S[0]
c = 0
for i in range(1,N):
    if S[i] == p:
        c += 1
        p = ''
    else:
        p = S[i]

ans = c*K
if S[0] == S[-1] and S[-2] != S[-1]:
    ans += K-1

print(ans)

Submission status: Runtime Error

Solution code:
import sys
import collections
import bisect


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

    if sk == 1:
        print(0)
        return

    s = sk + sk[0:1]
    changeCnt = 0
    cnt = 1
    while cnt < len(sk)+1:
        if s[cnt] != s[cnt - 1]:
            cnt += 1
        else:
            if s[cnt + 1] != s[cnt]:
                changeCnt += 1
                s[cnt] = "aaa" + str(cnt)
                cnt += 1
            else:
                changeCnt += 1
                s[cnt] = "aaa" + str(cnt)
                cnt += 2
    print(changeCnt * k)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
import collections
import bisect


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

    if s == 1:
        print(0)
        return

    s = sk + sk[0:1]
    changeCnt = 0
    cnt = 1
    while cnt < len(sk):
        if s[cnt] != s[cnt - 1]:
            cnt += 1
        else:
            if s[cnt + 1] != s[cnt]:
                changeCnt += 1
                s[cnt] = "aaa" + str(cnt)
                cnt += 1
            else:
                changeCnt += 1
                s[cnt] = "aaa" + str(cnt)
                cnt += 2
    print(changeCnt * k)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
import collections
import bisect


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

    if s == 1:
        print(0)
        return

    s = s + s[0:3]
    changeCnt = 0
    cnt = 1
    while cnt < len(s):
        if s[cnt] != s[cnt - 1]:
            cnt += 1
        else:
            if s[cnt + 1] != s[cnt]:
                changeCnt += 1
                s[cnt] = "aaa" + str(cnt)
                cnt += 1
            else:
                changeCnt += 1
                s[cnt] = "aaa" + str(cnt)
                cnt += 2
    print(changeCnt * k)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
import collections
import bisect


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

    if s == 1:
        print(0)
        return

    s = s + s[0:1]
    changeCnt = 0
    cnt = 1
    while cnt < len(s):
        if s[cnt] != s[cnt - 1]:
            cnt += 1
        else:
            if s[cnt + 1] != s[cnt]:
                changeCnt += 1
                s[cnt] = "aaa" + str(cnt)
                cnt += 1
            else:
                changeCnt += 1
                s[cnt] = "aaa" + str(cnt)
                cnt += 2
    print(changeCnt * k)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
s=list(input())
k=int(input())
b=""
c=0 
for i in range(len(s)):
  if b== s[i]:
    s[i]="0"
    c+=1
  b=s[i]

if s[0]==s[-1] and s[1]=="0":s[0]=="1"
d=k-1 if s[0]==s[-1] else 0
print(c*k+d)

Submission status: Runtime Error

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

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

b = [1]
for i in S[:-1]:
    if S[i] == S[i+1]:
        b[-1] += 1
    else:
        b.append(1)

ans = 0
for i in b:
    ans += i//2
ans *= K

if S[0] == S[-1] and b[0]%2 == b[-1]%2 == 1:
    ans += K - 1
print(ans)

Submission status: Runtime Error

Solution code:
from itertools import groupby
import sys
input = sys.stdin.readline


def main():
    S = input().strip()
    K = int(input())
    grouped = [len(tuple(s)) for _, s in groupby(S)]

    if len(grouped) > 1 and S[0] == S[-1]:
        _sum = 0
        top = grouped[0]
        tail = grouped[-1]
        for group in grouped[1:-1]:
            _sum += group[1] // 2
        dif = (top // 2) + (tail // 2) + (top + tail) * (K - 1) // 2
        print(_sum * K + dif)
    else:
        _sum = 0
        for group in grouped:
            _sum += group[1] // 2
        print(_sum * K)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import re

# thanks to https://qiita.com/norioc/items/324d9210ae2db29d08e6
def nchars(s, n):
    assert n > 0
    reg = re.compile("(.)\\1{%d,}" % (n - 1))
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]

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

connection = list(nchars(S, 2))
count = sum([len(c) // 2 for c in connection]) * K

if S[0] == S[-1]:
    l1 = len(connection[0])
    l2 = len(connection[-1])
    count = count + ( -l1 // 2 - l2 // 2 + (l1 + l2) // 2) * (K - 1)

print(count)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  string s;
  ll k;
  cin >> s >> k;
  ll n=s.size();
  if(n==1){
    cout << k-1 << endl;
  }
  
  s.push_back(s[0]);
  //cout << s << endl;
  vector<int> dp(n+1,0);
  for(int i=1;i<n+1;i++){
    if(s[i] == s[i-1]){
      dp[i] = dp[i-1]+1;
      s[i] = (s[i-1]+1-'a')%26 + 'a';
    }else{
      dp[i] = dp[i-1];
    }
  }
  
  ll ans = dp[n-1]*k;
  //cout << dp[n-1] << endl;
  cout << ans << endl;
  
  return(0);
}

Submission status: Runtime Error

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

i=1
count=0
N=len(S)
while i<N:
    if S[i]==S[i-1]:
        count+=1
        if i==N-1:
            S+='A'
        i+=1
    i+=1

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