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:
s = input()
k = int(input())
n = len(s)
ss = set(s)
# print(ss)
ans = 0
if len(ss) == 1:
    print(k*n//2)
    # print('y')
    exit()
if n == 1:
    print(n//2)
    exit()
streaks = []
cnt = 0
now = s[0]
for i in range(n):
    if now == s[i]:
        cnt += 1
    else:
        streaks.append(cnt)
        now = s[i]
        cnt = 1
streaks.append(cnt)
# print(streaks)
m = len(streaks)
if s[0] == s[-1]:
    for i in range(1, m-1):
        ans += streaks[i]//2
    # print(ans)
    ans *= k
    # print(ans)
    ans += (streaks[0] + streaks[m-1])//2*(k-1)
    # print(ans)
    ans += streaks[0]//2 + streaks[m-1]//2
    # print(ans)
else:
    for i in range(m):
        ans += streaks[i]//2
    ans *= k

print(ans)

Submission status: Accepted

Solution code:
s = input()
k = int(input())
l=len(s)
s+='?'
ch = 1
ans=0
j=0

for i in range(l):
    if s[i]==s[i+1]:
        ch+=1
    else:
        ans+=ch//2
        if j==0:
            st=ch
        if s[i+1]=='?':
            gl=ch
        ch=1
        j=1
ans*=k
if st%2==1 and gl%2==1 and s[0]==s[l-1]:
    ans+=k-1

if st==l:
    ans=l*k//2

print(ans)

Submission status: Accepted

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

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

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

if S[0] != S[-1]:
    ans = 0
    tmp = ""
    for s in S:
        if s == tmp:
            ans += 1
            tmp = ""
            continue
        else:
            tmp = s
    ans *= K
    print(ans)
    exit()
elif S[0] == S[-1]:
    A = 0
    B = 0
    for s in S:
        if s == S[0]:
            A+=1
        else:
            break
    for s in S[::-1]:
        if s == S[0]:
            B+=1
        else:
            break
    ans = 0
    tmp = ""
    for s in S:
        if s == tmp:
            ans += 1
            tmp = ""
            continue
        else:
            tmp = s
    ans *= K
    ans = ans - (A//2 + B//2)*(K-1) + (A+B)//2*(K-1)
    print(ans)
    exit()

Submission status: Accepted

Solution code:
s = list(input())
k = int(input())
ans = []
now = s[0]
cou = 0
if len(s) == 1:
    print(k//2)
    exit()

for i in range(len(s)):
    if now == s[i]:
        cou += 1
    elif now != s[i]:
        ans.append(cou)
        cou = 1
        now = s[i]
if cou > 0:
    ans.append(cou)
if ans[0] == len(s):
    print((ans[0]*k)//2)
    exit()
    
p = 0
for i in ans:
    p += (i//2)*k

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

print(p)

Submission status: Accepted

Solution code:
# AGC039A

import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))


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

def sub(s):
    if not s:
        return 0
    # 連続する文字のカウント
    prev = s[0]
    counts = []
    count = 1
    for c in s[1:]:
        if c==prev:
            count += 1
        else:
            counts.append(count)
            count = 1
        prev = c
    if count>=2:
        counts.append(count)
    return sum([item//2 for item in counts])

if s[0]!=s[-1]:
    ans = k * sub(s)
elif all(s[0]==s[i] for i in range(1, len(s))):
    ans = (k * len(s)) // 2
else:
    i = 0
    while s[i]==s[-1]:
        i += 1
    ss = s[i:] + s[:i]
    ans = sub(s[:i]) + (k-1)*sub(ss) + sub(s[i:])
print(ans)
#     s1 = s[:-1] + "A"
#     c1 = sub(s1)
#     s2 = "B" + s[1:]
#     c2 = sub(s2)

Submission status: Accepted

Solution code:
s = input()
k = int(input())
ren = []
n = 1
p = s[0]
for i in range(1,len(s)):
    if p == s[i]:
        n += 1
    else:
        ren.append(n)
        p = s[i]
        n = 1
ren.append(n)

ans = 0
if ren[0] == len(s):
    ans = (ren[0]*k)//2
elif s[0] == s[-1]:
    ans = ren[0]//2 + ren[-1]//2 - (ren[0]+ren[-1])//2
    ren[0] += ren[-1]
    ren[-1] = 0
    for i in ren:
        ans += (i//2)*k
else:
    for i in ren:
        ans += (i//2)*k

print(ans)

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 = list(readline().rstrip().decode())
k = int(read())
if len(set(s)) == 1:
    print(len(s) * k // 2)
    exit()
ans = 0
memo = 0
flag = True
for i, (bf, af) in enumerate(zip(s, s[1:])):
    if s[0] == af and flag:
        memo += 1
    else:
        flag = False
    if bf == af:
        ans += 1
        s[i + 1] = '?'
ans *= k
if s[-1] == s[0] and memo % 2 == 0:
    ans += k - 1
print(ans)

Submission status: Accepted

Solution code:
import sys
import re
import math
import collections
import decimal
import bisect
import itertools

import copy

# import heapq
# from collections import deque
# import decimal

# sys.setrecursionlimit(100001)
INF = sys.maxsize
# MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    s = list(input())
    k = ni()

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

    def changer(sss):
        ts = sss.copy()
        cnt = 0
        for i in range(1, len(ts)):
            if ts[i] == ts[i - 1]:
                ts[i] = "@"
                cnt += 1

        return cnt

    print(changer(s) + (k - 1) * (changer(s + s) - changer(s)))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s = input()
n = len(s)
k = int(input())
start = True
start_count = 0
start_char = s[0]
inside_num = 0
end_count = 0
end_char = ""
memory = s[0]
count = 1
for i in range(1,n):
    if s[i] == memory:
        count += 1
    else:
        if start:
            start_count = count
            start = False
        else:
            inside_num += count//2
        count = 1
        memory = s[i]
end_count = count
end_char = memory
ans = 0
if start_char == end_char:
    if end_count == n:
        ans = n*k//2
    else:
        ans += inside_num*k
        ans += (start_count+end_count)//2*(k-1)+start_count//2+end_count//2
else:
    inside_num += start_count//2
    inside_num += end_count//2
    ans += inside_num*k
print(ans)

Submission status: Accepted

Solution code:
import sys
s = input()
k = int(input())
n = len(s)
if n==1:
    print(k//2)
    sys.exit()
if n==2:
    print(k if s[0]==s[1] else 0)
    sys.exit()
i = 1
change = [0, 0]
same = True
while i<n:
    if s[i-1]==s[i]:
        if i==n-1:
            same = False
        change[0] += 1
        i += 1
    i += 1
if s[0]!=s[-1]:
    same = False
if not same:
    print(change[0]*k)
    sys.exit()
i = 2
change[1] = 1
same = True
while i<n:
    if s[i-1]==s[i]:
        if i==n-1:
            same = False
        change[1] += 1
        i += 1
    i += 1
if same:
    print(change[0]+change[1]*(k-1))
else:
    print(change[0]*((k+1)//2)+change[1]*(k//2))

Submission status: Accepted

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

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

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

print(n_changes(S) + (K-1) * (n_changes(2*S) - n_changes(S)))

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:
import collections
s = str(input())
k = int(input())

ans = 0
#if s[0] == s[-1]:
#    ans += k

tmp = ''
tmp_num = 0

check = ''
check_num = 0

if len(collections.Counter(s).keys()) == 1:
    ans = len(s)*k//2

else:
    for i, w in enumerate(s):
        if i == 0:
            tmp += w
            tmp_num += 1
            continue
        
        if tmp[-1] == w:
            tmp += w
            tmp_num += 1
        
        elif tmp[-1] != w:
            if check_num == 0:
                check = tmp
                check_num = 1

            ans += (tmp_num // 2) * k
            #print('ans', ans)
            tmp = w
            tmp_num = 1
        
        if i == len(s)-1:
            if len(tmp) == 1:
                if check[0] == tmp and (len(tmp)%2 == 1 and len(check)%2 == 1):
                    ans += k-1
            else:
                #print(check, tmp)
                if check[0] == tmp[-1] and (len(tmp)%2 == 1 and len(check)%2 == 1):
                    ans += k-1

            ans += (tmp_num // 2) * k
            #print('last', ans)

print(ans)

Submission status: Accepted

Solution code:
s=input()
k=int(input())
n = len(s)
same = True
for i in range(1,len(s)):
    if s[i]!=s[i-1]:
        same = False
if same:
    print(len(s)*k//2)
    exit()

for i in range(1,len(s)):
    if s[i] != s[i-1]:
        before = i-1
        break
for i in range(len(s)-1,-1,-1):
    if s[i] != s[i-1]:
        after = i
        break

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

Submission status: Accepted

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

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

first, last = s[0], s[-1]
first_flag = 1
a, b  = 0, 0
tmp = s[0]
tmp_cnt = 0
cnt = 0

for i in range(len(s)):
    c = s[i]
    if c == first and first_flag:  # 最初に続く文字数をカウント (a)
        a += 1
    elif c != first:
        first_flag = 0
    
    if c == tmp:  # s単体のとき必要な操作回数を求める (cnt)
        tmp_cnt += 1
    else:
        cnt += (tmp_cnt // 2)
        tmp_cnt = 1
        tmp = c
        
    if i == len(s) -1:  # 最後に続く文字数を取得 (b)
        b = tmp_cnt
        cnt += (tmp_cnt // 2)

if first != last:  # sの最初の文字と最後の文字が違う場合
    ans = cnt * k
else:  # sの最初の文字と最後の文字が同じ場合
    ans = cnt * k - (a//2 + b//2 - (a+b)//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)
    exit()

first, last = s[0], s[-1]
first_flag = 1
a, b  = 0, 0
tmp = s[0]
tmp_cnt = 0
cnt = 0

for i in range(len(s)):
    c = s[i]
    if c == first and first_flag:
        a += 1
    elif c != first:
        first_flag = 0
    
    if c == tmp:
        tmp_cnt += 1
    else:
        cnt += (tmp_cnt // 2)
        tmp_cnt = 1
        tmp = c
    if i == len(s) -1:
        b = tmp_cnt
        cnt += (tmp_cnt // 2)

if first != last:
    ans = cnt * k
else:
    ans = cnt * k - (a//2 + b//2 - (a+b)//2)*(k-1)

print(ans)

Submission status: Accepted

Solution code:
s = input()
k = int(input())
cnt = 0
if len(set(list(s))) == 1:
	cnt = k * len(s) // 2
	print(cnt)
	exit()

x, y = 0, 0
t = s + s
for i in range(len(s) - 1):
	if s[i] == s[i + 1]:
		x += 1
		s = s[:i+1] + '#' + s[i+2:]

for i in range(len(t) - 1):
	if t[i] == t[i + 1]:
		y += 1
		t = t[:i+1] + '#' + t[i+2:]

cnt = x + (k - 1) * (y - x)
print(cnt)

Submission status: Accepted

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

w = ""
c = 0
cnt = 0
for i in s:
    if w != i:
        #print(w)
        cnt += c//2
        c = 1
        w = i
    else:
        c +=1
cnt += c//2
#print(cnt)

start = 0
start_w = s[0]
for i in s:
    if start_w != i:
        break
    else:
        start +=1

s.reverse()

end = 0
end_w = s[0]
for i in s:
    if end_w != i:
        break
    else:
        end +=1

same = len(list(set(s)))
#print(same)

if s[0] != s[-1]:
    print(cnt*a)
elif same ==1:
    print(len(s)*a//2)
else:
    print(cnt*a-(a-1)*(start//2+end//2-(start+end)//2))

Submission status: Accepted

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

l = len(s)

ss = s + s
ll = len(ss)

single = 0
cnt = 0

for i in range(1, l):
    if s[i - 1] == s[i]:
        if cnt == 0:
            single += 1
            cnt = 1
        else:
            if cnt % 2 == 0:
                single += 1
                cnt += 1
            else:
                cnt += 1
    else:
        cnt = 0

double = 0
cnt = 0

for i in range(1, ll):
    if ss[i - 1] == ss[i]:
        if cnt == 0:
            double += 1
            cnt = 1
        else:
            if cnt % 2 == 0:
                double += 1
                cnt += 1
            else:
                cnt += 1
    else:
        cnt = 0

if len(set(s)) == 1:
    ans = double * (k // 2) + single * (k % 2)
elif single * 2 == double:
    ans = single * k
else:
    ans = single + (single + 1) * (k - 1)

# print(single, double)
print(ans)

Submission status: Accepted

Solution code:
s = list(input())

k = int(input())

n = len(s)

if(len(set(s)) == 1):
    print(n*k//2)
else:
    t = 0
    c = 1
    for i in range(n-1):
        if(s[i] == s[i+1]):
            c += 1
        else:
            t += c // 2
            c = 1
    t += c // 2
    if(s[0] != s[-1]):
        print(t*k)
    else:
        a = 0
        b = 0
        for i in range(n):
            if(s[i] == s[0]):a+=1
            else:break
        for i in range(n)[::-1]:
            if(s[i] == s[-1]):b+=1
            else:break
        print(t*k - (a//2 + b//2 - (a+b)//2) * (k-1))

Submission status: Accepted

Solution code:
s = list(input())
n = int(input())
ans = 0
if s[0] != s[-1]:
    cnt = 1
    tmp = ''
    for i in range(len(s)):
        if i == 0:
            tmp = s[i]
            continue
        if tmp == s[i]:
            cnt += 1
        else:
            ans += cnt // 2
            cnt = 1
            tmp = s[i]
    ans += cnt // 2
    print(ans*n)
else:
    cnt = 1
    tmp = ''
    f = 0
    for i in range(len(s)):
        if i == 0:
            tmp = s[i]
            continue
        if tmp == s[i]:
            cnt += 1
        else:
            if f == 0:
                f = cnt
            ans += cnt // 2
            cnt = 1
            tmp = s[i]
    ans += cnt // 2
    if f == 0:
        print((cnt*n)//2)
    else:
        m = ans - f//2 - cnt//2
        ans = f // 2 + cnt // 2
        # print(m,ans)
        ans += m * n
        # print(m*n)

        ans += (n-1) * ((f+cnt) // 2)
        # print((n-1)*((f+cnt)//2))
        print(ans)

Submission status: Accepted

Solution code:
#AGC039-A Connection and Disconnection
"""
sをk回繰り返して得られる文字列tを、全て隣り合う文字が異なるようにする
為に必要な操作回数を求めよ
解法：
最初と最後だけ愚直にカウント
あとは中間部分を連結してそれをk-1回繰り返す
"""
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0
s = readline().rstrip().decode('utf-8')
k = int(readline())
first = 0
last = 0
l = 0
r = len(s)

#全部同じ文字だとまずい
if len(set(s)) == 1:
    print(len(s)*k//2)
    exit()

if s[0] == s[-1]:
    res = s[0]
    for idx,i in enumerate(s):
        if i == res:
            first += 1
        else:
            l = idx
            break
    res = s[-1]
    for idx,i in enumerate(s[::-1]):
        if i == res:
            last += 1
        else:
            r = len(s)-idx
            break

count = 0
res = ""
for i in s[l:r]:
    if res == "":
        res = i
    elif res == i:
        count += 1
        res = ""
    else:
        res = i

print(first//2+last//2+(first+last)//2*(k-1)+count*k)

Submission status: Accepted

Solution code:
s=input()
k=int(input())
l=len(s)
now=1
seq=[]
re=False
all=False
for i in range(l):
    if s[i%l]==s[(i+1)%l]:
        if i==l-1:
            re=True
            if len(seq)==0:
                all=True
            else:
                first=seq[0]//2
                end=now//2
                seq[0]=(seq[0]+now)
        now+=1
    else:
        seq.append(now)
        now=1
if all:
    ans=len(s)*k//2
else:
    seq=[i//2 for i in seq]
    if re:
        ans=sum(seq)*k-seq[0]+first+end
    else:
        ans=sum(seq)*k

print(ans)

Submission status: Accepted

Solution code:
from itertools import groupby
s = input()
k = int(input())
ans = 0
count = []
for i, j in groupby(s):
    count.append(len(list(j)))
if len(set(s)) == 1:
    ans = (len(s)*k)//2
elif k > 1 and s[0] == s[-1]:
    f = count[0]
    e = count[-1]
    ans += f//2 + e//2 + (f+e)//2*(k-1)
    for n in count[1:-1]:
        ans += n//2 * k
else:
    for n in count:
        ans += n//2 * k
print(ans)

Submission status: Accepted

Solution code:
import itertools
def main():
  S = input()
  K = int(input())
  
  if len(set(S)) == 1:
    print(len(S) * K // 2)
    return
  
  cc = 0
  gg = itertools.groupby(S)
  for key, item in gg:
    cc += len(list(item))//2
  a = b = 0
  if S[0] == S[-1]:
    while a < len(S) and S[a] == S[0]:
      a += 1
    S = S[::-1]
    while b < len(S) and S[b] == S[0]:
      b += 1
  output = K * cc - (K-1) * (a//2 + b//2 - (a+b)//2)
  print(output)
  return

if __name__ == '__main__':
  main()

Submission status: Accepted

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


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

    N = len(S)
    if S == S[0] * N:
        ans = (N * K) // 2
    elif S[0] == S[-1]:
        a = 1
        for i in range(N):
            if S[i] == S[i+1]:
                a += 1
            else:
                break
        b = 1
        for i in reversed(range(N)):
            if S[i-1] == S[i]:
                b += 1
            else:
                break
        seq = 1
        ans = 0
        for i in range(a, N-b):
            if S[i] == S[i+1]:
                seq += 1
            elif seq > 1:
                ans += seq//2
                seq = 1
        if seq > 1:
            ans += seq//2
        ans *= K
        ans += a//2
        ans += (a+b)//2 * (K-1)
        ans += b//2
    else:
        seq = 1
        ans = 0
        for i in range(N-1):
            if S[i] == S[i+1]:
                seq += 1
            elif seq > 1:
                ans += seq//2
                seq = 1
        if seq > 1:
            ans += seq//2
        ans *= K

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

res = 0
i = 0
while i < n - 1:
  if S[i] == S[i + 1]:
    res += 1
    i += 1
  i += 1

if S[0] != S[-1]:
  print(res * k)
else:
	i = 1
	while S[0] == S[i]:
		i += 1

	j = 1
	while S[-1] == S[-1 - j]:
		j += 1
	
	if i % 2 == 0 or j % 2 == 0:
		print(res * k)
	else:
		print(res * k + (k - 1))

Submission status: Accepted

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

ans = 0
i = 0
while i < N-1:
  if S[i] == S[i+1]:
    ans += 1
    i += 1
  i += 1

if S[0] != S[N-1]:
  print(ans*K)
  exit()

a = 1
while S[0] == S[a]:
  a += 1

b = 1
while S[N-1] == S[N-1-b]:
  b += 1
print(ans*K - (K-1)*(a//2 + b//2 - (a+b)//2))

Submission status: Accepted

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

prev = ''
cnt = 0
for s in S:
    if s == prev:
        cnt += 1
        prev = ''
    else:
        prev = s

b = 0
if K % 2 == 0:
    b += 1
if K > 2 and S[0] == S[-1]:
    mae = 1
    while mae < len(S) and S[mae] == S[0]:
        mae += 1
    if mae % 2 == 1 and mae != len(S):
        usiro = 1
        i = len(S) - 2
        while S[i] == S[-1]:
            usiro += 1
            i -= 1
        if usiro % 2 == 1:
            b = K // 2

if K % 2 == 0:
    res = cnt * (K // 2) + (b - 1)
else:
    res = cnt * (K // 2) + cnt // 2 + b
print(res)

Submission status: Accepted

Solution code:
s=str(input())
k=int(input())
ans=0
count=1
c1=0
c2=0

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

if s[0]==s[-1]:
    for i in range(len(s)-1):
        if s[i]==s[i+1]:
            count+=1
        else:
            if 2<=count:
                ans+=count//2
            count=1
    ans+=count//2
    i=0
    while s[i]==s[0]:
        c1+=1
        i+=1
    
    i=len(s)-1
    while s[i]==s[-1]:
        c2+=1
        i-=1
    #print(c1,c2)
    ans*=k
    if c1%2==1 and c2%2==1:
        ans+=k-1

    print(ans)


else:
    for i in range(len(s)-1):
        if s[i]==s[i+1]:
            count+=1
        else:
            if 2<=count:
                ans+=count//2
            count=1
    ans+=count//2
    print(ans*k)

Submission status: Accepted

Solution code:
s = input()
k = int(input())
n = len(s)
c = 0
x = []
a = 1

for i in range(n):
    if i<n-1 and s[i]==s[i+1]:
        a += 1
    else:
        c += a//2
        x.append(a)
        a = 1

c2 = 0
if s[0]==s[-1]:
    c2 = x[0]//2+x[-1]//2-(x[0]+x[-1])//2

print(len(s)*k//2 if len(set(s))==1 else c*k-c2*(k-1))

Submission status: Accepted

Solution code:
import math
import sys

s = list(sys.stdin.readline().rstrip())
k = int(sys.stdin.readline().rstrip())

if len(set(s)) == 1:
    print(len(s) * k // 2)
    sys.exit()
ss = s * 2
tmp, tmp2 = 0, 0
for i in range(len(s) - 1):
    if s[i] == s[i + 1]:
        tmp += 1
        s[i + 1] = '-1'
for i in range(len(s) * 2 - 1):
    if ss[i] == ss[i + 1]:
        tmp2 += 1
        ss[i + 1] = '-1'
print(tmp + (tmp2 - tmp) * (k - 1))

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    s = list(input())
    ss = s + s
    k = int(input())
    if len(set(s)) == 1:
        print(len(s) * k // 2)
        exit(0)

    tmp, tmp2 = 0, 0
    for i in range(len(s) - 1):
        if s[i] == s[i + 1]:
            tmp += 1
            s[i + 1] = '-1'
    for i in range(len(s) * 2 - 1):
        if ss[i] == ss[i + 1]:
            tmp2 += 1
            ss[i + 1] = '-1'
    print(tmp + (tmp2 - tmp) * (k - 1))

Submission status: Accepted

Solution code:
import sys

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

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

if flg:
    print(len(s) * k // 2)
    sys.exit()

ans = 0
skip = -1
for i in range(len(s) - 1):
    if i == skip:
        continue
    
    if s[i] == s[i + 1]:
        ans += 1
        skip = i + 1
        
#print(ans)
ans *= k


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

print(ans)

Submission status: Accepted

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

if S[0] != S[-1]:
    ans = 0
    for i in range(len(S) - 1):
        if S[i] == S[i + 1]:
            S = S[:i + 1] + '*' + S[i + 2:]
            ans += 1
    print(ans * K)
else:
    # S -> s_1 s_2 ... s_k + A + t_l t_l-1 ... t_1 (s_i = t_j)と分解
    k, l = 0, len(S) - 1
    while k < len(S) and S[k] == S[-1]:
        k += 1
    while l > -1 and S[l] == S[0]:
        l -= 1

    # 連結に関係ない部分のカウント
    A = S[k:l + 1]
    ans = 0
    for i in range(len(A) - 1):
        if A[i + 1] == A[i]:
            A = A[:i + 1] + '*' + A[i + 2:]
            ans += 1
    ans *= K

    # 連結に関係ある部分のカウント
    if l == -1 and k == len(S):
        # 全部同じ
        print((len(S) * K) // 2)
    else:
        ans += (len(S) - l + k - 1) // 2 * (K - 1)
        ans += (len(S) - l - 1) // 2 + k // 2
        print(ans)

Submission status: Accepted

Solution code:
s=input()
k=int(input())
s0=s[0]
s1=s[-1]
cs=[]
p=s[0]
c=1
for i in range(1,len(s)):
      if p==s[i]:
            c+=1
      else:
            cs.append(c)
            p=s[i]
            c=1
cs.append(c)
if len(cs)==1:
      print((cs[0]*k)//2)
      exit()
ans=0
if s0!=s1:
      for csi in cs:
            ans+=csi//2
      ans*=k
      print(ans)
      exit()
else:
      for i in range(1,len(cs)-1):
            ans+=cs[i]//2
      ans*=k
      ans+=cs[0]//2+cs[-1]//2
      ans+=((cs[0]+cs[-1])//2)*(k-1)
      print(ans)

Submission status: Accepted

Solution code:
def cal(S):
    tmp, a, cnt = S[0], 1, 0
    flag = True
    for s in S[1:]:
        if S[0]!=s: flag=False
        if flag: a+=1
        if tmp[-1]==s:
            s = '*'
            cnt += 1
        tmp += s
    return a, cnt


S = input().replace('\n', '')
k = int(input())
ans = 0
if len(list(set(S)))==1:
    ans = len(S)*k//2
else:
    a, cnt = cal(S)
    b, _ = cal(S[::-1])
    ans = cnt*k
    if S[0]==S[-1]:
        ans -= ((a//2)+(b//2)-((a+b)//2))*(k-1)
print(ans)

Submission status: Accepted

Solution code:
def main():
    S = input()
    K = int(input())
    A = []
    last = S[0]
    count = 1
    for i in range(1, len(S)):
        s = S[i]
        if last == s:
            count += 1
        else:
            A.append({'key': last, 'count': count})
            count = 1
        last = s
    A.append({'key': last, 'count': count})
    # print(A)
    if len(A) == 1:
        c = A[0]['count']
        l = c*K
        ans = l//2
        print(ans)
        return
    ans = 0
    if A[0]['key'] != A[-1]['key']:
        for a in A:
            c = a['count']
            ans += (c//2) * K
        print(ans)
        return
    for i in range(1, len(A) - 1):
        a = A[i]
        c = a['count']
        ans += (c//2) * K
    a0 = A[0]
    a1 = A[-1]
    c0 = a0['count']
    c1 = a1['count']
    ans += c0//2
    ans += c1//2
    ans += ((c0 + c1)//2) * (K - 1)
    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)
    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:
from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np  # Pythonのみ！
# from operator import xor
# import re
# from scipy.sparse.csgraph import connected_components  # Pythonのみ！
# ↑cf.  https://note.nkmk.me/python-scipy-connected-components/
# from scipy.sparse import csr_matrix
# import string
import sys

sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():
    S = list(input())
    K = int(input())
    l = []
    cnt = 0
    if len(S)==1:
        print(K//2)
    else:
        gr = groupby(S)
        for key, group in gr:
            l.append(list(group))
        if len(l)==1:
            print(len(S)*K//2)
        else:
            for i in range(len(l)):
                cnt += len(l[i]) // 2
                
            if l[0][0] == l[-1][0]:
                dev = len(l[0]) // 2 + len(l[-1]) // 2 - (len(l[-1]) + len(l[0])) // 2
            else:
                dev = 0
            print(cnt * K - dev * (K - 1))


resolve()

Submission status: Accepted

Solution code:
# import sys
# sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
def II(): return int(input())

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

def LI(): return list(map(int, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]


def solve():
    S = list(input())
    k = II()
    n = len(S)
    S2 = S[:]

    if len(set(S)) == 1:
        print(n * k // 2)
    else:
        cnt = 0
        for i in range(n-1):
            j = i + 1
            si, sj = S[i], S[j]
            if si == sj:
                S[j] = i
                cnt += 1
        if S[0] == S[-1]:
            moji = S[0]
            sl = 0
            for s in S2:
                if s == moji:
                    sl += 1
                else:
                    break
            sr = 0
            for s in S2[::-1]:
                if s == moji:
                    sr += 1
                else:
                    break

            print(cnt * k + ((sl+sr)//2 - sl//2 - sr//2) * (k - 1))
        else:
            print(cnt * k)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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



def func(S):
    inner = 0
    S = list(S)
    for i in range(len(S) - 1):
        if S[i] == S[i + 1]:
            S[i + 1] = '#'
            inner += 1
    return inner

if len(set(list(S))) == 1:
    print(len(S) * K // 2)
else:
    inner = func(S)
    inter = func(S * 2) - func(S) * 2
    print(inner * K + inter * (K - 1))

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def run_length(s):
    ans=[]
    if len(s)==1:
        ans.append((s,1))
        return ans
    s_pre=s[0]
    cunt=1
    for i,ss in enumerate(s[1:],1):
        if ss==s_pre:
            cunt+=1
        else:
            ans.append((s_pre,cunt))
            cunt=1
            s_pre=ss
    ans.append((s_pre,cunt))
    return ans

def main():
    s=input()
    k=int(input())
    s_lis=run_length(s)
    ans=0    
    if len(s_lis)==1:
        print(k*s_lis[0][1]//2)
    else:
        for tup in s_lis:
            ans+=tup[1]//2  
        if s_lis[0][0]==s_lis[-1][0]:
            a=s_lis[0][1]
            b=s_lis[-1][1]
            print(ans*k-(a//2+b//2-(a+b)//2)*(k-1))
        else:
            print(ans*k)


if __name__=='__main__':
    main()

Submission status: Accepted

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

def count_replace(l):
    ret = 0
    cnt = 1
    for i in range(len(l)-1):
        if l[i] == l[i+1]: #
            cnt += 1
        elif l[i] != l[i+1]:
            ret += cnt//2
            cnt = 1
    ret += cnt // 2 # 最後尾（ループが尽きた）のための処理. 末尾が連続していた場合には連続分が追加、それ以外は0が追加
    return ret

if len(set(s)) == 1: # すべての文字が同じとき
    ans = (len(s) * k) // 2
elif s[0] != s[-1]: #先頭末尾が一致しないとき
    ans = count_replace(s)
    ans = k * ans
else: # 先頭末尾が一致
    # 先頭の連続する文字数
    cnt_pre = 1
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            cnt_pre += 1
        else:
            break
    # 末尾の連続する文字数
    cnt_pos = 1
    for i in reversed(range(len(s))):
        if s[i] == s[i-1]:
            cnt_pos += 1
        else:
            break
    # 先頭の連続数//2+末尾の連続数//2＋(k-1)*（先頭+末尾）の連続数//2
    ans = cnt_pre//2 + cnt_pos//2 + (k-1) * ((cnt_pos+cnt_pre)//2)
    # 先頭、末尾以外の部分のカウント
    ret = count_replace(s[cnt_pre:-cnt_pos])
    ans += k * ret

print(ans)