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().replace(" ", "")
K = int(input())

str1 = S
str2 = S * 2
str3 = S * 3
str4 = S * 4
n1 = 0
n2 = 0
n3 = 0
n4 = 0
ln1 = []
ln2 = []
ln3 = []
ln4 = []

for c in str1:
  ln1.append(c)
for i in range(len(ln1)):
  if i == 0:
    pass
  elif ln1[i-1] == ln1[i]:
    n1 += 1
    ln1[i] = "A"
    
for c in str2:
  ln2.append(c)
for i in range(len(ln2)):
  if i == 0:
    pass
  elif ln2[i-1] == ln2[i]:
    n2 += 1
    ln2[i] = "A"
    
for c in str3:
  ln3.append(c)
for i in range(len(ln3)):
  if i == 0:
    pass
  elif ln3[i-1] == ln3[i]:
    n3 += 1
    ln3[i] = "A"
    
for c in str4:
  ln4.append(c)
for i in range(len(ln4)):
  if i == 0:
    pass
  elif ln4[i-1] == ln4[i]:
    n4 += 1
    ln4[i] = "A"
  
if len(S) == 1:
  print(K//2)
elif n1 == (n2 - n1) and (n2 - n1) == (n3 - n2):
  print(int(n1 * K))
elif n1 != (n2 - n1) and (n2 - n1) == (n3 - n2) and n1 == (n4 - n3):
  if K >= 3:
    print(int(n1 + (n3 - n1) * (K - 1)//2 - 1))
  elif K == 2:
    print(int(n2))
  else:
    print(int(n1))
elif n1 != (n2 - n1) and (n2 - n1) == (n3 - n2):
  if K >= 3:
    print(int(n1 + (n3 - n1) * (K - 1)//2))
  elif K == 2:
    print(int(n2))
  else:
    print(int(n1))
else:
  if K % 2 == 1:
    print(int(n1 + (n3 - n1) * (K - 1)//2))
  else:
    print(int(n2 * K / 2))

Submission status: Accepted

Solution code:
T = input()
# T = "xaaxxxx"
K = int(input())
# S = T*K
S_ = T*2
N = len(T)
last = T[-1]
mod = 10**9+7
"""
後ろを前につなげて、一番長くなるのはいくつ？
cooooooooonteeeeeeeeeest
3
"""
def check(S):
    S += "1"
    cnt = 1
    pre = "-1"
    flg = False
    ret = 0
    for i, s in enumerate(S):
        if pre == s:
            cnt += 1
            flg = True
        else:
            if flg:
                ret += cnt // 2
                flg = False
            cnt = 1
        pre = s
    return ret

s = 0
for i in range(N):
    if last != T[i]:
        s = i
        break
e = 0 
for i in range(N):
    if last != T[-i]:
        e = -i
        break
# 一回
if K == 1:
    print(check(T))
    exit()
# 同じ文字の連続
if len(set(T)) == 1:
    ans = check(T*2)*(K//2)+check(T)*(K%2)
    print(ans)
    exit()
# 最初と最後が違う
if s == e:
    print(check(T)*K)
    exit()


# その他
pre, mid, end = T[:s], S_[s:s+N], S_[s+N:]
# pre = S_[:s]
# mid = S_[s:e+2*N]
# end = S_[e+2*N:]
a = check(pre)    
b = check(mid)    
c = check(end)
# print(pre,mid,end)
print(a+b*(K-1)+c)

Submission status: Accepted

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

cnt,ans=0,0
T=S+S
for i in range(1,len(T)):
  if T[i-1]==T[i]:
    cnt+=1
  else:
    ans+=(cnt+1)//2
    cnt=0
  if i==len(S)-1:
    ans+=(cnt+1)//2
    tmp=ans

if len(S)==1:
  print(k//2)
  exit()
if len(S)==2:
  if S[0]==S[1]:
    print(k)
  else:
    print(0)
  exit()
  
if len(set(T))==1:
  if len(S)%2==0:
    print(len(S)//2*k)
  else:
    out=(len(S)//2)*(k-(k//2))+(len(S)//2+1)*(k//2)
    print(out)
  exit()
      
print(tmp+(ans-tmp)*(k-1))

Submission status: Accepted

Solution code:
s=input()
k=int(input())
p=s[0]
cnt=1
q=[]
for i in range(len(s)-1):
	if s[i+1]==p:
		cnt+=1
	else:
		q.append(cnt)
		cnt=1
		p=s[i+1]
q.append(cnt)
if q[-1]%2==0:
	cnt=True
else:
	cnt=False
ans=0
if cnt==True:
	for i in range(len(q)):
		ans+=q[i]//2
	print(ans*k)
	exit()
if len(q)==1:
	print(len(s)*k//2)
	exit()
for i in range(len(q)-2):
	ans+=q[i+1]//2
ans*=k
ans+=q[0]//2
if s[0]==s[-1]:#miss
	ans+=(q[0]+q[-1])//2*(k-1)
	ans+=q[-1]//2
	print(ans)
	exit()
else:
	ans=0
	for i in range(len(q)):
		ans+=q[i]//2
	print(ans*k)

Submission status: Accepted

Solution code:
#-------------------------------------------------------------------
import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
#-------------------------------------------------------------------
s = input()		# 文字列の入力  abcdefg...
k = int(input())						# 5


ans=0

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

if len(s)==2:
	if s[0]==s[-1]: ans=k
	print(ans)
	exit(0)



# グループ作成
#「groupby」は、「キーとグループ」を返すようなイテレータを作成します
from itertools import groupby
m=[]
for key, g in groupby(s):
	#print( key, list(g) )
	m.append([key,len(list(g))])

p(m)

if len(m)==1:
	i=m[0][1]
	ans+=((i*k)//2)
	print(ans)
	exit(0)
		

for a,i in m:
	#p(a,i)
	ans+=(i//2)*k


if m[0][0]==m[-1][0]:
	if m[0][1]%2==1:
		if m[-1][1]%2==1:
			ans+=(k-1)

print(ans)

Submission status: Accepted

Solution code:
s = input()
k = int(input())
A = []
b = 1
if s[0] != s[-1]:
  for i in range(len(s)-1):
    if s[i] == s[i+1]:
      b+= 1
    else:
      A.append(b)
      b = 1
  A.append(b)
  print(k*sum(a//2 for a in A))
elif all(a  == s[0] for a in s):
  print((k*(len(s))//2))
else:
  t = s[0]
  ahead = 0
  back = 0
  ans = 0
  for i in range(len(s)):
    if s[i] == t:
      ahead += 1
    else:
      break
  for i in reversed(range(len(s))):
    if s[i] == t:
      back += 1
    else:
      break
  ans += ahead//2 + back//2 + (k-1)*((ahead+back)//2)
  l = s[ahead:-back]
  for i in range(len(l)-1):
    if l[i] == l[i+1]:
      b+= 1
    else:
      A.append(b)
      b = 1
  A.append(b)
  ans += (k*sum(a//2 for a in A))
  print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
 
s = str(input()).replace('\n', '')
k = int(input())
 
s = s + s[0]
 
lst = []
last = ''
cnt = 0
for i in range(len(s)):
  if s[i] == last or last == '':
    cnt += 1
  else:
    lst.append(cnt)
    cnt = 1
  last = s[i]
lst.append(cnt)
 
    
n_lst = [i // 2 for i in lst]
answer = sum(n_lst)*k

if lst[-1]%2 == 0:
  answer -=  1
  
if len(lst) == 1:
  answer = (len(s)-1)*k//2
elif s[0] == s[-2] and lst[-1]%2 == 0 and lst[0]%2 == 0:
  answer -= k-1

print(answer)

Submission status: Accepted

Solution code:
S=list(str(input()))
s=set(S)
K=int(input())
num=0
i=0
lst=[]
def dp(i,lst):
  if i==len(S)-1:
    return lst
  if S[i]!=S[i+1]:
    return dp(i+1,lst)
  j=i+1
  while j<=len(S)-1  and S[i]==S[j]:
    j+=1
  lst.append(list(S[i:j]))
  return dp(j-1,lst)

if 2<=len(s):
  L=dp(i,lst)
  for i in range(len(L)):
    num+=(len(L[i])//2)
  if num==0:
    if S[0]!=S[-1]:
      print(0)
    else:
      print(K-1)
  elif L[-1][-1]==S[0] and len(L[-1])%2==len(L[0])%2==1:
    print(num+(K-1)*(num+1))
  else:
    print(num*K)
else:
  print((len(S)*K)//2)

Submission status: Accepted

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

count = 0
i=0
join_flag =0
join_n = 0

if len(s)==1:
  print(k//2)
    
elif len(s)==2:
  if s[0]==s[1]:
    print(k)
  else:
    print(0)
  
elif len(set(s))==1:
  print(len(s)*k //2)
  
else:
  while i<len(s)-1:
    if s[i] == s[i+1]:
      count+=1
      i+=2
    else:
      i+=1
  
  if (s[-1] == s[0]) :
    join_flag =1
    
  if join_flag:      
    left_len = 1
    right_len = 1

    for i in range(len(s)-1):
      if s[i] == s[i+1]:
        left_len +=1
      else:
        break

    for i in range(len(s)-1):    
      if s[-1-i]==s[-2-i]:
        right_len +=1
      else:
        break            
    
    if left_len %2 ==1 and right_len %2 ==1:
      join_n = k-1
    else:
      join_n =0
    
  print(count*k + join_n)

Submission status: Accepted

Solution code:

s = list(input())
t = int(input())
answer=0

def classify(s):
    ans=0
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            s[i+1] = '0'
            ans +=1
    return ans

if t == 1:
    print(classify(s))
else:
    s2 = s*2
    s21 = s*2 + list(s[0])
    t2 = t//2
    t2a = t%2
    c1 = classify(s)
    c2 = classify(s2)
    c21 = classify(s21)
    
    if c1*2 == c2:
        answer = c1 * t
    elif c2 == c21:
        #iii type
        answer = c2 * t2 + c1 * t2a
    else:
        answer = c1 * t + t - 1
         
    print(answer)

Submission status: Accepted

Solution code:
s = input()
k = int(input())
cnt = 0
cntw = 0
cntwflg = False
i = 0

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

if len(list(set(s))) == 1:
    print((k*len(s))//2)
    exit()
while True:
    # print('i, s[i] ', i, s[i])
    if len(s)-1 < i:
        break
    if len(s)-1 == i:
        if s[i] == s[0]:
            cntw = 1
            if s[i-1]==s[i-2]:
                j = 0
                while s[j] == s[j+1]:
                    if j%2 == 0:
                        cntw = 0
                    else:
                        cntw = 1
                    j += 1
    elif s[i] == s[i+1]:
        cnt+= 1
        i += 1
    i += 1


ans = cnt*k+cntw*(k-1)
print(ans)

Submission status: Accepted

Solution code:
import math

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


before = ""
count = 0
if len(S) == 1:
    out = K // 2
else:
    for s in S:
        if before == s:
            count += 1
            before = ""
        else:
            before = s
    ### If before is empty at the end of the loop, the last character should be replaced ->
    ### Can aboid to be same as the first character of the string
    if before == S[0]:
        first_same_num = 1
        before2 = S[0]
        for s in S[1:]:
            if before2 == s:
                first_same_num += 1
            else:
                break
        if first_same_num % 2 == 0:
            first_last_replace = 0      # Even -> the first one could be replaced anyway
        else:
            if first_same_num != len(S):
                first_last_replace = K - 1
            else:
                first_last_replace = math.ceil((K - 1) / 2)
    else:
        first_last_replace = 0

    out = count * K + first_last_replace

print(out)

Submission status: Accepted

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

if len(S) == 1:
    print(K // 2)
    exit()
    
cnt, s, l, r = 1, S[0], 0, len(S)-1
while l + 1 <= r and S[l+1] == s:
    cnt += 1
    l += 1
while l <= r - 1 and S[r] == s:
    cnt += 1
    r -= 1
if cnt == len(S):
    print(len(S) * K // 2)
    exit()
changed = [False] * len(S)
for i in range(1, len(S)):
    if i > 1 and S[i] == S[i-1] == S[i-2] and changed[i-1]:
        continue
    elif S[i] == S[i-1]:
        changed[i] = True
ans = sum(changed)
res = ans * K
if changed[-1] or S[0] != S[-1] or cnt & 1:
    print(res)
    exit()
res += K - 1
print(res)

Submission status: Accepted

Solution code:
s = input()
k = int(input())
m = 0
n = 0
j = 0
for i in range(len(s)):
    if s.count(s[0]) == len(s):
        print(len(s) * k // 2)
        exit()
    if len(s) <= j + 1:
        if s[0] == s[-1] == s[1] == s[len(s) // 3]:
            n += 1
        break
    if s[j] == s[j + 1]:
        if j == len(s) - 1:
            n += 1
        else:
            m += 1
        j += 1
    j += 1
print(m * k + n * (k - 1))

Submission status: Accepted

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

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

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

    ans = 0
    for b in box:
        ans += b // 2
    print(ans * k)
    exit()

flag = 0
word = s[0]
for si in s:
    if si != word:
        flag = 1

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

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

    ans = 0
    for b in box:
        ans += b // 2
    print(ans)
    exit()


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

front = box[0] // 2
back = box[-1] // 2

mid = 0
for i in range(len(box)-1):
    if i == 0:
        continue
    mid += box[i] // 2

joint = (box[0] + box[-1]) // 2

print(front + back + mid * k + joint * (k-1))

Submission status: Accepted

Solution code:
s = input()
k = int(input())
m = 0
n = 0
j = 0
for i in range(len(s)):
    if s.count(s[0]) == len(s):
        print(len(s) * k // 2)
        exit()
    if len(s) <= j + 1:
        if s[0] == s[-1] == s[len(s) // 3] ==s[1]:
            n += 1
        break
    if s[j] == s[j + 1]:
        if j == len(s) - 1:
            n += 1
        else:
            m += 1
        j += 1
    j += 1
print(m * k + n * (k - 1))

Submission status: Accepted

Solution code:
a=input()
b=int(input())
s=""
zn=0
n=0
nn=0
i=0
aa=[]
ab=[]
abb=0
abc=0

for x in a:
    if aa==[]:
        n+=1
    elif x==s:
        n+=1
    elif x!=s:
        ab.append(n)
        n=1
    s=x
    i+=1
    aa.append(x)
    if i==len(a):
        ab.append(n)
if aa[0]==aa[-1] and len(ab)>2:
    if ab[-1]!=1:
        abb=ab[-1]
        del ab[-1]
    if ab[0]!=1:
        abc=ab[0]
        del ab[0]
    s=aa[0]
    i=0
    for x in aa:
        if aa[i]==s:
            nn+=1
        else:
            break
        i+=1
    i=1
    for x in aa:
        if aa[-i]==s:
            nn+=1
        else:
            break
        i+=1    
for x in ab:
    zn+=(x//2)
if len(a)==1:
    print(b//2)
elif len(ab)==1 and abb==0 and abc==0:
    print((b*ab[0])//2)
else:
    print(zn*b+(nn//2)*(b-1)+abb//2+abc//2)

Submission status: Accepted

Solution code:
s = input()
k = int(input())
m = 0
n = 0
j = 0
for i in range(len(s)):
    if s.count(s[0]) == len(s):
        print(len(s) * k // 2)
        exit()
    if len(s) <= j + 1:
        if s[0] == s[-1] == s[-2]== s[-3]== s[-4]== s[-5]== s[len(s)//3] \
                and s[0] == s[1] == s[2]== s[3]== s[4]== s[5]:
            n+=1
        break
    if s[j] == s[j + 1]:
        if j == len(s) - 1:
            n += 1
        else:
            m += 1
        j += 1
    j += 1
print(m * k + n*(k-1))

Submission status: Accepted

Solution code:
from itertools import groupby

S = input()
K = int(input())
A = []
for s in S:
  A.append(s)

def cont(A):
  B = []
  b = 0
  for key, value in groupby(A):
    B.append([key, list(value)])
  for i in range(len(B)):
    b += len(B[i][1]) // 2
  return b

def plus1(A):
  plu1 = 0
  for i in range(1,len(A)):
    if A[0] == A[i]:
      plu1 += 1
    else:
      break
  return plu1

def plus2(A):
  plu2 = 0
  for j in range(1,len(A)):
    if A[0] == A[-j]:
      plu2 += 1
    else:
      break
  return plu2

X = len(S) - len(list(set(A)))
if len(list(set(A))) == 1:
  an = len(S)*K
  ans = an // 2
elif X == 0:
  ans = 0
elif A[0] != A[-1]:
  ans = cont(A) * K
else:
  if plus1(A)%2 == 0 and plus2(A)%2 == 1:
    ans = cont(A)*K + (K-1)
  else:
    ans = cont(A) * K
print(ans)

Submission status: Accepted

Solution code:
S=list(input())
K=int(input())
c=0
f=1
if len(S) == 1:
    ans=K//2
else:
    for a in range(len(S)-1):
        if S[a] == S[a+1]:
            c+=1
            if a != len(S)-2 and S[a+1] == S[a+2]:
                f+=1
            else:
                c-=f//2
                f=1
    ans=c*K
    m=1
    u=1
    if S[0] == S[len(S)-1]:
        for i in range(len(S)-1):
            if S[i]==S[i+1]:
                m+=1
            else:
                break
        for i in reversed(range(len(S)-1)):
            if S[i]==S[i+1]:
                u+=1
            else:
                break
        if m%2 == 1 and u%2==1:
            if m == len(S):
                ans+=K//2
            else:
                ans+=K-1
print(ans)

Submission status: Accepted

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

# 1種類の文字の連続の場合
if len(set(s)) == 1:
    print(len(s) * k // 2)

else:
    count_l = []
    before_mozi = ''

    for mozi in s:
        if before_mozi == mozi:
            count_l[-1] += 1
        else:
            count_l.append(1)
        before_mozi = mozi

    # 先頭と末端の文字が同じ場合
    if s[0] == s[-1]:
        result = sum([x//2 for x in count_l[1:-1]]) * k + count_l[0]//2 + count_l[-1]//2 + (count_l[0] + count_l[-1])//2 * (k-1)

    else:
        result = sum([x//2 for x in count_l]) * k

    print(result)

Submission status: Accepted

Solution code:
import sys
# input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def main():
    s = input()
    k = int(input())

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

    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

    cnt2 = 1
    ans2 = 0
    s2 = s*2
    for i in range(len(s2)-1):
        if s2[i] == s2[i+1]:
            cnt2 += 1
        else:
            ans2 += cnt2 // 2
            cnt2 = 1
    ans2 += cnt2 // 2

    if s.count(s[0]) == len(s) and len(s) % 2 != 0:
        if k % 2 == 0:
            print(ans2*k//2)
        else:
            print(ans2*(k-1)//2+ans)
        exit()

    if ans * 2 != ans2:
        print(ans*k+k-1)
        exit()

    print(ans*k)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

import math

C = 0
if S[0]==S[-1]:
    C = 1
if all([True if S[0]==S[i] else False for i in range(len(S))]):
    C = 2

f = '!'
count = 0
if C == 0:
    #print('違う時')
    for i in range(len(S)):
        if S[i]==f:
            count += 1
            f = '!'
        else:
            f = S[i]
    print(K*count)
elif C==1:
    #print('同じ時')
    count = 0
    #前何文字同じかチェック
    Fflag = 0
    for i in range(len(S)):
        if S[i]==S[0]:
            Fflag += 1
        else:
            break
    Bflag = 0
    for j in range(len(S))[::-1]:
        if S[j]==S[-1]:
            Bflag += 1
        else:
            break
    f = '!'
    for k in range(Fflag,len(S)-Bflag):
        if S[k]==f:
            count += 1
            f = '!'
        else:
            f = S[k]
    print(math.floor(Fflag/2) + math.floor(Bflag/2)+ count*K + math.floor((Fflag+Bflag)/2)*(K-1))
elif C == 2:
    print(math.floor(len(S)*K/2))

Submission status: Accepted

Solution code:
slist=list(input())
k=int(input())
n=len(slist)
kaisu=0
i=1
shirushi=0
kotae=0
rouge=0
flag=0

for l in range(1,n):
	if slist[l]==slist[l-1]:
		rouge+=1
	else:
		break
		
if rouge==n-1:
	flag=1

if n>1 and flag==0:

	while 1<=i<=n-1:
		if slist[i]==slist[i-1]:
			kaisu+=1
			if i==n-1:
				shirushi=1
			i+=2
		else:
			i+=1
	
	if k>1:
		if shirushi==0 and slist[0]==slist[n-1]: #このとき二回目以降は初項書き換え
			if slist[0]==slist[1]: #やばいパターン
				yabaikaisu=1
				i=2
				shirushi=0
			
				while 1<=i<=n-1:
					if slist[i]==slist[i-1]:
						yabaikaisu+=1
						if i==n-1:
							shirushi=1
						i+=2
					else:
						i+=1
					
				kotae=kaisu+(k-1)*yabaikaisu
			else: #やばくはないパターン
				kotae=k+kaisu*k-1
	
		else: #楽勝パターン
			kotae=kaisu*k
			
	else: #k=1のとき
		kotae=kaisu

elif flag==1:
	kosuu=n*k
	if kosuu%2==0:
		kotae=kosuu/2
	else:
		kotae=(kosuu-1)/2

else: #n=1のとき
	if k%2==0:
		kotae=k/2
	else:
		kotae=(k-1)/2

print(int(kotae))

Submission status: Accepted

Solution code:
from itertools import groupby
s=input()
k=int(input())
l=len(s)
g=groupby(s)
a=[]
for i,j in g:
  a.append(len(list(j)))
if len(a)==1:
  x=len(s)*k
  if x%2==0:
    print(x//2)
  else:
    print((x-1)//2)
else:
  c=0
  for i in a:
    if i%2==0:
      t=i//2
    else:
      t=(i-1)//2
    c+=t
  if s[0]==s[l-1] and k!=1:
    a[0]=a[0]+a[len(a)-1]
    a=a[:len(a)-1]
    z=0
    for i in a:
      if i%2==0:
        t=i//2
      else:
        t=(i-1)//2
      z+=t
    print(z*(k-1)+c)
  else:
    print(c*k)

Submission status: Accepted

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

def func(s, k):
    ans = 0
    n = len(s)
    s_list = list(s)
    for i in range(n - 1):
        if s_list[i] == s_list[i + 1]:
            ans += 1
            s_list[i + 1] = str(i + 1)
    ans *= k
    if s_list[0] == s_list[n - 1]:
        ans += k - 1
    return ans

ans1 = func(s, k)
s_inv = ''.join(list(reversed(s)))
ans2 = func(s_inv, k)

if len(s) % 2 == 1:
    check = True
    for i in range(len(s) - 1):
        if s[i] == s[i + 1]:
            pass
        else:
            check = False
            break
    if check:
        ans3 = len(s) * k // 2
    else:
        ans3 = ans1
else:
    ans3 = ans1

print(min(ans1, ans2, ans3))

Submission status: Accepted

Solution code:
from sys import stdin
S = (stdin.readline().rstrip())
K = int(stdin.readline().rstrip())
ans = 0
N=len(S)
cnt = 1
G = S+"@"
for i in range(0,N):
    if G[i] == G[i+1]:
        cnt += 1
    else:
        if cnt != 1:
            #print(cnt)
            ans += (cnt//2)
            cnt = 1
for_eq = 1
rev_eq = 1
revS = S[::-1]
for j in range(0,N-1):
    if S[j] == S[j+1]:
        for_eq += 1
    else:
        break
for k in range(0,N-1):
    if revS[k] == revS[k+1]:
        rev_eq += 1
    else:
        break
#print(ans)
ans = ans*K
#print(rev_eq,for_eq)
if S[0] == S[-1]:
    ans+=((K-1)*((rev_eq+for_eq)//2))
    ans-=((rev_eq//2)*(K-1))
    ans-=((for_eq//2)*(K-1))
    """
    if (rev_eq % 2 != 0) and (for_eq % 2 != 0):
        ans += ((K+1)//2)
    """
if len(set(S))==1:
    ans = (len(S)*K)//2
print(ans)

Submission status: Accepted

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

ans = 0
now = 0
flag = 1
onaji = False
kazu = 1

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

for i in range(len(s)):
    #print(i,now,ans,flag)
    if now >= len(s) - 1:
        break
    if s[now] == s[now+1]:
       ans += 1
       if now+1 == len(s) -1:
           flag = 0
       now += 2
    else:
        now += 1

if len(s) == 2:
    print(int(ans*k))
    exit()

for i in range(len(s)-1):
    if s[i] != s[i+1]:
        break
    kazu += 1
    if i == len(s) - 2:
        onaji = True

if flag == 1:
    if s[0] == s[-1]:
        if s[0] == s[1]:
            flag = 2
        else:
            flag = 1
    else:
        flag = 0
#print(flag)

if flag != 2:
    print(int(ans*k+flag*(k-1)))
else:
    if onaji == True:
        print(int(ans*k+k//2))
    else:
        if kazu % 2 == 0:
            print(int(ans*k))
        else:
            print(int(ans*k+1*(k-1)))
        
#print(ans,ans*k,ans*k+flag*(k-1))

Submission status: Accepted

Solution code:
import re
import math

def f(S):
    n = len(S)
    if n == 0 or n == 1:
        return 0
    elif n == 2:
        return 1 if S[0] == S[1] else 0
    else:
        if S[-1] == S[-2] and S[-2] == S[-3]:
            return f(S[0:-2]) + 1
        elif S[-1] == S[-2] and S[-2] != S[-3]:
            return f(S[0:-1]) + 1
        else:
            return f(S[0:-1])

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

reg_exp = S[0] + '+'
l = len( re.match(reg_exp, S).group() ) # 最初の文字が連続する長さ

reg_exp = S[-1] + '+$'
r = len( re.findall(reg_exp, S)[0] ) # 最後の文字が連続する長さ

if len(S) == l: # 全て同じ文字
    result = math.floor(len(S) * K / 2)
elif S[0] == S[-1] and l % 2 == 1 and r % 2 == 1:
    result = K * f(S) + K - 1
else:
    result = K * f(S)

print(result)

Submission status: Accepted

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

prev = ""
count1 = 0
count2 = 0
count3 = 0
flag = 0
for i in s:
    if flag == 1:
        prev = i
        flag = 0
        continue
    if prev == i:
        count1 += 1
        flag = 1
    prev = i



for i in s:
    if flag == 1:
        prev = i
        flag = 0
        continue
    if prev == i:
        count2 += 1
        flag = 1
    prev = i

for i in s:
    if flag == 1:
        prev = i
        flag = 0
        continue
    if prev == i:
        count3 += 1
        flag = 1
    prev = i

if k % 2 == 1:
    ret = count1 + (k-1) // 2 * (count2 + count3)
else:
    ret = count1 + ((k-1)//2+1) * count2 + (k-1)//2 * count3

print(ret)

Submission status: Accepted

Solution code:
S = str(input())
K = int(input())
tmp = 1
cnt1 = 0
cnt2 = 0
for i in range(1, len(S)):
    if S[i-1] == S[i]:
        tmp += 1
    else:
        cnt1 += tmp // 2
        tmp = 1
if tmp == len(S):
        print(len(S) * K // 2)
        exit()
if tmp > 1:
    cnt1 += tmp//2
S2 = S + S
tmp = 1
for i in range(1, len(S2)):
    if S2[i-1] == S2[i]:
        tmp += 1
    else:
        cnt2 += tmp // 2
        tmp = 1
if tmp > 1:
    cnt2 += tmp//2
if K == 1:
    print(cnt1)
else:
    print((cnt2 - cnt1) * (K-2) + cnt2)

Submission status: Accepted

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math
import collections


def simple_count(S, changed=False):
    count = 0
    for i in range(len(S) - 1):
        if changed:
            changed = False
        else:
            if S[i] == S[i + 1]:
                changed = True
                count += 1
    return count, changed


def solve(S, K):
    if K == 1:
        return simple_count(S)[0]
    if S[0] != S[-1]:
        return simple_count(S)[0] * K
    else:
        count2, changed = simple_count(S * 2)
        # print(count2, changed)
        if changed:
            return count2 * (K // 2) + simple_count(S, changed=False)[0] * (K % 2)
        else:
            return (count2) * (K // 2) + 1 * (K // 2-1 + K % 2) + simple_count(S, changed=True)[0] * (K % 2)
        # return simple_count(S + S[0])[0] * K


def test(S, K):
    SS = "".join([c for c in reversed(S)])
    # assert solve(S, K) == solve(SS, K), "{} {} = {} {}".format(S, SS, solve(S, K), solve(SS, K))
    assert solve(S, K) == simple_count(S * K)[0], "{} {} = {} {}".format(S, K, solve(S, K), simple_count(S * K)[0])


assert solve("cooooooooonteeeeeeeeeest", 999993333) == 8999939997
assert solve("aqqq", 2) == simple_count("aqqq" * 2)[0]
test("qq", 81)
# print(solve("qqq", 2), simple_count("qqq" * 2)[0])
assert solve("qqq", 2) == simple_count("qqq" * 2)[0]
# print(solve("qqq", 3), simple_count("qqq" * 3)[0])
assert solve("qqq", 3) == simple_count("qqq" * 3)[0]
assert solve("issii", 2) == simple_count("issii" * 2)[0]

test("iqi", 3)
test("iqqqqiiii", 1)
test("iqi", 2)
test("iqqqqiiii", 2)
test("iqii", 3)
test("iqii", 4)
test("iqqqqiiii", 6)
test("iqqqqiiii", 7)
test("qqqqqiiiii", 7)
test("q", 7)
test("qiq", 1)
test("qiq", 3)
test("qiq", 7)
test("qqq", 7)
test("qq", 7)
test("abcdef", 7)
test("qbbbq", 7)
test("qqqq", 7)
test("qqqqq", 7)

S = sys.stdin.readline().rstrip()
K, = map(int, sys.stdin.readline().rstrip().split(" "))
SS = "".join([c for c in reversed(S)])
print(min(solve(S, K), solve(SS, K)))

exit(0)

Submission status: Accepted

Solution code:
s=input()
k=int(input())
p=0
if s[0]==s[-1]:
  p+=1

if len(s)==1:
  print(k//2)
else:
  l=[1]
  for i in range(len(s)-1):
    if s[i]!=s[i+1]:
      l.append(1)
    else:
      l[-1]+=1
  x=[i//2 for i in l]
  y=sum(x)
  if len(l)>1:
    if s[0]==s[-1]:
      if (l[0]+l[-1])%2==1:
        print(y*k)
      else:
        print(y*k+p*(k-1))
    else:
      print(y*k+p*(k-1))
  else:
    print((len(s)*k)//2)

Submission status: Accepted

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

flag = False

itr = 0
cnt = 1
len_cnt = []
for x in S :
  if itr == 0 :
    pre = x
    itr += 1
    continue
  now = x
  if pre == now :
    cnt += 1
    itr += 1
    if itr == len(S) :
      len_cnt.append(cnt)
    continue
  if pre != now :
    len_cnt.append(cnt)
    itr+=1
    if itr == len(S) and cnt == 1 :
      len_cnt.append(cnt)
    cnt = 1
    pre = x
    continue

if sum(len_cnt) != len(S) :
  len_cnt.append(1)

if len(len_cnt) == 1 :
  print(int(len_cnt[0] * K / 2))
  exit()

ans = 0
for x in len_cnt :
  if x == 1 :
    continue
  else :
    ans += int(x / 2)

# if S[0] == S[-1] and len_cnt[-1] > 1 and len_cnt[-1] % 2 == 1 and K > 1:
#   ans += 1
if S[0] == S[-1] and len_cnt[-1] % 2 == 1 and len_cnt[0] % 2 == 1 :
  ans += 1
  flag = True

ANS = K * ans
# print (ANS, len_cnt)
if flag :
  ANS -= 1

# if S[0] ==S[-1] and len_cnt[-1] % 2 == 1 and len_cnt[-1] != 1 :
#   ANS -= 1
print(ANS)

Submission status: Accepted

Solution code:

# import sys
# input = sys.stdin.readline
from collections import Counter
import string


def main():
    # while True:
        S = input()
        K = int(input())
        S_MAP = Counter(S)

        count_1st = 0
        S1 = list(S * 1)
        for i in range(len(S) - 1):
            if S1[i] == S1[i+1]:
                count_1st += 1
                S1[i+1] = '_'
                # for c in string.ascii_lowercase:
                #     if i == 0:
                #         if c != S1[i+1]:
                #             count_1st += 1
                #             S1[i+1] = c
                #             break
                #     else:
                #         if c != S1[i-1] and c != S1[i+1]:
                #             count_1st += 1
                #             S1[i+1] = c
                #             break

        count_2nd = 0
        S2 = list(S * 2)
        for i in range(len(S) * 2 - 1):
            if S2[i] == S2[i+1]:
                count_2nd += 1
                S2[i+1] = '_'

        count_3rd = 0
        S3 = list(S * 3)
        for i in range(len(S) * 3 - 1):
            if S3[i] == S3[i+1]:
                count_3rd += 1
                S3[i+1] = '_'

        count_4th = 0
        S4 = list(S * 4)
        for i in range(len(S) * 4 - 1):
            if S4[i] == S4[i+1]:
                count_4th += 1
                S4[i+1] = '_'

        # count_k = 0
        # SK = list(S * K)
        # for i in range(len(S) * K - 1):
        #     if SK[i] == SK[i+1]:
        #         for c in string.ascii_lowercase:
        #             if i == 0:
        #                 if c != SK[i+1]:
        #                     count_k += 1
        #                     SK[i+1] = c
        #                     break
        #             else:
        #                 if c != SK[i-1] and c != SK[i+1]:
        #                     count_k += 1
        #                     SK[i+1] = c
        #                     break

        # if K <= 4:
        #     if K == 1:
        #         print(count_1st)
        #     elif K == 2:
        #         print(count_2nd)
        #     elif K == 3:
        #         print(count_3rd)
        #     elif K == 4:
        #         print(count_4th)
        # print(len(S_MAP))
        if len(S_MAP) == 1 and len(S) % 2 == 1:
            d1 = count_2nd - count_1st
            d2 = count_3rd - count_2nd
            # print(d1)
            # print(d2)
            print(d1 * K//2 + d2 * K//2)
        # elif len(S)
        else:
            d = count_3rd - count_2nd
            # last_d = count_4th - count_3rd
            print(count_1st + d * (K-1))
            # print(*S1)
            # print(*S2)
            # print(*S3)
            # print(*S4)
            # print(d * K)
        # # print(K, d, count_1st)
        # # print(count_1st, count_2nd, count_3rd)
        # # print(count_k)
        # # print(*SK)


if __name__ == '__main__':
    main()