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:
def main():
    S = input()
    K = int(input())
    ans = 0
    L = len(S)
    s = set([i for i in S])
    if len(s) == 1:
        print((L*K)//2)
        exit()
    cnt = 1
    for i in range(L):
        if i < L-1 and S[i] == S[i+1]:
            cnt += 1
        else:
            ans += cnt//2
            cnt = 1
    ans *= K
    if S[0] == S[-1]:
        t = S[0]
        i = 0
        a = 0
        b = 0
        while i < L and S[i] == t:
            a += 1
            i += 1

        i = L-1
        while i >= 0 and S[i] == t:
            b += 1
            i -= 1

        tmp = a//2+b//2-(a+b)//2
        tmp *= (K-1)
        ans -= tmp
    print(ans)
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#!python3

iim = lambda: map(int, input().rstrip().split())

def resolve():
    S = input()
    K = int(input())

    ls = len(S)

    if ls == 1:
        print(K//2)
        return

    if ls == 2:
        print(0 if S[0] != S[1] else K)
        return

    if S[0] == S[-1]:
        c0 = S[0]
        for i in range(1, ls):
            if c0 != S[i]:
                break
        else:
            print(ls*K//2)
            return
        i0 = i
    else:
        i = i0 = 0

    ans = 0
    c0 , cn = "", 0
    for i in range(i, ls):
        c1 = S[i]
        if c1 == c0:
            cn += 1
        else:
            if cn > 1:
                ans += cn // 2
            cn = 1
            c0 = c1

    if i0 == 0:
        if cn > 1:
            ans += cn // 2
        ans *= K
    else:
        ans = ans * K + ((i0+cn)//2) * (K-1) + i0//2 + cn//2


    print(ans)

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
s = input()
k = int(input())

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

count = []
tmp = 0
for i in range(len(s) - 1):
    if s[i] == s[i + 1]:
        tmp += 1
    if s[i] != s[i + 1]:
        count.append(tmp + 1)
        tmp = 0
count.append(tmp + 1)
#print(count)

flag = True
ans = 0
if s[0] == s[-1] and (count[-1] > 1 or count[0] > 1) and (count[-1] + count[0]) % 2 == 0:
    flag = False
if s[0] == s[-1]:
    if count[-1] == 1 and count[0] == 1:
        ans += k - 1
        flag = False
    else:
        ans += k - 1
        if count[-1] >= count[0]:
            count[-1] -= 1
        else:
            count[0] -= 1
#print(flag)
#print(ans)
#print(count)
for i in range(len(count)):
    if count[i] > 1:
        ans += (count[i] // 2) * k
if s[0] == s[-1] and count[-1] >= 1 and flag:
    #print("B")
    ans += 1
print(ans)

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7

s = STR()
k = INT()
ans = 0
a = 0
b = 0
if s[0] == s[-1]:
    if len(set(s)) == 1:
        ans = len(s) * k // 2
        print(ans)
        exit()
    for j in range(len(s)):
        if s[0] == s[j]:
            a += 1
        else:
            break
    for j in reversed(range(len(s))):
        if s[-1] == s[j]:
            b += 1
        else:
            break
    ans = a // 2 + b // 2 + (a + b) // 2 * (k - 1)
i = a
ans2 = 0
while i < len(s) - b:
    tmp = 1
    for j in range(i + 1, len(s)):
        if s[i] == s[j]:
            tmp += 1
        else:
            break
    i += tmp
    tmp //= 2
    ans2 += tmp
print(ans + ans2 * k)

Submission status: Accepted

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

if len(set(S))==1:
  ans = int(len(S)*K/2)
elif K == 1:
  ans = 0
  serial = 1
  #print(serial)
  for i in range(len(S)-1):
    if S[i] == S[i+1]:
      serial += 1
    else:
      ans += int(serial/2)
      serial = 1
  ans += int(serial/2)
else:
  #first
  first=0
  serial = 1
  for i in range(len(S)-1):
    if S[i] == S[i+1]:
      serial += 1
    else:
      first += int(serial/2)
      serial = 1
  initial = 1
  for i in range(len(S)-1,1,-1):
    if S[i]==S[i-1]:
      initial += 1
    else:
      break
  cnt = 0
  serial = initial
  #print(serial)
  for i in range(len(S)):
    if S[i] == S[i-1]:
      serial += 1
    else:
      cnt += int(serial/2)
      serial = 1
    #print(cnt)
  #last
      
  #print(cnt)
  last = 0
  serial = initial
  for i in range(len(S)):
    if S[i] == S[i-1]:
      serial += 1
    else:
      last += int(serial/2)
      serial = 1
  last += int(serial/2)
  #print(first,cnt,last)
  ans = first+(K-2)*cnt+last

print(ans)

Submission status: Accepted

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

def splitSeries(s:str):
    result = []
    last = ""
    count = 0
    for c in s:
        if last == c:
            count+=1
        else:
            if last != "":
                result.append((last,count))
            last = c
            count = 1
    if last != "":
        result.append((last,count))
    return result



def solve(S: str, K: int):
    if len(S) == 1:
        print(K//2)
    else:
        count = 0
        series = splitSeries(S)
        sp = 0
        if len(series) > 1:
            for s in series:
                if s[1] > 1:
                    sp += s[1]//2
            count += sp * K
            if series[0][0] == series[-1][0]:
                count -= (K-1)*(series[0][1]//2 + series[-1][1]//2 - (series[0][1] + series[-1][1])//2)
            print(count)
        else:
            print(series[0][1]*K//2)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    S = next(tokens)  # type: str
    K = int(next(tokens))  # type: int
    solve(S, K)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

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

# 全部同じ文字の場合。Sが1文字の場合もここ
if len(set(S)) == 1:
    print((N*K) // 2)
    exit()

# Sの両端の文字が異なるので、Sの中の文字を置き換える回数 * K の場合
if S[0] != S[-1]:
    change = 0
    cnt = 1
    for i in range(1,N):
        if S[i] != S[i-1]:
            change += cnt // 2
            cnt = 1
        else:
            cnt += 1


    if cnt != 1:
        change += cnt // 2

    ans = change * K
    print(ans)
# 両端の文字が同じなので、Kこ並べた時の結合部分について考えなければならない場合
else:
    
    head = 1
    tail = 1
    # 先頭の重複を調べる
    for i in range(1,N):
        if S[i] == S[i-1]:
            head += 1
        else:
            break
    # 後ろの重複を調べる
    for i in range(N-2,-1,-1):
        if S[i] == S[i+1]:
            tail += 1
        else:
            break
    change = 0
    cnt = 1
    # 間の部分の変更するべき個数を調べる
    for i in range(head+1, N-tail+1):
        #print("--", i, S[i], S[i-1])
        if S[i] == S[i-1]:
            cnt += 1
        else:
            change += cnt // 2
            cnt = 1
    
    if cnt != 1:
        change += cnt // 2

        

    ans = ((head + tail) // 2) * (K - 1) + head // 2 + tail // 2 + change * K
    #print(head, tail, change)
    print(ans)

Submission status: Accepted

Solution code:
def main():
    S = input()
    K = int(input())
    l = []
    t = 1
    m = S[0]
    if len(S) == 1:
        print(K//2)
        return
    if len(S) == 2:
        if S[0] == S[1]:
            print(K)
            return
        else:
            print(0)
            return
    if len(S) == 3:
        if S[0] == S[1] == S[2]:
            print(3*K//2)
            return
        elif S[0] == S[1] or S[1] == S[2]:
            print(K)
            return
        elif S[0] == S[2]:
            print(K-1)
            return
        else:
            print(0)
            return
    for v in S[1:]:
        if v == m:
            t += 1
        elif t != 1:
            l.append(t)
            t = 1
        m = v
    if t != 1:
        l.append(t)
    ls = 0
    lsm = 0
    lsu = 0
    if S[0] == S[-1]:
        if len(l) == 1 and l[0] == len(S):
            print(l[0] * K//2)
            return
        elif len(l) == 1:
            print(((l[0] + 1)//2) *(K-1) + (l[0] // 2))
            return
        if S[0] == S[1] == S[-2]:
            ls = l[-1] + l[0]
            lsm = l[0]
            lsu = l[-1]
            l = l[1:-1]
        elif S[-1] == S[-2]:
            ls = l[-1] + 1
            lsu = l[-1]
            l = l[:-1]
        elif S[0] == S[1]:
            ls = l[0] + 1
            lsm = l[0]
            l = l[1:]
        else:
            ls = 2
    r = 0
    for i in l:
        r += i // 2
    r *= K
    r += (K-1) * (ls // 2)
    r += (lsm  // 2) + (lsu // 2)
    print(r)
main()

Submission status: Accepted

Solution code:
def main():
    S = [i for i in input()]
    O = S[:]
    K = int(input())
    if len(S) == 1:
        return K // 2
    r = 0
    for i in range(1, len(S)):
        if S[i] == S[i - 1]:
            r += 1
            S[i] = S[i].upper()
    if S[-1] != S[0]:
        return r * K

    S = S + O
    p = 0
    for i in range(len(O), len(S)):
        if S[i] == S[i - 1]:
            p += 1
            S[i] = S[i].upper()
    if S[-1] == S[0]:
        return r + p * (K - 1)
    return (r + p) * (K // 2) + r * (K % 2)
   

print(main())

Submission status: Accepted

Solution code:
# Author: cr4zjh0bp
# Created: Sat Mar 14 20:41:10 UTC 2020
import sys
 
stdin = sys.stdin
inf = 1 << 60
mod = 1000000007
 
ni = lambda: int(ns())
nin = lambda y: [ni() for _ in range(y)]
na = lambda: list(map(int, stdin.readline().split()))
nan = lambda y: [na() for _ in range(y)]
nf = lambda: float(ns())
nfn = lambda y: [nf() for _ in range(y)]
nfa = lambda: list(map(float, stdin.readline().split()))
nfan = lambda y: [nfa() for _ in range(y)]
ns = lambda: stdin.readline().rstrip()
nsn = lambda y: [ns() for _ in range(y)]
ncl = lambda y: [list(ns()) for _ in range(y)]
nas = lambda: stdin.readline().split()

from collections import Counter

s = ns()
n = len(s)
s = s + "$"
k = ni()

c = []
cnt = 1
for i in range(len(s) - 1):
    if s[i] == s[i + 1]:
        cnt += 1
    elif cnt > 1:
        c.append(cnt)
        cnt = 1

if (len(c) == 1 and c[0] == n) or n == 1:
    print(n * k // 2)
elif s[0] != s[-2]:
    print(sum([x // 2 for x in c[:]]) * k)
else:
    a, b = 1, 1
    if s[0] == s[1]:
        a = c[0]
    if s[-2] == s[-3]:
        b = c[-1]
    print(sum([x // 2 for x in c[:]]) * k - (a // 2 + b // 2 - (a + b) // 2) * (k - 1))

Submission status: Accepted

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

def count(s):
    """何文字変更が必要か
    """
    a = []
    b = ''
    c = 1
    for si in s:
        if b == si:
            c += 1
        else:
            a.append(c)
            c = 1
        b = si
    a.append(c)

    r = 0
    for ai in a:
        r += ai // 2
    # print(s, a, r)
    return r

if K == 1:
    ans = count(S)
elif len(S) == 1:
    ans = K // 2
elif len(set(S)) == 1:
    ans = len(S) * K // 2
else:
    a1 = count(S*2)
    a2 = count(S*3)
    ans = a1 + (a2-a1) * (K-2)

print(ans)

# for i in range(1, 10):
#     print(i, S*i, count(S*i))

Submission status: Accepted

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

def count(s):
    """何文字変更が必要か
    """
    a = []
    b = ''
    c = 1
    for si in s:
        if b == si:
            c += 1
        else:
            a.append(c)
            c = 1
        b = si
    a.append(c)

    r = 0
    for ai in a:
        r += ai // 2
    # print(s, a, r)
    return r

if K == 1:
    ans = count(S)
elif len(S) == 1:
    ans = K // 2
elif len(set(S)) == 1:
    c = count(S*2)
    if K % 2 == 0:
        ans = c * (K//2)
    else:
        diff = count(S*3) - count(S*2)
        ans = c * (K//2) + diff
else:
    a1 = count(S*2)
    a2 = count(S*3)
    ans = a1 + (a2-a1) * (K-2)

print(ans)

# for i in range(1, 10):
#     print(i, S*i, count(S*i))

Submission status: Accepted

Solution code:
S=input()
K=int(input())
h=1
b=0
l=[]
f=False
c1=0
c2=0
c3=0
c4=0
r=[]
v=[]
vf=False
vf1=False
vf2=False
for i in range(len(S)-1):
    if S[i]==S[i+1]:
        if i==0:
            c1=1
            c3=1
        if (i+1)==(len(S)-1):
            c2=1
            c4=1
        h+=1
        f=True
    else:
        f=False
    if not f:
        if h!=1:
            l.append(h)
            if c3==1:
                r.append(h)
                c3=0
            if c4==1:
                r.append(h)
                c4=0
        h=1
#print(l)
if f:
    l.append(h)
    #print(l)
    if c4==1:
        r.append(h)
        c4=0
#print(l)
if S[0]==S[-1]:
    #print(l)
    if c1==1 and c2==1:
        if len(l)==1:
            s=len(S)*K
            print(s//2)
            exit()
        else:
            v.append(l[0])
            v.append(l[-1])
            l[-1]+=l[0]
            del l[0]
            vf=True
    elif c1==1:
        #l[0]+=1
        v.append(l[0])
        #v.append(l[-1])
        l[0]+=1
        v.append(l[0])
        del l[0]
        vf1=True
    elif c2==1:
        #l[-1]+=1
        #v.append(l[0])
        v.append(l[-1])
        l[-1]+=1
        v.append(l[-1])
        del l[-1]
        vf2=True
    else:
        if len(S)==1:
            print(K//2)
            exit()
        print(K-1)
        exit()
h=0
if vf:
    h+=v[0]//2
    h+=v[-1]//2
    for i in range(len(l)):
        if i==(len(l)-1):
            h+=(l[i]//2)*(K-1)
        else:
            h+=(l[i]//2)*K
elif vf1:
    h+=(v[1]//2)*(K-1)
    h+=(v[0]//2)
    for i in range(len(l)):
        h+=(l[i]//2)*K
elif vf2:
    h+=(v[1]//2)*(K-1)
    h+=(v[0]//2)
    for i in range(len(l)):
        h+=(l[i]//2)*K
else:
    for i in range(len(l)):
        h+=(l[i]//2)*K

#print(l)
print(h)

Submission status: Accepted

Solution code:
s=input()
n=len(s)
k=int(input())
if k==1:
  if n==1:
    print(0)
  else:
    i=1
    cnt=0
    while i<n:
      if s[i-1]==s[i]:
        cnt+=1
        i+=1
      i+=1
    print(cnt)
else:
  single=True
  for c in s:
    if c!=s[0]:
      single=False
      break
  if single:
    print(n*k//2)
  elif s[0]!=s[-1]:
    i=1
    cnt=0
    while i<n:
      if s[i-1]==s[i]:
        cnt+=1
        i+=1
      i+=1
    print(cnt*k)
  else:
    i=1
    while s[i]==s[0]:
      i+=1
    kk=i
    j=-1
    while s[j]==s[0]:
      j-=1
    num=i-j-1
    cnt=0
    i+=1
    while i<=n+j:
      if s[i-1]==s[i]:
        cnt+=1
        i+=1
      i+=1
    print(cnt*k+num//2*(k-1)+kk//2+(-j-1)//2)

Submission status: Accepted

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

if len(set(S)) == 1:
  print(len(S)*K//2)
  exit()
  
if S[0] != S[-1]:
  cnt = 1
  s = S[0]
  l = []
  for i in range(1,len(S)):
    if S[i] == s:
      cnt += 1
    else:
      l.append(cnt//2)
      cnt = 1
      s = S[i]
  l.append(cnt//2)
  print(sum(l)*K)
else:
  cnt = 1
  s = S[0]
  l = []
  for i in range(1, len(S)):    
    if S[i] == s:
      cnt += 1
    else:
      l.append(cnt)
      cnt = 1
      s = S[i]
  l.append(cnt)
  num = l[0]+l[-1]
  ans = (num//2)*(K-1)
  for i in range(len(l)):
    if i == 0 or i == len(l)-1:
      if l[i] > 1:
        ans += l[i]//2
    else:
      ans += (l[i]//2)*K
  print(ans)

Submission status: Accepted

Solution code:
def runlen(a):
    n = len(a)
    result = []
    count = 1
    for i in range(n):
        if i == n - 1 or a[i] != a[i + 1]:
            result.append((count, a[i]))
            count = 1
        else:
            count += 1
    return result

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

rl = runlen(S)

if len(rl) == 1:
    result = (len(S) * K) // 2
else:
    result = rl[0][0] // 2 + rl[-1][0] // 2 + K * sum(map(lambda i: rl[i][0] // 2, range(1, len(rl) - 1)))
    if rl[0][1] == rl[-1][1]:
        result += (K - 1) * ((rl[0][0] + rl[-1][0]) // 2)
    else:
        result += (K - 1) * (rl[0][0] // 2 + rl[-1][0] // 2)

print(result)

Submission status: Accepted

Solution code:
S=input()
K=int(input())
if len(S)==1:
  print(K//2)
  exit()
if len(S)==2:
  if S[0]==S[1]:
    print(K)
  else:
    print(0)
  exit()
ans=0
# 内部
cnt=1
fro=0
flag=1
for i in range(len(S)-1):
  if S[i+1]==S[i]:
    cnt+=1
  else:
    if flag: 
      fro=cnt
      flag=0
    ans+=cnt//2
    cnt=1
ans+=cnt//2
ans*=K
# print(ans)
# 接続部
if fro==0:
  print((K*cnt)//2)
  exit()
if S[0]==S[-1]:
  if fro%2==1 and cnt%2==1:
    ans+=K-1
print(ans)

Submission status: Accepted

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

    if s == s[0]*n:
        print( n*k//2 )
        return 0

    cnt, t = 0, 1
    for key, value in enumerate(s):
        if key == 0: continue
        if value == s[key-1]:
            t += 1
        else:
            cnt += t // 2
            t = 1
    cnt += t//2
    left, right = 0, len(s)-1
    a, b = 0, 0
    while s[left] == s[0]:
        left += 1
        a += 1
    while s[right] == s[-1]:
        right -= 1
        b += 1

    ans = (cnt*k)
    if s[0] != s[-1]:
        print(ans)
    else:
        ans -= (a//2 + b//2)*(k-1)
        ans += (a+b)//2 * (k-1)
        print(ans)

main()

Submission status: Accepted

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

ans = 0

count = 1
past = ''

for i in s:
    if i == past:
        count += 1
    else:
        ans += count // 2
        count = 1
    past = i

if count == len(s):
    print((len(s)*n)//2)
    exit()

ans += count // 2
ans *= n

if s[0] == s[-1]:
    past = s[0]
    idx = 1
    begin = 1

    while idx < len(s) and past == s[idx] :
        begin += 1
        idx += 1
    past = s[-1]
    idx = -1
    end = 1

    while idx > 0 and past == s[idx] :
        end += 1
        idx -= 1

    if begin %2 != 0 and count %2 != 0:
        ans += n - 1

print(ans)

Submission status: Accepted

Solution code:
# 人の解答
s=input()
k=int(input())

# 1文字だけからなる場合
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:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)
 
S = readline().rstrip().decode('utf-8')
K = int(read())
 
# どうせ2つしか状態数がない。十分大きなKで、周期2の等差数列だろう
 
def F(S):
    arr = list(S)
    prev = 'A' # 衝突回避
    cnt = 0
    for x in S:
        if x == prev:
            cnt += 1
            prev = 'A'
        else:
            prev = x
    return cnt
 
arr = [F(S*i) for i in range(10)]
 
if K < 10:
    answer = arr[K]
else:
    if K&1:
        d = arr[9] - arr[7]
        answer = arr[9] + d * ((K-9)//2)
    else:
        d = arr[8] - arr[6]
        answer = arr[8] + d * ((K-8)//2)
print(answer)

Submission status: Accepted

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

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

elif s[0] != s[-1]:
    cnt = 0
    for i in range(1, len(s)):
        if s[i] == s[i-1]:
            s = s[:i]+'.'+s[i+1:]
    print(s.count('.')*k)
else:
    a,b = 0,0
    c = s[0]
    rev_s = s[::-1]
    for i in range(len(s)):
        if s[i]==c:
            a += 1
        else:
            break
    for i in range(len(s)):
        if rev_s[i]==c:
            b += 1
        else:
            break
    cnt = 0
    for i in range(1, len(s)):
        if s[i] == s[i-1]:
            s = s[:i]+'.'+s[i+1:]
    print(s.count('.')*k - (a//2 + b//2 - (a+b)//2)*(k-1))

Submission status: Accepted

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

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

count_list.append(count)

if len(count_list) == 1:
    print((len(s) * repeat_num ) // 2)
else:
    if s[0] != s[-1]:
        single = sum(list(map(lambda x: x//2, count_list)))
        print(single * repeat_num)
    else:
        # s[0] == s[-1] 繰り返しによってつながる
        single = sum(list(map(lambda x: x//2, count_list[1:-1])))
        print(single * repeat_num
            + (count_list[0]) // 2
            + (count_list[-1]) // 2
            + (count_list[0] + count_list[-1]) // 2 * (repeat_num - 1))

Submission status: Accepted

Solution code:
def RanLengthEncoding(S):
    N = len(S)
    RLE = []
    now_char = S[0]
    now_cnt = 1
    for i in range(1, N):
        if now_char == S[i]:
            now_cnt += 1
        else:
            RLE.append([now_char, now_cnt])
            now_char = S[i]
            now_cnt = 1
    RLE.append([now_char, now_cnt])
    return RLE

S = input()
K = int(input())
rle = RanLengthEncoding(S)
if len(rle) == 1:
    ans = rle[0][1] * K // 2
    print(ans)
    exit()
cnt = 0
for i in range(1, len(rle)-1):
    cnt += rle[i][1] // 2

if rle[0][0] != rle[-1][0]:
    cnt += rle[0][1] // 2
    cnt += rle[-1][1] // 2
    ans = cnt * K
else:
    tmp = (rle[0][1] + rle[-1][1]) // 2
    ans = cnt * K
    ans += tmp * (K-1)
    ans += rle[0][1] // 2
    ans += rle[-1][1] // 2
print(ans)

Submission status: Accepted

Solution code:
from math import floor

def solve(s, k):
    counts = [0] * len(s)
    prev = ''
    index = -1
    for l in s:
        if prev == l:
            counts[index] += 1
        else:
            index += 1
            counts[index] = 1
        prev = l
    if index == 0:
        return floor(len(s) * k / 2)
    total = sum(floor(counts[i] / 2) * k for i in range(index + 1) if counts[i] > 1)
    if s[0] == s[-1] and counts[0] % 2 == 1 and counts[index] % 2 == 1:
        total += k - 1
    return total

_s = input()
_k = int(input())
print(solve(_s, _k))

Submission status: Accepted

Solution code:
from math import floor

def solve(s, k):
    counts = [0] * len(s)
    prev = ''
    index = -1
    for l in s:
        if prev == l:
            counts[index] += 1
        else:
            index += 1
            counts[index] = 1
        prev = l
    if index == 0:
        return floor(len(s) * k / 2)
    total = 0
    for i in range(index + 1):
        if counts[i] > 1:
            total += floor(counts[i] / 2) * k
    if s[0] == s[-1] and counts[0] % 2 == 1 and counts[index] % 2 == 1:
        if (counts[0] + counts[index]) % 2 == 0:
            total += k - 1
    return total

_s = input()
_k = int(input())
print(solve(_s, _k))

Submission status: Accepted

Solution code:
from collections import Counter
s = input()
N = len(s)
K = int(input())
if s[0] != s[-1]:
    count = 1
    ans = 0
    now = s[0]
    for i in range(1,len(s)):
        if now == s[i]:
            count += 1
        else:
            now = s[i]
            ans += count//2
            count = 1
    if count != 1:
        ans += count//2
    print(ans*K)
elif len(Counter(s).values()) == 1:
    print(K*len(s)//2)
else:
    ans = 0
    fcount = 1
    bcount = 1
    now = s[0]
    start = N
    for i in range(1,N):
        if s[i] == now:
            fcount += 1
        else:
            start = i
            break
    now = s[-1]
    end= 0
    for i in range(N-2,-1,-1):
        if s[i] == now:
            bcount += 1
        else:
            end = i
            break   
    ans = fcount//2 + bcount//2 + ((fcount+bcount)//2)*(K-1)
    now = s[start]
    count = 1
    mcount = 0
    for i in range(start+1,end+1):
        if s[i] == now:
            count += 1
        else:
            mcount += count//2
            count = 1
            now = s[i]
    if count != 1:
        mcount += count//2
    print(ans + mcount*K)

Submission status: Accepted

Solution code:
s = input()
k = int(input())
if s[0] != s[len(s)-1]:
  count = 0
  cur = '*'
  for i in range(len(s)):
    if cur == s[i]:
      count += 1
      cur = '*'
    else:
      cur = s[i]
  print(count*k)
else:
  flag = True
  for i in range(len(s)-1):
    if s[i] != s[i+1]:
      flag = False
      break
  if flag:
    print(len(s)*k//2)
  else:
    ans = 0
    t = s[0]
    mae = 0
    ushiro = len(s)-1
    for i in range(len(s)-2,-1,-1):
      if s[i] == t:
        ushiro = i
      else:
        break
    for j in range(1,len(s)):
      if s[j] == t:
        mae = j
      else:
        break
    count = 0
    cur = s[mae]
    for i in range(mae+1,ushiro):
      if cur == s[i]:
        count += 1
        cur = '*'
      else:
        cur = s[i]
    ans = count*k
    ans += ((len(s)-ushiro+mae+1)//2)*(k-1)+(mae+1)//2+(len(s)-ushiro)//2
    print(ans)

Submission status: Accepted

Solution code:
def solve(t):
    same=0
    count=0
    for i in range(len(t)):
        same+=1
        if i==len(t)-1 or t[i]!=t[i+1]:
            count+=same//2
            same=0
    return count

s=input()
k=int(input())
f=True
for i in range(len(s)-1):
    if s[i]!=s[i+1]:
        f=False
if f==True:
    print(len(s)*k//2)
else:
    st=s+s;
    scount=solve(s)
    stcount=solve(st)
    print(scount+(stcount-scount)*(k-1))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def roundUp(a, b):
        return -(-a // b)

    @staticmethod
    def toUpperMultiple(a, x):
        return Math.roundUp(a, x) * x

    @staticmethod
    def toLowerMultiple(a, x):
        return (a // x) * x

    @staticmethod
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

    @staticmethod
    def sign(n):
        if n == 0:
            return 0
        if n < 0:
            return -1
        return 1

    @staticmethod
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


class PriorityQueue:
    def __init__(self, l=[]):
        self.__q = l
        heapq.heapify(self.__q)
        return

    def push(self, n):
        heapq.heappush(self.__q, n)
        return

    def pop(self):
        return heapq.heappop(self.__q)


MOD = int(1e09) + 7
INF = int(1e15)


def calc(N):
    return sum(int(x) for x in str(N))


def main():
    # sys.stdin = open("sample.txt")
    S = Scanner.string()
    K = Scanner.int()
    if len(Counter(S)) == 1:
        print(len(S) * K // 2)
        return
    ans = 0
    if S[0] == S[-1]:
        c1 = 1
        for i in range(1, len(S)):
            if S[i] == S[0]:
                c1 += 1
            else:
                break
        c2 = 1
        for j in reversed(range(len(S)-1)):
            if S[j] == S[-1]:
                c2 += 1
            else:
                break
        ans = c1 // 2
        ans += c2 // 2
        ans += (c1 + c2) // 2 * (K-1)
        c3 = 0
        c = ""
        for k in range(i, j + 1):
            if S[k] == c:
                c3 += 1
            else:
                ans += c3 // 2 * K
                c = S[k]
                c3 = 1
        ans += c3 // 2 * K
        print(ans)
        return
    else:
        c = "0"
        cnt = 0
        for i in range(len(S)):
            if c == S[i]:
                cnt += 1
            else:
                ans += cnt // 2
                c = S[i]
                cnt = 1
        ans += cnt // 2
        print(ans * K)
    return


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
s = input()
k = int(input())
import math
if len(set(list(s))) == 1:
    print(len(s) * k // 2)
    exit()
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()

l_length=1
last=s[0]
for i in range(1,len(s)):
    if s[i] == last:
        l_length+=1
    else:
        break
r_length=1
last=s[-1]
for i in range(len(s)-2,-1,-1):
    if s[i] == last:
        r_length+=1
    else:
        break

if l_length%2==1 and r_length%2==1:
    print(ans+k-1)
    exit()
print(ans)

Submission status: Accepted

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

if len(set(list(s))) == 1:
    print(len(s) * k // 2)
    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()
    
    
l_length=1
last=s[0]
for i in range(1,len(s)):
    if s[i] == last:
        l_length+=1
    else:
        break
r_length=1
last=s[-1]
for i in range(len(s)-2,-1,-1):
    if s[i] == last:
        r_length+=1
    else:
        break

if l_length%2==1 and r_length%2==1:
    print(ans+k-1)
    exit()

print(ans)