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:
#!/usr/bin/env python3
s = input()
n = len(s)
k = int(input())
if s.count(s[0]) == n:
    print(n * k // 2)
    exit()
# pre,post を数えた上で前から連続しているやつの長さを数えて和をとる
ans = 0  # k倍する前をおく
cnt = 1
pre, post = 0, 0
pre_bit = 1
for i in range(n - 1):
    if s[i] == s[i + 1]:
        cnt += 1
    else:
        ans += cnt // 2
        if pre_bit:
            pre = cnt
            pre_bit = 0
        cnt = 1

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
s=list(input())
n=len(s)
k=int(input())
count=[]
i=0
while i<=n-1:
    j=0
    while i+j+1<=n-1 and s[i+j]==s[i+j+1]:
        j+=1
    count.append(j+1)
    i+=(j+1)

length=len(count)
if length==1:
    print((n*k)//2)
   
else:
    if s[0]==s[-1]:
        ans=0
        for l in range(length):
            if l!=0 and l!=length-1:
                ans+=(count[l]//2)*k
        ans+=(count[0]//2+count[-1]//2)
        ans+=((count[0]+count[-1])//2)*(k-1)
        print(ans)
    else:
        ans=0
        for m in count:
            ans+=(m//2)*k
        print(ans)

Submission status: Accepted

Solution code:
import copy
s = list(input())
n = len(s)
s2 = copy.deepcopy(s)
la = s[-1]
for i in range(n):
    if s[i] == la:
        s2.append(la)
    else:
        break

n2 = len(s2)
k = int(input())
if n2 == 2*n:
    print(n*k//2)
    exit()
count = 0
for i in range(n2-n+1,n2):
    if s2[i] == s2[i-1]:
        s2[i] = "1"
        count += 1
ans = k*count

c = 0

for i in range(n-1,-1,-1):
    if la == s[i]:
        c += 1
    else:
        break

if n2 > n and (n2-n)%2 and c > 1 and c%2:
    ans -= 1
print(ans)

Submission status: Accepted

Solution code:
def solve(S):
    pre = S[0]
    flag = False
    ans = 0
    cnt = 0
    for s in S[1:]:
        if flag:
            if pre == s:
                cnt += 1
            else:
                ans += cnt // 2
                cnt = 0
                flag = False
        else:
            if pre == s:
                cnt += 2
                flag = True
        pre = s
    if flag:
        ans += cnt // 2
    return ans


S = input()
K = int(input())
if len(S) == 1:
    print(K // 2)
elif len(S) == S.count(S[0]):
    print((len(S)*K)//2)
else:
    if K == 1:
        print(solve(S))
    elif K == 2:
        print(solve(S + S))
    else:
        ans2 = solve(S + S)
        ans3 = solve(S + S + S)
        print(ans2 + (ans3-ans2)*(K - 2))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

if list(S).count(S[0])==len(S):
    print(len(S)*K//2)

else:
    S_count=0
    i=0
    while i<len(S)-1:
        if S[i]==S[i+1]:
            S_count+=1
            i+=1
        i+=1

    if S[0]==S[-1]:
        a=1
        i=0
        while i<len(S)-1:
            if S[i]==S[i+1]:
                a+=1
                i+=1
            else:
                break
        b=1
        i=len(S)-1
        while i>0:
            if S[i]==S[i-1]:
                b+=1
                i+=-1
            else:
                break

        S_count*=K
        S_count-=(K-1)*(a//2 + b//2 - (a+b)//2)

    else:
        S_count*=K
    print(S_count)

Submission status: Accepted

Solution code:
S = input().strip()
K = int(input())
if K==1 or S[0]!=S[-1]:
    x = list(S)
    cnt = 0
    for i in range(1,len(x)):
        if x[i]==x[i-1]:
            x[i]=0
            cnt += 1
    print(cnt*K)
else:
    x = list(S+S)
    cnt = 0
    for i in range(1,len(x)):
        if x[i]==x[i-1]:
            x[i]=0
            cnt += 1
    k1 = len(x)
    x = x+list(S)
    d1 = 0
    for i in range(k1,len(x)):
        if x[i]==x[i-1]:
            x[i]=0
            d1 += 1
    k2 = len(x)
    x += list(S)
    d2 = d1
    for i in range(k2,len(x)):
        if x[i]==x[i-1]:
            x[i]=0
            d2 += 1
    if K==2:
        print(cnt)
    elif K%2==0:
        print(cnt+d2*(K//2-1))
    else:
        print(cnt+d2*(K//2-1)+d1)

Submission status: Accepted

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

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

def n_change(sss):
    s = sss.copy()
    x = 0
    for i in range(1, len(s)):
        if s[i] == s[i-1]:
            s[i] = "X"
            x += 1
    return x

print(n_change(s) + (k-1) * (n_change(2*s) - n_change(s)))

Submission status: Accepted

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

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

for i in range(len(S)-1):
    if S[i]!=S[i+1]:
        rle.append((S[i], cnt))
        cnt = 1
    else:
        cnt += 1

rle.append((S[-1], cnt))
#print(rle)

if rle[0][0]==rle[-1][0]:
    ans = rle[0][1]//2+rle[-1][1]//2
    
    for i in range(1, len(rle)-1):
        ans += rle[i][1]//2*K
    
    ans += (rle[0][1]+rle[-1][1])//2*(K-1)
else:
    ans = 0
    
    for i in range(len(rle)):
        ans += rle[i][1]//2*K
    
print(ans)

Submission status: Accepted

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

ss = []
seq = 1
for a,b in zip(S,S[1:]):
    if a==b:
        seq += 1
    else:
        ss.append(seq)
        seq = 1
ss.append(seq)

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

if S[0] != S[-1]:
    ans = sum([v//2 for v in ss]) * K
    print(ans)
else:
    ans = sum([v//2 for v in ss[1:-1]]) * K
    ans += (ss[0]+ss[-1])//2 * (K-1)
    ans += ss[0]//2 + ss[-1]//2
    print(ans)

Submission status: Accepted

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

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

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

if len(rep) == 1:
    print(rep[0]*k//2)
    exit()

cnt = 0
for r in rep:
    cnt += r//2

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

Submission status: Accepted

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

N = len(S)
p = S[0]
c = 0

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

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

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

ans = c*K
if S[0] == S[-1]:
    p = S[0]
    c = 1
    for i in range(1,N):
        if p == S[i]:
            c += 1
        else:
            break

    d = 1
    for i in range(2,N+1):
        if p == S[-i]:
            d += 1
        else:
            break

    if (c%2)*(d%2) == 1:
        ans += K-1

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
s = input()
k = int(input())

# 全部同じ
C = Counter(s)
if len(C) == 1:
    print(len(s) * k // 2)
    exit()

# 両端が異なる
if s[0] != s[-1]:
    aida = 0
    cnt = 0
    for i in range(1,len(s)):
        if s[i] == s[i-1]:
            cnt += 1
        else:
            tmp = cnt // 2
            if cnt % 2 != 0:
                aida += tmp + 1
            else:
                aida += tmp
            cnt = 0
    tmp = cnt // 2
    if cnt % 2 != 0:
        aida += tmp + 1
    else:
        aida += tmp


    print(aida * k)

# 両端が同じ
else:
    # 先端の個数
    cnt = 1
    for i in range(1,len(s)):
        if s[i] == s[i-1]:
            cnt += 1
        else:
            break
    # 末端の個数
    cnt2 = 1
    for i in range(len(s)-1,-1,-1):
        if s[i] == s[i-1]:
            cnt2 += 1
        else:
            break

    # 先端で書き換える個数
    syoko = cnt // 2

    # 末端＋先端で書き換える個数
    sotogawa = (cnt + cnt2) // 2

    # 先端と末端の間で書き換える個数
    aida = 0
    cnt3 = 0
    #print(s[cnt : len(s)-cnt2])
    for i in range(cnt+1,len(s)-cnt2+1):
        if s[i] == s[i-1]:
            cnt3 += 1
        else:
            tmp = cnt3 // 2
            if cnt3 % 2 != 0:
                aida += tmp + 1
            else:
                aida += tmp
            cnt3 = 0

    saikuru = aida + sotogawa
    print(syoko + saikuru * k - saikuru + aida + cnt2//2)

Submission status: Accepted

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

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

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

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

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

Submission status: Accepted

Solution code:
S = input()
K = int(input())
now,cnt,num = S[0],1,0
for i in S[1:-1]:
    if i == now:
        cnt += 1
    else:
        num += cnt//2
        cnt = 1
        now = i
if S[-1] == now:
    num += (cnt+1)//2
else: num += cnt//2

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

length = len(S)//2
if len(set(S)) == 1 and length%2 == 1:
    print(length//2*K + K//2)
    exit()

if num2 - num*2 == 1:
    ans = K - 1 + num*K
else:
    ans = num*K
print(ans)

Submission status: Accepted

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

N = len(S)
#全て同じ文字の時
if len(set(S)) == 1:
    print(N*K//2)
    exit()

#隣合う文字列が何個等しいか
cycle = 0
tmp = S[0]
cnt = 1
for i in range(1, N):
    if tmp != S[i]:
        cycle += cnt//2
        tmp = S[i]
        cnt = 1
    else:
        cnt += 1
cycle += cnt//2

if S[0] == S[-1]:
    i = 0
    a = 0
    tmp = S[0]
    while S[i] == tmp:
        i += 1
        a += 1
    j = N-1
    b = 0
    tmp = S[-1]
    while S[j] == tmp:
        j -= 1
        b += 1
    print((cycle*K) - (a//2 + b//2 - (a+b)//2)*(K-1))
else:
    print(cycle*K)

Submission status: Accepted

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

def f(S):
    cnt = 0
    t = 0
    for i in range(1, len(S)):
        if S[i-1] == S[i]:
            t += 1
        else:
            t = 0
        if t % 2 == 1:
            cnt += 1
    return cnt

if len(set(S)) == 1:
    l = len(S) * K
    print(l // 2)
elif K < 10:
    print(f(S * K))
else:
    v2 = f(S * 2)
    v3 = f(S * 3)
    print(v3 + (v3-v2) * (K - 3))

Submission status: Accepted

Solution code:
from itertools import groupby
import sys
 
input = lambda: sys.stdin.readline().strip()
 
 
def main():
 
    S = input()
    K = int(input())
    a = [len(tuple(s)) for _, s in groupby(S)]
    ans = 0
 
    if S[0] == S[-1] and K > 1:
        if len(a) == 1:
            ans = a[0] * K // 2
        else:
            ans = a[0] // 2 + a[-1] // 2 + (a[0] + a[-1]) // 2 * (K - 1)
            ans += sum(n // 2 for n in a[1:-1]) * K
 
    else:
        ans += sum(n // 2 for n in a) * K
 
    print(ans)
 
 
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from itertools import groupby
import sys

input = lambda: sys.stdin.readline().rstrip()


def main():

    S = input()
    K = int(input())
    a = [len(tuple(s)) for _, s in groupby(S)]
    ans = 0

    if S[0] == S[-1] and K > 1:
        if len(a) == 1:
            ans = a[0] * K // 2
        else:
            ans = a[0] // 2 + a[-1] // 2 + (a[0] + a[-1]) // 2 * (K - 1)
            ans += sum(n // 2 for n in a[1:-1]) * K

    else:
        ans += sum(n // 2 for n in a) * K

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from itertools import groupby


def main():

    S = input()
    K = int(input())
    a = [len(tuple(s)) for _, s in groupby(S)]
    ans = 0

    if S[0] == S[-1] and K > 1:
        if len(a) == 1:
            ans = a[0] * K // 2
        else:
            ans = a[0] // 2 + a[-1] // 2 + (a[0] + a[-1]) // 2 * (K - 1)
            ans += sum(n // 2 for n in a[1:-1]) * K

    else:
        ans += sum(n // 2 for n in a) * K

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from itertools import groupby


def main():

    S = input()
    K = int(input())
    a = [len(tuple(s)) for _, s in groupby(S)]
    ans = 0

    if S[0] == S[-1] and K > 1:
        if len(a) == 1:
            ans = a[0] * K // 2
        else:
            ans = a[0] // 2 + a[-1] // 2 + (a[0] + a[-1]) // 2 * (K - 1)
            ans += sum(n // 2 for n in a[1:-1]) * K
    else:
        ans = sum(n // 2 for n in a) * K
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

if len(set(S)) == 1:
    print(len(S)*K //2)
else:
    cnt = [1]
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            cnt[-1] += 1
        else:
            cnt.append(1)
    
    res = 0
    for c in cnt:
        res += c//2*K

    if S[0] == S[-1]:
        if (cnt[0]+cnt[-1])%2 == 0:
            res += K-1
    
    print(res)

Submission status: Accepted

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

# 全部同じ文字の場合
if s==[s[0]]*len(s):
  print((len(s)*k)//2)
  exit()

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

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

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

Submission status: Accepted

Solution code:
S=input()
K=int(input())
N=len(S)
ans=None
if len(set(S))==1:
    ans=N*K//2
else:
    li=[]
    i=0
    while(i<N):
        j=i
        c=S[i]
        cnt=0
        while(j<N and c==S[j]):
            cnt+=1
            j+=1
        li.append((c,cnt))
        i=j
    res=0
    if li[0][0]!=li[-1][0]:
        for x,y in li:
            res+=y//2
        ans=res*K
    else:
        for i in range(1,len(li)-1):
            res+=li[i][1]//2
        ans=res*K+li[0][1]//2+li[-1][1]//2+(li[0][1]+li[-1][1])//2*(K-1)
print(ans)

Submission status: Accepted

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


if len(set(S)) == 1:
    print(len(S)*K//2)
else:
    i, count1 = 1, 0
    while i < len(S):
        if S[i] == S[i-1]:
            count1 += 1
            i += 2
        else:
            i += 1
    ss = S*2
    i, count2 = 1, 0
    while i < len(ss):
        if ss[i] == ss[i-1]:
            count2 += 1
            i += 2
        else:
            i += 1
    print(count1+(count2-count1)*(K-1))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
s = input()
k = int(input())
n = len(s)
b = [0] * n
c = [0] * n
c[0] = 1

for i in range(n):
    if s[i - 1] == s[i] and not c[i - 1]:
        c[i] = 1
    if i and s[i - 1] == s[i] and not b[i - 1]:
        b[i] = 1
s1 = sum(b)
s2 = sum(c)
if s[-1] != s[0] or b[-1]:
    print(s1 * k)
else:
    if c[-1]:
        print((s1 + s2) * (k // 2) + s1 * (k % 2))
    else:
        print(s1 + (k - 1) * s2)

Submission status: Accepted

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

def calc(S):
  ret = 0
  seq = 1
  for i in range(1,len(S)):
    if S[i-1] == S[i]:
      seq += 1
    else:
      ret += seq // 2
      seq = 1
  ret += seq // 2
  return ret
## pK + q（K-1)  として解けるきがします
## pK + q


if S[0] * len(S) == S:
  # 全部おなじだったら
  print((len(S) * K) // 2)
elif K < 1000:
  print(calc(S*K))
else:
  x1 = calc(S)    # p+q
  x2 = calc(S*2)  #2p+q
  print((x2-x1) * K + 2*x1 - x2)

Submission status: Accepted

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

    if len(set(s)) == 1:
        print(len(s)*k//2)
    else:
        s1 = list(s)
        s2 = list(s + s)

        c1, c2 = 0, 0
        for i in range(len(s1) - 1):
            if s1[i] == s1[i + 1]:
                    c1 += 1
                    s1[i + 1] = "-"

        for i in range(len(s2) - 1):
            if s2[i] == s2[i + 1]:
                    c2 += 1
                    s2[i + 1] = "-"

        d = c2 - c1
        print(c1 + (k - 1) * d)

if __name__ == "__main__":
    main3()

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from itertools import groupby

    s = input()
    k = int(readline())
    l = len(s)

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

    ans = 0
    gr = [list(g) for k, g in groupby(s)]
    for g in gr:
        ans += len(g) // 2 * k

    if gr[0][0] == gr[-1][-1]:
        ans += ((len(gr[0]) + len(gr[-1])) // 2) * (k - 1)
        ans -= (len(gr[0]) // 2 + len(gr[-1]) // 2) * (k - 1)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

# In[8]:


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


# In[10]:


cnt = 0
ans = 0
tmp = S[0]
fcnt = 0
for s in S:
    if tmp == s:
        cnt += 1
        tmp = s
    else:
        ans += cnt//2
        if fcnt == 0:
            fcnt = cnt
        cnt = 1
        tmp = s
if fcnt == 0:
    fcnt = cnt
ans += cnt//2
ans *= K

if len(S) == fcnt:
    ans = (len(S)*K)//2
elif S[0] == S[-1]:
    ans -= (fcnt//2 + cnt//2 - (fcnt+cnt)//2) * (K-1)
print(ans)


# In[ ]:

Submission status: Accepted

Solution code:
S = list(input())
K = int(input())
N = len(S)
left = 1
while left < N and S[0] == S[left]:
    left += 1
right = 0
while N - 1 - right and S[0] == S[N - 1 - right]:
    right += 1
S += S
cnt = 0
for i in range(len(S) - 1):
    if S[i + 1] == S[i]:
        S[i + 1] = 'A'
        cnt += 1
if left + right < N and left % 2 != 0 and right % 2 != 0:
    print(max(0, (cnt + 1) * (K // 2) + ((cnt + 1) // 2) * int(K % 2 != 0) - 1))
else:
    print(cnt * (K // 2) + (cnt // 2) * int(K % 2 != 0))

Submission status: Accepted

Solution code:
def INT():
    return int(input())

def MI():
    return map(int, input().split())

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

def l_s(L):
    return "".join(map(str, L))

import sys
from itertools import groupby as gb

S = input()
K = INT()
g1 = []

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

for _, s in gb(S):
    g1.append(list(s))

tmp = l_s(g1[0]) + l_s(g1[len(g1) - 1])
g2 = []

for _, s in gb(tmp):
    g2.append(list(s))
    
ans = len(g1[0]) // 2 + len(g1[len(g1) - 1]) // 2

for i in range(1, len(g1) - 1):
    ans += (len(g1[i]) // 2) * K

for i in range(len(g2)):
    ans += (len(g2[i]) // 2) * (K - 1)
    
print(ans)

Submission status: Accepted

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

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

count = 0
i = 1
flg = [False] * len(S)
while i < len(S):
    if S[i] == S[i-1]:
        count += 1
        flg[i] = True
        i += 2
    else:
        i += 1
count *= K
if (not flg[len(S)-1]) and S[0] == S[-1]:
    a = 1
    for i in range(1, len(S)):
        if S[0] == S[i]:
            a += 1
        else:
            break
    if a % 2 == 1:
        count += (K-1)
print(count)

Submission status: Accepted

Solution code:
s=input()
k=int(input())
s1=[]
num1=1
str1=s[0]
for i in range(1,len(s)):
    if str1==s[i]:
        num1+=1
    else:
        s1.append([str1,num1])
        num1=1
        str1=s[i]
s1.append([str1,num1])
if len(s1)>1:
    if s1[0][0]!=s1[-1][0]:
        num2=0
        for i in range(len(s1)):
            num2+=s1[i][1]//2
        print(k*num2)
    else:
        num2=0
        num3=s1[0][1]//2
        num4=s1[-1][1]//2
        num5=(s1[0][1]+s1[-1][1])//2
        for i in range(1,len(s1)-1):
            num2+=s1[i][1]//2
        print((num2*k)+(num5*(k-1))+num3+num4)
else:
    print((len(s)*k)//2)

Submission status: Accepted

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

i=1
count1=0
while i<len(S):
    if S[i]==S[i-1]:
        count1+=1
        i+=1
    i+=1

ss=S*2
i=1
count2=0
while i<len(ss):
    if ss[i]==ss[i-1]:
        count2+=1
        i+=1
    i+=1

if 2*count1+1==count2 and len(set(S))!=1:
    print(K*count1+(K-1))
else:
    print(K*count2//2)

Submission status: Accepted

Solution code:
from itertools import groupby

S = input()
K = int(input())
grp = [[k, len(list(g))] for k, g in groupby(S)]  # grp[文字][個数]
if len(grp) == 1:
    print(grp[0][1] * K // 2)
else:
    head = grp[0][1]
    tail = grp[-1][1]
    body_sum = 0
    for g in grp[1:len(grp) - 1]:
        body_sum += g[1] // 2 * K
    if grp[0][0] == grp[-1][0]:
        print(head // 2 + tail // 2 + (head + tail) // 2 * (K - 1) + body_sum)
    else:
        print(head // 2 * K + tail // 2 * K + body_sum)

Submission status: Accepted

Solution code:
from itertools import groupby

# RUN LENGTH ENCODING str -> tuple
def run_length_encode(S: str):
    """
    'AAAABBBCCDAABBB' -> [('A', 4), ('B', 3), ('C', 2), ('D', 1), ('A', 2), ('B', 3)]
    """
    grouped = groupby(S)
    res = []
    for k, v in grouped:
        res.append((k, len(list(v))))
    return res


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

if len(set(list(S))) == 1:
    print((len(S) * K) // 2)
else:
    rle = run_length_encode(S)
    cnt = 0
    for w, length in rle:
        if length > 1:
            cnt += length // 2

    ans = cnt * K
    if S[0] == S[-1]:
        a = rle[0][1]
        b = rle[-1][1]
        ans -= (a // 2 + b // 2 - (a + b) // 2) * (K - 1)
    print(ans)

Submission status: Accepted

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

li = []
cnt = 1
for i in range(len(S)-1):
  if S[i] == S[i+1]:
    cnt += 1
  else:
    li.append(cnt)
    cnt = 1
li.append(cnt)
  
ans = 0
first = li[0]
last = li[-1]
if (S[0]==S[-1]) and (len(li)>1):
  li[-1] += first
  li.pop(0)
  for l in li:
    ans += l // 2
  ans *= K
  ans -= li[-1] // 2
  ans += first//2 + last//2
elif S[0] == S[-1]:
  ans = li[0] * K // 2
else:
  for l in li:
    ans += l // 2
  ans *= K
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd, ceil
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')
import string
# string.ascii_lowercase
from bisect import bisect_left

def solve():
    # n, m = [int(x) for x in input().split()]
    s = [x for x in input()]
    k = int(input())
    n = len(s)
    if len(set(s)) == 1:
        print(n*k//2)
        return
    b = [1]
    for i in range(1, n):
        if s[i] == s[i-1]:
            b[-1] += 1
        else:
            b.append(1)
    ans = 0
    for e in b:
        ans += e // 2 * k

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


t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""

azyxwvutsrqponmlkjihgfedcb

"""

Submission status: Accepted

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

def count_change(st):
    st=st+'!'
    cnt=0
    moji=st[0]
    l=0
    r=0
    for i in range(1,len(st)):
        if moji!=st[i]:
            cnt+=(r-l+1)//2
            l=i
            r=i
            moji=st[i]
        else:
            r=i

    return cnt

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

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

else:
    ans=count_change(s)*k

print(ans)

Submission status: Accepted

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

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

    cnt = 0
    ans = 0
    s_temp = S[0]
    fcnt = 0
    for s in S:
        if s_temp == s:
            cnt += 1
            s_temp = s
        else:
            ans += cnt // 2
            if fcnt == 0:
                fcnt = cnt
            cnt = 1
            s_temp = s
    if fcnt == 0:
        fcnt = cnt
    ans += cnt // 2
    ans *= K
    if len(S) == fcnt:
        ans = (len(S) * K) // 2
    elif S[0] == S[-1] and cnt % 2 == 1 and fcnt % 2 == 1:
        ans += K - 1

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

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

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
s=input()
n=int(input())
ans=0
tmp=1
for i in range(len(s)-1):
  if s[i]==s[i+1]:
    tmp+=1
  else:
    ans+=tmp//2
    tmp=1
if tmp!=0:
  ans+=tmp//2
ans*=n
if len(s)!=1 and s[0]==s[-1]:
  top=0
  bottom=0
  for i in range(len(s)):
    if s[:i].count(s[0])==i:
      top=i
    if s[-i:].count(s[-1])==i:
      bottom=i
  if top!=len(s)-1:
    ans-=(top//2+bottom//2-(top+bottom)//2)*(n-1)
  elif len(s)%2==1:
    ans+=n//2
if len(s)==1:
  print(n//2)
else:
  print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
def main():
    S = input().rstrip()
    K = int(input())
    if len(set(S)) == 1:
        ans = len(S)*K //2 
        print(ans)
        return
    CNT = 1
    ans = 0
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            CNT += 1
        else:
            ans += CNT//2
            CNT = 1
    ans += CNT//2

    if S[0] != S[len(S)-1]:
        print(ans*K)
    else:
        ans2 = 0
        CNT = 1
        SS = S + S
        for i in range(len(SS)-1):
            if SS[i] == SS[i+1]:
                CNT += 1
            else:
                ans2 += CNT//2
                CNT = 1
        ans2 += CNT//2
        # print(ans*(K-1))
        print(ans*K + (ans2-ans*2)*(K-1))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def f(S):
    all_cnt = 0
    cnt = 0
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            cnt += 1
        else:
            if cnt > 0:
                if cnt % 2 == 1:
                    all_cnt += (cnt + 1) / 2
                else:
                    all_cnt += cnt / 2
            cnt = 0
        # print(cnt,all_cnt)
    if cnt > 0:
        if cnt % 2 == 1:
            all_cnt += (cnt + 1) / 2
        else:
            all_cnt += cnt / 2
    return int(all_cnt)

def main():
    S = input()
    K = int(input())
    if S.count(S[0]) == len(S):
        print(len(S)*K // 2)
        return
    if S[-1] == S[0]:
        print((f(S + S) - f(S))*(K-1) + f(S))
    else:
        print(f(S)*K)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

SS = input().rstrip()
ls = len(SS)
Ki = int(input())
lis= []

for K in range(1,4):
    S = list(SS*K)
    N = len(S)
    cnt = 0
    for i in range(1,N-1):
        if S[i-1] == S[i] == S[i+1]:
            S[i] = '5'
            cnt += 1
    for i in range(N-1):
        if S[i]==S[i+1]:
            S[i] = '5'
            cnt += 1
    lis.append(cnt)    
a = lis[0]
d1 = lis[1]-lis[0]
d2 = lis[2]-lis[1]

print(a + d1 *(Ki//2) + d2*((Ki-1)//2))

Submission status: Accepted

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

if(S.count(S[0]) == len(S)):
    print(len(S)*K//2)
else:
    cnt = 0
    lst = []
    tmp = S[0]
    for i in range(len(S)):
        if(tmp == S[i]):
            cnt += 1
        else:
            tmp = S[i]
            if(cnt > 1):
                lst.append(cnt)
            cnt = 1   
    if(cnt > 1):
        lst.append(cnt)
        
    ans = 0
    for i in lst:
        ans += K*(i//2)
    if(S[0] != S[-1]):
        print(ans)
    else:
        a = 0
        b = 0
        for i in range(len(S)):
            if(tmp != S[i]):
                break
            a += 1
        for i in S[::-1]:
            if(tmp != i):
                break
            b += 1
        print(ans+((a+b)//2-(a//2+b//2))*(K-1))

Submission status: Accepted

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


first = 0

for i in range(1, len(s)):
    if s[i] != s[0]:
        first = i
        break

if first == 0:
    if len(s) % 2 == 0:
        print((len(s) // 2) * k)
    else:
        print(len(s) * k // 2)
    exit()

ans = 0
cnt = 1
i = 0

while i < len(s) - 1:
    if s[i] == s[i+1]:
        cnt += 1
        if i == len(s) - 2:
            ans += cnt // 2
    else:
        ans += cnt // 2
        cnt = 1
    i += 1

ans *= k

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

print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def s(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 1
ans = 0
inf = float("inf")

s = s()
k = k()

if s[0]*len(s) == s:
    print(len(s)*k // 2)
    sys.exit()

res = [1]

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

for i in res:
    ans += (i//2)*k
if s[0] == s[-1]:
    diff = (res[0]+res[-1])//2 - (res[0]//2 + res[-1]//2)
    ans += diff*(k-1)
print(ans)