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: Wrong Answer

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

    c = []
    
    i = 0
    if S[0] == S[-1]:
        connected = True
    else:
        connected = False

    while i < len(S):

        char = S[i]

        cnt = 1
        cnt2 = 1
        for j in range(i + 1, len(S) * 2):
            i = j
            if char == S[j % len(S)]:
                if j >= len(S):
                    cnt2 += 1
                else:
                    cnt += 1
                    cnt2 +=1
            else:
                if j-1 < len(S):
                    cnt2 = 0
                    i = j
                else:
                    i = len(S)
                break
        
        if cnt >= 2:
            c.append([char, cnt, cnt2])
        else:
            c.append([char, 0, cnt2])
    
    #  print(c)

    ans = 0
    flg = True
    for elem in c:
        if connected:
            if flg and len(c) >= 2:
                flg = False
                continue
            if len(c) == 1:
                ans = elem[1] * K // 2 
            elif elem[2] >= 2:
                ans += (elem[2] // 2) * (K - 1)  + (elem[1] // 2) + c[0][1] // 2
            else:
                ans += (elem[1] // 2) * K

        else:
            ans += (elem[1] // 2) * K
        
    print(ans)

if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

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:
    ans+=k-1
    
if t.count(t[0])==L:
  ans=(k*L)//2
print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
d= [c  for c in s]
n=int(input())
l=len(d)
count=0
singi=0

for i in range(l-2):
  if i==0 and d[0]==d[l-1] and d[i]==d[i+1]:
    d[0]="1"
    count+=1
  if d[i]==d[i+1]:
    if d[i]!=d[i+2]:
       d[i]="1"
       count+=1
    elif d[i]==d[i+2]:
      d[i+1]="1"
      count+=1
if d[l-2]==d[l-1] and d[0]!=d[l-1]:
  count+=1
elif d[l-2]!=d[l-1] and d[0]==d[l-1]:
  count+=1
  singi=1
if singi==1:
   print(count*n-1)
else:
  print(count*n)

Submission status: Wrong Answer

Solution code:
S, K = open(0).read().split()
# print(S, K)
# print(alpha)
# print(type(S))
list_s = list(S)
# print(list_s)
cnt = 0
for i in range(len(list_s)-1):
    if list_s[i] == list_s[i+1]:
        list_s[i+1] = "_"
        cnt += 1 
#     print(list_s)
# print(cnt)
# print(list_s)
print(cnt * int(K))

Submission status: Wrong Answer

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

ss = s + s

def counter(string):
    count = 0
    answer = 0
    prev = ""
    for index,sss in enumerate(string):
        if prev == "" or sss == prev:
            count+=1
            if count == 2: 
                answer+=1
                count=0
                string[index] = "1"
        else:
            count=1
        prev = sss
    return answer,string

kk = k//2

if k%2 == 0: 
    answer, string = counter(ss)
    if string[0] == string[-1]:
        print(answer*kk+kk)
    else:
        print(answer*kk)
else:
    answer, string = counter(s)
    answer2, string2 = counter(ss)

    if string2[0] == string2[-1]:
        print(answer2*kk + answer + kk)
    else:
        print(answer2*kk + answer)

Submission status: Wrong Answer

Solution code:
S = list(input())
N = len(S)
K = int(input())
T = S.copy()
X = S.copy()
X += T
S += T
ans = 0
cnt = 0

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

for i in range(1, N):
    if S[i - 1] == S[i]:
        cnt += 1
        S[i] = "-1"
if T[0] == T[N - 1]:
    # print(cnt)
    # cnt2 = 0
    # for i in range(1, N):
    #     if T[i] == T[i - 1]:
    #         cnt2 += 1
    #         T[i] = "-1"
    # ans = cnt + cnt2 * (K - 1)
    # print(cnt2)
    cnt1 = 0
    skip = set()
    for i in range(N):
        if i in skip:
            continue
        if X[i + 1] == X[i]:
            cnt1 += 1
            skip.add(i + 1)
    ans = min(cnt * (K // 2) + cnt1 * ((K + 1) // 2), cnt1 * ((K + 1) // 2) + cnt * (K // 2))
else:
    ans = cnt * K
print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
t = s[:]
c = 0

if len(s) == 1:
    print(k // 2)
else:
    t.append(s[0])
    # print(t)
    for i in range(1, len(t) - 1):
        if t[i - 1] == t[i]:
            c += 1
            t[i] = "AA"
    # print(t)
    c *= k
    if t[-1] == t[-2]:
        c += k - 1
    print(c)

Submission status: Wrong Answer

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 > 1:
    if tmp == len(S):
        print(len(S) * K // 2)
        exit()
    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: Wrong Answer

Solution code:
s = list(input())
k = int(input())
a = '0'
cnt = 0
flag = 1
out = 0

for i in range(len(s)-1):
    if s[i] == s[i+1]:
        s[i+1] = a
        cnt += 1

if s[0] == s[len(s)-1]:
    cnt += 1
    flag = -1
    
out = cnt * k

if flag == -1:
    out -= 1
if len(s) >= 2:
	print(out)
if len(s) == 1:
	print(k//2)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
 
c = S[0]
isSameCahr = True
for i in range(0, len(S)):
    if c != S[i]:
        isSameCahr = False
        break

if isSameCahr:
    print(int(len(S) * K / 2))
else:    
    count = 0
    bef = ""
    isbefchange = False
    for i in range(0, len(S)):
        if(isbefchange == False and S[i] == bef):
            count += 1
            isbefchange = True
            bef = S[i]
            continue
        bef = S[i]
        isbefchange = False
    count *= K
    if(isbefchange == False and S[len(S)-1] == S[0]):
        count += (K-1)
    print(count)

Submission status: Wrong Answer

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 and 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
count3=0
S=list(s)
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 and 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] and j !=S[0]:
          S[i]=j
          count3+=1
          break
  return count3

if len(s)==1:
  print(k-1)
elif k==1:
  c,_=c1(s)
  print(c)
elif k==2:
  count1,last=c1(s)
  count3=c3(s,last)
  print(count1+count3)
else:
  count1,last=c1(s)
  count2=c3(s,last)
  count3=c3(s,last)
  print(count1+count2*(k-2)+count3)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
c1 = 0
xd = 0
def func(plane,v):
    global xd
    c2 = 0
    while True:
        if v == len(s)-1:
            break
        elif s[v] == plane:
            c2 += 1
        else:
            break
        v += 1
    xd = c2
    if c2 == 1:
        return 1
    else:
        return c2//2
first = 0
last = 0
mid = 0
midlist = []
if s[0] == s[-1]: #最初と最後の文字が同じ時
    for i in range(1,len(s)):
        if i == len(s)-1: #全て同じ文字なら
            if len(s)%2 == 0:
                print(func(s[0],0)*k)
                exit()
            else:
                print(func(s[0],0)*k +1)
                exit()
        elif s[0] == s[i]:
            first += 1
        else:
            midlist += s[:first+1]
            del s[:first+1]
            if first != 1:
                first = first//2
            break
    for i in range(len(s)-2,-1,-1):
        if s[-1] == s[i]:
            last += 1
        else:
            midlist += s[len(s)-1-last:]
            del s[len(s)-1-last:]
            if last != 1:
                last = last//2
            break
    mid = len(midlist)//2*(k-1)

#中央の連続部分の処理
i = 0
z = 0
while True:
    if i == len(s)-1:
        break
    elif s[i] == s[i+1]:
        z += func(s[i],i)
        i += xd
    else:
        i+=1

print(first+last+mid+z*k)

Submission status: Wrong Answer

Solution code:
from sys import stdin
S = (stdin.readline().rstrip())
K = int(stdin.readline().rstrip())
ans = 0
N=len(S)
cnt = 1
G = S+"@"
flag = False
for i in range(0,N):
    if G[i] == G[i+1]:
        cnt += 1
        flag = True
    else:
        if cnt != 1:
            #print(cnt)
            ans += (cnt//2)
            cnt = 1
        #print(ans,flag)
if flag != True:
  ans = 0
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 K != 1:
    if S[0] == S[-1]:
        ans+=((K-1)*((rev_eq+for_eq)//2))
        ans-=(rev_eq//2)
        ans-=(for_eq//2)
        """
        if (rev_eq % 2 != 0) and (for_eq % 2 != 0):
            ans += ((K+2)//2)
        """
        """
        if rev_eq == 1 and for_eq != 1:
            ans -= (K//2)
        if rev_eq != 1 and for_eq == 1:
            ans -= (K//2)
        """
        if (flag != True) and K == 1:
            ans = 0
if len(set(S))==1:
    ans = (len(S)*K)//2

print(ans)

Submission status: Wrong Answer

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

count = 0 
f = False
for index, st in enumerate(S):
  try:
    if st == S[index + 1]:
      if f == True:
        f = False
      else:
      	count += 1
      	f = True
      
    else:
      f = False

  except:
    pass
  
if len(S) > 2:
	if S[-1] == S[-2]:
		if S[-2] == S[-3]:
			count = count * K + count - 1
		else:
			count = count * K
    	
	else:
  		count = count * K
        
elif len(S) == 1:
  if K > 3:
    count = int(K / 3) + 1
  else:
    count = K - 1
else:
  count = count * K 
 
print(count)

Submission status: Wrong Answer

Solution code:
import math
def is_even(num):
    if num % 2 == 0:
        return True
    else:
        return False

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

s_pre=""
flag=1
ret=0
st=0
for i,s in enumerate(S):
  if s==s_pre:
    flag+=1
    if (i+1)==flag:
      st+=1
    
  else:
    if not(is_even(flag)):
      ret+=(flag-1)//2
    else:
      ret+=flag//2
    flag=1
  s_pre=s

if flag!=1:
  if not(is_even(flag)):
  	ret+=(flag-1)//2
  else:
    ret+=flag//2

if ret==0:
  ret+=st
  
  
st+=1

s_pre=""
fi=0
fiflag=1
for i,s in enumerate(S[::-1]):
  if s==s_pre:
    fiflag+=1
    if (i+1)==fiflag:
      fi+=1
    else:
      break
  
  s_pre=s
fi+=1

if S[0]==S[-1]:
  if st==1 and fi==1:
    print(ret+K-1)
  else:
    if not(is_even(st)) and not(is_even(fi)):
      print((2*ret)+(ret+1)*(K-2))
    else:
      print(ret*K)
else:
  print(ret*K)

Submission status: Wrong Answer

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:
        if streak >= 2:
            L.append(streak)
            LS.append(s)
        if streak == len(S):
            ans = K*(len(S)//2)
            print(ans)
            exit()

if LS[0] != LS[-1]:
    for l in L:
        if i >=2:
            ans += l//2
    ans *= K
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: Wrong Answer

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):
        print((len(S)//2)+K-1)
 
print(ans)

Submission status: Wrong Answer

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

sl = list(s)
cnt = 1
i = 0
su = 0
tmp = "0"
f = 0
ff =0
fst =0
lst = 0

for i in range(len(s)):
  if sl[i] == tmp:
    cnt += 1
  else:
    su += int(cnt / 2)
    if ff == 1:
      fst = cnt
    ff += 1
    cnt = 1
    tmp = sl[i]

if cnt != 1:
  su += int(cnt / 2)
  lst = cnt

if sl[0] == sl[-1]:
  if (fst == 0) or (fst % 2 != 0):
    if lst % 2 != 0:
      f = k / 2

t = k * su + f

print(int(t))

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
ans = 0
    
for i in range(len(s) - 1):
    if s[i] == s[i + 1]:
        s[i + 1] = "Z"
        ans += 1
if s[0] == s[-1]:
    print(ans*k + k - 1)
else:
    print(ans*k)

Submission status: Wrong Answer

Solution code:
a=input()
b=int(input())
s="A"
zn=0
n=0
nn=0
i=0
aa=[]
ab=[]
abb=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)!=1:
    if ab[-1]!=1:
        abb=ab[-1]
        del ab[-1]
    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:
    print((b*ab[0])//2)
else:
    print(zn*b+(nn//2)*(b-1)+abb//2)

Submission status: Wrong Answer

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

count = 0 
f = False
for index, st in enumerate(S):
  try:
    if st == S[index + 1]:
      if f == True:
        f = False
      else:
      	count += 1
      	f = True
      
    else:
      f = False

  except:
    pass
  
if len(S) > 2:
	if S[-1] == S[-2]:
		if S[-2] == S[-3]:
			count = count * K + count - 1
		else:
			count = count * K
    	
	else:
  		count = count * K
        
elif len(S) == 1:
  if K > 3:
    count = int(5 / 3) + 1
  else:
    count = K - 1
else:
  count = count * K 
 
print(count)

Submission status: Wrong Answer

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 and 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
count3=0
S=list(s)
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 and 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] and j !=S[0]:
          S[i]=j
          count3+=1
          break
  return count3

if len(s)==1:
  print(k-1)
elif k==1:
  c,_=c1(s)
  print(c)
elif k==2:
  count1,last=c1(s)
  count3=c3(s,last)
  print(count1+count3)
else:
  count1,last=c1(s)
  count2=c3(s,last)
  count3=c3(s,last)
  print(count1+count2+count3)

Submission status: Wrong Answer

Solution code:
X=input()
a=int(input())
count=0
ans=0
z=list(X)
b=0
if len(z)==1:
    ans=int(a/2)
elif z[0]!=z[len(z)-1]:
    for i in range(len(z)-1):
        if z[i]==z[i+1]:
            z[i+1]=0
            count=count+1
        ans=count*a
else:
     for k in range(len(z)-1):
        if z[k]==z[len(z)-1]:
            b+=1
     for i in range(len(z)-1):
        if z[i]==z[i+1]:
            z[i+1]=0
            count=count+1
     ans=int(a*count+(a-1)*(((b+1)/2)-1))

print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
N = len(S)
cnt = 0
flg1 = False
flg2 = False
flg3 = False
flg4 = False
flg5 = False
for i in range(N-1):
    if S[i] != S[i+1]:
        flg3 = True
        break
if (flg3 == False) and N % 2 == 1:
    print((N*K)//2)
    exit()
if (S[0] == S[N-1]) and K != 1:
    if S[0] != S[1]:
        cnt += 1
        flg1 = True
        flg5 = True
    if (3 <= N) and (S[1] == S[2]) and (flg5 == False):
        cnt += 1
        flg1 = True
if (S[N-2] == S[N-1]):
    flg4 = True
for i in range(N-1):
    if flg2:
        flg2 = False
        continue
    if (i == N - 2) and flg1:
        break
    if S[i] == S[i+1]:
        cnt += 1
        flg2 = True
if flg1 and flg4 == False:
    print(cnt*K-1)
else:
    print(cnt*K)

Submission status: Wrong Answer

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

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

if S[0] != S[-1]:
    change = 0
    T = list(S)
    for i, t in enumerate(T[1:-1], start=1):
        if t == T[i - 1] and T[i - 1] != '.':
            change += 1
            T[i] = '.'
    print(change * K)
else:
    T1 = list(S)
    T1[0] = '.'

    change1 = 0
    for i, t in enumerate(T1[1:], start=1):
        if t == T1[i - 1] and T1[i - 1] != '.':
            change1 += 1
            T1[i] = '.'

    T2 = list(S)
    change2 = 0
    for i, t in enumerate(T2[1:-1], start=1):
        if t == T2[i - 1] and T2[i - 1] != '.':
            change2 += 1
            T2[i] = '.'

    ans = min(
        change1 * K + K - (1 if S[0] != S[1] else 0),
        change2 * K + K - (1 if T2[-2] != T2[-1] else 0)
    )

    print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
count = 0
i = 0
while i <= len(s):
    tmp = 1
    for j in range(i + 1, len(s)):
        if s[i] == s[j]:
            tmp += 1
        else:
            break
    i += tmp
    tmp //= 2
    count += tmp

if s[0] == s[len(s)-1]:
    s = s[::-1]
    length = 0
    for i in range(len(s)-1):
        for j in range(i, len(s)):
            if s[i] == s[j]:
              length += 1
            else:
              break
    if length % 2 != 0:
        count += 1

print(count * k)

Submission status: Wrong Answer

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

if len(S) == 1:
    print(K // 2)
    exit()
res, flag = 0, False
for i in range(1, len(S)):
    if i > 1 and S[i] == S[i-1] == S[i-2] and flag:
        flag = False
    elif S[i] == S[i-1]:
        res += 1
        flag = True
res *= K

if S[0] == S[-1] and not flag and S[-1] == S[-2]:
    res += K - 1
print(res)

Submission status: Wrong Answer

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

ops = 0
i = 1
last = 1
while i < len(s):
    last = 1
    if s[i-1] == s[i]:
        ops += 1
        i += 1
        last = 2
    i += 1

i = 1

ops2 = 0

ss = s + s

while i < len(ss):
    if ss[i-1] == ss[i]:
        ops2 += 1
        i += 1
    i += 1

ops = (k // 2) * ops2 + (k%2) * ops

print(ops)

Submission status: Wrong Answer

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
#s='AAABBCABBB'

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

p(m)

if m[0][0]==m[-1][0]:

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

	ans+=(m[0][1]//2)
	ans+=(m[-1][1]//2)

	ans+=((m[0][1]+m[-1][1])//2)*(k-1)

else:
	for a,i in m:
		#print(a,i)
		ans+=(i//2)


print(ans)

Submission status: Wrong Answer

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] and s[-1]!=s[-2] and s[0]!=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: Wrong Answer

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:
  if s[0]==s[l-1] and k!=1:
    a[0]=a[0]+a[len(a)-1]
    a=a[:len(a)-1]
  c=0
  for i in a:
    if i%2==0:
      t=i//2
    else:
      t=(i-1)//2
    c+=t
  print(c*k)

Submission status: Wrong Answer

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:
        print(s[0],s[-1])
        if s[0] == s[-1] and s[-1]!=s[-2] and s[0]!=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: Wrong Answer

Solution code:
S = list(input())
K = int(input())
#S = list("aabca")
#K = 2
if S.count(S[0]) == len(S):
    print(len(S) * K // 2)
    exit()

cnt = 0
continuous = 0
chr = S[0]
for i in range(1, len(S)):
    if chr == S[i]:
        cnt += 1
    if chr != S[i] or i == len(S) - 1:
        continuous += (cnt + 1) // 2
        cnt = 0
    chr = S[i]
if continuous == 0 and S[0] == S[-1]:
    print(K - 1)
else:
    if S[-1] == S[0] and S[0] == S[1] or S[-1] == S[0] and S[0] == S[-2]:
        print(K * continuous)
    elif S[0] == S[-1] and S[-2] != S[0] and S[2] != S[0]:
        print(K * continuous + K - 1)
    else:
        print(K * continuous)

Submission status: Wrong Answer

Solution code:
S = input().replace(" ", "")
K = int(input())

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

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"
  
if len(S) == 1:
  print(K//2)
elif n1 == (n2 - n1) and (n2 - n1) == (n3 - n2):
  print(int(n1 * K))
else:
  if K % 2 == 1:
    print(int(n1 + (n3 - n1) * (K - 1)//2))
  else:
    print(int(n2 * K / 2))

Submission status: Wrong Answer

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

ss = s + s

def counter(string):
    count = 0
    answer = 0
    prev = ""
    for index,sss in enumerate(string):
        if prev == "" or sss == prev:
            count+=1
            if count == 2: 
                answer+=1
                count=0
                string[index] = "1"
        else:
            count=1
        prev = sss
    return answer,string

kk = k//2

if k%2 == 0: 
    answer, string = counter(ss)
    if string[0] == string[-1]:
        print(answer*kk+k-1)
    else:
        print(answer*kk)
else:
    answer, string = counter(s)
    answer2, string2 = counter(ss)

    if string2[0] == string2[-1]:
        print(answer2*kk + answer + k-1)
    else:
        print(answer2*kk + answer)

Submission status: Wrong Answer

Solution code:
S = list(input())
K = int(input())
A = ['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']
num = 0
TTL_num = 0
first = 0

for i in range(len(S)-1):
	if S[i] == S[i+1]:
		if i <= (len(S)-3):
			for j in range(len(A)):
				if S[i] != A[j] and S[i+2] != A[j]:
					S[i+1] == A[j]
					num += 1
					break
		if i == (len(S)-2):
			for k in range(len(A)):
				if S[i] != A[k] and S[0] != A[k]:
					S[i+1] == A[k]
					num += 1
					break
	if i == (len(S)-1) and S[i+1] == S[0]:
		for l in range(len(A)):
			if S[0] != A[l]:
				S[i+1] == A[l]
				num += 1
				first += 1
				break

if first == 1:
	TTL_num = num*K -1
else:
	TTL_num = num*K

print(TTL_num)

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
T0=0
count=1
ans=0
l=0
c0=0

for i in range(len(S)):
	if S[i]==T0:
		count+=1
	else:
		ans+=count//2
		l+=1
		if l==2:
			c0=count
		count=1
	T0=S[i]

if S[0]==S[-1]:
	a=count+c0
else:
	a=count

print(c0//2+(ans-c0//2)*K+(a//2)*(K-1)+count//2)

Submission status: Wrong Answer

Solution code:
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    S = input()
    K = int(input())

    if len(S) == 1:
        s = S * K
    else:
        s = S + S

    num = 0
    l, prev = 1, ''
    for si in s:
        if si == prev:
            l += 1
        else:
            num += l // 2
            l = 1
        prev = si
    num += l // 2

    if len(S) == 1:
        print(num)
    else:
        n1 = num // 2
        n2 = (num + 1) // 2
        print(n1 + n2 * (K - 1))


main()

Submission status: Wrong Answer

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
#s='AAABBCABBB'

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

p(m)

if m[0][0]==m[-1][0]:

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

	ans+=(m[0][1]//2)
	ans+=(m[-1][1]//2)

	ans+=((m[0][1]+m[-1][1])//2)*(k-1)

else:
	for a,i in m:
		print(a,i)
		ans+=(i//2)


print(ans)

Submission status: Wrong Answer

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

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

if S[0] != S[-1]:
    change = 0
    T = list(S)
    for i, t in enumerate(T[1:-1], start=1):
        if t == T[i - 1] and T[i - 1] != '.':
            change += 1
            T[i] = '.'
    print(change * K)
else:
    T1 = list(S)
    T1[0] = '.'

    change1 = 0
    for i, t in enumerate(T1[1:], start=1):
        if t == T1[i - 1] and T1[i - 1] != '.':
            change1 += 1
            T1[i] = '.'

    T2 = list(S)
    change2 = 0
    for i, t in enumerate(T2[1:-1], start=1):
        if t == T2[i - 1] and T2[i - 1] != '.':
            change1 += 1
            T2[i] = '.'

    ans = min(
        change1 * K + K - (1 if S[0] == S[1] else 0),
        change2 * K + K - (1 if T2[-2] == T2[-1] else 0)
    )

    print(ans)

Submission status: Wrong Answer

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

    c = []
    
    i = 0
    if S[0] == S[-1]:
        connected = True
    else:
        connected = False

    while i < len(S):

        char = S[i]

        cnt = 1
        cnt2 = 1
        for j in range(i + 1, len(S) * 2):
            i = j
            if char == S[j % len(S)]:
                if j >= len(S):
                    cnt2 += 1
                else:
                    cnt += 1
                    cnt2 +=1
            else:
                if j-1 < len(S):
                    cnt2 = 0
                    i = j
                else:
                    i = len(S)
                break
        
        if cnt >= 2:
            c.append([char, cnt, cnt2])
        else:
            c.append([char, 0, cnt2])
    
    #print(c)

    ans = 0
    flg = True
    for elem in c:
        if connected:
            if flg and len(c) >= 2:
                flg = False
                continue
            if len(c) == 1:
                ans = (elem[1] //2) * K
            elif elem[2] >= 2:
                ans += (elem[2] // 2) * (K - 1)  + (elem[1] // 2) + c[0][1] // 2
            else:
                ans += (elem[1] // 2) * K

        else:
            ans += (elem[1] // 2) * K
        
    print(ans)

if __name__ == '__main__':
    solve()