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:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
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
    cnt=0
    for x in RLE:
        cnt+=x//2
    if len(RLE)==1:
        print(cnt*K//2)
        exit()
    RLE = []
    before = ""
    for t in S:
        if before != t:
            RLE.append(1)
            before = t
        else:
            RLE[-1]+=1

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

    cnt3 = cnt - cnt2*2

    ans=cnt2*K + cnt3*(K-1)
    print(ans)
    

if __name__ == '__main__':
    main()

Submission status: Accepted

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

L = len(S)
rtn = 0
las = False

if (L == 1):
    rtn = K // 2

else:
    ichi = 0
    mae = S[0]
    i = 1
    while(i < L):
        las = False
        if (S[i] == mae):
            ichi += 1
            i += 1
            las = True
        if (i < L):
            mae = S[i]
        i += 1

    if (las and (i == L+1)): #らすかえた
        rtn = ichi * K

    else: #らすかえてない
        ni = 0
        mae = S[L-1]
        i = 0
        while(i < L):
            las = False
            if (S[i] == mae):
                ni += 1
                i += 1
                las = True
            if (i < L):
                mae = S[i]
            i += 1

        if (las and (i == L+1)): #らすかえた
            T = K // 2
            rtn = ichi * (K - T) + ni * T
        else:
            rtn = ichi + ni * (K - 1)

print(rtn)

Submission status: Accepted

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

count1 = 0
x = "A"
con = 1
#仮に1回でもダメなやつ
for i in range(N):
  if x == S[i]:
    con += 1 
  else:
    x = S[i]
    if con != 1:
      count1 += con //2
    con = 1
  #print(count1, i)
if con != 1:
  count1 += con //2
  
if con == N:
  print(N * K // 2)
  quit()
#print(count1)
count2 = 0
count0 = 0
countf = 0
if S[0] == S[-1]:
  p = 0
  q = 0
  count0 = 1
  countf = 1
  i = 0
  j = -1
  while p == 0:
    if S[i] == S[i + 1]:
      count0 += 1
      i += 1
    else:
      p = 1
  while q == 0:
    if S[j] == S[j - 1]:
      countf += 1
      j -= 1
    else:
      q = 1

if (count0 % 2 == 1) and (countf % 2 == 1):
  count2 = 1
print(count1 * K + count2 * (K - 1))

Submission status: Accepted

Solution code:
S=input()
k=int(input())
flag=0
if S[0]==S[-1]:
    flag=1

ss=S[0]
seq=[]
count=1

for s in S[1:]:
    if ss==s:
        count+=1
    else:
        seq.append(count)
        count=1
    ss=s
seq.append(count)
    
sousa=0
for i in seq:
    sousa+=i//2
    
sousa=sousa*k
if flag==1:
    if len(seq)==1:
        if seq[0]%2==1:
            sousa+=k//2
    elif seq[0]%2==1 and seq[-1]%2==1:
        sousa+=k-1
        
print(sousa)

Submission status: Accepted

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

left = S[0]
right = S[-1]

inside = []
tmp = 1
bef = ""
for idx, val in enumerate(S):
    if idx == 0:
        bef = val
        continue
    if val == bef:
        tmp += 1
    else:
        inside.append(tmp)
        tmp = 1
    bef = val
else:
    inside.append(tmp)

ans = 0
if left == right:
    if len(inside) == 1:
        print((len(S) * K )// 2)
    elif len(S) > 2:
        head = inside[0]
        tail = inside[-1]
        center = inside[1:-1]
        init = inside[:-1]
        last = inside[1:]
        joint = head + tail

        init_op = 0
        last_op = 0
        for i in init:
            if i != 1:
                init_op += i //2
        for i in last:
            if i != 1:
                last_op += i //2
        joint_op = joint // 2
        center_op = 0
        for i in center:
            if i != 1:
                center_op += i //2

        if K == 1:
            ans = init_op + tail // 2
        else:
            ans += init_op
            ans += last_op
            ans += joint_op * (K - 1)
            ans += center_op * (K - 2)
        print(ans)
    elif len(S) == 1:
        print(K // 2)
    elif len(S) == 2:
        print(K)
else:
    for i in inside:
        if i != 1:
            ans += i //2
    print(ans * K)

Submission status: Accepted

Solution code:
import sys
inputs = sys.stdin.readlines()

S = inputs[0].strip()
K = int(inputs[1])

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

elif S[0] != S[-1]:
    counts = []
    count = 1
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            count += 1
        else:
            counts.append(count)
            count = 1
    counts.append(count)
    print(K*sum(map(lambda x: x//2, counts)))
else:
    counts = []
    count = 1
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            count += 1
        else:
            counts.append(count)
            count = 1
    counts.append(count)
    if counts[0] % 2 == 1 and counts[-1] % 2 == 1:
        t = 1
    else:
        t = 0
    counts[0] += counts.pop()
    print(K*sum(map(lambda x: x//2, counts)) - t)

Submission status: Accepted

Solution code:
s=str(input())
k=int(input())
t=s*2

ans=0
a=1
for i in range(len(s)-1):
    if s[i]==s[i+1]:
        a+=1
    else:
        ans+=a//2
        a=1
ans+=a//2
#print(ans,ans*k)

ans_=0
a_=1
for i in range(len(t)-1):
    if t[i]==t[i+1]:
        a_+=1
    else:
        ans_+=a_//2
        a_=1
ans_+=a_//2
#print(ans_)
if len(set(s))!=1:
    print(ans+(k-1)*(ans_-ans))
elif k%2==0:
    print(ans+(k-1)*(ans_-ans))
else:
    print((k//2)*(len(s))+(k%2)*(len(s)//2))

Submission status: Accepted

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

tmp = ""
cnt = 0
ans = 0
for s in S:
    if s == tmp:
        cnt += 1
    else:
        ans += cnt//2
        cnt = 1
    tmp = s

if cnt == len(S):
    ans += (cnt*K)//2
else:
    ans *= K
    
    first_cnt = 0
    if S[0] == S[-1]:
        for s in S:
            if s == S[0]:
                first_cnt += 1
            else:
                break
        ans -= first_cnt//2 * (K-1)
        ans += (cnt+first_cnt)//2 * (K-1)
        ans += cnt//2
    else:
        ans += cnt//2 * K

print(ans)

Submission status: Accepted

Solution code:
from itertools import groupby

s = input()
n = int(input())
ans = 0
comp_k = []
comp_s = []
for k,v in groupby(s):
  comp_k += [k]
  comp_s += [len(list(v))]

if len(comp_s) == 1:
  print(comp_s[0] * n // 2)
  quit()

a = 0
if comp_k[0] == comp_k[-1]:
  a += comp_s[0]//2 + comp_s[-1]//2
  comp_s[0] += comp_s.pop()
  a -= comp_s[0]//2

for i in comp_s:
  ans += i // 2
ans *= n

print(ans + a)

Submission status: Accepted

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


prev = ""

n = 0

if len(s) == 1:
    print(num // 2)
else:
    if len(set(s)) == 1:
        ans = len(s) * num // 2
    
    elif s[0] == s[-1]:
        b = 1
        for i in range(1, len(s)):
            if s[0] == s[i]:
                b += 1
            else:
                start = i
                break
        
        ss = s[::-1]
        c = 1
        for i in range(1, len(s)):
            if ss[0] == ss[i]:
                c += 1
            else:
                end = i
                break
        
        for i in range(start, len(s) - end):
            if prev == s[i]:
                n += 1
                prev = ""
            else:
                prev = s[i]
        ans = ((b + c) // 2) * (num - 1)
        ans += b // 2 + c // 2
        ans += n * num

    else:
        for x in s:
            if prev == x:
                n += 1
                prev = ""
            else:
                prev = x
        ans = n * num


    print(ans)

Submission status: Accepted

Solution code:
s, k = input(), int(input())
char=[]
prev = "0"
cnt = 0
for i in s:
    if i==prev: cnt+=1
    else: char.append(cnt);cnt=1
    prev = i
char.append(cnt)

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

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

print(ans)

Submission status: Accepted

Solution code:
from itertools import groupby
s = list(input())
k = int(input())

G = []
for key, group in groupby(s):
    G += [list(group)]

count = 0

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

elif s[0]!=s[-1]:
    for g in G:
        count += len(g)//2
    print(count*k)

else:
    d = len(G[0]+G[-1])//2-(len(G[0])//2)-(len(G[-1])//2)
    for g in G:
        count += len(g)//2
    print(count*k + d*(k-1))

Submission status: Accepted

Solution code:
S=input()
K=int(input())
x=0
l=[]
for i in range(len(S)):
    if S[i-1]==S[i]:
        x+=1
    else:
        l.append(x)
        x=0
l.append(x)
if len(l)==1:
    print(-(-len(S)*K)//2)
elif S[-1]!=S[0]:
    print(sum([-(-e//2) for e in l])*K)
elif S[-2]==S[-1]:
    print(-(-(l[0]-1)//2)+sum([-(-e//2) for e in l[1:-1]])*K-((-l[0]-l[-1])//2)*(K-1)-((-l[-1])//2))
else:
    print(-(-(l[0]-1)//2)+sum([-(-e//2) for e in l[1:]])*K-(-l[0]//2)*(K-1))

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

# Summarize count of factor within list
def summarize_list(l):
  sl=l

  a=sl[0]
  c=1
  res=[]

  for x in sl[1:]:
    if x==a:
      c+=1
    else:
      res.append([a,c])
      a=x
      c=1
  res.append([a,c])

  return res

def main():
  s=S()
  k=I()

  if len(s)==1:
    return k//2

  l=summarize_list(s)

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

  ans=0
  bf=False
  
  if l[0][0]==l[-1][0] and (l[0][1]//2+l[-1][1]//2!=(l[0][1]+l[-1][1])//2):
    bf=True

  _ans=0
  for x in l:
    _ans+=x[1]//2
  ans+=_ans*k

  if bf:
    ans+=k-1

  return ans

# main()
print(main())

Submission status: Accepted

Solution code:
word = list(input())
num = int(input())
if len(word) == 1:
  print(num//2)
else:
  p = []
  count = 1
  for i in range(len(word)-1):
    if word[i] == word[i+1]:
      count += 1
      if i == len(word)-2:
        p.append(count)
    else:
      p.append(count)
      count = 1

  point = 0
  
  for i in p:
    point += i // 2
  if len(p) == 1:
    print(p[0]*num//2)
  elif word[0] == word[-1]:
    print((p[0] + p[-1])//2 * (num-1) + p[0]//2 + p[-1]//2 + (point - p[0]//2 - p[-1]//2)*num)
  else:
    print(point * num)

Submission status: Accepted

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

def do(s):
    x = 0
    cur = ""
    count = 1
    #print("s={0}".format(s))
    for i in range(len(s)):
        #print("i={0}, cur = ".format(i, cur))
        if cur == s[i]:
            count += 1
            #print(" same count = {0}".format(count))
        else:
            x += count // 2
            #print(" change add = {0}".format(count // 2))
            count = 1
        cur = s[i]
    x += count // 2
    #print(" change add = {0}".format(count // 2))
    return x

res1 = do(s)
res2 = do(s*2)
res3 = do(s*3)


if k == 1:
    print(res1)
elif k == 2:
    print(res2)
elif k == 3:
    print(res3)
else:
    if (k-1) % 2 == 1:
        res = res1
        res += ( (res3-res2) + (res2-res1) ) * ( (k-1) // 2)
        res += (res2-res1)
    elif (k-1) % 2 == 0:
        res = res1
        res += ( (res3-res2) + (res2-res1) ) * ( (k-1) // 2)
    print(res)

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
inf=float("inf")
s=input()
k=int(input())
cnt=0
tmp=0
flag=0

for i in range(len(s)):
    if s[0]==s[i]:
        flag+=1
if flag==len(s):
    print(len(s)*k//2)
    quit()

for i in range(len(s)-1):
    if s[i]==s[i+1]:
        tmp+=1
    else:
        cnt+=(tmp+1)//2
        tmp=0
cnt+=(tmp+1)//2
hoka=0
# print(cnt)
left=0
right=0
if s[0]==s[-1]:
    for i in range(len(s)):
        if s[0]==s[i]:
            left+=1
        else:
            break
    for i in range(len(s)):
        if s[-1]==s[-1-i]:
            right+=1
        else:
            break
    if right%2==1 and left%2==1:
        hoka=1

print(cnt*k+hoka*(k-1))

Submission status: Accepted

Solution code:
import sys
import collections
import copy
import math
from operator import itemgetter
def input(): return sys.stdin.readline().strip()
S= input()
K =int(input())
count = 1
ans_list = []
ans = 0
if len(S) ==1:
    print(len(S)*K//2)
else:
    if S [0] != S[-1]:
        for i in range(len(S)-1):
            if S[i] == S[i+1]:
                count += 1
            else:
                if count ==1:
                    continue
                else:
                    ans_list.append(count)
                    count = 1
        if count !=1:
            ans_list.append(count)
        for j in range(len(ans_list)):
            if ans_list[j]%2 == 1:
                ans += (ans_list[j]-1)//2
            else:
                ans += (ans_list[j])//2
        print(ans*K)
    else:
        a = 0
        while S[a] == S[a+1] :
            a +=1
            if a == len(S)-1:
                break
        a += 1
        b = 0
        while S[-1-b] == S[-2-b]:
            b += 1
            if b == len(S)-1:
                break
        b += 1
        if a != len(S):
            S_alt = S[a:len(S)-b]
            for i in range(len(S_alt)-1):
                if S_alt[i] == S_alt[i+1]:
                    count += 1
                else:
                    if count ==1:
                        continue
                    else:
                        ans_list.append(count)
                        count = 1
            if count !=1:
                ans_list.append(count)
            for j in range(len(ans_list)):
                if ans_list[j]%2 == 1:
                    ans += (ans_list[j]-1)//2
                else:
                    ans += (ans_list[j])//2
            ans = (ans * K)
            ans += a//2 + b//2
            ans += (a+b)//2*(K-1)
            print(ans)
        else:
            ans += len(S)*K//2
            print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8

def ri():
    return int(input())

def rl():
    return list(input().split())

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

def change(s, s0=""):
    t = list(s0+s)
    ret = 0
    for i in range(1, len(t)):
        if t[i] == t[i-1]:
            t[i] = '*'
            ret += 1
    return "".join(t[len(s0):]), ret

def main():
    s = input()
    k = ri()
    memo = dict()
    memo2 = []
    bef = ""
    if k < 100:
        _, c = change(s*k, "")
        print(c)
        return
    rep = 0
    for i in range(k):
        ns, c = change(s, bef)
        # print(bef, s, ns, c)
        if ns in memo:
            # print("OK", ns, memo[ns])
            rep = i-memo[ns][1]
            break
        memo[ns] = (c, i)
        memo2.append((c, ns))
        bef = ns
    ans = 0
    for m in memo2:
        ans += m[0]
    assert(len(memo2) < 3)
    rem = k-len(memo2)
    if rep == 1:
        ans += rem*memo2[-rep][0]
    elif rep == 2:
        ans += (rem//2)*sum(m[0] for m in memo2)
        if rem % 2 == 1:
            ans += memo2[0][0]
    print(ans)
    # print(rep, memo, memo2)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from itertools import groupby
S = input()
K = int(input())
lenS = len(S)

cnt1 = 0

idx = 0
for key, values in groupby(S):
  len_values = len(list(values))
  if idx == 0:
    fst_key = key
    fst_len_values = len_values

  if len_values >= 2:
    cnt1 += len_values//2
  idx += 1

if idx == 1:
  print((lenS*K)//2)
  exit()

cnt2 = cnt1
cnt3 = 0
if key == fst_key:
  if fst_len_values >= 2:cnt2 -= fst_len_values//2
  if len_values >= 2:cnt2 -= len_values//2 
  cnt3 = (fst_len_values+len_values)//2
    
print(cnt1 + (cnt2+cnt3)*(K-1))
#print(cnt1, cnt2, K, fst_len_values, len_values)

Submission status: Accepted

Solution code:
S = input()
K = int(input())
S_0 = [i for i in S]
S_1 = S_0+S_0
ans_0 = 0
tmp = S_0[0]
tmp_0 = S_0[0]
flag = True
for i in range(1,len(S_0)):
    if S_0[i] == tmp:
        S_0[i] = "0"
        ans_0 += 1
    tmp = S_0[i]
    if S[i] != tmp_0:
        flag = False
ans_1 = 0
tmp = S_1[0]
for i in range(1,len(S_1)):
    if S_1[i] == tmp:
        S_1[i] = "0"
        ans_1 += 1
    tmp = S_1[i]
ans = ans_0 + (ans_1 - ans_0)*(K-1)
if flag == False:
    print(ans)
else:
    print((len(S)*K)//2)

Submission status: Accepted

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


new_s = s + "#"
n = len(new_s)
i = 0
j = 1
cnt = 1
ans = 0
while True:
    if j >= n:
        break
    if new_s[i] == new_s[j]:
        cnt += 1
        j += 1
        continue
    if new_s[i] != new_s[j]:
        ans += cnt // 2
        cnt = 1
        i = j
        j = i + 1
        
ans1 = ans
t = 2*s + "#"
n = len(t)
i = 0
j = 1
cnt = 1
ans2 = 0
while True:
    if j >= n:
        break
    if t[i] == t[j]:
        cnt += 1
        j += 1
        continue
    if t[i] != t[j]:
        ans2 += cnt // 2
        cnt = 1
        i = j
        j = i + 1


t = 3*s + "#"
n = len(t)
i = 0
j = 1
cnt = 1
ans3 = 0
while True:
    if j >= n:
        break
    if t[i] == t[j]:
        cnt += 1
        j += 1
        continue
    if t[i] != t[j]:
        ans3 += cnt // 2
        cnt = 1
        i = j
        j = i + 1

tmp1 = ans2 - ans1
tmp2 = ans3 - ans2
ans = ans1 
k = k - 1
ans = ans + tmp1*(k - k//2) + tmp2*(k//2)
print(ans)

Submission status: Accepted

Solution code:

def solve():
  S=input()
  K=int(input())
  if len(S)==1:
    return K//2
  count=1
  L=[]
  for i in range(len(S)):
      try:
          if S[i] == S[i+1]:
             count = count + 1
          else:
              L.append(count)
              count = 1
      except:
          # 最後の文字セットがOutOfRangeになるので結果に出力
          L.append(count)

  if S[-1]==S[1]:
    flag=0
    if len(L)==1:
      return len(S)*K//2
    else:
      if L[-1]%2==1 and L[0]%2==1:
        flag=1
      L[0]=(L[0]+L[-1])//2*2
      L[-1]=0
    return sum([x//2 for x in L])*K-flag
  else:
    return sum([x//2 for x in L])*K

print(solve())

Submission status: Accepted

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

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

SS = S[:] + S[:]
S = list(S)
SS = list(SS)
ans0 = 0
ans1 = 0

c = 0
cnt = 1
for i in range(1, n):
    if S[i - 1] == S[i]:
        cnt += 1
        if i == n-1:
            c += cnt // 2
    else:
        if cnt >= 2:
            c += cnt // 2
        cnt = 1
ans0 = c
c = 0
cnt = 1
m = len(SS)
for i in range(1, m):
    if SS[i - 1] == SS[i]:
        cnt += 1
        if i == m - 1:
            c += cnt // 2
    else:
        if cnt >= 2:
            c += cnt // 2
        cnt = 1

ans1 = c


num = ans1 - ans0
print(ans0 + (K-1) * num)

Submission status: Accepted

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

n=len(s)
ss=s+s
nn=len(ss)
sss=ss+s
nnn=len(sss)

tmps=1
cnts=0
tmpss=1
cntss=0
tmpsss=1
cntsss=0

for i in range(n-1):
    if s[i]==s[i+1]:
        tmps+=1
    else:
        cnts+=(tmps//2)
        tmps=1
cnts+=(tmps//2)

for i in range(nn-1):
    if ss[i]==ss[i+1]:
        tmpss+=1
    else:
        cntss+=(tmpss//2)
        tmpss=1
cntss+=(tmpss//2)

for i in range(nnn-1):
    if sss[i]==sss[i+1]:
        tmpsss+=1
    else:
        cntsss+=(tmpsss//2)
        tmpsss=1
cntsss+=(tmpsss//2)

if n==1:
    if k%2==0:
        print(k//2)
    else:
        print((k-1)//2)

elif (cntsss-cntss)!=(cntss-cnts):
    print((cntsss-cntss)*((k+1)//2)+(cntss-cnts)*(k//2))

else:
    print((cntsss-cntss)*(k-1)+cnts)

Submission status: Accepted

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

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

    return r

x1=f(1)
x2=f(2)
x3 = f(3)





if s.count(s[0]) == len(s):
    ans = k // 2 * x2 + (x1 if k % 2 == 1 else 0)
else:
    ans = x1 + (x2 - x1) * (k - 1)

print(ans)

Submission status: Accepted

Solution code:
s = input()
k = int(input())
if len(s) == 1:
    print(k // 2)
else:

    def counts(s):
        count = 0
        for i in range(len(s) - 1):

            if s[i] == s[i + 1]:
                count += 1
                s = s[: i + 1] + "." + s[i + 2 :]
        return count

    res1 = counts(s)
    res2 = counts(s * 2)
    res3 = counts(s * 3)
    if res3 - res2 == res2 - res1:
        print(res1 + (res2 - res1) * (k - 1))
    else:
        if k % 2 == 1:
            print(res2 * (k // 2) + res1)
        else:
            print(res2 * (k // 2))

Submission status: Accepted

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

l = []
cnt = 1
for i in range(1, n + 1):
    if s[i - 1] != s[i]:
        l.append(cnt)
        cnt = 1
    else:
        cnt += 1
m = sum(map(lambda li: li // 2, l))
if len(l) == 1:
    ans = k * n // 2
elif s[0] == s[-2] and l[0] % 2 == l[-1] % 2 == 1:
    ans = k * m + k - 1
else:
    ans = k * m
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
s = list(input())
k = int(input())
n = len(s)
if s.count(s[0]) == n:
    print(n * k // 2)
    exit()
c = 0
t = s * 3
for i in range(n * 3 - 1):
    if t[i + 1] == 'X':
        continue
    if t[i] == t[i + 1]:
        t[i + 1] = 'X'
ans = t[0:n].count('X') + t[n:2 * n].count('X') * (k - 2) + t[2 * n:3 * n].count('X')
print(ans)

Submission status: Accepted

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


l=len(s)


if len(s)==1:
    print(k//2)
else:
    i=0
    cnt=0
    flag1=0
    while(i<l-1):
        if s[i]==s[i+1]:
            cnt+=1
            if i+1==l-1:
                flag1=1
            i+=2
        else:
            i+=1
    
    #cnt2=cnt
    
    i=1
    cnt2=1
    flag2=0
    while(i<l-1):
        if s[i]==s[i+1]:
            cnt2+=1
            if i+1==l-1:
                flag2=1
            i+=2
        else:
            i+=1
    
    #print(cnt,cnt2,flag1,flag2)
    if s[0]==s[-1]:
        if flag1==1:
            ans=cnt*k
        elif flag2==1:
            ans=(k//2+k%2)*cnt+(k//2)*cnt2
        else:
            ans=cnt+cnt2*(k-1)
    else:
        ans=cnt*k
            
    print(ans)

Submission status: Accepted

Solution code:
s=input()
n=int(input())
ss=s+s
tmp1=0
tmps=""
for i in s:
    if tmps==i:
        tmp1+=1
        tmps=""
    else:
        tmps=i
tmp2=0
tmps=""
for i in ss:
    if tmps==i:
        tmp2+=1
        tmps=""
    else:
        tmps=i
sss=s+s+s
tmp3=0
tmps=""
for i in sss:
    if tmps==i:
        tmp3+=1
        tmps=""
    else:
        tmps=i

sa1=tmp2-tmp1
sa2=tmp3-tmp2
if n%2==1:
    print(tmp1+(sa1+sa2)*(n//2))
else:
    print(tmp1+(sa1+sa2)*(n//2-1)+sa1)

Submission status: Accepted

Solution code:

S = list(input())
K = int(input())
N = len(S)

import sys

if N==1:
    print(K*N//2)
    sys.exit()

if S[0] == S[-1]:
    
    cnt = 1
    m = 0
    flag = True
    tmpcnt1 = 0
    for i in range(N-1):
        if S[i+1] == S[i]:
            cnt += 1
        else:
            if flag:
                tmpcnt1 = cnt
                flag = False
                cnt = 1
            else:
                m += cnt//2
                cnt = 1
    ans = 0
    ans += m*K
    ans += (tmpcnt1 + cnt)//2 *(K-1)
    ans += tmpcnt1//2
    ans += cnt//2
    
    if cnt == N:
        print(K*N//2)    
    else:
        print(ans)
        
else:
    cnt = 1
    m = 0
    for i in range(N-1):
        if S[i+1] == S[i]:
            cnt += 1
        else:
            m += cnt//2
            cnt = 1
    m += cnt//2
    print(m*K)

Submission status: Accepted

Solution code:
from itertools import groupby

s1=input()
k=int(input())

s2=[[h, len(list(g))] for h, g in groupby(s1)]

c=0
if len(s2)!=1:
    for i in range(len(s2)):
        if i!=0 and i!=len(s2)-1:
            c+=(s2[i][1]//2)*k

    if s2[0][0]==s2[-1][0]:
        c+=((s2[0][1]+s2[-1][1])//2)*(k-1)
        c+=(s2[0][1]//2+s2[-1][1]//2)
    else:
        c+=(s2[0][1]//2+s2[-1][1]//2)*k
else:
    c+=(s2[0][1]*k)//2

print(c)

Submission status: Accepted

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

# 全部同じ文字
before = S[0]
check = False
for s in S:
    if s != before:
        check = True

if not(check):
    x = len(S)*K
    print(x//2)

elif S[0] == S[len(S)-1]:
    before = ""
    cnt = 0
    x = 0
    head_size = 0
    tail_size = 0
    head = S[0]
    for s in S:
        if head == s:
            head_size += 1
        else:
            break

    for s in S[head_size:]:
        if before == s:
            cnt+=1
        else:
            x += (cnt//2)
            cnt = 1
            before = s
    tail_size = cnt
    x += ((head_size + tail_size)//2)
    ans = K * x

    # 先頭数えてない分
    ans += (head_size//2)

    # 最後数えすぎ分を減らす
    ans -= ((head_size + tail_size)//2)
    ans += (tail_size//2)

    print(ans)

else:
    before = ""
    cnt = 0
    x = 0
    for s in S:
        if before == s:
            cnt+=1
        else:
            x += (cnt//2)
            cnt = 1
            before = s
    x += (cnt//2)

    print((K*x))

Submission status: Accepted

Solution code:
s=input()
k=int(input())
n=len(s)
ss=s[0]
li=[]
check=0
for i in range(n-1):
    if s[i+1]==s[i]:
        check+=1
    elif s[i+1]!=s[i]:
        li.append(check)
        check=0
li.append(check)

ans=0
for i in li:
    ans+=(i+1)//2
ans*=k

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

    if aa==n-1:
        ans=(n*k)//2
    elif s[0]==s[-1]:

        a=li[0]+li[-1]+2
        ans=ans+(a//2)*(k-1)
        ans=ans-((li[0]+1)//2*(k-1))-((li[-1]+1)//2*(k-1))

   
print(ans)

Submission status: Accepted

Solution code:
import math

S = input()
K = int(input())
#print(S)
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 
#print("ans", ans)    
ans *= K
#print("ans*K",ans)

if S[0] == S[-1]:
    cntf = 1
    cnte = 1
    for i in range(1,len(S)):
        #print(S[i], S[i-1] )
        if S[i] == S[i-1]:
            cntf += 1
        else:
            break
    #print("---------------")
    for i in range(1,len(S)):
        #print(S[-i], S[-i+1] )
        if S[-i-1] == S[-i]:
            cnte += 1
        else:
            break
    #print(cnte, cntf)
    if cnte % 2 ==1 and cntf % 2 ==1:
        #print("bbbb")
        ans += (K-1)  

    if cnte == len(S):
        #print("aaa")
        ans = len(S) * K // 2
print(ans)

Submission status: Accepted

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

def count(S, K):
    S += S * (K-1)
    tmp = ''
    cnt = 0
    for i in range(len(S)):
        if S[i] == tmp:
            cnt += 1
            tmp = 'X'
        else:
            tmp = S[i]
    return cnt

cnt1 = count(S,1)
cnt21 = count(S, 2) - cnt1
cnt32 = count(S, 3) - count(S, 2)

print(cnt1 + cnt21*(K//2) + cnt32*((K-1)//2))

Submission status: Accepted

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

def main():
    S = input().strip()
    K = int(input())

    isSameChar = True
    for s in S:
        if s != S[0]:
            isSameChar = False
    if isSameChar:
        print((len(S) * K) // 2)
        exit()

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

    doubleS = S * 2
    count2 = 0
    now_count = 1
    for i in range(len(doubleS) - 1):
        if doubleS[i] == doubleS[i + 1]:
            now_count += 1
        else:
            count2 += now_count // 2
            now_count = 1
    count2 += now_count // 2

    ans = count1 + (count2 - count1) * (K - 1)
    print(ans)



    

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#import sys
#input = sys.stdin.readline
#import heapq
#from fractions import gcd
#from collections import defaultdict
#sys.setrecursionlimit(10**9)
#map(int,input().split())

def main():
    s=input()
    k=int(input())
    n=len(s)
    cnt1=0
    tmp1=0
    ss=s*2
    nn=n*2
    sss=s*3
    nnn=n*3
    for i in range(n-1):
        if s[i]==s[i+1] and tmp1==0:
            cnt1+=1
            tmp1=1
        elif s[i]==s[i+1]:
            tmp1=0
        else:
            tmp1=0
    cnt2=0
    tmp2=0
    for i in range(nn-1):
        if ss[i]==ss[i+1] and tmp2==0:
            cnt2+=1
            tmp2=1
        elif ss[i]==ss[i+1]:
            tmp2=0
        else:
            tmp2=0
    cnt3=0
    tmp3=0
    for i in range(nnn-1):
        if sss[i]==sss[i+1] and tmp3==0:
            cnt3+=1
            tmp3=1
        elif sss[i]==sss[i+1]:
            tmp3=0
        else:
            tmp3=0
    if cnt2==2*cnt1:
        print(cnt1*k)
    elif cnt3-cnt2!=cnt2-cnt1:
        if k%2==0:
            print(cnt2*(k//2))
        else:
            print(cnt2*(k//2)+cnt1)
    else:
        print(cnt1+(cnt2-cnt1)*(k-1))



if __name__ == '__main__':
    main()