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())
code_list = 'abcdefghijklmnopqrstuvwxyz'

def c1(s):#len(s)>1
  count1=0
  S = list(s)
  l_s=len(S)
  for i in range(1,l_s):
    #print(S,count)
    if S[i]==S[i-1] and i<l_s-1:
      for j in code_list:
        if j != S[i+1] and j !=S[i-1]:
          S[i]=j
          count1+=1
          break
    elif S[i]==S[i-1]:
      for j in code_list:
        if j != S[i-1] and j !=S[0]:
          S[i]=j
          count1+=1
          break
  return count1,S[-1]

def c2(s,last):#k>2 and len(s)>1
  S=list(s)
  count2=0
  l_s=len(S)
  for i in range(0,l_s):
    #print(S,count)
    if i==0:
      if S[0]==last:
        for j in code_list:
          if j != S[i+1] and j !=last:
            S[i]=j
            count2+=1
            break
    elif S[i]==S[i-1] and i<l_s-1:
      for j in code_list:
        if j != S[i+1] and j !=S[i-1]:
          S[i]=j
          count2+=1
          break
    elif S[i]==S[i-1]:
      for j in code_list:
        if j != S[i-1] and j !=S[0]:
          S[i]=j
          count2+=1
          break
  return count2,S[-1]

def c3(s,last):#k>1 len(s)>1
  S=list(s)
  count3=0
  l_s=len(S)
  for i in range(0,l_s):
    #print(S,count)
    if i==0:
      if S[0]==last:
        for j in code_list:
          if j != S[i+1] and j !=last:
            S[i]=j
            count3+=1
            break
    elif S[i]==S[i-1] and i<l_s-1:
      for j in code_list:
        if j != S[i+1] and j !=S[i-1]:
          S[i]=j
          count3+=1
          break
    elif S[i]==S[i-1]:
      for j in code_list:
        if j != S[i-1]:
          S[i]=j
          count3+=1
          break
  return count3

if len(s)==1:
  print(k//2)
elif k==1:
  c,_=c1(s)
  print(c)
elif k==2:
  count1,last=c1(s)
  count3=c3(s,last)
  print(count1+count3)
else:
  if k%2==0:
    count1,last=c1(s)
    count21,last21=c2(s,last)
    count22,last22=c2(s,last21)
    count3=c3(s,last22)
    #print(count1,count2,count3)
    print(count1+count21*((k-2)//2)+count22*((k-2)//2)+count3)
  else:
    count1,last=c1(s)
    count21,last21=c2(s,last)
    if k>3:
      count22,last22=c2(s,last21)
    else:
      count22=0
    count3=c3(s,last21)
    #print(count1,count21,count22,count3)
    print(count1+count21*((k-2)//2+1)+count22*((k-2)//2)+count3)

Submission status: Accepted

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

def solve():
    global S,K
    if len(set(list(S)))==1:
        return len(S)*K//2

    initchar=S[0]
    initn=0
    for ss in S:
        if initchar==ss:
            initn+=1
        else:
            break
    count=0
    i=1
    while i<len(S):
        if S[i]==S[i-1]:
            count+=1
            S[i]="."
        i+=1
    
    a=(S[0]==S[-1])
    if initn%2==0:
        a=0


    if len(S)==1:
        return (K*len(S))//2
    else:
        return(count*K+a*(K-1))


print(int(solve()))

Submission status: Accepted

Solution code:
S = list(input()) * 2
K = int(input())
Len_S2 = len(S)
Len_S1 = Len_S2 // 2
count1 = 0
count2 = 0
tmp = all(c == S[0] for c in S[:Len_S1:])
for i in range(Len_S2-1):
    if S[i] == S[i+1]:
        if i < Len_S1 - 1:
            count1 += 1
        if i < Len_S2:
            count2 += 1
        S[i+1] = "1"
diff = count2 - count1
if tmp and K >= 3 and Len_S1 % 2 != 0:
    print((count1 + (diff - 1) * (K-1)) + (1 * (K // 2)))
else:
    print(count1 + diff * (K-1))

Submission status: Accepted

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

if len(S) == 1:
    print(K//2)
    exit()
L = []
LS = []
ans = 0
streak = 1
s = ''

for i in range(len(S)-1):
    if i == 0:
        s = S[0]
        streak = 1
    
    if s == '':
        if S[i+1:i+2] == S[i:i+1]:
            s = S[i:i+1]
            streak += 1
    else:
        if S[i+1:i+2] == s:
            streak += 1
        else:
            L.append(streak)
            LS.append(s)
            s = ''
            streak = 1
    
    if i == len(S)-2:
        L.append(streak)
        LS.append(S[-1])
        # if streak == len(S):
        #     ans = K*(len(S)//2)
        #     print(ans)
        #     exit()

# print(LS)
# print(L)
if streak == len(S):
    ans = (len(S)*K)//2
    # print(len(S))
    print(ans)
    exit()

if LS[0] != LS[-1]:
    for l in L:
        # if l >=2:
        ans += l//2
    ans = K*ans
elif LS[0] == LS[-1]:
    for i in range(1,len(L)-1):
        ans += L[i]//2
    ans = K*ans + L[0]//2 + (K-1)*((L[0]+L[-1])//2) + L[-1]//2
print(ans)

Submission status: Accepted

Solution code:
s = input()
k = int(input())
s = list(s)
ind = 0
anss = [[] for i in range(100)]
for i in range(len(s)-1):
  anss[ind].append(s[i])
  if s[i] != s[i+1] and i != len(s)-2:
    ind += 1
  elif i == len(s)-2:
    ind += 1
#print(ind)
#print(anss)
for i in range(100-ind):
  anss.remove([])
#print(anss)
f = 0
pf = 0
if len(anss) == 0:
  anss.append([])
if s[0] == s[len(s)-1] and s[len(s)-2] == s[len(s)-1] and len(anss)>1 and k > 1:
  if len(anss[0]) % 2 == 1:
    pf = 1
  anss[0] += anss[len(anss)-1]
  anss.remove(anss[len(anss)-1])
  anss[0].append(s[len(s)-1])
  if len(anss[0]) % 2 == 0 and pf == 1 and len(anss) > 1:
    f = 1
elif s[0] == s[len(s)-1] and k > 1:
  if len(anss[0]) % 2 == 1:
    pf = 1
  anss[0].append(s[len(s)-1])
  if len(anss[0]) % 2 == 0 and pf == 1 and len(anss) > 1:
    #print(len(anss[0]),pf)
    f = 1
elif s[len(s)-2] == s[len(s)-1] and k > 1:
  anss[len(anss)-1].append(s[len(s)-1])
else:
  anss.append([s[len(s)-1]])
pans = 0
#print(anss)
for i in anss:
  pans += len(i) // 2
#print(f)
if len(anss) == 1 and len(anss[0]) % 2 == 1:
  print((len(s)*k)//2)
elif f == 1:
  print(pans*k-1)
else:
  print(pans*k)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
import sys
s = input()
k = int(input())
# print (s,k)
n=len(s)
# print (s[0],s[n-1])

skip=0
if n == 1: 
    print((k-1) // 2 + (k-1) % 2 )

else:
    flag=0
    for i in range(1,n):
        #print("loop",i)
       if s[i] != s[i-1]:
           flag =1
           break
    
    if flag == 0: 
        print((k*n-1) // 2 + (k*n-1) % 2 )
    else:
        p=0
        q=0
        if s[0] == s[n-1]:
            l=1 
            # Start 
            for i in range(1,n):
                #print("loop",i)
                if s[i] == s[i-1]:
                    p +=1
                else:
                    break
            if l < n:
                for i in range(n-2,0,-1):
                    if s[i] == s[i+1]:
                        q +=1
                    else:
                        break
            l = 1 + p + q
            # print("Ring ", l, p,q)
            op = ((l // 2) + (l % 2)) * (k-1)
            op += ((p // 2) + (p % 2)) 
            op += ((q // 2) + (q % 2))
        else: 
            op = 0     
        
        # print("OP",op)
        
        if s[0] == s[n-1]:
            s2 = s[p+1:n-1-q] 
        else:
            s2 = s
        #print("s2",s2)
        n=len(s2)
                    
        
        l = 0
        for i in range(1,n):
            # print(s2[i],i,l,op)
            if s2[i-1] != s2[i]:
                op += (l // 2 + l % 2) * k
                l = 0 
            else:
                l += 1 
        if l != 0:
            op += (l // 2 + l % 2) * k
        
                
        print(op)

Submission status: Accepted

Solution code:
S=input()
K=int(input())
T=S+S
if len(set(S))!=1:
  A=[]
  D=1
  for i in range(len(S)-1):
    if S[i]==S[i+1]:
      D+=1
    else:
      A.append(D)
      D=1
  A.append(D)
  B=[i for i in A if not i==1]
  C=[(i//2) for i in B]
  Scount=(sum(C))

  E=[]
  H=1
  for j in range(len(T)-1):
    if T[j]==T[j+1]:
      H+=1
    else:
      E.append(H)
      H=1
  E.append(H)
  F=[j for j in E if not j==1]
  G=[(j//2) for j in F]
  Tcount=(sum(G))
  ans=(Tcount-Scount)*(K-1)+Scount
  print(ans)
else:
  print(K*len(S)//2)

Submission status: Accepted

Solution code:
s = input()
k = int(input())
l = len(s)
ans = 0
if k <= 3: s = s*k
else:
    if k % 2 == 0: s = s*4
    else: s = s*5
a = [0]*len(s)
for i in range(len(s)):
    if i > 0 and s[i] == s[(i-1)]: a[i] += 2
    if i < len(s)-1 and s[i] == s[(i+1)]: a[i] += 1
for i in range(len(s)):
    if a[i] == 3:
        a[i] = -1
        if a[i+1] >= 2: a[i+1] -= 2
        if a[i-1] == 1: a[i-1] = 0
#print(a)
for i in range(len(s)-1):
    if a[i] == 1 and a[i+1] == 2:
        a[i] = -1
        a[i+1] = 0
#print(a)
if k <= 3: 
    ans = len([i for i in range(len(s)) if a[i] == -1])
else:
    if k % 2 == 0:
        ans += len([i for i in range(l) if a[i] == -1])
        ans += len([i for i in range(3*l,4*l) if a[i] == -1])
        ans += len([i for i in range(l, 3*l) if a[i] == -1]) * (k//2-1)
    else:
        ans += len([i for i in range(l) if a[i] == -1])
        ans += len([i for i in range(4*l,5*l) if a[i] == -1])
        ans += len([i for i in range(l, 2*l) if a[i] == -1]) * (k//2)
        ans += len([i for i in range(2*l, 3*l) if a[i] == -1]) * (k//2-1)
print(ans)

Submission status: Accepted

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


def c(ss, k):
    s = list(ss)
    s *= k
    n = len(s)
    ret = 0
    for i in range(n - 2):
        if s[i] == s[i + 1] == s[i + 2]:
            s[i + 1] = 'AB' [(i + 1) % 2]
            ret += 1
    for i in range(n - 1):
        if s[i] == s[i + 1]:
            s[i + 1] = 'AB' [(i + 1) % 2]
            ret += 1
    return ret


x1 = c(S, 1)
x2 = c(S, 2)
x3 = c(S, 3)
x4 = c(S, 4)
x5 = c(S, 5)
if K < 100:
    print(c(S, K))
elif K % 2:
    K //= 2
    K -= 1
    print(x3 + K * (x5 - x3))
else:
    K //= 2
    K -= 1
    print(x2 + K * (x4 - x2))

Submission status: Accepted

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

s = [v for v in S]

block = []
count = 1

if len(s) == 1:
    if K == 1:
        print(0)
        exit()
    else:
        if K%2 == 1:
            print((K-1)//2)
            exit()
        else:
            print(K//2)
            exit()

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

z = len(block)
t = 0
d = 0

if not z == 1:
    for j in range(len(block)):
        x = len(block[j])
        if x == 1:
            continue
        else:
            if x%2 == 0:
                t += x//2
            elif x%2 == 1:
                t += x - (x+1)//2
    
    if block[0][0] == block[-1][-1]:
        joint = block[0] + block[-1]
        y = len(joint)
        if y%2 == 1:
            d = 0
        else:
            d = 1
else:
    p = len(block[0])
    if p%2 == 0:
        t = p//2
    else:
        t = (p-1)//2
        if K%2 == 1:
            ans = t + p*(K-1)//2                       
            print(ans)
            exit()
        else:
            ans = p + p*(K-2)//2
            print(ans)
            exit()

ans = t*K + d*(K-1)
print(ans)

Submission status: Accepted

Solution code:
import sys

S=input()
K=int(input())
result=0

a=S[0]
renzoku=0
sumInS=0
for s in S:
    if a==s:
        renzoku=renzoku+1
    else:
        sumInS=sumInS+renzoku//2
        renzoku=1
    a=s
sumInS=sumInS+renzoku//2
result = sumInS*K

if S[0]==S[-1]:
    rst=0
    for s in S:
        if s==S[0]:
            rst=rst+1
        else:
            break
    ren=0
    for s in S[::-1]:
        if s==S[-1]:
            ren=ren+1
        else:
            break
    if rst%2==1 and ren%2==1:
        result=result+(K-1)

if renzoku==len(S):
    total=len(S)*K
    result=total//2

print(result)

Submission status: Accepted

Solution code:
s = list(input().strip())
t = int(input())
ans = 0
change = False
last = False
# 長さ1の時は回数割る2切り捨てだけ変える必要
if len(s) == 1:
    print(t // 2)
    exit()
before = s[0]
count = 1
first = True
for i in range(1, len(s)):
    if before == s[i]:
        count += 1
    else:
        before = s[i]
        # 最初の文字が何個続くか偶奇判定
        if first:
            first = False
            if count % 2 == 1:
                first_odd = True
            else:
                first_odd = False
        ans += count // 2
        count = 1
# 同じ文字のみの場合
if first:
    print(count*t // 2)
    exit()
# 最後の同じ文字列について
ans += count // 2
# 最後の文字が何個続くか偶奇判定
if count % 2 == 1:
    last_odd = True
else:
    last_odd = False

# 最初と最後が同じ文字かつ、最後が奇数の場合
if s[0] == s[-1] and first_odd and last_odd:
    print(ans + (ans + 1) * (t - 1))
else:
    print(ans * t)

Submission status: Accepted

Solution code:
import collections
S =[i for i in input()]
K = int(input())
a = 1
b = []
c = []
SS = len(S)
result = 0
if S[0] != S[-1]:
  try:
    for j in range(len(S)):
      if S[j] == S[j+1]:
        a += 1
      else:
        b.append(a)
        a = 1
  except:
    b.append(a)
  for k in b:
    result += k//2
  print(result*K)
elif len(collections.Counter(S)) == 1:
  print(len(S)*K//2)
else:
  result2 = 0
  for j in range(len(S)):
    if S[j] == S[j + 1]:
      a += 1
    else:
      c.append(a)
      a = 1
      break
  S2 = S[c[0]:]
  try:
    for j in range(len(S2)):
      if S2[j] == S2[j + 1]:
        a += 1
      else:
        c.append(a)
        a = 1
  except:
    c.append(a)
  S = S+S
  S = S[c[0]:-(SS-c[0])]
  try:
    a = 1
    for j in range(len(S)):
      if S[j] == S[j+1]:
        a += 1
      else:
        b.append(a)
        a = 1
  except:
    b.append(a)
  for k in b:
    result += k//2
  for l in c:
    result2 += l//2
  print(result*(K-1)+result2)

Submission status: Accepted

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

def main():
    s = list(input().rstrip())
    k = int(input())


    if len(s) == 1:
        print(k//2)
    else:
        sa = s[:]

        c1 = 0
        c2 = 0

        for i in range(len(s) - 1):
            if sa[i] == sa[i+1]:
                c1 += 1
                sa[i+1] = '1'


        if sa[0] == sa[-1]:
            s[0] = '2'
            c2 += 1

            for i in range(len(s) - 1):
                if s[i] == s[i+1]:
                    c2 += 1
                    s[i+1] = '2'


            if s[-1] == sa[0]:
                print(c1 + c2*(k-1))
            elif s[-1] != sa[0]:
                if k % 2 == 0:
                    x = k // 2
                    print(c1*x + c2*x)
                else:
                    x = k // 2
                    print(c1*(x+1) + c2*x)
        else:
            print(c1*k)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
S, K = open(0).read().split()

cGroups = []
tempList = []
for i, c in zip(range(len(S)), S):
    tempList.append(c)
    if (i == len(S) - 1) or c != S[i + 1]:
        cGroups.append(tempList)
        tempList = []

singleResult = 0
for g in cGroups:
    if len(g) > 1:
        singleResult += len(g) // 2

result = singleResult * int(K)

if cGroups[0][0] == cGroups[-1][0] and len(cGroups[0]) % 2 == 1 and len(cGroups[-1]) % 2 == 1:
    if len(cGroups) == 1:
        result += int(K) // 2
    else:
        result += int(K) - 1

print(result)

Submission status: Accepted

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

l=len(s)
ans=0

if K==1:
  Ls=list(s)
  for n in range(1,l):
    if Ls[n]==Ls[n-1]:
      ans+=1
      Ls[n]=str(n)
elif K==2:
  Ls=list(s+s)
  for n in range(1,2*l):
    if Ls[n]==Ls[n-1]:
      ans+=1
      Ls[n]=str(n)
else:
  ansA=0
  ansB=0
  ansC=0
  Ls=list(s+s+s)
  for n in range(1,l):
    if Ls[n]==Ls[n-1]:
      ansA+=1
      Ls[n]=str(n)
  for n in range(l,2*l):
    if Ls[n]==Ls[n-1]:
      ansB+=1
      Ls[n]=str(n)
  for n in range(2*l,3*l):
    if Ls[n]==Ls[n-1]:
      ansC+=1
      Ls[n]=str(n)
  if Ls[0]==Ls[l]:
    ans=ansA*K
  elif Ls[0]!=Ls[l] and Ls[0]==Ls[2*l]:
    if K%2==0:
      ans=(ansA+ansB)*(K//2)
    else:
      ans=(ansA+ansB)*(K//2)+ansA
  else:
    ans=ansA+ansB*(K-1)
print(ans)

Submission status: Accepted

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

tail = s[0]
plus = 0
flg = False
if tail == s[-1]:
  tmp_cnt1 = 0
  tmp_cnt2 = 0
  for i in s:
    if tail == i:
      tmp_cnt1 += 1
    else:
      break
  for i in s[::-1]:
    if tail == i:
      tmp_cnt2 += 1
    else:
      break
  if (tmp_cnt1 % 2) == 1 and (tmp_cnt2%2)==1:
    plus = 1
  if tmp_cnt1 == len(s):
    flg = True

cnt=0
if len(s)==1:
  ret = k//2
else:
  tmp=s[0]
  tmp_cnt=0
  for i in s[1:]:
    if tmp==i:
      tmp_cnt+=1
    else:
      cnt+=(tmp_cnt+1)//2
      tmp_cnt=0
    tmp=i
  cnt+=(tmp_cnt+1)//2
  ret=cnt*k
  if plus == 1:
    ret += k-1
  if flg:
    ret = len(s)*k//2
print(ret)

Submission status: Accepted

Solution code:
import copy

S=input()
K=int(input())
S=list(S)
n=len(S)
ans=0
j=1

def allsame(str):
    flag = True
    for i in range(len(str)-1):
        if str[i+1]!=str[i]:
            flag=False
            break;
    return flag

if S[0]!=S[n-1]:
    for i in range(n):
        if i==n-1:
            ans += j//2
            break
        if S[i+1]==S[i]:
            j += 1
        else:
            ans += j//2
            j=1
    ans = ans*K
elif allsame(S):
    ans = n*K//2
else:
    i=0
    l=1
    for i in range(n):
        if S[i+1]==S[i]:
            l += 1
        else:
            tempans =l//2
            break;
    T=copy.copy(S)
    T.reverse()
    k=1
    for i in range(n):
        if T[i+1]==T[i]:
            k += 1
        else:
            tempans +=k//2
            break;
        
    for i in range(n-l-k):
        if i==n-l-k-1:
            ans += j//2
            break;
        if S[i+l+1]==S[i+l]:
            j += 1
        else:
            ans += j//2
            j=1
    ans = (ans+(l+k)//2)*K
    ans = ans + tempans -(k+l)//2
    
print(ans)

Submission status: Accepted

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

if not(1 <= len(S) and len(S) <= 100):
    exit()
if not (S.isalpha() and S.islower()):
    exit()
if not float(K).is_integer():
    exit()
if not (1 <= int(K) and int(K) <= 10 ** 9):
    exit()

L = []
tmp = []
pre = ""
for s in S:
    if len(tmp) == 0:
        pre = s
        tmp.append(s)
    elif pre == s:
        tmp.append(s)
    else:
        L.append(tmp)
        pre = s
        tmp = [s]
L.append(tmp)

count = 0
for l in L:
    count += int(len(l) / 2)
    # print(l)
count *= int(K)
if len(L) == 1:
    if len(L[0]) == 1:
        count = int(int(K) / 2)
    elif len(L[0]) % 2 == 1:
        count += int(int(K) / 2)
    
elif L[0][0] == L[len(L)-1][0]:
    if len(L[0]) % 2 == 1 and len(L[len(L)-1]) % 2 == 1:
        count = count + int(K) - 1
print(count)

Submission status: Accepted

Solution code:
import math

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

if l==1 :
  print(int(K/2))
  exit()
  
s0 = ""
c = 0
d = 1
idx = 0
for s in S:
  s1 = s
  if s0==s1:
    d += 1 
    if idx==l-1:
      c += int(d/2)
  else:
    c += int(d/2)
    d = 1
  s0 = s1
#  print(s0, s1, c, d, idx)
  idx = idx+1

c0 = c
#print(c)
if K==1:
  print(c0)
  exit()

s0 = ""
c = 0
d = 1
idx=0
for s in S*2:
  s1 = s
  if s0==s1:
    d += 1 
    if idx==l*2-1:
      c += int(d/2)
  else:
    c += int(d/2)
    d = 1
#  print(s0, s1, c, d, idx)
  s0 = s1
  idx = idx+1

#print(c)
c1 = c-c0
if K==2:
  print(c)
  exit()


s0 = ""
c = 0
d = 1
idx=0
for s in S*3:
  s1 = s
  if s0==s1:
    d += 1 
    if idx==l*3-1:
      c += int(d/2)
  else:
    c += int(d/2)
    d = 1
#  print(s0, s1, c, d, idx)
  s0 = s1
  idx = idx+1

if K==3:
  print(c)
  exit()
#print(c)
c2 = c-c1-c0
  
print(c1*math.ceil((K-1)/2)+c2*math.floor((K-1)/2)+c0)

Submission status: Accepted

Solution code:
def main():
    S = input()
    K = int(input())
    count = 0
    same = True
    for i in range(len(S) - 1):
        if S[i] == S[i+1]:
            continue
        else:
            same = False
            break
    if same:
        count += int((len(S) * K) / 2)
    else:
        i = 0
        while i < len(S):
            c = 1
            flag = False
            if i < len(S) - 1:
                flag = True
                while S[i] == S[i+1]:
                    c += 1
                    i += 1
                    if i + 1 >= len(S):
                        flag = False
                        break
            i += 1
            if c % 2 == 0:
                count += K * int(c / 2)
            else:
                count += K * int((c - 1) / 2)
            if not flag:
                u = 0
                a = 0
                while S[u] == S[-1]:
                    u += 1
                    a += 1
                if c % 2 == 0 and a % 2 == 0:
                    continue
                elif c % 2 == 1 and a % 2 == 0:
                    continue
                elif c % 2 == 0 and a % 2 == 1:
                    continue
                else:
                    count += K - 1
            
    print(count)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
s = input()
k = int(input())
n = len(s)
if len(set(s)) == 1:
    print((n*k)//2)
else:
    arr = []
    cnt = 0
    last = s[0]
    for i in s + '0':
        if i != last:
            arr.append(cnt)
            cnt = 1
            last = i
        else:
            cnt += 1
    ans = 0
    if s[0] == s[-1]:
        ans += (arr[0] + arr[-1])//2*(k-1) + arr[0]//2 + arr[-1]//2
        for i in arr[1:-1]:
            ans += (i//2)*k
    else:
        for i in arr:
            ans += (i//2)*k
    print(ans)

Submission status: Accepted

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

cnt = 0
ans = 0

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

if S[0] == S[len(S)-1]:
    fcnt = 0
    for i in range(len(S) - 1):
        if S[i] == S[i + 1]:
            fcnt += 1
        else:
            break
    bcnt = 0
    for i in range(len(S)-1, 0, -1):
        if S[i] == S[i - 1]:
            bcnt += 1
        else:
            break
    if fcnt%2 == 0 and bcnt%2 == 0:
        if fcnt == len(S)-1:
            print((ans+ans+1)*N//2)
        else:
            print((ans+1)*N-1)
    else:
        print(ans*N)
else:
    print(ans*N)

Submission status: Accepted

Solution code:
t=str(input())
k=int(input())
L = len(t)
tmp = t[0]
n = 0 #change
flag = 0
for i in range(1,L):
  if flag==1:
    flag=0
  elif t[i]==tmp:
    n+=1
    flag=1  
  tmp=t[i]
ans=k*n
if L>2:
  if t[0]==t[-1] and flag==0:
    u = t[0]+t
    tmp = u[0]
    m = 0 #change
    flag = 0
    for i in range(1,L+1):
      if flag==1:
        flag=0
      elif u[i]==tmp:
        m+=1
        flag=1  
      tmp=u[i]
    if m!=n: 
      ans+=k-1

if t.count(t[0])==L:
  ans=(k*L)//2
print(ans)

Submission status: Accepted

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

S = input()
K = int(input().split()[0])

# 連続する文字が奇数かどうか
# 先頭と末尾が異なる場合はシンプル
# 先頭と末尾が同じ場合は、
count_seq_start = 1  # 先頭からの連続文字数
count_seq_end = 1  # 末尾の連続文字数agca

# 先頭からの連続文字数を数える
for i in range(1, len(S)):
    if S[i] == S[0]:
        count_seq_start += 1
    else:
        break

# 末尾からの連続文字数を数える
count_seq_end = 1  # 末尾の連続文字数

for i in range(1, len(S)):
    if S[-1 - i] == S[-1]:
        count_seq_end += 1
    else:
        break

unit_c = 0
count_seq = 1
for i in range(1, len(S)):
    if S[i] == S[i - 1]:
        count_seq += 1
    else:
        # count_seqの偶奇判定
        if count_seq % 2 == 0:
            unit_c += count_seq // 2
        else:
            unit_c += (count_seq - 1) // 2
        count_seq = 1

# 末尾まで連続していた時の処理
# 末尾が1文字だけの場合は、unit_cには0が足されるのでOK
# 末尾が2文字以上の場合、まだunit_cにはカウントされていないのでここで足してOK
if count_seq % 2 == 0:
    unit_c += count_seq // 2
else:
    unit_c += (count_seq - 1) // 2

c = 0
c += unit_c * K

if S[0] == S[-1] and count_seq_start % 2 == 1 and count_seq_end % 2 == 1:
    c += K - 1

is_all = True
for i in range(1, len(S)):
    if S[i] == S[0]:
        pass
    else:
        is_all = False
        break

if is_all:
    if (len(S) * K) % 2 == 0:
        c = len(S) * K // 2
    else:
        c = (len(S) * K - 1) // 2

ans = int(c)
print(ans)

Submission status: Accepted

Solution code:
import sys
s = input()
k = int(input())
ret = 0
t = 0
ss = []
ss.append(s[0])
for i in range(1, len(s)):
    if s[i-1] == s[i]:
        ss[-1]+=s[i]
    else:
        ss.append(s[i])

for ss_ in ss[1:-1]:
    ret += len(ss_) // 2 * k

if len(ss) >= 3:
    if ss[0][0] != ss[-1][0]:
        ret += len(ss[0]) // 2 * k
        ret += len(ss[-1]) // 2 * k
    else:
        ret += (len(ss[-1]) + len(ss[0]))  // 2 * (k-1)
        ret += len(ss[0]) // 2
        ret += len(ss[-1]) // 2

if len(ss) == 1:
    ret += len(ss[0]) * k // 2

if len(ss) == 2:
    ret += len(ss[0]) * k // 2
    ret += len(ss[1]) * k // 2

print(ret)

Submission status: Accepted

Solution code:
s = list(input())
k = int(input())
lis = []
n = 1
for i in range(len(s)-1):
    if s[i] == s[i + 1]:
        n += 1
    else:
        lis.append(n)
        n = 1
lis.append(n)
if len(lis) == 1:
    print(len(s) * k // 2)
else:
    if s[0] == s[len(s) - 1]:
        if len(lis) >= 2:
            m = lis[0] // 2
            m = m + lis[len(lis)-1]//2
            m = m + (lis[0] + lis[len(lis)-1]) // 2 * (k - 1)
        if len(lis) >= 3:
            for i in range(len(lis) - 2):
                m = m + lis[i + 1] // 2 * k
        print(m)
    else:
        m = 0
        for i in range(len(lis)):
            m = m + lis[i] // 2 * k
        print(m)

Submission status: Accepted

Solution code:
s=input()
k=int(input())
ans=0
if len(set(list(s)))==1:
  ans=(k*len(s))//2
  print(ans)
  exit()
x=0
y=0
t=s*2
for i in range(len(s)-1):
  if s[i]==s[i+1]:
    x+=1
    s=s[:i+1]+'0'+s[i+2:]
for i in range(len(t)-1):
  if t[i]==t[i+1]:
    y+=1
    t=t[:i+1]+'0'+t[i+2:]   
ans=x+(k-1)*(y-x)
print(ans)

Submission status: Accepted

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

ans = 0
tmp = 0
head = 1
hip = 1
if S[0] == S[-1]: key = True
else: key = None
itr = 0
b = 0
for s1, s2 in zip(S[:-1], S[1:]):
    if s1 == s2:
        tmp += 1
    else:
        ans += (tmp+1)//2
        if (key == True) and (b == 0):
            head = tmp
            b+=1
        tmp = 0

    if (key == True) and (itr == len(S)-2):
        hip = tmp
    itr += 1
ans += (tmp+1)//2
ans = ans*K
if key==True and b==0:
    ans = len(S)*K//2
if (head%2 == 0) and (hip%2 == 0):
    ans += K-1
print(ans)

Submission status: Accepted

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

def f(s):
    i = 0
    n = 0
    while i < len(s)-1:
        if s[i] == s[i+1]:
            n += 1
            i += 2
        else:
            i += 1
    return n

def main():
    S = input().strip()
    K = int(input())
    a1 = f(S)
    a2 = f(S*2)
    a3 = f(S*3)
    if K == 1:
        print(a1)
        return
    if K == 2:
        print(a2)
        return

    r1 = a2 - a1
    r2 = a3 - a2
    ans = a1 + r1 * (K//2) + r2 * ((K-1)//2)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s =  input()
num = int(input())
out = 0

cha = s[0]
countf = 1
countl = 0
for i in range((len(s)-1)//2):
  if s[i+1] == cha:
    countf += 1
  else:
    break
for i in range((len(s)+1)//2):
  if s[-i-1] == cha:
    countl += 1
  else:
    break

fs = s[:-countl]
ms = s[countf:-countl]
ls = s[countf:]

if countf == 1 and countl == 0:
  fs = s
  ms = s
  ls = s

if countf != 1 and countl == 0:
  fs = s
  ms = s[countf:]
  ls = s[countf:]
  
count = 1
for i in range(len(fs)-1):
  if fs[i] == fs[i+1]:
    count += 1
  else:
    out += count//2
    count = 1
out += count//2
  
count = 1
for i in range(len(ls)-1):
  if ls[i] == ls[i+1]:
    count += 1
  else:
    out += count//2
    count = 1
out += count//2    

count = 1
tout = 0
for i in range(len(ms)-1):
  if ms[i] == ms[i+1]:
    count += 1
  else:
    tout += count//2
    count = 1
tout += count//2
out += tout*(num-2)

out += ((countf+countl)//2) * (num-1)

if s.count(s[0]) == len(s):
  out = (len(s)*num)//2

print(out)

Submission status: Accepted

Solution code:
S = input()
K = int(input())
a = []
ans = 0
count = 0
for i in range(len(S)-1):
    if S[i] == S[i+1] :
        count += 1
    else:
        if count!=0:
            ans += (count+1)//2
            a.append(count+1)
            count = 0
if count !=0:
    ans += (count+1)//2 
ans = ans*K
a.append(0)
if len(S) == 1:
    ans = K//2
else:    
    
    if (count+1)%2==1 and a[0]%2 == 1 and S[0]== S[1] and S[0] == S[len(S)-1]:
    
        ans+=K-1
    if S[0]!= S[1] and (count+1)%2 ==1 and  S[0] == S[len(S)-1]:
        ans+=K-1
    if count+1 == len(S):
        if len(S)%2==1:
            ans = (len(S)*K)//2
 
print(ans)

Submission status: Accepted

Solution code:
import copy

S = input()
K = int(input())
S_list = list(S)

if len(S_list) == 1:
    print(K//2)
else:
    for i in range(1, len(S_list)):
        if S_list[i-1] == S_list[i]:
            S_list[i] = "change"
    S1_count = S_list.count("change")

    if S_list[0] == S_list[-1]:
        S2_list = list(S)
        S2_list[0] = "change"
        for i in range(1, len(S2_list)):
            if S2_list[i-1] == S2_list[i]:
                S2_list[i] = "change"
        S2_count = S2_list.count("change")
        if S2_list[-1] == S_list[0]:
            print(S1_count + (K-1)*S2_count)
        else:
            print((((K//2)+1)*S1_count) + ((K//2)*S2_count))
    else:
        print(K * S1_count)

Submission status: Accepted

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

import re

def nchars(s, n):
    assert n > 0
    reg = re.compile("(.)\\1{%d,}" % (n - 1))
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]

if len(S)==1:
  print(K//2)
  
elif S[0]!=S[-1]:
  S_l = list(nchars(S,2))
  answer = 0
  for s in S_l:
    answer += len(s)//2
  print(answer*K)

else:
  if (S[0]!=S[1])&(S[-1]!=S[-2]):
    answer = 0
    S_lr=0
    S_l = list(nchars(S,2))
    for s in S_l:
      S_lr += len(s)//2

    answer = S_lr + (1+S_lr)*(K-1)
    print(answer)
  
  elif (S[0]!=S[1])&(S[-1]==S[-2]):
    answer = 0
    S_lr=0
    S_l = list(nchars(S,2))
    for s in S_l:
      S_lr += len(s)//2
    
    a=len(S_l[-1])
    answer = S_lr + ((a+1)//2 + (S_lr-a//2))*(K-1)
    print(answer)
  
  elif (S[0]==S[1])&(S[-1]!=S[-2]):
    answer = 0
    S_lr=0
    S_l = list(nchars(S,2))
    for s in S_l:
      S_lr += len(s)//2
    
    a=len(S_l[0])
    answer = S_lr + ((a+1)//2 + (S_lr-a//2))*(K-1)
    print(answer)
    
  else:
    answer = 0
    S_lr=0
    S_l = list(nchars(S,2))
    if len(S_l)==1:
      print(len(S)*K//2)
    else:
      for s in S_l:
        S_lr += len(s)//2

      a=len(S_l[0])
      b=len(S_l[-1])
      answer = S_lr + ((a+b)//2 + (S_lr-a//2-b//2))*(K-1)
      print(answer)

Submission status: Accepted

Solution code:

s = input()
check = False
if len(set(s)) == 1:
    check = True
n = len(s)
K = int(input())
s = s*2
s = list(s)
cnt = 0
for i in range(1, len(s)):
    if s[i] == s[i-1]:
        s[i] = 'A'
        cnt += 1
res = 0
if cnt % 2 == 1:
    res = (cnt//2)*K + K-1
else:
    res = cnt//2 * K
if check:
    res = n * K // 2
if n == 1:
    res = K//2
print(res)