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:
#A
s = input()
k = int(input())
s_sum = 0

s_f = 1
s_l = 1

if s[0] == s[-1]:
    
    for i in range(len(s)):
        if i != 0:
            if s[i] == s[i-1]:
                s_f += 1
            else:
                break
    
    for i in range(len(s)-1 , -1, -1):
        if i != len(s)-1:
            if s[i] == s[i+1]:
                s_l += 1
            else:
                break
                
if s_f + s_l % 2 == 0 and s_f != 1 and s_l != 1:
    s_sum += 1

n = 1
for i in range(len(s)):
    if i != 0:
        if s[i] == s[i-1]:
            n += 1
        else:
            if n != 1:
                if n % 2 == 0:
                    s_sum += (n / 2)
                else:
                    s_sum += ((n - 1)/ 2)
                n = 1
                
    if i == (len(s) - 1):
        if n != 1:
            if n % 2 == 0:
                s_sum += (n / 2)
            else:
                s_sum += ((n - 1)/ 2)

print(int(s_sum * k))

Submission status: Wrong Answer

Solution code:
import re
 
s = input()
k = int(input())
 
frag = False
 
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():]
        
s_ = list(nchars(s,1))
 
for i in s_[0]:
  s1 = i
for i in s_[-1]:
  s2 = i
 
if s1 == s2:
  frag = True
 
l = [len(i) for i in s_]
 
c = l[0]//2
 
c1 = 0
c2 = 0
c3 = 0

for i in range(len(l)):
  if i != len(l)-1:
    c1 += l[i]//2
  else:
    if not frag:
      c1 += l[i] // 2
    else:
      c2 = (l[0]+l[i])//2
      c3 = l[i]//2
  
if not frag:
  print(c1*k)
else:
  print(c + (c1-c)*k + c2*(k-1) + c3)

Submission status: Wrong Answer

Solution code:
import re
 
s = input()
k = int(input())
 
frag = False
 
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():]
        
s_ = list(nchars(s,1))
 
for i in s_[0]:
  s1 = i
for i in s_[-1]:
  s2 = i
 
if s1 == s2:
  frag = True
 
l = [len(i) for i in s_]
 
c = l[0]//2
 
c1 = 0
c2 = 0
 
for i in range(len(l)):
  if i != len(l)-1:
    c1 += l[i]//2
  else:
    if not frag:
      c1 += l[i] // 2
    else:
      c2 = (l[0]+l[i])//2
  
if not frag:
  print(c1*k)
else:
  print(c + (c1-c)*k + c2*(k-1))

Submission status: Wrong Answer

Solution code:
import re

def nchars(s, n):
    """文字列 s に、同じ文字が n 個以上連続している部分文字列を見つける
    """
    assert n > 0
    reg = re.compile("(.)\\1{%d,}" % (n - 1))  # カンマを取ると n 個ちょうどになる
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]

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

ans1 = 0
#print(S[0])
#print(len(S))

"""
for i in range(len(S)-1):
    if S[i]==S[i+1]:
        ans += 1
"""

con = list(nchars(S, 2))

for coni in con:
    """
    if int(len(coni))%2==0:
        print("even")
    elif int(len(coni))%2==1:
        print("odd")
    else:
        print("break")
    """
    #print(int(len(coni)/2))
    ans1 +=int(len(coni)/2)

ans2 = 0
if S[0]==S[len(S)-1]:
    ans2 += 1


print(ans1*K+ans2*(K-1))
#print(ans*K)

Submission status: Wrong Answer

Solution code:
import re
 
s = input()
k = int(input())
 
frag = False
 
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():]
        
s_ = list(nchars(s,1))
 
for i in s_[0]:
  s1 = i
for i in s_[-1]:
  s2 = i
 
if s1 == s2:
  frag = True
 
l = [len(i) for i in s_]
 
c = l[0]//2
 
c1 = 0
c2 = 0
 
for i in range(len(l)):
  if i != len(l)-1:
    c1 += l[i+1]//2
  else:
    if frag:
      c1 += l[i] // 2
    else:
      c2 = (l[0]+l[i])//2
  
if not frag:
  print(c1*k)
else:
  print(c + (c1-c)*k + c2*(k-1))

Submission status: Wrong Answer

Solution code:
import re
 
s = input()
k = int(input())
 
frag = False
 
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():]
        
s_ = list(nchars(s,1))
 
for i in s_[0]:
  s1 = i
for i in s_[-1]:
  s2 = i
 
if s1 == s2:
  frag = True
 
l = [len(i) for i in s_]
 
c = l[0]//2
 
c1 = 0
c2 = 0
 
for i in range(len(l)-1):
  if i != len(l)-1:
    c1 += l[i+1]//2
  else:
    if frag:
      c1 += l[i] // 2
    else:
      c2 = (l[0]+l[i])//2
  
if not frag:
  print(c1*k)
else:
  print(c1)

Submission status: Wrong Answer

Solution code:
import re

def nchars(s, n):
    """文字列 s に、同じ文字が n 個以上連続している部分文字列を見つける
    """
    assert n > 0
    reg = re.compile("(.)\\1{%d,}" % (n - 1))  # カンマを取ると n 個ちょうどになる
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]

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

ans = 0
#print(S[0])
#print(len(S))

"""
for i in range(len(S)-1):
    if S[i]==S[i+1]:
        ans += 1
"""

con = list(nchars(S, 2))

for coni in con:
    """
    if int(len(coni))%2==0:
        print("even")
    elif int(len(coni))%2==1:
        print("odd")
    else:
        print("break")
    """
    #print(int(len(coni)/2))
    ans +=int(len(coni)/2)

print(ans*K)
#print(ans*K)

Submission status: Wrong Answer

Solution code:
import re

def nchars(s, n):
    """文字列 s に、同じ文字が n 個以上連続している部分文字列を見つける
    """
    assert n > 0
    reg = re.compile("(.)\\1{%d,}" % (n - 1))  # カンマを取ると n 個ちょうどになる
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]

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

ans = 0
#print(S[0])
#print(len(S))

"""
for i in range(len(S)-1):
    if S[i]==S[i+1]:
        ans += 1
"""

con = list(nchars(S, 2))

for coni in con:
    if int(len(coni))%2==0:
        print("even")
    elif int(len(coni))%2==1:
        print("odd")
    else:
        print("break")

    print(int(len(coni)/2))
    ans +=int(len(coni)/2)

print(ans*K)
#print(ans*K)

Submission status: Wrong Answer

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

import re

def nchars(s, n):
    """文字列 s に、同じ文字が n 個以上連続している部分文字列を見つける
    """
    assert n > 0
    reg = re.compile("(.)\\1{%d,}" % (n - 1))  # カンマを取ると n 個ちょうどになる
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]

S = "cooooooooonteeeeeeeeeest"
K = 999993333

ans = 0
#print(S[0])
#print(len(S))

"""
for i in range(len(S)-1):
    if S[i]==S[i+1]:
        ans += 1
"""

con = list(nchars(S, 2))

for coni in con:
    if int(len(coni))%2==0:
        print("even")
    elif int(len(coni))%2==1:
        print("odd")
    else:
        print("break")

    print(int(len(coni)/2))
    ans +=int(len(coni)/2)

print(ans*K)

Submission status: Wrong Answer

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

s_sum = 0
n = 1
for i in range(len(s)):
    if i != 0:
        if s[i] == s[i-1]:
            n += 1
        else:
            if n != 1:
                if n % 2 == 0:
                    s_sum += (n / 2)
                else:
                    s_sum += ((n - 1)/ 2)
                n = 1
                    
if s[0] == s[len(s)-1]:
    s_sum += 1
print(int(s_sum * k))

Submission status: Wrong Answer

Solution code:
from itertools import*
s,k=open(0)
k=int(k)
g=[len(list(v))for _,v in groupby(s)]
print(sum(c//2for c in g)*k+((s[0]==s[-2])*g[0]%2&g[-2]%2)*(k//2**(-len(s)==~g[0])-1))

Submission status: Wrong Answer

Solution code:
#coding: utf-8
strings = str(input())
k = int(input())
s = strings + strings
# print(s)
count = 0

for i in range(len(s)-1):
	# print('strj'+str(j))
	if s[i] == s[i+1]:
		# print(str('s[i]=') + s[i] + str(' s[i+1]=') + s[i+1])
		count += 1
		# s[i] = str(i)
		s = s[:i+1] + str('A') + s[i+2:]
		# print(s)

# print(count)
ans = count*k//2

# if k % 2 == 0:
# 	count = count * k // 2
# else:
# 	print(count*k//2)
# 	print(count//2)
# 	count = count * k // 2 - (count // 2)
if strings[0] == strings[len(strings)-1] and strings[len(strings)-1] != strings[len(strings)-2] and strings[0] != strings[1]:
	ans += (k - 1) // 2

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import*
s,k=open(0)
k=int(k)
g=[len(list(v))for _,v in groupby(s)]
print(sum(c//2for c in g)*k-((s[0]==s[-2])*g[0]%2&g[-2]%2)*(~k//2**(-len(s)==~g[0])+1))

Submission status: Wrong Answer

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

#f(0)をカウント
def listCount(S):
    box = "" #１つ前の文字を格納
    count=0 #f(0):重複ペアをカウント

    for mozi in S:
        if box != mozi:
            box = mozi
        else:
            count += 1
            box=""
            
    return count
  
#f'(0)をカウント
def listCount_dash(S,K):
    box =""
    S_origin = S
    
    #Sの後ろから前まで試行
    for k in range(0,len(S)-1):
        #もし先頭と最後尾が同じならque
        if S[0] == S[len(S)-1]:
            S.append(S[0])
            S.pop(0)
        else:
            break
            
    if S[0] != S[len(S)-1]:
        count = listCount(S)*(K-1) + listCount(S_origin)
    else:
        count = listCount(S)*K if len(S)%2 == 0 else ( 2*listCount(S)-1 )*(K/2) + listCount(S)*(K%2)
        
    return count
  
if S[0] != S[len(S)-1]:
    #両端が異なる場合
    ans = listCount(S)*K
else:
    #両端が同じ場合
    ans = listCount_dash(S,K)
    
print(ans)

Submission status: Wrong Answer

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

#f(0)をカウント
def listCount(S):
    box = "" #１つ前の文字を格納
    count=0 #f(0):重複ペアをカウント

    for mozi in S:
        if box != mozi:
            box = mozi
        else:
            count += 1
            box=""
            
    return count
  
#f'(0)をカウント
def listCount_dash(S):
    box =""
    
    #Sの後ろから前まで試行
    for k in range(0,len(S)-1):
        #もし先頭と最後尾が同じならque
        if S[0] == S[len(S)-1]:
            S.append(S[0])
            S.pop(0)
        else:
            break
    count = listCount(S) #全部同じ文字でもこの数でok
    return count
  
if S[0] != S[len(S)-1]:
    #両端が異なる場合
    ans = listCount(S)*K
else:
    #両端が同じ場合
    ans = listCount(S) + listCount_dash(S)*(K-1)
    
print(ans)

Submission status: Wrong Answer

Solution code:
def MAP(): return list(map(int,input().split()))
def INT(): return int(input())
def FLOAT(): return float(input())
MOD = 10**9+7

s = input()
k = INT()
n = len(s)
res=0
tmp=0

for i in range(1,n):
    if s[i-1]==s[i]:
        tmp+=1
        continue
    res+=(tmp+1)//2
    tmp=0
res+=(tmp+1)//2

if s[0]!=s[n-1]:
    print(res*k)
else:
    a=1
    b=1
    for i in range(1,n):
        if s[0]!=s[i]:
            break
        a+=1
    for i in range(n-2,-1,-1):
        if s[i]!=s[n-1]:
            break
        b+=1
    tmp = (a//2) + (b//2) - ((a+b)//2)
    print(res*k-(tmp*(k-1)))

Submission status: Wrong Answer

Solution code:
def MAP(): return list(map(int,input().split()))
def INT(): return int(input())
def FLOAT(): return float(input())
MOD = 10**9+7

s = input()
k = INT()
n = len(s)
res=0
tmp=0

for i in range(1,n):
    if s[i-1]==s[i]:
        tmp+=1
        continue
    res+=(tmp+1)//2
    tmp=0
res+=(tmp+1)//2

if s[0]!=s[n-1]:
    print(res*k)
else:
    a=1
    b=1
    for i in range(1,n):
        if s[0]!=s[i]:
            break
        a+=1
    for i in range(n-2,-1,-1):
        if s[i]!=s[n-1]:
            break
        b+=1
    tmp = (a//2) + (b//2) - ((a+b)//2)
    print(res*k-tmp)

Submission status: Wrong Answer

Solution code:
def MAP(): return list(map(int,input().split()))
def INT(): return int(input())
def FLOAT(): return float(input())
MOD = 10**9+7

s = input()
k = INT()
res=0
tmp=0

for i in range(1,len(s)):
    if s[i-1]==s[i]:
        tmp+=1
        continue
    res+=(tmp+1)//2
    tmp=0
res+=(tmp+1)//2
print(res*k)

Submission status: Wrong Answer

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

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

i = 0
while s[i] == s[i+1]:
    i += 1
    if i == n-1:
        break

if i == n-1:
    print(n*k//2)
    exit()

p = i + 1

mode = 0
count = 0
ans = 0
while i < n-1:
    if s[i] == s[i+1]:
        if mode == 0:
            count = 2
            mode = 1
        else:
            count += 1
    elif mode == 1:
        ans += count // 2
        count = 0
        mode = 0
    i += 1

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

print(ans)

Submission status: Wrong Answer

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

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

i = 0
while s[i] == s[i+1]:
    i += 1
    if i == n-1:
        break

if i == n-1:
    print(n*k//2)
    exit()

p = i

mode = 0
count = 0
ans = 0
while i < n-1:
    if s[i] == s[i+1]:
        if mode == 0:
            count = 2
            mode = 1
        else:
            count += 1
    elif mode == 1:
        ans += count // 2
        count = 0
        mode = 0
    i += 1

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

print(ans)

Submission status: Wrong Answer

Solution code:
def MAP(): return list(map(int,input().split()))
def INT(): return int(input())
def FLOAT(): return float(input())
MOD = 10**9+7

s = list(input())
k = INT()
res=0

for i in range(len(s)-1):
    if s[i]==s[i+1]:
        s[i+1]="01"
        res+=1

print(res*k)

Submission status: Wrong Answer

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

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

i = 0
while s[i] == s[i+1]:
    i += 1
    if i == n-1:
        break

if i == n-1:
    print(n*k//2)
    exit()

p = i

mode = 0
count = 0
ans = 0
while i < n-1:
    if s[i] == s[i+1]:
        if mode == 0:
            count = 2
            mode = 1
        else:
            count += 1
    elif mode == 1:
        ans += count // 2
        count = 0
        mode = 0
    i += 1

if mode == 1:
    if s[0] != s[-1]:
        ans += p // 2
        ans += count // 2
        p = 0
        count = 0

ans = ans * k + (p+count) * (k-1) // 2 + p // 2 + count // 2

print(ans)

Submission status: Wrong Answer

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

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

i = 0
while s[i] == s[i+1]:
    i += 1
    if i == n-1:
        break

if i == n-1:
    print(n*k//2)
    exit()

p = i

mode = 0
count = 0
ans = 0
while i < n-1:
    if s[i] == s[i+1]:
        if mode == 0:
            count = 2
            mode = 1
        else:
            count += 1
    elif mode == 1:
        ans += count // 2
        count = 0
        mode = 0
    i += 1

if mode == 1:
    if s[0] != s[-1]:
        ans += p // 2
        ans += count // 2
        p = 0

ans = ans * k + (p+count) * (k-1) // 2 + p // 2 + count // 2

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
def inputs():
    return [int(x) for x in input().split()]
S = list(input())
N = int(input())
if len(S)==2:
  print(N//2)
  exit()
ans = 0
flag=0
if S[0]==S[-2]:
  cnt=1
  cntb=1
  for i in range(len(S)-2):
    if S[i]==S[i+1]:
      cnt+=1
    else:
      break
  for j in range(len(S)-2,0,-1):
    if S[j]==S[j-1]:
      cntb +=1
    else:
      break
  print(cnt,cntb)
  if cnt==cntb==len(S)-1 and cnt%2==1:
    S[0]="aaaaaa"
    ans+=1
    flag=2
  elif cntb%2!=0:
    if cnt%2==0:
      S[0]="aaa"
      ans+=1
    else:
      S[0]="aaa"
      ans+=1
      flag = 1
for i in range(len(S)-2):
    if S[i]==S[i+1]:
        ans +=1
        S[i+1]="aa"
if flag==0:
	print(ans*N)
elif flag==1:
  print(ans*(N-1)+ans-1)
else:
  print(ans*N-(N+2-1)//2)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
def inputs():
    return [int(x) for x in input().split()]
S = list(input())
N = int(input())
if len(S)==2:
  print(N//2)
  exit()
ans = 0
flag=0
if S[0]==S[-2]:
  cnt=1
  cntb=1
  for i in range(len(S)-2):
    if S[i]==S[i+1]:
      cnt+=1
    else:
      break
  for j in range(len(S)-2,0,-1):
    if S[j]==S[j-1]:
      cntb +=1
    else:
      break
  if cnt==cntb==len(S)-2 and cnt%2==1:
    S[0]="aaaaaa"
    ans+=1
    flag=2
  elif cntb%2!=0:
    if cnt%2==0:
      S[0]="aaa"
      ans+=1
    else:
      S[0]="aaa"
      ans+=1
      flag = 1
for i in range(len(S)-2):
    if S[i]==S[i+1]:
        ans +=1
        S[i+1]="aa"
#print(S)
if flag==0:
	print(ans*N)
elif flag==1:
  print(ans*(N-1)+ans-1)
else:
  print(ans*N-(N+2-1)//2)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
def inputs():
    return [int(x) for x in input().split()]
S = list(input())
N = int(input())
if len(S)==2:
  print(N//2)
  exit()
ans = 0
flag=0
if S[0]==S[-2]:
  cnt=1
  cntb=1
  for i in range(len(S)-2):
    if S[i]==S[i+1]:
      cnt+=1
    else:
      break
  for j in range(len(S)-2,0,-1):
    if S[j]==S[j-1]:
      cntb +=1
    else:
      break
  if cnt==cntb and cnt%2==1:
    S[0]="aaaaaa"
    ans+=1
    flag=2
  elif cntb%2!=0:
    if cnt%2==0:
      S[0]="aaa"
      ans+=1
    else:
      S[0]="aaa"
      ans+=1
      flag = 1
for i in range(len(S)-2):
    if S[i]==S[i+1]:
        ans +=1
        S[i+1]="aa"
#print(S)
if flag==0:
	print(ans*N)
elif flag==1:
  print(ans*(N-1)+ans-1)
else:
  print(ans*N-(N+2-1)//2)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
query = []
query2 = []
tmp = 1
for i in range(len(s)-1):
  if s[i] == s[i+1]:
    tmp += 1
  else:
    query.append(tmp)
    tmp = 1
query.append(tmp)
tmp = 1
s2 = s+s
for i in range(len(s2)-1):
  if s2[i] == s2[i+1]:
    tmp += 1
  else:
    query2.append(tmp)
    tmp = 1
query2.append(tmp)
ans1 = 0
ans2 = 0
for i in query:
  ans1 += i//2
for i in query2:
  ans2 += i//2

if k== 1:
  print(ans1)
else:
  ans = ans1+(ans2-ans1)*(k-1)
  print(ans)

Submission status: Wrong Answer

Solution code:
s = str(input())
k = int(input())
sm = 0
i = 0
l = len(s)
count = 0
while i <= l-2:
  if s[i]==s[i+1]:
    count += 1
  else:
    sm += (count+1)//2
    count = 0
  i += 1
sm += (count+1)//2
sm = sm*k
if s[0] == s[l-1]:
  j = 0
  while j+1 < l and s[0] == s[j+1]:
    j += 1
  if count%2 == 0 and j%2 == 0:
    sm += k-1
print(sm)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
def inputs():
    return [int(x) for x in input().split()]
S = list(input())
N = int(input())
if len(S)==2:
  print(N//2)
  exit()
ans = 0
flag=0
if S[0]==S[-2]:
  cnt=1
  cntb=1
  for i in range(len(S)-2):
    if S[i]==S[i+1]:
      cnt+=1
    else:
      break
  for j in range(len(S)-2,0,-1):
    if S[j]==S[j-1]:
      cntb +=1
    else:
      break
  if cntb%2!=0:
    if cnt%2==0:
      S[0]="aaa"
      S[-3]="aaaa"
      ans+=2
    else:
      S[0]="aaa"
      S[-3]="aaaa"
      ans+=2
      flag = 1
for i in range(len(S)-2):
    if S[i]==S[i+1]:
        ans +=1
        S[i+1]="aa"
if flag==0:
	print(ans*N)
else:
  print(ans*(N-1)+ans-1)

Submission status: Wrong Answer

Solution code:
#A
#入力
s=input()
K=int(input())

z=0#必要繰り返し数のカウンター

#処理
#先頭と末端が同じか否かで場合分け

if s[0]==s[len(s)-1]:
    a=0#最初のやつの個数
    b=0#最後のやつ
    c=0#いるかわからん。
    #最初の方の個数カウント
    x=s[0]
    while True:
        if s=="":
            break
        elif s[0]==x:
            s=s[1:]
            a+=1
        else:
            break
#最後の方の個数カウント
    if s!="": #三種類あるとき
        while True:
            if s=="":
                break
            elif s[-1]==x:
                s=s[:-1]
                b+=1
            else:
                break
        #真ん中のやつのカウント
        while len(s)!=0:
            x=s[0]
            y=1 #文字連続数カウンター
            s=s[1:]
            while True:
                if s=="":
                    break
                elif s[0]==x:
                    s=s[1:]
                    y+=1
                else:
                    break
            c+=y//2
    #置き換え数のチェック
    z=c*K+a//2+b//2+(a+b)//2*(K-1)
    print(z)

        
else:
    while len(s)!=0:
        x=s[0]
        y=1 #文字連続数カウンター
        s=s[1:]
        while True:
            if s=="":
                break
            elif s[0]==x:
                s=s[1:]
                y+=1
            else:
                break
        z+=y//2
    z=z*K
    print(z)

Submission status: Wrong Answer

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

if len(set(S))==1:
  print(len(S)*K//2)
else:
  tmp=0
  cnt=1
  for i in range(1,len(S)):
    if S[i]==S[i-1]:
      cnt+=1
    else:
      tmp+=cnt//2
      cnt=1
  #print(tmp)
  if S[0]!=S[-1]:
    print(tmp*K)
  else:
    f,b=1,1
    for i in range(len(S)-1):
      if S[i]==S[i+1]:
        f+=1
      else:
        break
    for i in range(len(S)-1,0,-1):
      if S[i]==S[i-1]:
        b+=1
      else:
        break
    #print(f,b)
    print(tmp*K+(K-1) if f%2 and b%2 else tmp*K)

Submission status: Wrong Answer

Solution code:
#A
#入力
s=input()
K=int(input())

z=0#必要繰り返し数のカウンター

#処理
#先頭と末端が同じか否かで場合分け

if s[0]==s[len(s)-1]:
    a=0#最初のやつの個数
    b=0#最後のやつ
    c=0#いるかわからん。
    #最初の方の個数カウント
    x=s[0]
    while True:
        if s=="":
            break
        elif s[0]==x:
            s=s[1:]
            a+=1
        else:
            break
#最後の方の個数カウント
    if s!="": #三種類あるとき
        while True:
            if s=="":
                break
            elif s[-1]==x:
                s=s[:-1]
                b+=1
            else:
                break
        #真ん中のやつのカウント
        while len(s)!=0:
            x=s[0]
            y=1 #文字連続数カウンター
            s=s[1:]
            while True:
                if s=="":
                    break
                elif s[0]==x:
                    s=s[1:]
                    y+=1
                else:
                    break
            c+=y//2
        print(s)
    #置き換え数のチェック
    z=c*K+a//2+b//2+(a+b)//2*(K-1)
    print(z)

        
else:
    while len(s)!=0:
        x=s[0]
        y=1 #文字連続数カウンター
        s=s[1:]
        while True:
            if s=="":
                break
            elif s[0]==x:
                s=s[1:]
                y+=1
            else:
                break
        z+=y//2
    z=z*K
    print(z)

Submission status: Wrong Answer

Solution code:
#A
#入力
s=input()
K=int(input())

z=0#必要繰り返し数のカウンター

#処理
#先頭と末端が同じか否かで場合分け

if s[0]==s[len(s)-1]:
    a=0#最初のやつの個数
    b=0#最後のやつ
    c=0#いるかわからん。
    #最初の方の個数カウント
    x=s[0]
    while True:
        if s=="":
            break
        elif s[0]==x:
            s=s[1:]
            a+=1
        else:
            break
#最後の方の個数カウント
    if s!="": #三種類あるとき
        while True:
            if s=="":
                break
            elif s[-1]==x:
                s=s[:-1]
                b+=1
            else:
                break
        print(s)
        #真ん中のやつのカウント
        while len(s)!=0:
            x=s[0]
            y=1 #文字連続数カウンター
            s=s[1:]
            while True:
                if s=="":
                    break
                elif s[0]==x:
                    s=s[1:]
                    y+=1
                else:
                    break
            c+=y//2
        print(s)
    #置き換え数のチェック
    z=c*K+a//2+b//2+(a+b)//2*(K-1)
    print(z)

        
else:
    while len(s)!=0:
        x=s[0]
        y=1 #文字連続数カウンター
        s=s[1:]
        while True:
            if s=="":
                break
            elif s[0]==x:
                s=s[1:]
                y+=1
            else:
                break
        z+=y//2
    z=z*K
    print(z)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
def inputs():
    return [int(x) for x in input().split()]
S = list(input())
N = int(input())
if len(S)==2:
  print(N//2)
  exit()
ans = 0
flag=0
if S[0]==S[-2]:
  cnt=0
  cntb=0
  ans+=1
  for i in range(len(S)-2):
    if S[i]==S[i+1]:
      cnt+=1
    else:
      break
  for j in range(len(S)-2,0,-1):
    if S[j]==S[j-1]:
      cntb +=1
    else:
      break
  if cntb%2==0:
    if cnt%2==0:
      flag=1
    S[0]="aaa"
  else:
    if cntb%2==0:
      flag=1
    S[-2]="aaaa"
for i in range(len(S)-2):
    if S[i]==S[i+1]:
        ans +=1
        S[i+1]="aa"
if flag==0:
	print(ans*N)
else:
  print(ans*N-N)

Submission status: Wrong Answer

Solution code:
# 圧縮
# 001101 -> [0,1,0,1],[2,2,1,1]
def press(array):
    pressed = []
    cnts = []
    remark = array[0]
    cnt = 0
    for i in range(len(array)):
        cnt += 1
        if i == len(array)-1:
            pressed.append(remark)
            cnts.append(cnt)
        elif array[i] != array[i+1]:
            pressed.append(remark)
            cnts.append(cnt)
            remark = array[i+1]
            cnt = 0
    return pressed,cnts

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

N = len(S)
pressed, cnts = press(S)
res = 0
for c in cnts:
    res += (c // 2) * K

if N == 1:
    ans = K // 2
elif N == 2:
    ans = res
elif N == 3:
    flag = S[0] == S[-1]
    ans = res + K-1 * flag
else:
    flag = S[0] == S[-1] and S[0] != S[1] and S[-2] != S[-1]
    ans = res + (K-1) * flag

print(ans)

Submission status: Wrong Answer

Solution code:
# 圧縮
# 001101 -> [0,1,0,1],[2,2,1,1]
def press(array):
    pressed = []
    cnts = []
    remark = array[0]
    cnt = 0
    for i in range(len(array)):
        cnt += 1
        if i == len(array)-1:
            pressed.append(remark)
            cnts.append(cnt)
        elif array[i] != array[i+1]:
            pressed.append(remark)
            cnts.append(cnt)
            remark = array[i+1]
            cnt = 0
    return pressed,cnts

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

N = len(S)
ans = 0
res = 0
pressed, cnts = press(list(S))
for c in cnts:
    res += (c // 2) * K

if N == 1:
    ans += K // 2
elif N == 2:
    ans += res
elif N == 3 and S[0] == S[-1]:
    ans += res + K-1
elif S[0] == S[-1] and S[0] != S[1] and S[-2] != S[-1]:
    ans += res + K-1

print(ans)

# def debug(S, K):
#     S = S * K 
#     ans = 0
#     pressed, cnts = press(list(S))
#     for c in cnts:
#         ans += c // 2
#     print(ans)

# debug(S, K)

Submission status: Wrong Answer

Solution code:
# 圧縮
# 001101 -> [0,1,0,1],[2,2,1,1]
def press(array):
    pressed = []
    cnts = []
    remark = array[0]
    cnt = 0
    for i in range(len(array)):
        cnt += 1
        if i == len(array)-1:
            pressed.append(remark)
            cnts.append(cnt)
        elif array[i] != array[i+1]:
            pressed.append(remark)
            cnts.append(cnt)
            remark = array[i+1]
            cnt = 0
    return pressed,cnts

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

N = len(S)
ans = 0
pressed, cnts = press(list(S))
for c in cnts:
    ans += (c // 2) * K

if N == 1:
    ans += K // 2
elif N == 2:
    pass
elif N == 3 and S[0] == S[-1]:
    ans += K-1
elif S[0] == S[-1] and S[0] != S[1] and S[-2] != S[-1]:
    ans += K-1

print(ans)

# def debug(S, K):
#     S = S * K 
#     ans = 0
#     pressed, cnts = press(list(S))
#     for c in cnts:
#         ans += c // 2
#     print(ans)

# debug(S, K)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
def inputs():
    return [int(x) for x in input().split()]
S = list(input())
N = int(input())
if len(S)==2:
  print(N//2)
  exit()
ans = 0
flag=0
if S[0]==S[-2]:
  if S[0]==S[1]:
    S[0]="aaa"
    ans+=1
for i in range(len(S)-2):
    if S[i]==S[i+1]:
        ans +=1
        S[i+1]="aa"
if S[0]==S[-2]:
  ans+=1
  if S[-2]!=S[-3]:
    flag=1
    S[-2]="aaaa"
if flag==0:
	print(ans*N)
else:
  print(ans*N-1,S)

Submission status: Wrong Answer

Solution code:
S = input()
N = int(input())
c=0
f=0
if S[0]==S[len(S)-1]:
    c+=1
    f=1
for i in range(len(S)-1):
   if S[i+1] == S[i]:
        if f!=1:
            c+=1
            f=1
        else:f=0
print(c*N)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
def inputs():
    return [int(x) for x in input().split()]
S = list(input())
N = int(input())
if len(S)==2:
  print(N//2)
  exit()
ans = 0
flag=0
if S[0]==S[-2]:
  ans+=1
  if S[0]==S[1]:
    S[0]="aaa"
for i in range(len(S)-2):
    if S[i]==S[i+1]:
        ans +=1
        S[i+1]="aa"
if S[0]==S[-2]:
  if S[-2]!=S[-3]:
    flag=1
    S[-2]="aaaa"
if flag==0:
	print(ans*N)
else:
  print(ans*N-1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
def inputs():
    return [int(x) for x in input().split()]
S = list(input())
N = int(input())
if len(S)==2:
  print(N//2)
  exit()
ans = 0
flag=0
for i in range(len(S)-2):
    if S[i]==S[i+1]:
        ans +=1
        S[i+1]="aa"
if S[0]==S[-2]:
  ans+=1
  if S[0]==S[1]:
    S[0]="aaa"
  else:
    if S[-2]!=S[-3]:
      flag=1
    S[-2]="aaaa"
if flag==0:
	print(ans*N)
else:
  print(ans*N-1)

Submission status: Wrong Answer

Solution code:
# 圧縮
# 001101 -> [0,1,0,1],[2,2,1,1]
def press(array):
    pressed = []
    cnts = []
    remark = array[0]
    cnt = 0
    for i in range(len(array)):
        cnt += 1
        if i == len(array)-1:
            pressed.append(remark)
            cnts.append(cnt)
        elif array[i] != array[i+1]:
            pressed.append(remark)
            cnts.append(cnt)
            remark = array[i+1]
            cnt = 0
    return pressed,cnts

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

N = len(S)
ans = 0
pressed, cnts = press(list(S))
for c in cnts:
    ans += (c // 2) * K

if N == 1:
    ans += K-1
elif N == 2:
    pass
elif N == 3 and S[0] == S[-1]:
    ans += K-1
elif S[0] == S[-1] and S[0] != S[1] and S[-2] != S[-1]:
    ans += K-1

print(ans)

Submission status: Wrong Answer

Solution code:
# 圧縮
# 001101 -> [0,1,0,1],[2,2,1,1]
def press(array):
    pressed = []
    cnts = []
    remark = array[0]
    cnt = 0
    for i in range(len(array)):
        cnt += 1
        if i == len(array)-1:
            pressed.append(remark)
            cnts.append(cnt)
        elif array[i] != array[i+1]:
            pressed.append(remark)
            cnts.append(cnt)
            remark = array[i+1]
            cnt = 0
    return pressed,cnts

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

N = len(S)
ans = 0
pressed, cnts = press(list(S))
for c in cnts:
    ans += (c // 2) * K

if N == 1 or (S[0] == S[-1] and S[0] != S[1] and S[-2] != S[-1]):
    ans += K-1

print(ans)