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 = str(input())
k=int(input())



if len(s)==1:
    if k<2:
        ans = 0
    else:
        ans = k//2
elif len(s)==2:
    if s[0]==s[1]:
        ans = k
    else:
        ans = 0
else:
    sset = set()
    for i in range(len(s)):
        sset.add(s[i])
    if len(sset)==1:
        llen = len(s)*k
        ans = llen//2
        
    else:
        if k <= 2:
            ans = 0
            bef = ""
            befchen = False
            trignum = 0
            s = s+s
            for i in range(len(s)):
                now = s[i]
                if bef==now:
                    if befchen == False:
                        ans += 1
                        befchen = True
                        trignum = i
                if  befchen == True:
                    if trignum == i-1:
                        befchen = False
                bef = now

        elif k >=3:
            ans = 0
            fans = 0
            sans = 0
            tans = 0
            s2 = s+s+s
            bef = ""
            befchen = False
            trignum = 0
            for i in range(len(s2)):
                now = s2[i]
                if bef==now:
                    if befchen == False:
                        
                        if i <=len(s)-1:
                            fans +=1
                        elif i >=2*len(s):
                            tans +=1
                        else:
                            sans+=1
                        
                        befchen = True
                        trignum = i
                if  befchen == True:
                    if trignum == i-1:
                        befchen = False
                bef = now
                
            ans = fans+tans+(k-2)*sans
            

print(ans)

Submission status: Accepted

Solution code:
import re
import sys
import copy

def get_count(s):
  count02=0
  pattern = "a+|b+|c+|d+|e+|f+|g+|h+|i+|j+|k+|l+|m+|n+|o+|p+|q+|r+|s+|t+|u+|v+|w+|x+|y+|z+"
  result = re.finditer(pattern, s)
  for res in result:
    l = res.end()-res.start()
    if l>1:
      #切り捨て
      count02 += l//2
  return count02

s = input()
k = int(input())
count01=0
count02=0

#case01
if len(s)==1:
  print(k//2)
  sys.exit(0)
  
#case02
tmp = s[0]
flag=True
for i in range(len(s)):
  if s[i]!=tmp:
    flag=False
if flag==True:
  print((len(s)*k)//2)
  sys.exit(0)

#case03
s_org = copy.deepcopy(s)
s2 = copy.deepcopy(s)
if s[0]==s[-1]:
  tmp = s[0]

  offset01=0
  while s[0]==tmp:
    offset01+=1
    s = s[1:] + s[0]
    #print(s)
  offset02=0
  while s2[-1]==tmp:
    offset02+=1
    s2 = s2[-1] + s2[:-1]
    #print(s2)
  k -= 1
  print(get_count(s_org[:offset01]) + get_count(s)*k + get_count(s_org[offset01:]))
  sys.exit(0)

  
#case04
print(get_count(s)*k)

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[]]*num
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))

#################

S = str(input())
n = len(S)
K = I()

for i in range(1,n):
    if S[i]!=S[i-1]:
        break
    else:
        if i==n-1:
            print(K*n//2)
            exit()

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

ren = 1
num = 0
for i in range(1,n):
    if S[i]==S[i-1]:
        ren += 1
    else:
        if i!=n-1:
            num += ren//2
            ren = 1

last = ren

if S[0]==S[-1]:
    num2 = 0
    for i in range(n):
        if S[i]==S[-1]:
            ren += 1
        else:
            now = i
            break
    num2 += ren//2
    ren = 1
    for i in range(now,n):
        if S[i]==S[i-1]:
            ren += 1
        else:
            if i!=n-1:
                num2 += ren//2
                ren = 1
    num3 = 0
    ren = 1
    for i in range(n-1-last+2,n):
        if S[i]==S[i-1]:
            ren += 1
        else:
            num3 += ren//2
            ren = 1
    num3 += ren//2
    print(num+num2*(K-1)+num3)
else:
    num += ren//2
    print(num*K)

Submission status: Accepted

Solution code:
S=input()
K=int(input())
count=1
ans=0
old='old'
for x in S:
    if old==x:
        count+=1
    else:
        ans+=count//2
        count=1
    old=x

ans+=count//2
check1=0
check1+=ans

ans-=count//2

for x in S:
    if old==x:
        count+=1
    else:
        ans+=count//2
        count=1
#     print(count)
    old=x

ans+=count//2
check2=0
check2+=ans

ans-=count//2

for x in S:
    if old==x:
        count+=1
    else:
        ans+=count//2
        count=1
#     print(count)
    old=x

ans+=count//2

# print(check1,check2,ans)

if K==1:
    print(check1)
elif K==2:
    print(check2)
elif K==3:
    print(ans)
else:
    if (check2-check1)==(ans-check2):
        print((check2-check1)*(K-1)+check1)
    else:
        print((check2-check1)*((K-1)//2+(K-1)%2)+ (ans-check2)*((K-1)//2) +check1)

Submission status: Accepted

Solution code:
from itertools import groupby

S = input()
N = int(input())

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

if len(S) == 2 and S[0] == S[1]:
    print(N)
    exit()

check = S
check_num = sum(len(list(v))//2 for k, v in groupby(check))
check2 = S * 2
check2_num = sum(len(list(v))//2 for k, v in groupby(check2))
check3 = S * 3
check3_num = sum(len(list(v))//2 for k, v in groupby(check3))
check4 = S * 4
check4_num = sum(len(list(v))//2 for k, v in groupby(check4))

if N % 2 == 0:
    print(check2_num + (check4_num-check2_num)*(N//2-1))
else:
    print(check_num + (check3_num-check_num)*(N//2))
#ans = check_num*N + (check2_num-2*check_num)*(N-1)
#ans3 = check_num*3 + (check2_num-2*check_num)*(3-1)
#if check3_num == ans3:
#    print(ans)
#else:
#    print(round(N/2)*check_num + N//2*(check2_num-check_num))

Submission status: Accepted

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

ans = 0
rep = 1
first_rep = 1

tmp = None
for i in s:
    if tmp:
        if tmp == i:
            first_rep += 1
        else:
            break
    else:
        tmp = i

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


for i in range(0, len(s) - 1):
    current = s[i]
    next = s[i+1]
    if current == next:
        rep += 1
    else:
        if rep > 1:
            ans += rep // 2
            rep = 1

if rep > 1:
    ans += rep // 2

start_eq_end = 0
if len(s) > 2 and s[0] == s[-1] and first_rep % 2 != 0:
    if rep % 2 != 0:
        start_eq_end = k - 1


print(ans * k + start_eq_end)

Submission status: Accepted

Solution code:
S = input()
K = int(input())
flag = False
count = 0
for i in range(len(S)-1):
    if not flag:
        if S[i] == S[i+1]:
            count += 1
            flag = True
    else:
        flag = False
ans = 0

if len(S) == 1:
    ans = K // 2
elif S.count(S[0]) == len(S) and len(S) % 2 == 1:
    ans += count
    ans += (count + count + 1) * (K // 2)
elif S[0] == S[-1] and not flag:
    ans += count
    count = 0
    for i in range(1, len(S)-1):
        if not flag:
            if S[i] == S[i+1]:
                count += 1
                flag = True
        else:
            flag = False
    ans += (count + 1)*(K-1)

else:
    ans = count * K
print(ans)

Submission status: Accepted

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

n = len(S)
s = S[0]
c = 0

if S.count(s) == n:
  print((n*K)//2)
  exit()
else:
  head = 1
  for i in range(1, n):
    if S[i] == s:
      head += 1
    else:
      c += head//2
      S = S[i:]
      break
  if S[-1] == s:
    tail = 1
    for i in range(-2, -n, -1):
      if S[i] == s:
        tail += 1
      else:
        c += tail//2
        S = S[:i+1]
        break
    c += (K-1)*((head + tail)//2)
  else:
    c += (K-1)*(head//2)
  start = 0
  s = S[start]
  S += 'X'
  for i in range(1, len(S)):
    if S[i] != s:
      c += K*((i - start)//2)
      start = i
      s = S[start]
print(c)

Submission status: Accepted

Solution code:
def cad(s, k):
    if len(s) == 1:
        return k // 2
    if len(set(s)) == 1:
        return len(s) * k // 2

    l = list(s)

    count = 0
    new_l = l.copy()

    first_len = 1
    first_block = True
    for i, c in enumerate(s[1:]):
        if c == new_l[i]:
            new_l[i + 1] = 0
            count += 1

        if first_block:
            if c == s[0]:
                first_len += 1
            else:
                first_block = False

    result = count * k

    if new_l[0] == new_l[-1] and first_len % 2 == 1:
        result += k - 1

    return result


s = input()
k = int(input())
print(cad(s, k))

Submission status: Accepted

Solution code:
import copy
a=list(input())
b=int(input())
if len(a)==1:
  print(b//2)
else:
  c=copy.copy(a) 
  l=len(a)
  ans1=0
  ans2=0
  t=0
  for i in range(1,len(a)):
    if c[i]==c[i-1]:
      ans1+=1
      c[i]=str(i%10)
      if i==len(a)-1:
        t=2
  c=a*2
  if t==2:
    c[l-1]=str(1)
  for i in range(l,len(c)):
    if c[i]==c[i-1]:
      ans2+=1
      c[i]=str(i%10)
      if i==len(c)-1:
        t=1
  if b==1:
    print(ans1)
  elif t:
    if b%2==0:
      print((ans1+ans2)*b//2)
    else:
      print((ans1+ans2)*(b//2)+ans1)
  else:
    print(ans1+ans2*(b-1))

Submission status: Accepted

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


count=0
n=len(s)
x=s[0]
y=s[-1]

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

if s[0]!=s[-1]:
    for i in range(1,n):
        if s[i]==s[i-1]:
            count+=1
            s[i]='1'
    print(count*k)


else:
    countA=1
    countB=1
    v=0
    w=0
    for i in range(1,n):
        if s[i]==s[i-1]:
            countA+=1
            
        else:
            v=i
            break
    for i in range(1,n):
        if s[n-1-i]==s[n-1]:
            countB+=1
        else:
            w=n-1-i
            break
    
    for i in range(v,w+1):
        if s[i+1]==s[i]:
            count+=1
            s[i+1]='1'
    print(count*k+countA//2+((countA+countB)//2)*(k-1)+countB//2)

Submission status: Accepted

Solution code:
from collections import defaultdict
S=input()
K=int(input())
d=defaultdict(int)
first_last=0
memo=[0]*len(S)
count=0
for i in range(len(S)):
  if i>=1:
    if (S[i]==S[i-1])and(memo[i-1]==0):
      count+=1
      memo[i]=1
      
      
memo2=[0]*len(S)
count2=0
if (S[0]==S[-1]) and (memo[-1]==0):
  count2+=1
  memo2[0]=1
  first_last+=1
  for i in range(1,len(S)):
    if (S[i]==S[i-1])and(memo2[i-1]==0):
        count2+=1
        memo2[i]=1
      
type3=0
if (S[0]==S[-1])and(memo2[-1]==1):
  type3=1

if type3==1:
  if K%2==1:
    print(count*(K//2+1) + count2*(K//2))
  else:
    print(count*(K//2)+count2*(K//2))
elif first_last==1:
  print(count + count2*(K-1))
else:
  print(count*K)

Submission status: Accepted

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

results = [0]*2
result = 0

lastchanged = False
pre = None
for r in range(2):
    for i, x in enumerate(S):
        if pre is not None and not lastchanged and x == pre:
            results[r] += 1
            lastchanged = True
        else:
            lastchanged = False
        pre = x

if results[0] == results[1]:
    result = results[0] * K
else:
    if lastchanged:
        result = (results[0] + results[1]) * (K // 2)
        if K % 2 != 0:
            result += results[0]
    else:
        result = results[1] * K
        result -= 1

print(result)

Submission status: Accepted

Solution code:
import math

def main():
    s = input()
    k = int(input())
    cnt = 0
    if (len(s) == 0):
        cnt = 0
    elif (len(s) == 1):
        cnt = math.floor(k/2)
    elif (len(s) == 2):
        if (s[0] == s[1]):
            cnt = k
        elif (s[0] != s[1]):
            cnt = 0
    else:
        l = list()
        prev = ''
        for c in s:
            if(c == prev):
                l[-1][1] += 1
            else:
                l.append([c, 1])
                prev = c
        if (len(l) == 1):
            cnt += l[0][-1] * k // 2
        elif (len(l) == 2):
            cnt += l[0][-1] // 2
            cnt += l[1][-1] // 2
            cnt *= k
        else:
            edge1 = l[0]
            edge2 = l[-1]
            mid = l[1:-1]
            for e in mid:
                cnt += e[1] // 2 * k
            if (edge1[0] == edge2[0]):
                cnt += (edge1[1] + edge2[1]) // 2 * (k-1)
            else:
                cnt += (edge1[1]) // 2 * (k-1)
                cnt += (edge2[1]) // 2 * (k-1)
            cnt += edge1[1] // 2
            cnt += edge2[1] // 2

    print(cnt, end='')


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

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

ngongo = 0
while ngongo < len(s) and s[len(s)-ngongo-1] == s[-1]:
    ngongo+=1

ngongongo = 0
while ngongongo < len(s) and s[ngongongo] == s[0]:
    ngongongo += 1
    
f = 0
if ngongo %2 == 1 and ngongongo %2 == 1 and s[0] == s[-1]:
    count += 1
    f += 1

print(count*k-f)

Submission status: Accepted

Solution code:
import sys
s = input()
k = int(input())
ans = 0
#print(s[0] == s[2])
if len(s) == 1:
  print(k//2)
  sys.exit()

for i in range(len(s)):
  if s[i] != s[i-1]:
    break
  if i == len(s)-1:
    print(len(s) * k // 2)
    #print('f')
    sys.exit()
tmp = []
tmpnum = 0
for i in range(len(s)):
  if s[i] == s[i-1]:
    tmpnum += 1
  else:
    tmp.append(tmpnum+1)
    tmpnum = 0
tmp.append(tmpnum+1)

if s[0] == s[-1]:
  tmp[0] -= 1

for i in tmp:
  ans += i//2

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

Submission status: Accepted

Solution code:
import sys
s=input()
k=int(input())
lengthofs=len(s)

if lengthofs == 1:
	print(k//2)
	sys.exit()

i=0
j=0
while i+1 < lengthofs :
	if s[i] != s[i+1]:
		j=1
	i=i+1
if j == 0:
	print((lengthofs*k)//2)
	sys.exit()

i=0
j=1
ans=0
while i+1 < lengthofs :
	if s[i] == s[i+1]:
		j=j+1
	else:
		ans=ans+(j//2)
		j=1
	i=i+1
ans=ans+(j//2)
ans=ans*k

if s[0]!=s[lengthofs-1]:
	print(ans)
else:
	i=0
	while s[i]==s[i+1]:
		i=i+1
	i=(i+1)%2
	j=1
	while s[lengthofs-j] == s[lengthofs-j-1]:
		j=j+1
	j=j%2
	if i*j==1:
		ans=ans+k-1
	print(ans)

Submission status: Accepted

Solution code:
s = input()
k = int(input())
flag = 1
cnt = 1
ans = 0
a1 = 0
a2 = 0
for i in range(1, len(s)):
    if s[i] == s[i - 1]:
        flag += 1
if flag == len(s):
    print(len(s) * k // 2)
    exit()

for i in range(1, len(s)):
    if s[i] == s[i - 1]:
        cnt += 1
    else:
        a1 += cnt // 2
        cnt = 1
a1 += (cnt // 2)
cnt = 1

s2 = s + s
for i in range(1, len(s2)):
    if s2[i] == s2[i - 1]:
        cnt += 1
    else:
        a2 += cnt // 2
        cnt = 1
a2 += (cnt // 2)
r = a2 - a1
print(a1 + (k - 1) * r)

Submission status: Accepted

Solution code:
#ライブラリインポート
from collections import defaultdict

#入力受け取り
def getlist():
	return list(map(int, input().split()))

#処理内容
def main():
	#初期化
	S = list(input())
	N = len(S)
	SS = S + S
	K = int(input())

	#計算
	x = 0
	c = 1
	for i in range(1, N):
		if S[i] == S[i - 1]:
			c += 1
		else:
			x += c // 2
			c = 1
	x += c // 2

	y = 0
	c = 1
	for i in range(1, 2 * N):
		if SS[i] == SS[i - 1]:
			c += 1
		else:
			y += c // 2
			c = 1
	y += c // 2

	judge = "Yes"
	for i in range(N - 1):
		if S[i] != S[i + 1]:
			judge = "No"


	#答え
	if judge == "No":
		print(x + (y - x) * (K - 1))
	else:
		print(N * K // 2)

if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
def getCount(target, index):
    count = 0
    key_char = target[index]
    
    for char in target[index::] :
        if (key_char == char) :
            count += 1
        else :
            break
    
    return count

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

head_seq = getCount(s, 0)

if head_seq == len(s) :
    print((len(s) * k) // 2)
elif s[0] != s[-1:] :
    position = 0
    replace_count = 0
    while position < len(s) :
        temp_seq = getCount(s, position)
        replace_count += temp_seq // 2
        position += temp_seq
    
    print(replace_count * k)
else :
    target_str = s[head_seq::] + s[0:head_seq:]

    position = 0
    replace_count = 0
    while position < len(target_str) :
        temp_seq = getCount(target_str, position)
        replace_count += temp_seq // 2
        position += temp_seq

    tail_seq = getCount(target_str[::-1], 0)
    org_head_seq = getCount(s, 0)
    org_tail_seq = getCount(s[::-1], 0)

    print(replace_count * k - tail_seq // 2 + org_head_seq // 2 + org_tail_seq // 2)

Submission status: Accepted

Solution code:
from itertools import groupby

s = input()
k = int(input())
a = [sum(1 for _ in g) for _, g in groupby(s)]
if len(a) == 1:
    print(len(s) * k // 2)
else:
    x, y = a[0], a[-1]
    b = x // 2 + y // 2
    c = (x + y ) // 2 if s[0] == s[-1] else b
    print(b + sum(x // 2 for x in a[1:-1]) * k + c * (k - 1))

Submission status: Accepted

Solution code:
s=list(input())
ss=s*3
k=int(input())
cnt1,cnt2,cnt3=0,0,0
for i in range(len(ss)-1):
    if ss[i]==ss[i+1]:
        ss[i+1]='.'
        if i<=len(s)-2:
            cnt1+=1
        elif len(s)-2<i<=2*len(s)-2:
            cnt2+=1
        else:
            cnt3+=1

#print(cnt1,cnt2,cnt3)

if cnt1==cnt3:
    if k%2==0:
        print((cnt1*(k//2))+cnt2*((k//2)))
    else:
        print((cnt1*((k//2)+1))+cnt2*(k//2))
elif cnt2==cnt3:
        print(cnt1+(cnt2*(k-1)))

Submission status: Accepted

Solution code:
s = list(input())
k = int(input())
s.append('-1')
ran = []
counter = 0
while counter < len(s)-1:
    j = counter +1
    while s[counter] == s[j]:
        j += 1
    ran.append([s[counter], j - counter])
    counter = j
counter = 0
result = 0
if k >= 2:
    if len(ran) >= 2:
        if ran[0][0] == ran[-1][0]:
            result += ((ran[0][1]+ran[-1][1])//2)*(k-1)
            if ran[-1][1] != 1:
                result += ran[-1][1]//2
            if ran[0][1] != 1:
                result += ran[0][1]//2
        else:
            result += (ran[0][1]//2 + ran[-1][1]//2)*k
    else:
        result += (ran[0][1]*k)//2
    for  i in range(1, len(ran)-1):
        result += ((ran[i][1])//2)*k
    print(result)
else:
    for i in range(len(ran)):
        result += (ran[i][1])//2
    print(result)

Submission status: Accepted

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

if len(S) == 1:
    print(K//2)
elif len(set(S)) == 1:
    print(len(S)*K//2)
else:
    c = 0
    flg = False
    for i in range(len(S)-1):
        if S[i] == S[i+1] and flg is False:
            c += 1
            flg = True
        else:
            flg = False

    S2 = S*2
    d = 0
    flg = False
    for i in range(len(S2)-1):
        if S2[i] == S2[i+1] and flg is False:
            d += 1
            flg = True
        else:
            flg = False

    ar = 0
    if c*2 != d:
        ar = 1

    if ar == 1:
        print((c+1)*K-1)
    else:
        print(c*K)

Submission status: Accepted

Solution code:
def solve(text, count):
    if len(text) == 1:
        return count // 2

    # split chars into first part & last part & middle parts & joint parts
    first_ch = text[0]

    # Endpoints exclusive
    first_index = -1
    middle_index = -1

    for i, ch in enumerate(text):
        if ch == first_ch:
            continue
        else:
            first_index = i
            break

    if first_index == -1:
        # All Same Character
        return (len(text) * count) // 2

    for i in range(len(text) - 1, first_index - 1, -1):
        if text[i] == first_ch:
            continue
        else:
            middle_index = i + 1
            break

    first_part = text[:first_index]
    middle_part = text[first_index:middle_index]
    end_part = text[middle_index:]
    joint_part = end_part + first_part

    def calc_changes(raw_part):
        change = 0

        part = [ch for ch in raw_part]

        for i, ch in enumerate(part):
            if i == 0:
                continue
            if part[i - 1] == part[i] and part[i - 1] != '!':
                part[i] = '!'  # Any char fine since we can choose any
                change += 1
                continue

        return change

    return calc_changes(first_part) + calc_changes(middle_part) * count + \
           calc_changes(joint_part) * (count - 1) + calc_changes(end_part)


if __name__ == "__main__":
    s = input()
    k = int(input())
    print(solve(s, k))

Submission status: Accepted

Solution code:
s=input()
k=int(input())
l=len(s)
s_list=[s[i] for i in range(l)]
ser_list=[]
ser_list.append(int(1))
for i in range(l-1):
    if s_list[i]==s_list[i+1]:
        ser_list[-1]=ser_list[-1]+1
    else:
        ser_list.append(1)
if s_list[0]!=s_list[l-1]:
    divide = [x//2 for x in ser_list]
    print(sum(divide)*k)
elif s_list.count(s_list[0])==len(s_list):
    print(l*k//2)
else:
    b=sum([x//2 for x in ser_list])
    ser_list[0]=ser_list[0]+ser_list[-1]
    ser_list.pop()
    a= sum([x//2 for x in ser_list])
    print(a*(k-1)+b)

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-

s = input()
k = int(input())
flag = 1
lenth = 1
ans = 0
flag2 = 0
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        lenth += 1
    else :
        flag = 0
    if flag == 0:
       ans += lenth//2
       lenth = 1
       flag = 1
if lenth == len(s):
    print(lenth*k//2)
    flag2 = 4
ans += lenth//2
lenth = 1
flag = 1

ji = 0
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        lenth += 1
    else :
        flag = 0
    if flag == 0:
       # print(lenth)
       if lenth%2 == 1:
            ji += 1
       break
if lenth%2 == 1 and flag ==1:
    ji += 1
lenth = 1
flag = 1
for i in range(len(s)-1):

    if s[len(s)-i-1] == s[len(s)-1-(i+1)]:
        lenth += 1
    else :
        flag = 0
    if flag == 0:
       # print(lenth)
       if lenth%2 == 1:
            ji += 1
       break
if lenth%2 == 1 and flag == 1:
    ji += 1
ans = ans*k
# print(ans, ji)
if flag2 == 0:
    if ji == 2 and s[0] == s[len(s)-1]:
        ans = ans+k-1
    if len(s) == 1:
        print(k//2)
    else:
        print(ans)

Submission status: Accepted

Solution code:
S = input().strip()
K = int(input())
if len(S)==1:
    cnt = K//2
else:
    A = []
    cur = 0
    for i in range(1,len(S)):
        if S[i]!=S[i-1]:
            if i-cur>=2:
                A.append([S[i-1],cur,i])
            cur = i
    if S[-1]==S[-2]:
        A.append([S[-1],cur,len(S)])
    cnt = 0
    for j in range(len(A)):
        cnt += (A[j][2]-A[j][1])//2
    if S[0]!=S[-1]:
        cnt = cnt*K
    else:
        if S[0]!=S[1] and S[-1]!=S[-2]:
            cnt = cnt+(cnt+1)*(K-1)
        elif S[0]==S[1] and S[-1]!=S[-2]:
            if (A[0][2]-A[0][1])%2==0:
                cnt = cnt*K
            else:
                cnt = cnt+(cnt+1)*(K-1)
        elif S[0]!=S[1] and S[-1]==S[-2]:
            if (A[-1][2]-A[-1][1])%2==0:
                cnt = cnt*K
            else:
                cnt = cnt+(cnt+1)*(K-1)
        elif S[0]==S[1] and S[-1]==S[-2] and len(A)>1:
            if (A[0][2]-A[0][1])%2==0 or (A[-1][2]-A[-1][1])%2==0:
                cnt = cnt*K
            else:
                cnt = cnt+(cnt+1)*(K-1)
        else:
            cnt = (len(S)*K)//2
print(cnt)

Submission status: Accepted

Solution code:
def main():
    S=input()
    k=int(input())
    L=[S[0]]
    N=[]
    prev=S[0]
    num=1
    for s in S[1:]:
        if s!=prev:
            L.append(s)
            N.append(num)
            prev=s
            num=1
        else:
            num+=1
    N.append(num)
    #print(L,N)
    l=len(L)
    if l==1:
        return (len(S)*k)//2
    ans=0
    if L[0]!=L[-1]:
        for num in N:
            ans+=num//2
        return ans*k
    ans+=N[0]//2+N[-1]//2
    tmp=0
    for num in N[1:-1]:
        tmp+=num//2
    ans+=tmp*k
    ans+=(N[0]+N[-1])//2*(k-1)
    return ans
    
if __name__=='__main__':
    print(main())

Submission status: Accepted

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

countlap = [0]
for i in range(len(s) - 1):
    if s[i] == s[i+1]:
        countlap[-1] += 1
    else:
        countlap.append(0)
ans = 0
if len(countlap) == 1:
    ans = (len(s)*k)//2
elif s[0]==s[-1]:
    ans += (countlap[0]+1)//2 + (countlap[-1]+1)//2
    countlap[0] += countlap[-1] +1
    countlap[-1] = 0
    for i in range(1, len(countlap)):
        ans += ((countlap[i]+1)//2)*k
    ans += ((countlap[0]+1)//2)*(k-1)
else:
    for i in range(len(countlap)):
        ans += ((countlap[i]+1)//2)*k

print(ans)

Submission status: Accepted

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

x = [0]*len(s)
for i in range(len(s)-1):
    if (s[i] == s[i+1]) and x[i] == 0:
        x[i+1] = 1


y = [0]*len(s)
if (k > 1) and (s[0] == s[-1]) and (x[-1] == 0):
    y[0] = 1
    for i in range(len(s)-1):
        if (s[i] == s[i+1]) and y[i] == 0:
            y[i+1] = 1
else:
    y = x

if y[-1] == 1:
    z = k//2
    print(sum(x)*(k-z) + sum(y)*z)
else:
    print(sum(x) + sum(y)*(k-1))

Submission status: Accepted

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

li = [0 for i in range(len(s)+1)]
wrd = [i for i in s]
wrd.append(wrd[0])


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


if k == 1:
    for i in range(len(s)-1):
        if wrd[i]==wrd[i+1] and li[i]==0:
            li[i+1] = 1
    ans = 0    
    for i in range(len(s)+1):
        if li[i] == 1:
            ans += 1
    print(ans)
    sys.exit()


for i in range(len(s)):
    if wrd[i]==wrd[i+1] and li[i]==0:
        li[i+1] = 1

ans = 0    
for i in range(len(s)+1):
    if li[i] == 1:
        ans += 1
ans1 = ans
ans *= k

if li[len(s)]!=1:
    print(ans)
    sys.exit()

ans1 -= 1
    
""""""

li = [0 for i in range(len(s)+1)]
li[0] = 1
wrd = [i for i in s]
wrd.append(wrd[0])

for i in range(len(s)):
    if wrd[i]==wrd[i+1] and li[i]==0:
        li[i+1] = 1

ans2 = 0  
ans = 0  
for i in range(len(s)+1):
    if li[i] == 1:
        ans2 += 1

if li[len(s)] == 0:
    ans += ans1*(math.ceil(k/2))
    ans += ans2*(k//2)
    print(ans)
    sys.exit()

ans2 -= 1
ans += ans1
ans += ans2*(k-1)

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
S = input()
K = int(input())
fletter = 0 # 最初の文字の連続数を数える
lletter = 0 # 最後の文字の連続数を数える
cletter = 0 # 現在連続している文字の連続数を数える
cng = 0 # 文字変更の必要がある文字をカウント
for i in range(len(S)): # 最初の文字の連続数を数える
    if S[0] == S[i]:
        fletter += 1
    else:
        break
for i in range(len(S)):
    if S[-1] == S[-(i + 1)]:
        lletter += 1
    else:
        break
for i in range(len(S) - 1): # 1文字ずつ連続しているかを判定
    if S[i] == S[i + 1]:
        cletter += 1
    else:
        cng -= - cletter // 2
        cletter = 0
cng -= - cletter // 2 # 最終文字まで到達したら文字変更数の処理
if fletter == len(S): # 文字列の種類数が1種類だったら話がだいぶ変わるようだ
    ans = len(S) * K // 2
elif S[0] == S[-1] and fletter % 2 == 1 and lletter % 2 == 1:
    ans = int((cng + 1) * K - 1)
else:
    ans = int(cng * K)
print(ans) #解答の出力

Submission status: Accepted

Solution code:
import collections
from itertools import groupby

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

all_s_list = []
for i in range(len(S)):
  all_s_list.append(S[i])

diff_element_count = 0
sum = 0
another_sum = 0
element_list = []
for key, value in groupby(all_s_list):
  sum += len(list(value))//2
for key, value in groupby(all_s_list):
  element_list.append(list(value))
  diff_element_count += 1
#print(element_list)
if len(element_list) >= 2:
  for i in range(len(element_list)):
    if i >= 1 and i != len(element_list)-1:
      another_sum += len(element_list[i])//2
      #print("aa", another_sum)
    if i == len(element_list)-1:
      another_sum += (len(element_list[0])+len(element_list[i]))//2


if all_s_list[0] != all_s_list[len(S)-1]:    
  sum = sum*K
else:
  if diff_element_count == 1:
    sum = 0
    sum += len(S)*K//2
  else:
    if K >= 2:
      sum += another_sum*(K-1)
print(sum)

Submission status: Accepted

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

    previous = S[0]
    i = 0
    continue_dict = {
        0: 0
    }
    for s in S:
        if s == previous:
            continue_dict[i] += 1
        else:
            i += 1
            continue_dict[i] = 1
        previous = s

    if len(continue_dict) == 1:
        ans = continue_dict[0] * K // 2
    else:
        # head + tail
        count_ht = 0
        for l in continue_dict.values():
            count_ht += l // 2

        if K > 1:
            # between
            if S[0] == S[-1]:
                continue_dict[0] += continue_dict[i]
                continue_dict.pop(i)

            count_btw = 0
            for l in continue_dict.values():
                count_btw += l // 2

            ans = count_ht + count_btw * (K-1)
        else:
            ans = count_ht

    print(ans)


if __name__ == '__main__':
    answer()

Submission status: Accepted

Solution code:
S=input()
K=int(input())
total=0
count=0
flag=0
ss=0
flag2=1
if len(S)==1:
    print(K//2)
    exit(0)
for i in range(len(S)-1):
    if S[i]==S[i+1]:
        count+=1
    else:
        if flag==0:
            ss=count+1
        flag=1
        count+=1
        total+=count//2
        count=0
    #print(count)

if flag==0:
    print((len(S)*K)//2)
    exit(0)


count+=1
if S[len(S)-1]==S[0]:
    flag2=0
    count+=1
total+=count//2
if (count+ss-1)%2==0 and flag2==0:
    print(total*K-1)
else:
    if ss%2==0 and flag2==0:
        print(total*K-K)
    else:
        print(total * K )

Submission status: Accepted

Solution code:
import sys

#+++++

def compute_cc(a_ll):
	cp=False
	ccc=0
	for pre_c, c in zip(a_ll,a_ll[1:]):
		if cp == True:
			cp = False
			continue
		elif pre_c == c:
			cp=True
			ccc+=1
	return ccc	




def main():
	s = input()
	k = int(input())
	
	if len(s)==1:
		return k//2
	
	if len(s)==2:
		if s[0]==s[1]:
			return k
		else:
			return 0
	
	first_diff=None
	i=0
	for pre_c, c in zip(s,s[1:]):
		i+=1
		if pre_c != c and first_diff is None:
			first_diff=i
			#pa(i)
	
	if first_diff is None:
		return len(s) * k // 2
		
	
	#len(s)>=3
	pp=s[:first_diff]
	nn=s[first_diff:]
	nc=nn+pp
	a=0
	a+=compute_cc(pp)
	a+=compute_cc(nc)*(k-1)
	a+=compute_cc(nn)
	print(a)
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)

if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)