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 = list(input().rstrip()) # len(S) <= 100
K = int(input())    # K <= 10**9

N = len(S)
cnt = 0
for i in range(N-1):
    if S[i] == S[i+1]:
        print(S[i], S[i+1])
        S[i+1] = "_"
        cnt += 1

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

Submission status: Wrong Answer

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

cnt=0
l=len(s)
i=0
flag=1
while(i<l-1):
    if s[i]==s[i+1]:
        if i+1==l-1:
            flag=0
        cnt+=1
        i+=2
    else:
        i+=1

cnt*=k

if s[0]==s[-1]:
    if flag==1:
        cnt+=k-1
        
print(cnt)

Submission status: Wrong Answer

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    S=ST()
    K=I()
    T=S*2
    before = ""
    RLE = []
    for t in T:
        if before != t:
            RLE.append(1)
            before = t
        else:
            RLE[-1]+=1

    ans = 0    
    for x in RLE:
        ans+=x//2

    ans*=K
    ans//=2

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
cnt = 0
streak = 1
for i in range(len(s)-1):
    if s[i + 1] == s[i]:
        streak += 1
    else:
        if streak > 1:
            cnt += streak // 2
        streak = 1
if s[-1] == s[0]:
    cnt += (streak + 1) // 2
else:
    if streak > 1:
        cnt += streak // 2

print(cnt*k)

Submission status: Wrong Answer

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

i = 0
n = len(S)
count = 0
while i < n - 1:
    if S[i] == S[i + 1]: #次のやつと連続の時
        count += 1
        i += 2
    else:
        i += 1

tmp = count
if K == 1:
    print (count)
    exit()

while i < 2 * n - 1:
    if S[i - n] == S[i + 1 - n]: #次のやつと連続の時
        count += 1
        i += 2
    else:
        i += 1

if K % 2 == 0:
    print (count * (K // 2))
else:
    print (count * (K // 2) + tmp)

Submission status: Wrong Answer

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

if len(S) == 1:
    print(K//2)
else:
    c = 0
    flg = False
    for i in range(len(S)-1):
        if S[i] == S[i+1] and flg is False:
            c += 1
            flg = True
        else:
            flg = False

    if flg is False and S[-1] == S[0]:
        j = 1
    else:
        j = 0

    print(c*K+j*(K-1))
# print(c, j)

Submission status: Wrong Answer

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
	else:
		y=k-1
print(x*k+y)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
if k!=1:
  S=s+s[0]
  i=0
  cnt=0
  while i < len(S)-1:
    if S[i]==S[i+1]:
      cnt+=1
      i+=2
    else:i+=1
  print(cnt*k)
else:
  i=0
  cnt=0
  while i < len(s)-1:
    if s[i]==s[i+1]:
      cnt+=1
      i+=2
    else:i+=1

Submission status: Wrong Answer

Solution code:
k=list(input())
n=int(input())
p=0
for i in range(len(k)):
    if i==0:
        continue
    a,b=k[i-1],k[i]
    if a==b:
        k[i]="0"
if k[-1]==k[0]:
    p=1
print(k.count("0")*n+p*(n-1))

Submission status: Wrong Answer

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

if len(S) == 1:
    print('0')
else:
    c = 0
    flg = False
    for i in range(len(S)-1):
        if S[i] == S[i+1] and flg is False:
            c += 1
            flg = True
        else:
            flg = False

    if flg is False and S[-1] == S[0]:
        j = 1
    else:
        j = 0

    print(c*K+j*(K-1))
# print(c, j)

Submission status: Wrong Answer

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

print(count + (n-1) * count2)

Submission status: Wrong Answer

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: Wrong Answer

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

S = S + S
i = 0
count = 0
while i < len(S)-1:
  if S[i] == S[i+1]:
    count += 1
    i += 2
    continue
  i += 1
one_time = count // 2
two_time = count // 2 + count % 2

print(one_time + two_time * (K - 1))

Submission status: Wrong Answer

Solution code:
def rle(s):
    n = len(s)
    if n == 0:
        return []
    res = []
    prev = s[0]
    count = 1
    index = 0
    while index < n:
        index += 1
        if index < n:
            if s[index] == prev:
                count += 1
                continue
        res.append([prev, count])
        if index < n:
            prev = s[index]
            count = 1
    return res


s = input().rstrip()
k = int(input())
counts = rle(s)
res = 0
for c, v in counts:
    res += v // 2 * k
if counts[0][0] == counts[-1][0]:
    if counts[0][1] % 2 == 1 and counts[-1][1] % 2 == 1:
        res += k - 1
print(res)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

a = list(input())
n = int(input())
ans = 0
if n == 1:
    kkk = s(a)
    for i in range(len(kkk)):
        if kkk[i][1] >= 2:
            ans += kkk[i][1]//2
    print(ans)
else:
    aa = a+a
    kkk = s(aa)
    for i in range(len(kkk)):
        if kkk[i][1] >= 2:
            ans += kkk[i][1]//2
    print(ans//2*n)

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
if len(s)==1:print(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: Wrong Answer

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

ans = 0

# 最後の文字以外をカウント
for si in range(1, len(S) - 1):
    if S[si] == S[si - 1]:
        S = S[: si] + "?" + S[si + 1:]
        ans += 1

# 最後の文字についてカウント
if S[-1] == S[0]:
    ans += 1

print(ans * K)

Submission status: Wrong Answer

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

cnt = 1
ans = 0
for i in range(1, len(S)):
  if S[i] == S[i - 1]:
    cnt += 1
  else:
    ans += cnt // 2
    cnt = 1


ans += cnt // 2
ans *= K
if S[0] == S[-1] and cnt % 2 == 1:
  ans += K - 1

print(ans)

Submission status: Wrong Answer

Solution code:
s=list(input())
k=int(input())
if len(s)==1:
    print(k//2)
    exit()
ans=0
f=1
for i in range(len(s)-1):
    if s[i]==s[i+1] and f:
        ans+=1
        f=0
    else:
        f=1
print(ans*k)

Submission status: Wrong Answer

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

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

print(cnt * k)

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
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: Wrong Answer

Solution code:
s=list(input())
k=int(input())
ans=0
f=1
for i in range(len(s)-1):
    if s[i]==s[i+1] and f:
        ans+=1
        f=0
    else:
        f=1
print(ans*k)

Submission status: Time Limit Exceeded

Solution code:
s=input()
k=int(input())
st=""
sum=0
for i in range(k):
    st=st+s
for x in range(len(st)-1):
    if(st[x]==st[x+1]):
        st=st[:x+1]+"#"+st[x+2:]
        sum+=1
print(sum)

Submission status: Time Limit Exceeded

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

if len(s) == 1:
    print(math.floor(k/2))
    exit()
if len(s) == 2:
    if s[0] == s[1]:
        print(k)
        exit()
    else:
        print(0)
        exit()

ans=0
last=s[0]
length=1
change=0
for i in range(1,len(s)):
    if s[i] == last:
        length+=1
    else:
        change += math.floor(length/2)
        length=1
    last=s[i]

change += math.floor(length/2)

ans=change*k
if s[0] != s[-1]:
    print(ans)
    exit()


if s[0] != s[1] and s[-1] != s[-2]:
    print(ans+k-1)
    exit()
for i in range(10**10):
    3*4

print(ans)

Submission status: Time Limit Exceeded

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

if len(s) == 1:
    print(math.floor(k/2))
    exit()
if len(s) == 2:
    if s[0] == s[1]:
        print(k)
        exit()
    else:
        print(0)
        exit()

ans=0
last=s[0]
length=1
change=0
for i in range(1,len(s)):
    if s[i] == last:
        length+=1
    else:
        change += math.floor(length/2)
        length=1
    last=s[i]

change += math.floor(length/2)

ans=change*k
if s[0] != s[-1]:
    print(ans)
    exit()

for i in range(10**10):
    3*4

if s[0] != s[1] and s[-1] != s[-2]:
    print(ans+k-1)
    exit()

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#AGC-041
S = input()
K = int(input())
ans = 0

# check_char = S[0]
check_char = ""
repeat = 1
first_loop = True

for j in range(K):
    for i in range(0, len(S)):
        if check_char == "":
            check_char = S[0]
        elif S[i] == check_char:
            repeat += 1
        else:
            ans += (repeat//2)
            check_char = S[i]
            repeat = 1

ans += (repeat//2)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S=input()
K=int(input())
flag=True
co=0
a=[]
while flag==True:
    a.append(S)
    co+=1
    if co==K:
        flag=False
S=list("".join(a))
t=[S[0]]
for i in range(1,len(S)):
    if S[i]!=t[-1]:
        t.append(S[i])
    else:
        t.append(":")
print(t.count(":"))

Submission status: Time Limit Exceeded

Solution code:
import os

def main():
    Str = ""
    s_list=[]
    s_con=0

    String = input()
    num = int(input())
    
    for i in range(num):
        Str = Str + String
    
    for s in Str:
        s_list.append(s)

    for i in range(len(s_list)-1):
        if s_list[i] == s_list[i+1]:
            s_list[i+1] = None
            s_con = s_con + 1

    print(s_con)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from math import floor

word = input()
num = int(input())
length=len(word)
charlist_size=num*length

string_generator=(word[i%length] for i in range(charlist_size))

char_before= ''
sum=0
count= 0

for index in range(charlist_size):
    char_now=next(string_generator)
    if index == 0 :
        char_before = char_now
        count = 1
        continue
    
    if char_before == char_now :
        count = count + 1
    else:
        char_before = char_now
        sum = sum+ floor(count/2)
        count = 1
    
    if index == charlist_size-1:
        sum = sum+ floor(count/2)
    
print(sum)

Submission status: Time Limit Exceeded

Solution code:
s=input()
k=int(input())
ls=len(s)
t=""
for i in range(k):
    t+=s[0:ls]

lt=0
ex=0
for i in range(ls*k):
    try:
        t[lt+1]
    except IndexError:
        lt+=1
    else:
        if t[lt]==t[lt+1]:
            ex+=1
            try:
                t[lt+2]
            except IndexError:
                lt+=1
            else:
                if t[lt+1]==t[lt+2]:
                    
                    lt+=2
                else:
                    
                    lt+=1
        else:
            lt+=1
print(ex)

Submission status: Time Limit Exceeded

Solution code:
s = input()
k = int(input())
#print (s)
#文字列の結合
r =""
for i in range(k):
    r += s
#文字検索用のマックス回数を求める
m = len(r) -1
#全体ループ用のカウンタと答え領域を初期化
zen = 0
ans = 0
#全体文字のループ
while zen <=  m:
    w = 1
    ko = zen + 1
    ends = 0
    #重複チェック用のループ
    while ko <= m and ends == 0:
        if r[zen] == r[ko]:
            w +=1
            ko +=1
        else:
            ends = 1
    #wが１なら重複なし＝変換なし。1より大きい場合は重複個数/2で変換数を算出してansに加算
    if w > 1:
        ans = ans + w//2
    #重複チェックが終わった分まで全体を進める
    zen = ko
print (ans)

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
K = int(input())
TTL_S = []
for a in range(K):
	TTL_S.extend(S)
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

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

print(num)

Submission status: Time Limit Exceeded

Solution code:
s=input()
l=len(s)
t=list(s)
k=int(input())
count=0
flag=0
if len(t)==1:
    print(k//2)
    flag=1
if flag==0:
    for i in range(len(t)-1):
        if t[i]==t[i+1]:
            t[i+1]='*'
            count+=1
    
    if t[0]!=t[len(t)-1]:
        print(count*k)
    else:
        count=0
        t=list(s)
        prev=s[0]
        for i in range(1,l*k):
            i%=l
            if i==0:t=list(s)
            if t[i]==prev:
                t[i]='*'
                count+=1
            prev=t[i]
        print(count)

Submission status: Time Limit Exceeded

Solution code:
s=input()
l=len(s)
t=list(s)
k=int(input())
count=0
flag=0
if len(t)==1:
    print(k//2)
    flag=1
if flag==0:
    if t[0]!=t[len(t)-1]:
        for i in range(len(t)-1):
            if t[i]==t[i+1]:
                t[i+1]='*'
                count+=1
        print(count*k)
    else:
        prev=s[0]
        for i in range(1,l*k):
            i%=l
            if i==0:t=list(s)
            if t[i]==prev:
                t[i]='*'
                count+=1
            prev=t[i]
        print(count)

Submission status: Time Limit Exceeded

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

lens = len(s)

cnt = 0
ans = 0


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

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
s = input()
k = int(input())
t = []
u = ""
v = []
a = 0

for i in range(k):
    t.append(s)
    
for j in range(len(t)):
    u += str(t[j])

for l in range(len(u)):
    v.append(u[l])
    
v.append("")
v.append("")
    
for m in range(len(v) - 2):
    if v[m] == v[m + 1] and v[m] != v[m - 1]:
        for n in range(len(v) - 1 - m):
            if not v[m] == v[n + m]:
                a += int(n / 2)
                break

print(a)

Submission status: Time Limit Exceeded

Solution code:
def count_changes(s, k, prev=None):
    count = 0
    for _ in range(k):
        for cur in s:
            if cur == prev:
                count += 1
                prev = None
            else:
                prev = cur
    return count, prev


if __name__ == '__main__':
    s = input()
    k = int(input())
    print(count_changes(s, k)[0])

Submission status: Time Limit Exceeded

Solution code:
def at(s,n):
    p=''
    for i in range(n):
        p=p+s
    return p
s=input()
n=int(input())
q=list(at(s,n))

count=0
for j in range(len(q)-1):

        if (q[j]==q[j+1]):
            q[j+1]='0'
            count+=1

print(count)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
s = input()
k = int(input())
t = []
u = ""
v = []
a = 0

for i in range(k):
    t.append(s)
    
for j in range(len(t)):
    u += str(t[j])

for l in range(len(u)):
    v.append(u[l])
    
v.append("")
v.append("")
    
for m in range(len(v) - 2):
    if v[m] == v[m + 1]:
        if v[m] != v[m - 1]:
            for n in range(len(v) - 1 - m):
                if not v[m] == v[n + m]:
                    a += int(n / 2)
                    break

print(a)

Submission status: Time Limit Exceeded

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

cnt,ans=0,0
T=S+S
for i in range(1,len(T)):
  if T[i-1]==T[i]:
    cnt+=1
  else:
    ans+=(cnt+1)//2
    cnt=0
  if i==len(S)-1:
    ans+=(cnt+1)//2
    tmp=ans

if len(S)==1:
  print(k//2)
  exit()
if len(S)==2:
  if S[0]==S[1]:
    print(k)
  else:
    print(0)
  exit()
  
if len(set(T))==1:
  if len(S)%2==0:
    print(len(S)//2*k)
  else:
    out=0
    for i in range(k):
      if i%2==0:
        out+=len(S)//2
      else:
        out+=len(S)//2+1
    print(out)
  exit()
      
print(tmp+(ans-tmp)*(k-1))

Submission status: Time Limit Exceeded

Solution code:
s = input()
k = int(input())
def unit(s):
    """sの両端を外した状態でいくつ変えるか
    """
    count = 0
    prev = s[0]
    for c in s[1:]:
        if c==prev:
            count += 1
            prev = None
        else:
            prev = c
    return count
if len(s)==2:
    if s[0]==s[1]:
        print(k)
    else:
        print(0)
elif k==1:
    print(unit(s))
elif len(s)==1:
    print(k//2)
elif not s:
    print(0)
elif s[0]!=s[-1]:
    print(k * unit(s))
else:
    s0 = unit(s[1:-1])
    s1 = unit(s[1:])
    s2 = unit(s[:-1])
    s3 = unit(s)
    a = [0,0]
    a[0] = s3
    a[1] = s1
    for n in range(2,k+1):
        a[0], a[1] = min(a[1]+s3, a[0]+s2) + 1, min(a[1]+s0, a[0]+s1) + 1
    print(a[0])

Submission status: Time Limit Exceeded

Solution code:
s = input()
k = int(input())
def unit(s):
    """sの両端を外した状態でいくつ変えるか
    """
    count = 0
    prev = s[0]
    for c in s[1:]:
        if c==prev:
            count += 1
            prev = None
        else:
            prev = c
    return count
if len(s)==2:
    if s[0]==s[1]:
        print(k)
    else:
        print(0)
elif k==1:
    print(unit(s))
elif len(s)==1:
    print(k//2)
elif not s:
    print(0)
elif s[0]!=s[-1]:
    print(k * unit(s))
else:
    s0 = unit(s[1:-1])
    s1 = unit(s[1:])
    s2 = unit(s[:-1])
    s3 = unit(s)
    import numpy as np
    a = [0,0]
    b = [0,0]
    a[0] = s3
    a[1] = s1
    for n in range(2,k+1):
        b[0] = min(a[1]+s3, a[0]+s2) + 1
        b[1] = min(a[1]+s0, a[0]+s1) + 1
        a = b
    print(a[0])

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**7)
S = input()
K = int(input())

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

cnt_one = 0
i = 0
while i < lenS-1:
    if S[i] == S[i+1]:
        cnt_one += 1
        i += 2
    else:
        i += 1

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

SS = S*2
lenS = len(SS)

cnt_two = 0
i = 0
while i < lenS-1:
    if SS[i] == SS[i+1]:
        cnt_two += 1
        i += 2
    else:
        i += 1

lenS = len(S)
if len(set(S)) == 1:
    if lenS%2 == 1:
        ans = 0
        for j in range(1, K+1):
            if j%2 == 1:
                ans += cnt_one
            else:
                ans += cnt_two - cnt_one
        print(ans)
        exit()

print((cnt_two - cnt_one)*(K-1) + cnt_one)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
from itertools import accumulate, dropwhile, takewhile, groupby

# 入力 =stdin.readline().rstrip()
# 分割 = stdin.readline().rstrip().split()

'''
条件に合うものを絞り込む
data = [1, 2, 3]
s = [x for x in data if x % 3 == 0]
'''

'''
文字配列を数値に
l_si = ['-10', '0', '100']

l_si_i = [int(s) for s in l_si]
'''

'''
a = [2, 4, 6, 8]
b = [3, 6, 9]

print(list(set(a) | set(b)))  # 和集合
print(list(set(a) & set(b)))  # 積集合
'''

'''
N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]
count = 0
while len([x for x in A if x % 2 == 0]) == N:
    A = list(map(lambda y: y / 2, A))
    count += 1
print(count)
'''

'''
.append(要素)
.insert(場所,要素)
-1で末尾で末尾から一つ前
'''
'''
間に空白追加
' '.join(lis)
'''
T = stdin.readline().rstrip()
num = int(stdin.readline().rstrip())
tes = (num * len(T))
ch = ''
counter = 0
changed = False
lis = []
for x in range(len(T) * num):
    if (ch == T[x % len(T)] and not changed):
        counter += 1
        changed = True
    elif (changed):
        changed = False
    ch = T[x % len(T)]
print(counter)

Submission status: Time Limit Exceeded

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

if len(s) == 1:
  print(k // 2)
else:
  cnt = 0
  p = 1
  while p < len(s):
    if s[p-1] == s[p]:
      cnt += 1
      p += 1
    p += 1
      
  cnt2 = 0
  p2 = 1
  s2 = s + s
  while p2 < len(s2):
    if s2[p2-1] == s2[p2]:
      cnt2 += 1
      p2 += 1
    p2 += 1

  if 2*cnt < cnt2:
    print(k*cnt + (k - 1))
  else:
    print(k*cnt)
    
for i in range(k):
  i += 1

Submission status: Time Limit Exceeded

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

T = ""
for _ in range(K):
    T += S

for ti in range(1, len(T)):
    if T[ti] == T[ti - 1]:
        T = T[: ti] + "?" + T[ti + 1:]
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

if len(s) == 1:
    print(int(K/2))
else:
    S = ""    
    for i in range(0 , K):
        S += s

    count = 0
    bef = ""
    isbefchange = False
    for i in range(0, len(S)):
        if(isbefchange == False and S[i] == bef):
            count += 1
            isbefchange = True
            bef = S[i]
            continue
        bef = S[i]
        isbefchange = False
    print(count)

Submission status: Time Limit Exceeded

Solution code:
S=input()
K=int(input())
T0=0
count=1
ans=0

for i in range(len(S)*K):
	j=i%len(S)
	if S[j]==T0:
		count+=1
	else:
		ans+=count//2
		count=1
	T0=S[j]
	if i==len(S)*K-1:
		ans+=count//2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
K = input()
T = ""
for i in range(int(K)):
    T = T + S

list_str = list(T)

a = len(T)
a = a - 1
n = 0
p = 0
for u in range(a):
  if list_str[n] == list_str[n+1]:
    p = p + 1
    list_str[n+1] = "1"
  n = n + 1
print(p)

Submission status: Time Limit Exceeded

Solution code:
import itertools

S = input()
K = int(input())
totalx = 0
s=''
for i in range(K):
    s = s + S
seq = [len(list(g)) for k, g in itertools.groupby(s)]
for i in range(len(seq)):
    if seq[i]%2==0:
        x = seq[i]
    else:
        x = seq[i]-1
    totalx+=int(x/2)
    
print(totalx)

Submission status: Time Limit Exceeded

Solution code:
s = input()
k = int(input())
ss = ""
for _ in range(k):
    ss += s

ss_list = list(ss)
count = 0
answer = 0
prev = ""
for sss in ss_list:
    if prev == "" or sss == prev:
        count+=1
        if count == 2: 
            answer+=1
            count=0

    else:
        count=1
    prev = sss

print(answer)

Submission status: Time Limit Exceeded

Solution code:
s=input()
k=int(input())
ans=0
f=0
for i in range(k):
	for j in range(len(s)):
		if i==0 and j==0:
			sf=s[j]
			continue
		elif f==1:
			sf=s[j]
			f=0
			continue
		elif sf==s[j]:
			ans+=1
			f=1
		sf=s[j]
		if j==len(s)-1:
			sf=s[0]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
X=input()
a=int(input())
Z=""
count=0
for k in range(a):
    Z=Z+X
z=list(Z)
for i in range(len(z)-1):
    if z[i]==z[i+1]:
        z[i+1]=0
        count=count+1
print(count)

Submission status: Time Limit Exceeded

Solution code:
#import numpy as np
#from numpy import*
#from scipy.sparse.csgraph import shortest_path #shortest_path(csgraph=graph) # dijkstra# floyd_warshall
#from scipy.sparse import csr_matrix
 
from collections import* #defaultdict Counter deque appendleft
from fractions import gcd
from functools import* #reduce
from itertools import* #permutations("AB",repeat=2) combinations("AB",2) product("AB",2) groupby accumulate
from operator import mul,itemgetter
from bisect import* #bisect_left bisect_right
from heapq import* #heapify heappop heappushpop
from math import factorial,pi
from copy import deepcopy
import sys
sys.setrecursionlimit(10**8)
#input=sys.stdin.readline  #危険！基本オフにしろ！
 
def main():
    s=input()
    k=int(input())
    ans=0
    
#    print(x)
    if s[0]!=s[-1]:
        y=[len(list(v))//2 for k,v in groupby(s)]
        print(sum(y)*k)
    else:
        while 1:
            print()
        x=[len(list(v))//2 for k,v in groupby(s)]
        #print(x)
        if len(x)!=1:
            ans+=x[0]+x[-1]
#            print(ans)
            ans+=sum(x[1:-1])*k
#            print(ans)
            ans+=(k-1)*(x[0]+x[-1])
            print(ans)
        else:
            print(len(s)*k//2)
        
    


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

SS = ""
before = ""
cnt = 0
for i in range(K):
    SS += S
for s in SS:
    if before == s:
        cnt+=1
        before = ""
    else:
        before = s
print(cnt)

Submission status: Accepted

Solution code:
def main():
    S = list(input())
    K = int(input())
    cnt = 0
    if K == 1:
        T = S
        for i in range(len(T)-1):
            if T[i] == T[i+1]:
                T[i+1] = '.'
        ans = T.count('.')
        print(ans)
        return

    if len(S) == 1:
        ans = max(0, (K - (K%2==1))//2)
        print(ans)
        return

    if len(set(S)) == 1:
        tmp = len(S)*K
        tmp = tmp - (tmp%2 == 1)
        ans = tmp//2
        print(ans)
        return

    T3 = S + S + S
    for i in range(len(T3)-1):
        if T3[i] == T3[i+1]:
            T3[i+1] = '.'
    
    T2aster = T3[len(S):(2*len(S))].count('.')
    T3aster = T3.count('.')
    ans = T3aster + T2aster * (K-3)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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 or t == ls:
    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: Accepted

Solution code:
s,k = input(),int(input())
n = len(s)
c = 0
x = []
a = 1
 
for i in range(n):
    if i<n-1 and s[i]==s[i+1]:
        a += 1
    else:
        c += a//2
        x.append(a)
        a = 1
 
c2 = 0
if s[0]==s[-1]:
    c2 = x[0]//2+x[-1]//2-(x[0]+x[-1])//2
 
print(len(s)*k//2 if len(set(s))==1 else c*k-c2*(k-1))

Submission status: Accepted

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
from heapq import heappop, heappush
from collections import defaultdict
from collections import Counter
from collections import deque
from itertools import permutations
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())
S = input()
K = int(input())
ans = 0

S = list(S)
def f(T):
    ret = 0
    l = 0
    r = 1
    n = len(T)
    while l < n:
        while r < n and T[l] == T[r]:
            r += 1
        c = r - l
        ret += c // 2
        l = r
        r = l + 1
    return ret

first = f(S)
second = f(S + S)
diff = second - first

if len(set(S)) == 1:
    print(len(S) * K // 2)
else:
    print(first + diff * (K - 1))

Submission status: Accepted

Solution code:
S = input()
K = int(input())
before = ''
count = 0
change_cnt = 0
for c in S:
  if c == before:
    count += 1
  else:
    if count >= 0:
      change_cnt += (count+1)//2
      count = 0
    before = c
if count > 0:
    change_cnt += (count+1)//2
c_cnt = 0
if S[0] == S[-1]:
  for c in S:
    if c == before:
      c_cnt += 1
    else:
      break
all_same = False
plus = False
if count%2 == 0 and c_cnt%2 != 0:
  plus = True
if count+c_cnt >= len(S):
  all_same = True
# print(all_same, plus, count, change_cnt, c_cnt)  
if all_same:
  print((K//2)*len(S) + (K%2)*(len(S)//2))
elif plus:
  print(K*change_cnt + K-1)
else:
  print(K*change_cnt)