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:
from itertools import groupby

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

s = [ len(list(j)) for i, j in groupby(S)]
ans = 0
for i in s:
    if i >= 2:
        ans += i // 2
ans *= K
if S[0] == S[-1] and s[0] % 2 == s[-1] % 2 == 1:
    ans += K - 1

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

Submission status: Accepted

Solution code:
S = list(input())
R = int(input())
lengths = []
same = False
All = False
odd = False
sl = 1
Sum = 0
if S[0] == S[len(S)-1]:
  same = True
S.append('0')
for i in range(len(S)-1):
  if S[i] == S[i+1]:
    sl += 1
  else:
    lengths.append(sl)
    sl = 1
if len(lengths) == 1:
  All = True
if lengths[0]%2 == 1 and lengths[len(lengths)-1]%2 == 1:
  odd = True
if same:
  if All:
    Sum += (lengths[0]*R)//2
  else:
    temp = lengths[0]
    lengths[0] = 0
    lengths[len(lengths)-1] += temp
    if odd:
      for l in range(len(lengths)):
        Sum += (lengths[l]//2)*R
      Sum -= 1
    else:
      for g in range(len(lengths)):
        Sum += (lengths[g]//2)*R
else:
  for j in range(len(lengths)):
    Sum += (lengths[j]//2)*R
print(Sum)

Submission status: Accepted

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

S = S + S + S
N = len(S)

a = 0
b = 0
c = 0
for i in range(1, N) : 
    if S[i] == S[i-1] :
        if i < N // 3 :
            a += 1
        elif i < N // 3 * 2 :
            b += 1
        else :
            c += 1
        S[i] = '#'
        
ret = a + K // 2 * b + (K - K // 2 - 1) * c
print(ret)

Submission status: Accepted

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

import itertools
if len(set(S))==1:
  print((len(S)*K)//2)
elif S[0]!=S[-1]:
  ans=0
  for key,g in itertools.groupby(S):
    ans+=len(list(g))//2
  print(ans*K)
else:
  first=0
  last=0
  ans=0
  isfirst=True
  for key,g in itertools.groupby(S):
    num=len(list(g))
    if isfirst:
      first=num
      isfirst=False
    else:
      last=num
      ans+=num//2
  ans-=num//2
  print(ans*K+((first+last)//2)*(K-1)+first//2+last//2)

Submission status: Accepted

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

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

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

    if s[0] == s[-1]:
        ans = li[0]//2 + li[-1]//2
        ans += ((li.pop(0) + li.pop())//2) * (k-1)
    else:
        ans = 0
    for i in li:
        ans += (i//2)*k
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

    if s[0] == s[-1]:
        ans = li[0]//2 + li[-1]//2
        ans += ((li.pop(0) + li.pop())//2) * (k-1)
    else:
        ans = 0
    for i in li:
        ans += (i//2)*k
    print(ans)






if __name__ == '__main__':
    main()

Submission status: Accepted

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

if(len(set(list(s)))==1):
    ans=(len(s)*k)//2
elif(s[0] != s[-1]):
    for i in range(len(t)-1):
        if(t[i] == t[i+1]):
            ans+=1
            t[i+1]="*"
    ans*=k
else:
    s_first=s[0]
    first=0
    end=len(s)-1

    while(s[first] == s_first):
        first+=1

    while(s[end] == s_first):
        end-=1

    len_first=first
    len_end=len(s[end+1:])

    rest_s=list(s[first:end+1])
    rest_ans=0
    for i in range(len(rest_s)-1):
        if(rest_s[i] == rest_s[i+1]):
            rest_ans+=1
            rest_s[i+1]="*"

    ans+=len_first//2
    ans+=rest_ans*k
    ans+=((len_first+len_end)//2)*(k-1)
    ans+=len_end//2
print(ans)

Submission status: Accepted

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

for j in range(len(li)):
    ans+=int(li[j]//2)*k

if s[0]==s[-1] and li[0]%2==li[-1]%2==1:
    ans+=k-1

print(ans)

Submission status: Accepted

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

tmp = '0'
count = 0
for x in s:
	if tmp == x:
		count += 1
		tmp = '0'
	else:
		tmp = x
if tmp == s[0]:
	start_count = 0
	for x in s:
		if tmp != x:
			break
		start_count += 1
	if start_count == len(s):
		print((len(s)*k)//2)
	else:
		print((count+start_count%2)*k-start_count%2)
else:
	print(count*k)

Submission status: Accepted

Solution code:
import math
S = list(input())
T = int(input())
prev = S[0]
same_cnt = 0
renzoku = []
for i in range(len(S)):
    if S[i] == prev:
        same_cnt += 1
        if i == len(S)-1:
            renzoku.append(same_cnt)
    else:
        prev = S[i]
        renzoku.append(same_cnt)
        same_cnt = 1
        if i == len(S)-1:
            renzoku.append(same_cnt)
#print(renzoku)
if S[0] != S[-1]:
    print(T*sum(list(map(lambda x: math.floor(x/2),renzoku))))
else:
    if len(renzoku) != 1:
        linked_renzoku = renzoku[:]
        tail = linked_renzoku[-1]
        linked_renzoku[0] += tail
        linked_renzoku.pop()
        #print(linked_renzoku)
        print((T-1)*sum(list(map(lambda x: math.floor(x/2),linked_renzoku))) + sum(list(map(lambda x: math.floor(x/2),renzoku))))
    else:
        print(math.floor((T*len(S))/2))

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

Solution code:
# coding: utf-8
# Your code here!
S=list(input())
K=int(input())

flag=True
for i in range(len(S)-1):
    if S[i+1]!=S[i]:
        flag=False
        break

if flag and K>1:
    S*=2
    for i in range(len(S)-1):
        if S[i+1]==S[i]:
            S[i+1]="?"
    temp=S.count("?")
    print(K//2*temp+(temp//2 if K%2==1 else 0))
    exit()

if K==1:
    for i in range(len(S)-1):
        if S[i+1]==S[i]:
            S[i+1]="?"
    print(S.count("?"))
    exit()

n=len(S)
S*=4
N=len(S)-1

for i in range(N):
    if S[i+1]==S[i]:
        S[i+1]="?"

l=[S[:n],S[n:2*n],S[2*n:3*n],S[3*n:]]

ans=[0]*4
for i in range(4):
    ans[i]=l[i].count("?")

print(ans[0]+ans[3]+ans[1]*(-(-(K-2)//2))+ans[2]*((K-2)//2))

#qqq 5

Submission status: Accepted

Solution code:
# 先頭と末尾が同じ場合はそれを省き、単体で完結するもの*K + 先頭と末尾が同じ場合の連結部分 * (K-1)
# ただし全部同じ場合は別で計算
# 3WA出した後にtestcase: aaaeiiiaiaa でやって気づいた
# -> なーにが気づきじゃ なんも変わっとらんやんけ
# *(k-1) 忘れてたっぽい？
from math import floor

s = input()
k = int(input())
if len(set(list(s))) == 1:
    print(floor((len(s) * k) / 2))
    exit()

if s[0] == s[-1]:
    # このへんの算数は手書きでいくつか書いて帰納的に導出した
    # だるいしs<=100なので愚直に数えるわ
    pre, suf = 0, 0
    for c in s:
        if c == s[0]:
            pre += 1
        else:
            break
    for c in s[::-1]:
        if c == s[-1]:
            suf += 1
        else:
            break
    # 先頭と末尾の連続する文字を除いたやつのうち連続するやつからいらないやつを数える
    idx = 0
    cc = 0
    t = s[pre : len(s) - suf]
    while idx < len(t):
        c = 0
        tmp = idx
        while tmp < len(t) and t[tmp] == t[idx]:
            tmp += 1
            c += 1
        if c > 1:
            cc += floor(c / 2)
        idx = tmp
    ans = floor(pre / 2) + cc * k + floor((pre + suf) / 2) * (k - 1) + floor(suf / 2)
    print(ans)
else:
    idx = 0
    cc = 0
    t = s[:]
    while idx < len(t):
        c = 0
        tmp = idx
        while tmp < len(t) and t[tmp] == t[idx]:
            tmp += 1
            c += 1
        if c > 1:
            cc += floor(c / 2)
        idx = tmp
    ans = cc * k
    print(ans)

Submission status: Accepted

Solution code:
# 2019-11-25 06:26:24(JST)
import sys

def main():
    s, k = sys.stdin.read().split()
    k = int(k)

    if s.count(s[0]) == len(s):
        if len(s) % 2 == 0:
            ans = len(s) // 2 * k
        else:
            if k % 2 == 0:
                ans = len(s) * (k // 2)
            else:
                ans = len(s) * (k // 2) + len(s) // 2
        print(ans)
        sys.exit()

    count = 0
    same = 1
    flag = False 
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            same += 1
        else:
            if not flag: 
                first_same = same
                flag = True 
                # 最初に何文字連続するかだけ記録しておく
                # (最初と最後の文字が同じだった場合の処理のため)
            count += same // 2
            same = 1
    
    bl = False
    if s[-1] == s[0]:
        if same % 2 == 1 and first_same % 2 == 1:
            # ここでのsameは一番最後に連続した文字数
            count += same // 2 + 1
            # この場合は最後だけ調整が必要なので記録しておく
            bl = True
        else:
            count += same // 2
    else:
        count += same // 2

    ans = count * k
    if bl:
        ans -= 1

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# 2019-11-25 06:26:24(JST)
import sys

def main():
    s, k = sys.stdin.read().split()
    k = int(k)

    if len(s) == 1:
        print(k // 2)
        sys.exit()
    
    if s.count(s[0]) == len(s):
        if len(s) % 2 == 0:
            ans = len(s) // 2 * k
        else:
            if k % 2 == 0:
                ans = len(s) * (k // 2)
            else:
                ans = len(s) * (k // 2) + len(s) // 2
        print(ans)
        sys.exit()

    count = 0
    same = 1
    flag = False
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            same += 1
        else:
            if not flag: 
                first_same = same
                flag = True 
                # 最初に何文字連続するかだけ記録しておく
            count += same // 2
            same = 1
    
    bl = False
    if s[-1] == s[0]:
        if same % 2 == 1 and first_same % 2 == 1:
            # ここでのsameは一番最後に連続した文字数
            count += same // 2 + 1
            # この場合は最後だけ調整が必要なので記録しておく
            bl = True
        else:
            count += same // 2
    else:
        count += same // 2

    ans = count * k
    if bl:
        ans -= 1

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# 2019-11-25 06:26:24(JST)
import sys

def main():
    s, k = sys.stdin.read().split()
    k = int(k)

    if len(s) == 1:
        print(k // 2)
        sys.exit()
    
    if s.count(s[0]) == len(s):
        if len(s) % 2 == 0:
            ans = len(s) // 2 * k
        else:
            if k % 2 == 0:
                ans = len(s) * (k // 2)
            else:
                ans = len(s) * (k // 2) + len(s) // 2
        print(ans)
        sys.exit()

    count = 0
    same = 1
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            same += 1
        else:
            if count == 0:
                first_same = same
            count += same // 2
            same = 1
    
    bl = False
    if s[-1] == s[0]:
        if same % 2 == 1 and first_same % 2 == 1:
            count += same // 2 + 1
            bl = True
        else:
            count += same // 2
    else:
        count += same // 2

    ans = count * k
    if bl:
        ans -= 1

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# 2019-11-25 06:26:24(JST)
import sys

def main():
    s, k = sys.stdin.read().split()
    k = int(k)

    if len(s) == 1:
        print(k // 2)
        sys.exit()
    
    if s.count(s[0]) == len(s):
        if len(s) % 2 == 0:
            ans = len(s) // 2 * k
        else:
            # if k % 2 == 0:
            ans = len(s) * k // 2
            # else:
            #     ans = len(s) // 2 * (k // 2 + 1) + (len(s) // 2 + 1) * k // 2
        print(ans)
        sys.exit()

    count = 0
    same = 1
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            same += 1
        else:
            if count == 0:
                first_same = same
            count += same // 2
            same = 1
    
    bl = False
    if s[-1] == s[0]:
        if same % 2 == 1 and first_same % 2 == 1:
            count += same // 2 + 1
            bl = True
        else:
            count += same // 2
    else:
        count += same // 2

    ans = count * k
    if bl:
        ans -= 1

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
S_input = list(input())
K = int(input())
 
# 書き換えの最小値を求める
def change_count(str_list):
    answer = 0
    for i in range(1,len(str_list)):
        if str_list[i] == str_list[i-1]:
            answer += 1
            str_list[i] = i
    return answer
 
count = 0
 
# K=1のとき、文字列Sを書き換えるだけ
if K == 1:
    count += change_count(S_input)
    print(count)
    exit()
 
# 文字列Sが同じ文字のとき
if len(set(S_input)) == 1:
    count += (len(S_input)*K) // 2
    print(count)
    exit()
 
# 文字列Tの両端について、書き換える
SS = S_input + S_input
count += change_count(SS)
# K=2のとき、文字列SSを書き換えるだけ
if K == 2:
    print(count)
    exit()
 
# 文字列Tの真ん中の文字列Sについて、書き換える
SSS = S_input + S_input + S_input
change = change_count(SSS) - count
 
# K>=3のとき、両端1個＆真ん中(K-2)個
count += change * (K-2)    
 
print(count)

Submission status: Accepted

Solution code:
s=str(input())
k=int(input())
n=len(s)
if s[0]!=s[-1] or k==1:
    ans=0
    a=0
    c='0'
    for i in range(n):
        if s[i]==c:
            a+=1
        else:
            c=s[i]
            ans+=-(-a//2)
            a=0
    ans+=-(-a//2)
    print(ans*k)
elif n==1:
    print(k//2)
elif len(set(s))==1:
    print(k*len(s)//2)
else:
    ans=0
    a=0
    c='0'
    for i in range(n):
        if s[i]==c:
            a+=1
        else:
            c=s[i]
            ans+=-(-a//2)
            a=0
    ans+=-(-a//2)
    ANS=0
    a=0
    c='0'
    s+=s
    for i in range(2*n):
        if s[i]==c:
            a+=1
        else:
            c=s[i]
            ANS+=-(-a//2)
            a=0
    ANS+=-(-a//2)
    print(ans+(ANS-ans)*(k-1))

Submission status: Accepted

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

def main(s, k):
  separation = []
  tmp = ""
  for x in range(len(s)-1):
    tmp += s[x]
    if s[x] == s[x+1]:
      if tmp == "":
        tmp += s[x]
    else:
      separation.append(tmp)
      tmp = ""
  tmp += s[-1]
  separation.append(tmp)

  total = 0

  if len(separation) == 1:
    if (len(separation[0]) == 1):
      return k//2
    else:
      return (len(separation[0])*k)//2

  if (s[0] == s[-1]) and k>1 and len(separation)>1:
    a = len(separation[0])//2
    b = len(separation[-1])//2
    total += a
    total += b
    separation[0] += separation[-1]
    a = separation[0]
    c = (len(a)//2)*(k-1)
    total += c
    separation = separation[1:-1]

  # print(separation)

  for x in separation:
    total += (len(x)//2)*k

  return total

print(main(s,k))

Submission status: Accepted

Solution code:
s = list(input())
k = int(input())
first = s[0]
last = s[-1]
first_num = 0
last_num = 0
now_num = 0
now = s[0]
ans = 0
for i in s:
  if i == first:
    first_num += 1
  else:
    break
for i in reversed(s):
  if i != last:
    break
  last_num += 1
for i in s:
  if i == now:
    now_num += 1
  else:
    ans += now_num // 2
    now_num = 1
    now = i
ans += now_num // 2
ans *= k
if first == last:
  if first_num % 2 and last_num % 2:
    ans += k - 1
  if len(set(s)) == 1:
    ans = (first_num * k) // 2 
print(ans)

Submission status: Accepted

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

def f(s):
  i=0
  c=0
  n=len(s)
  if n==1:
    return 0
  else:
    while i<n-1:
      if s[i]==s[i+1]:
        c+=1
        i+=2
      else:
        i+=1
    return c

a=0
n=len(S)
while a<n-1 and S[0]==S[a+1]:
  a+=1
  
b=-1
while a<n-1 and S[0]==S[b]:
  b-=1
  
if a==n-1:
  ans=n*K//2
elif b==-1:
  ans=f(S)*K
elif a==0 and b!=-1:
  ans=(K-1)*((-b-1)//2)+K*f(S[1:b+1])+(-b-1)//2
elif a!=0 and b!=-1:
  ans=(a+1)//2+f(S[a+1:b+1])*K+((a-b)//2)*(K-1)+(-b-1)//2
  
print(ans)

Submission status: Accepted

Solution code:
s=input()
k=int(input())
a=set(s)
if len(a)==1:print(k*len(s)//2)
else:
  V=[]
  unit=0
  for i in s:
    if len(V)==0 or V[-1][0]!=i:
      V.append([i,1])
    else:
      V[-1][1] += 1
  for i in V:
    unit+=i[1]//2
  ans=unit*k
  if s[0]==s[-1]:
    ans=ans-(V[0][1]//2+V[-1][1]//2-(V[0][1]+V[-1][1])//2)*(k-1)
  print(ans)

Submission status: Accepted

Solution code:
s=input()
t=int(input())
a=len(s)
b=0
i=0
while i<a-1:
  if s[i]==s[i+1]:
    b=b+1
    i+=2
  else:
    i+=1
if s[0]!=s[a-1]:
  print(b*t)
else:
  d=1
  i=0
  while i<a-1:
    if s[i]==s[i+1]:
      d+=1 
      i=i+1
    else:
      break
  e=1
  i=0
  while i<a-1:
    if s[a-1-i]==s[a-1-i-1]:
      e+=1
      i=i+1
    else:
      break
  if d+e==a*2:
    print(a*t//2)
  else:
    b-=d//2
    b-=e//2
    print(b*t+d//2+e//2+(d+e)//2*(t-1))

Submission status: Accepted

Solution code:
from sys import stdin
from itertools import groupby
S = stdin.readline().rstrip()
K = int(stdin.readline().rstrip())
l = len(S)
# Run Length Encoding
rle = [(k, sum(1 for _ in g)) for k, g in groupby(S)]

if len(set(S)) == 1:
    print(len(S) * K // 2)
else:
    tmp_ans = sum([t[1] // 2 for t in rle])*K
    if S[0] != S[-1]:
        print(tmp_ans)
    else:
        print(tmp_ans - (rle[0][1] // 2 + rle[-1][1] // 2 - (rle[0][1] + rle[-1][1]) // 2) * (K-1))

Submission status: Accepted

Solution code:
from sys import stdin
from itertools import groupby
S = stdin.readline().rstrip()
K = int(stdin.readline().rstrip())
l = len(S)
# Run Length Encoding
rle = [len(list(g)) for k, g in groupby(S)]

if len(set(S)) == 1:
    print(len(S) * K //2)
else:
    tmp_ans = sum([l // 2 for l in rle])*K
    if S[0] != S[-1]:
        print(tmp_ans)
    else:
        print(tmp_ans - (rle[0] // 2 + rle[-1] // 2 - (rle[0] + rle[-1]) // 2) * (K-1))

Submission status: Accepted

Solution code:
import sys
readline=sys.stdin.buffer.readline
 
S=readline().rstrip().decode("utf-8")
K=int(readline())
N=len(S)
#print("SK:",S*K)
 
if len(set(S))==1:
  answer=N*K//2
else:
  P=[0] #point
  C=[] #continuous
  S=S+"X"
  
  for i in range(N):
    if S[i]!=S[i+1]:
      P.append(i+1)
  for i in range(len(P)-1):
    C.append(P[i+1]-P[i])
  #print("C:",C)
    
  S=S[0:-1]
  answer=0
  if S[0]==S[-1]:
    for i in range(1,len(C)-1):
      answer+=C[i]//2
    answer*=K
    answer+=C[0]//2+C[-1]//2+((C[0]+C[-1])//2)*(K-1)
  else:
    for c in C:
      answer+=c//2
    answer*=K
print(answer)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from collections import Counter

def count(s):
    cnt = 1
    res = 0
    for i in range(len(s)-1):
        if s[i]==s[i+1]:
            cnt += 1
        else:
            res += cnt//2
            cnt = 1
    res += cnt//2
    return res

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

c = Counter(s)

if len(c.keys())==1:
    print(len(s)*k//2)
    exit()

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

cs = count(s)
cs2 = count(s+s)
print((cs2-cs)*(k-1) + cs)

Submission status: Accepted

Solution code:
s = input()
k = int(input())
l = []
cur = s[0]
c = 1
for i in s[1:]:
  if i != cur:
    l.append([cur,c])
    cur = i
    c = 1
  else:
    c += 1
l.append([cur,c])
c = 0
if len(l) == 1:
  print((l[0][1]*k)//2)
  exit()
elif len(l) == 2:
  print((l[0][1]//2+l[1][1]//2)*k)
  exit()

for i,j in l[1:-1]:
  c += j//2
if l[0][0] != l[-1][0]:
  c += l[0][1]//2 + l[-1][1] //2
  print(c*k)
else:
  re = c*k
  le = l[0][1] // 2
  r = l[-1][1] // 2
  ir = (l[0][1] + l[-1][1]) // 2
  print(re + le + r + ir * (k-1))

Submission status: Accepted

Solution code:
A = input()
n = len(A)
S = []
R = [A[n-1]]
for i in range(n):
    S.append(A[i])
    R.append(A[i])
K = int(input())
s = 0
p = S[0]
q = S[n-1]
for i in range(n-1):
    x = S[i]
    y = S[i+1]
    if x == y:
        S[i+1] = 1
        s += 1
t = 0
for j in range(n):
    x = R[j]
    y = R[j+1]
    if x == y:
        R[j+1] = 1
        t += 1
if p != q:
    ans = s*K
else:
    if S[n-1] == 1:
        ans = s*K
    else:
        if R[n] == 1:
            k = K//2
            ans = (K-k)*s + k*t
        else:
            ans = s + (K-1)*t
print(ans)

Submission status: Accepted

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

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

t1 = s + "#"
t2 = s * 2 +"#"
# t3 = s * 3 + "#"
# t4 = s * 4 + "#"

def f(text):
    start, end = 0, 0
    rc = []

    while start != len(text)-1:
        if text[start] == text[end]:
            end += 1
        else:
            rc.append(end-start)
            start = end

    return(rc)

first = sum([i//2 for i in f(t1)])
dif = sum([i//2 for i in f(t2)]) - sum([i//2 for i in f(t1)])

print(first + dif*(k-1))

Submission status: Accepted

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

_s = input()[:-1]
_k = int(input())


def chker(S, K):
    s3 = [item for item in S * 3]
    for i in range(1, len(s3)):
        if s3[i - 1] == s3[i]:
            s3[i] = 1

    s3 = [0 if item != 1 else item for item in s3]

    ls = len(S)
    cnt_0 = sum(s3[:ls])
    cnt_1 = sum(s3[ls:2 * ls])
    cnt_2 = sum(s3[2 * ls:3 * ls])

    print(cnt_0 + cnt_1 * (K // 2) + cnt_2 * ((K - 1) // 2))


chker(_s, _k)

Submission status: Accepted

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

t = bytearray(S * 3, 'us-ascii')
for i in range(1, len(t)):
    if t[i - 1] == t[i]:
        t[i] = 0
b0 = t[:len(S)].count(0)
b1 = t[len(S):len(S)*2].count(0)
b2 = t[len(S)*2:len(S)*3].count(0)
print(b0 + b1 * (K // 2) + b2 * ((K - 1) // 2))

Submission status: Accepted

Solution code:
from sys import exit

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

if K < 4:
    t = bytearray(S * K, 'us-ascii')
    for i in range(1, len(t)):
        if t[i - 1] == t[i]:
            t[i] = 0
    print(t.count(0))
    exit()

t = bytearray(S * 3, 'us-ascii')
for i in range(1, len(t)):
    if t[i - 1] == t[i]:
        t[i] = 0
b0 = t[:len(S)].count(0)
b1 = t[len(S):len(S)*2].count(0)
b2 = t[len(S)*2:len(S)*3].count(0)
print(b0 + b1 * (K // 2) + b2 * ((K - 1) // 2))

Submission status: Accepted

Solution code:
from itertools import groupby

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

def count(nums):
    return sum(num//2 for num in nums)

l = [len(list(g)) for k, g in groupby(S)]

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

Submission status: Accepted

Solution code:
s = input()
k = int(input())
same = len([i for i in s if i!=s[0]])
s = s+s
count = 1
a1 = 0
a2 = 0
for i in range(len(s)-1):
  if s[i] == s[i+1]:
    count += 1
  else:
    a2 += count//2
    count = 1
  if i == len(s)//2-2:
    a1 = a2
    if count > 1:
      a1 += count//2
if count > 1:
  a2 += count//2
ans = a1 + (a2-a1)*(k-1)
if same != 0:
  print(ans)
else:
  print((len(s)//2)*k//2)

Submission status: Accepted

Solution code:
s = input()
k = int(input())
ans = 0
cnt = 1
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        cnt += 1
    else:
        ans += (cnt//2)*k
        cnt = 1
ans += ((cnt)//2)*k
if cnt == len(s):
    ans = len(s)*k//2
elif s[0] == s[-1]:
    l = 1
    r = 1
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            l += 1
        else:
            break
    for i in range(len(s)-1,0,-1):
        if s[i] == s[i-1]:
            r += 1
        else:
            break
    ans -= (l//2 + r//2 - ((l+r)//2))*(k-1)
print(ans)

Submission status: Accepted

Solution code:
strings = list(input())
k = int(input())
counter = 0
same_starts = 0
for  i in range(len(strings)-1):
    if strings[i] != strings[i+1]:
        break
    same_starts += 1
for  i in range(len(strings)-1):
    if strings[i] == strings[i+1]:
        strings[i+1] = str(counter)
        counter += 1

if strings[0] != strings[-1]:
    print(counter * k)
else:
    # 文字列が１種類の時
    if (same_starts + 1) == len(strings):
        # 文字列の個数が偶数の時
        if len(strings) % 2 == 0:
            print(k * len(strings)//2)
        else:
        # 文字列が奇数の時
            # 文字列を繰り返す回数が偶数回の時
            if k % 2 == 0:
                print(counter * k // 2 + (counter + 1) * k // 2)
            else:
            # 文字列を繰り返す回数が奇数回の時
                print(counter * ((k - 1) // 2 + 1)+ (counter + 1) * (k - 1)//2)
    else:
        if (same_starts+1) % 2 == 0:
            print(counter * k)
        else:
            print((counter+1) * (k-1) + counter)

Submission status: Accepted

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

def OneString(a):
    seq = 0
    seqit = 0
    for i in range(len(a)):
        if i == len(a) - 1:
            seq += (seqit+1)//2
        elif a[i]!=a[i+1]:
            seq += (seqit+1)//2
            seqit = 0
        else:
            seqit += 1
    return seq

def TBodd(a):
    seqnumT = 1
    seqnumB = 1
    if a[0] != a[-1]:
        return False
    for i in range(len(a)-1):
        if a[i]==a[i+1]:
            seqnumT += 1
        else: break
    for i in range(len(a)-2):
        if a[-(i+1)]==a[-(i+2)]:
            seqnumB += 1
        else: break
    if seqnumT%2 != 0 and seqnumB%2 != 0:
        return True
    else: return False
            

if len(set(ListS)) == 1:
    ans = (len(S)*K)//2
elif TBodd(S):
    ans = (OneString(S) + 1)*K - 1
else: ans = OneString(S)*K
    
print(ans)

Submission status: Accepted

Solution code:
s = str(input())
n = int(input())
num = []
cnt = 1
for i in range(len(s)-1):
    if s[i+1] != s[i]:
        num.append(cnt)
        cnt = 1
    else:
        cnt += 1
num.append(cnt)
if len(num) == 1:
    print((num[0] * n)//2)
    exit()
ans = 0
for nu in num:
    ans += nu//2
if s[0] != s[-1]:
    ans *= n
else:
    ans -= num[0]//2
    ans -= num[-1]//2
    ans *= n
    ans += num[0]//2
    ans += num[-1]//2
    ans += ((num[0]+num[-1])//2)*(n-1)
print(ans)

Submission status: Accepted

Solution code:
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))



S=one_str()
K=one_int()

before = S[0]
count=1
ranrength = []
for i in S[1:]:
    if before==i:
        count+=1
    else:
        ranrength.append(count)
        count=1
    before=i

ranrength.append(count)

output_count=0
for r in ranrength:
    if r>1:
        output_count += r//2
output_count *= K

if S[0]==S[-1]:
    head=ranrength[0]
    tail=ranrength[-1]
    
    if head>1 and head%2==1 and tail>1 and tail%2==1:
        output_count += (K-1)

if len(ranrength)==1:
    print(ranrength[0]*K//2)
else:
    print(output_count)

Submission status: Accepted

Solution code:
from itertools import groupby

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

l=[(k, list(v)) for k, v in groupby(S)]
if len(l) == 1:
    n = len(S)*K // 2
else:
    n=sum([len(v)//2 for k, v in l if len(v) > 1])*K
    if l[0][0]==l[-1][0] and len(l[0][1])%2 !=0 and len(l[-1][1])%2 !=0:
        n = n + K - 1
print(n)

Submission status: Accepted

Solution code:
S=list(input())
K=int(input())
SS=S*2
F=S[0]
if len(S)==1:
	P=int(K//2)
elif S==[F]*len(S):
	P=int((len(S))*K//2)
else:
	for G in range(1,len(S)):
		if S[G-1]==S[G]:
			S[G]="O"
		else:
			continue
	A=S.count("O")
	for J in range(1,len(SS)):
		if SS[J-1]==SS[J]:
			SS[J]="O"
		else:
			continue
	B=SS.count("O")
	P=A+(B-A)*(K-1)
print(P)

Submission status: Accepted

Solution code:
from itertools import groupby

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

l=[(k, list(v)) for k, v in groupby(S)]
if len(l) == 1:
    len_S = len(S)
    if len_S%2:
        n = len_S * (K//2) + (len_S//2) * (K%2)
    else:
        n = (len_S//2) * K
else:
    n=sum([len(v)//2 for k, v in l if len(v) > 1])*K
    if l[0][0]==l[-1][0] and len(l[0][1])%2 !=0 and len(l[-1][1])%2 !=0:
        n = n + K - 1
print(n)

Submission status: Accepted

Solution code:
import numpy as np
#A = list(map(int,input().split()))
S = list(input())
K = int(input())

S1 = S + ['0']
S2 = S + S + ['0']

cur=S1[0]
cnt=0
Sl1 = list()
for s in S1:
    if cur==s:
        cnt+=1
    else:
        Sl1 += [cnt]
        cur = s
        cnt = 1

cur=S2[0]
cnt=0
Sl2 = list()
for s in S2:
    if cur==s:
        cnt+=1
    else:
        Sl2 += [cnt]
        cur = s
        cnt = 1

Sl1 = np.array(Sl1,dtype='int64')
Sl2 = np.array(Sl2,dtype='int64')

if 2*len(Sl1)==len(Sl2):
    N = int(K*np.sum(Sl1[::]//2))
else:
    if len(Sl1)>1 :
        N = int(Sl1[0]//2 + Sl1[-1]//2 + K*np.sum(Sl1[1:-1]//2) + (K-1)*((Sl1[0]+Sl1[-1])//2))
    else:
        N = int((K*Sl1[0])//2)


print(N)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
import sys,bisect,math,itertools,string,queue
from heapq import heappop, heappush
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

s = input()
n = len(s)
k = inp()
cnt = 1
res = 0
if s[0] == s[-1]:
    l = 1
    r = 1
    for i in range(n-1):
        if s[i] == s[i+1]:
            l += 1
        else:
            break
    for i in range(n-1)[::-1]:
        if s[i] == s[i+1]:
            r += 1
        else:
            break
    if l == n and r == n:
        res = (n*k)//2
    else:
        # print(l,r)
        if r > 1:
            res += r//2
        if l > 1:
            res += l//2
        res += ((r+l)//2)*(k-1)
        # print(res)
        for i in range(l,n-r-1):
            if s[i] == s[i+1]:
                cnt += 1
            elif cnt > 1:
                res += (cnt//2)*k
                cnt = 1
        if cnt > 1:
            res += (cnt//2)*k
else:
    for i in range(n-1):
        if s[i] == s[i+1]:
            cnt += 1
        elif cnt > 1:
            res += (cnt//2)*k
            cnt = 1
    if cnt > 1:
        res += cnt//2*k
print(res)

Submission status: Accepted

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

    ans=cnt=key=0
    for i in range(n-1):
        if key==0 and s[i]==s[i+1]:
            cnt+=1
            key=1
        elif key==1 and s[i]==s[i+1]:
            key=0
        else:
            key=0
    ans=cnt*k
    a=b=1
    for i in range(n-1):
        if s[i]==s[i+1]:
            a+=1
        else:
            break
    for i in range(n-1):
        if s[n-1-i]==s[n-2-i]:
            b+=1
        else:
            break
    if a!=n and s[0]==s[n-1]:
        ans+=(((a+b)//2)-(a//2)-(b//2))*(k-1)
    if a==n:
        if n%2==0:
            print(cnt*k)
            return
        else:
            print((n*k)//2)
            return
    print(ans)
main()