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: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd, ceil
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')
import string
# string.ascii_lowercase
from bisect import bisect_left

def solve():
    # n, m = [int(x) for x in input().split()]
    s = [x for x in input()]
    a = s[:]
    k = int(input())
    if len(s) == 1:
        print(k // 2)
        return
    if len(set(s)) == 1:
        print((len(s) * k)//2)
        return
    cnt = 0
    for i in range(1, len(s)):
        if s[i] == s[i-1]:
            s[i] = '.'
            cnt += 1

    cnt *= k
    
    if s[0] == s[-1] and s[0] != '.':
        cnt += (k - 1)

    print(min(cnt,ans))


t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""

azyxwvutsrqponmlkjihgfedcb

"""

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd, ceil
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')
import string
# string.ascii_lowercase
from bisect import bisect_left

def solve():
    # n, m = [int(x) for x in input().split()]
    s = [x for x in input()]
    a = s[:]
    k = int(input())
    if len(s) == 1:
        print(k // 2)
        return
    if len(set(s)) == 1:
        print((s * k)//2)
        return
    cnt = 0
    for i in range(1, len(s)):
        if s[i] == s[i-1]:
            s[i] = '.'
            cnt += 1

    cnt *= k
    
    if s[0] == s[-1] and s[0] != '.':
        cnt += (k - 1)

    print(min(cnt,ans))


t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""

azyxwvutsrqponmlkjihgfedcb

"""

Submission status: Runtime Error

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

if(S.count(S[0]) == len(S)):
    print(len(S)*K//2)
else:
    cnt = 0
    lst = []
    tmp = S[0]
    for i in range(len(S)):
        if(tmp == S[i]):
            cnt += 1
        else:
            tmp = S[i]
            if(cnt > 1):
                lst.append(cnt)
            cnt = 1   
    if(cnt > 1):
        lst.append(cnt)
        
    ans = 0
    for i in lst:
        ans += K*(i//2)
    if(S[0] != S[-1]):
        print(ans)
    else:
        for i in range(len(S)):
            if(tmp != S[i]):
                break
            a += 1
        for i in S[::-1]:
            if(tmp != i):
                break
            b += 1
        print(ans+((a+b)//2-(a//2+b//2))*(K-1))

Submission status: Runtime Error

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

if(S.count(S[0]) == len(S)):
    print(len(S)*K//2)
else:
    if(S[0] != S[-1]):
        cnt = 0
        lst = []
        tmp = S[0]
        for i in range(len(S)):
            if(tmp == S[i]):
                cnt += 1
            else:
                tmp = S[i]
                if(cnt > 1):
                    lst.append(cnt)
                cnt = 1   
        if(cnt > 1):
            lst.append(cnt)
        
        ans = 0
        for i in range(len(lst)):
            ans += K*(lst[i]//2)
        print(ans)
    else:
        cnt = 0
        lst = []
        tmp = S[0]
        for i in range(len(S)):
            if(tmp == S[i]):
                cnt += 1
            else:
                tmp = S[i]
                if(cnt > 1):
                    lst.append(cnt)
                cnt = 1
        if(cnt > 1):
            lst.append(cnt)
    
        ans = 0
        for i in range(len(lst)):
            ans += K*(lst[i]//2)
        a = 0
        b = 0
        tmp = S[0]
        for i in range(len(S)):
            if(tmp != S[i]):
                break
            a += 1
        for i in S[::-1]:
            if(tmp != i):
                break
            b += 1
        print(ans+((a+b)//2-(a//2+b//2)*(K-1))

Submission status: Runtime Error

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

if(S.count(S[0]) == len(S)):
    print((len(S)*K//2)
else:
    if(S[0] != S[-1]):
        cnt = 0
        lst = []
        tmp = S[0]
        for i in range(len(S)):
            if(tmp == S[i]):
                cnt += 1
            else:
                tmp = S[i]
                if(cnt > 1):
                    lst.append(cnt)
                cnt = 1   
        if(cnt > 1):
            lst.append(cnt)
        
        ans = 0
        for i in range(len(lst)):
            ans += K*(lst[i]//2)
        print(ans)
    else:
        cnt = 0
        lst = []
        tmp = S[0]
        for i in range(len(S)):
            if(tmp == S[i]):
                cnt += 1
            else:
                tmp = S[i]
                if(cnt > 1):
                    lst.append(cnt)
                cnt = 1
        if(cnt > 1):
            lst.append(cnt)
    
        ans = 0
        for i in range(len(lst)):
            ans += K*(lst[i]//2)
        a = 0
        b = 0
        tmp = S[0]
        for i in range(len(S)):
            if(tmp != S[i]):
                break
            a += 1
        for i in S[::-1]:
            if(tmp != i):
                break
            b += 1
        print(ans+((a+b)//2-(a//2+b//2)*(K-1))

Submission status: Runtime Error

Solution code:
# こういうのは2回繰り返してみるのが定石っぽいけど、1回だけのやつで「同じのが並んでたら後ろのほうを変える」でいいんじゃないか？
# んで最初と最後が同じならk-1回ぶん帳尻を合わせる

# なぜか3ケース合わない 意味不明
# aabaaa 2 で7になってしまう やっぱり2回繰り返してみる
# なぜか1ケース合わない aba 3 で1になってしまう
s = list(input())
k = int(input())
t = s * 2
a = 0

print(s * k)
for i in range(1, len(t)):
    if t[i] == t[i - 1]:
        t[i] = "夏"
        a += 1
if t[0] == t[-1]:
    print(a * (k - 1) // 2 + 1)  # 先頭の分？？？
else:
    print(a * k // 2)

Submission status: Runtime Error

Solution code:
a = input()
n = int(input())
count = 1
t = 0
for i in range(1,len(a)):
    if a[i] == a[i-1]:
        count += 1
    else:
        if count >= 2:
            t += count // 2
        count = 1
if count >= 2:
    t += count // 2
if a[0] == a[-1]:
    if a[0] != a[1] and a[-1] != a[-2]:
        t += n - 1
print(t * n)

Submission status: Runtime Error

Solution code:
S=input()
K=int(input())
flag=False
if S[0]==S[-1]:
    flag=True
cnt=1
A=[]
for i in range(1,len(S)):
    if S[i-1]==S[i]:
        cnt+=1
    else:
        A.append(cnt)
        cnt=1
if cnt>=2:
    A.append(cnt)
ans=0
for a in A:
    ans+=a//2
if flag:
    ans=(ans*K)+((A[0]+A[-1])//2-(A[0]//2+A[-1]//2))*(K-1)
    print(ans)
else:
    ans*=K
    print(ans)

Submission status: Runtime Error

Solution code:
s=input()
n=int(input())
sn=[s[i] for i in range(len(s))]
ani=0
ans=0
for j in range(len(sn)-1):
  if sn[j]==sn[j+1]:
    sn[j+1]="0"
    ans+=1
if sn[0]==sn[-1]:
      ani=1
sn=sn*2

if sn[len(s)]==sn[len(s)+1]:
    sn[k+1]="0"
    ani+=1
print(ans*n+ani*(n-1))

Submission status: Runtime Error

Solution code:
s=input()
k=int(input())
cnt=1
temp=[]
for i in range(len(s)-1):
    if s[i]==s[i+1]: cnt+=1
    else:
        temp.append([s[i],cnt])
        cnt=1
if cnt>=1: temp.append([s[-1],cnt])
if len(temp)==1:
    print(k*len(s)//2)
    exit()
ans=0
if temp[0][0]!=temp[-1][0]:
    for pair in temp:
        if pair[1]!=1: ans+=pair[1]//2
    print(ans*k)
else:
    for pair in temp[1:-1]:
        if pair[1]!=1: ans+=pair[1]//2
    ans*=k
    ans+=(k-1)*((temp[0][0]+temp[-1][0])//2)
    ans+=temp[0][0]//2+temp[-1][0]//2
    print(ans)

Submission status: Runtime Error

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

i = 0
T = []
U = []
while i <= N - 1:
    if i == N - 1:
        T.append(S[-1])
        if S[-2] == S[-1]:
            U.append("?")
        else:
            U.append(S[-1])
        break
    if S[i] == S[i + 1]:
        T.append(S[i] + "?")
        U.append("?" + S[i])
        i += 2
    else:
        T.append(S[i])
        U.append(S[i])
        i += 1

T = "".join(T)
U = "".join(U)
if T[0] == T[-1]:
    print((K + 1) // 2 * T.count("?") + K // 2 * U.count("?"))
else:
    print(K * T.count("?"))

Submission status: Runtime Error

Solution code:
s = str(input())
k = int(input())
 
n = len(s)
l = [0] * n

for i in range(n-1):
    if s[i] == s[i+1] and l[i] == 0:
        l[i+1] = 1

if s.count(s[0]) == n:
    print(s*k//2)
elif s[0] != s[-1]:
    print(sum(l) * k)
else:
    a = 0
    b = 0
    ac = True
    bc = True
    for i in range(n):
        if s[i] == s[0] and ac == True:
            a += 1
        elif s[i] != s[0]:
            ac = False
        if s[n-i-1] == s[0] and bc == True:
            b += 1
        else: bc = False  
    sub = a//2 + b//2 - (a+b)//2
    print(sum(l)*k - sub*(k-1))

Submission status: Runtime Error

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

s2 = s + s
s3 = s + s + s

tmp = s[0]
c = 0
for i in range(1,len(s)):
    if s[i] == tmp:
        s[i] = "@"
        c += 1
    tmp = s[i] 

tmp = s2[0]
c2 = 0
for i in range(1,len(s2)):
    if s2[i] == tmp:
        s2[i] ="@"
        c2 += 1
    tmp = s2[i]

tmp = s3[0]
c3 = 0
for i in range(1,len(s3)):
    if s3[i] == tmp:
        s3[i] ="@"
        c3 += 1
    tmp = s3[i]

if c2-c == c3-c2:
    ans = c + (k-1)*(c2-c)
else:
    if K%2 != 0:
        ans = c + ((k-1)//2)*(c3-c)
    else:
        ans = c + ((k-2)//2)*(c3-c2) + c2 - c
print(ans)

Submission status: Runtime Error

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

if len(S) == 1:
    ans = K//2
elif len(set(s)) == 1:
    ans = int(len(S)*K/2)
else:
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            ans += 1
            S[i+1] = "#"

    ans *= K

    if S[0] == S[-1]:
        ans += K-1
    # print(S)
print(ans)

Submission status: Runtime Error

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

s += s[0]

cnt = 0
pre = s[0]
for i in range(1,len(s)):
    if s[i]==pre:
        cnt += 1
        pre =''
    else:
        pre = s[i]
ans = cnt*k
if (s[-3]!=s[-2]) and (s[-2]==s[-1]):
    print(ans-1)
else:
    print(ans)

Submission status: Runtime Error

Solution code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: map(str, sys.stdin.buffer.readline().split())
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.buffer.readline().split())
rl = lambda: list(map(int, sys.stdin.buffer.readline().split()))

s = rr()
k = ri()
cnt = 0
i = 0
if len(set(s)) == 1:
  print(int(s*k/2))
  exit()
while i < len(s)-1:
  if s[i] == s[i+1]:
    cnt += 1
    i += 1
  i += 1
print(cnt)
cnt1 = 0
i1 = 0
s *= 2
while i1 < len(s)-1:
  if s[i1] == s[i1+1]:
    cnt1 += 1
    i1 += 1
  i1 += 1
print(cnt1)
print((cnt1 - cnt)*(k-1) + cnt)

Submission status: Runtime Error

Solution code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: map(str, sys.stdin.buffer.readline().split())
ri = lambda: int(sys.stdin.buffer.readline())
rm = lambda: map(int, sys.stdin.buffer.readline().split())
rl = lambda: list(map(int, sys.stdin.buffer.readline().split()))

s = rr()
k = ri()
cnt = 0
for i in range(len(s)-1):
  if s[i] == s[i+1]:
    cnt += 1
print(cnt*k)

Submission status: Runtime Error

Solution code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: map(str, sys.stdin.buffer.readline().split())
ri = lambda: int(sys.stdin.buffer.readline())
rm = lambda: map(int, sys.stdin.buffer.readline().split())
rl = lambda: list(map(int, sys.stdin.buffer.readline().split()))

s = rr()
k = ri()
cnt = 0
for i in range(len(s)-1):
  if len[i] == len[i+1]:
    cnt += 1
print(cnt*k)

Submission status: Runtime Error

Solution code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np

rr = lambda: str(sys.stdin.buffer.readline().rstrip())
rs = lambda: map(str, sys.stdin.buffer.readline().split())
ri = lambda: int(sys.stdin.buffer.readline())
rm = lambda: map(int, sys.stdin.buffer.readline().split())
rl = lambda: list(map(int, sys.stdin.buffer.readline().split()))

s = rr()
k = ri()
cnt = 0
for i in range(len(s)-1):
  if len[i] == len[i+1]:
    cnt += 1
print(cnt*k)

Submission status: Runtime Error

Solution code:
import numpy as np
s=input()
k=int(input())
cnt=1
a=[]
ss=s+"."
for i in range(len(s)):
    if ss[i]==ss[i+1]:
        cnt+=1
    else:
        a.append(cnt)
        cnt=1

pp=0
if s[0]==s[len(s)-1]:
    pp=a[0]+a[-1]
    
    b=[]
    if len(a)>1:
        b.append(pp)
        b+=a[1:len(a)-1]

    else:
        a=[len(s)*k]
        b=[]

arr=np.array(a)
arr2=np.array(b)

a=arr//2
b=arr2//2
if len(s)>=2:
    print(np.sum(a)+int((k-1)*np.sum(b)))
else:
    print(k//2)

Submission status: Runtime Error

Solution code:
import numpy as np
s=input()
k=int(input())
cnt=1
a=[]
ss=s+"."
for i in range(len(s)):
    if ss[i]==ss[i+1]:
        cnt+=1
    else:
        a.append(cnt)
        cnt=1

pp=0
if s[0]==s[len(s)-1]:
    pp=a[0]+a[-1]
    
b=[]
b.append(pp)
b+=a[1:n-2]
    
arr=np.array(a)
arr2=np.array(b)

a=arr//2
b=arr//2
print(np.sum(a)+(k-1)*np.sum(b))

Submission status: Runtime Error

Solution code:
s = list(str(input())) 
n = int(input())
ln = s * n
if ln[0] == ln[1]:
    ans = 1
    ln[1] = '!'
else:
    ans = 0
for i in range(2, len(ln)):
    if ln[i - 1] == ln[i] and ln[i - 1] != ln[i - 2]:
        ans += 1
        ln[i] = str(i)
print(ans)

Submission status: Runtime Error

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

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

N = len(S)

T = S + S

count1 = 0
for i in range(N - 1):
    if T[i] == T[i + 1]:
        T[i + 1] = '*'
        count1 += 1

count2 = 0
if K >= 2:
    for i in range(N - 1, 2 * N - 1):
        if T[i] == T[i + 1]:
            T[i + 1] = '*'
            count2 += 1
    ans = (count1 + count2) * (K // 2) + count1 * (K % 2)
    if T[0] == T[-1]:
        ans += (K // 2) - 1
    if K % 2 == 1 and T[-1] == T[0] and T[N - 1] == T[0]:
        ans += 1
    print (ans)

else:
    print (error)
    print (count1)

# print (T)

Submission status: Runtime Error

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

s = list(input().rstrip())
k = int(input())
n = len(s)

group = []

cnt = 0

for i in range(n):
    if i == 0:
        cnt += 1
        continue
    elif i == n - 1:
        if s[i] == s[i - 1]:
            cnt += 1
            group.append(cnt)
        else:
            group.append(cnt)
            group.append(1)
        continue
    
    if s[i] == s[i - 1]:
        cnt += 1
    else:
        group.append(cnt)
        cnt = 1
ans = 0
if k == 1:
    for i in group:
        ans += i//2
    print(ans)
    sys.exit()
from copy import deepcopy
first = deepcopy(group)
last = deepcopy(group)
if s[0] == s[-1]:
    first[-1] += last[0]
    last[0] = 0

for i in first:
    ans += i//2
for i in last:
    ans += i//2
if k == 2:
    print(ans)
    sys.exit()
mid = deepcopy(first)
if s[0] == s[-1]:
    mid[0] = 0
temp = 0
for i in mid:
    temp += i//2
ans += temp*(k - 2)
print(ans)

Submission status: Runtime Error

Solution code:
S = input().strip()
K = int(input())
T = S*K
N = len(T)
cur = T[0]
cnt = 1
ans = 0
for i in range(1,N):
    if T[i]==cur:
        cnt += 1
    else:
        ans += cnt//2
        cur = T[i]
        cnt = 1
ans += cnt//2
print(ans)

Submission status: Runtime Error

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

cnt = 0
c = 0
if s[0] == s[-1]:
  if s[-1] == s[-2]: cnt +=1
  else: c +=1
  s[-1] = ""

for i in range(len(s)-1):
  if s[i] == s[i+1]:
    s[i+1] = ""
    cnt += 1
print(k*cnt + (k-1)*c)

Submission status: Runtime Error

Solution code:
s = list(input())
k = int(input())
x = []
ans = 0
cnt = 1
for i in range(len(s)):
    if i < len(s) - 1 and s[i] == s[i + 1]:
        cnt += 1
    else:
        ans += cnt // 2
        x.append(cnt)
        cnt = 1
cnt = 0
if s[0] == s[len(s) - 1]:
    cnt = x[0] // 2 + x[-1] // 2 - (x[0] + x[1]) // 2

print(ans * k - cnt * (k - 1) if len(set(s)) != 1 else len(s) * k // 2)

Submission status: Runtime Error

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

inner_difference = 0
  
if len(set(list(S))) == 1:
  print(K*len(S)//2)
else:
  i = 0
  while i < len(S)-1:
    if S[i] == S[i+1]:
      inner_difference += 1
      i += 1
    i += 1

  if (S[0] != S[-1])or(S[0]==S[1])or(S[N-1]==S[N-2]):
    print(inner_difference*K)
  else:
    print(inner_difference*K + K-1)

Submission status: Runtime Error

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

# S単体で必要な置換数を求める
cnt = 0
for i in range(len(S)-1):
    if S[i] == S[i+1]:
        cnt += 0.5
    else:
        cnt = int(cnt+0.5)
cnt = int(cnt+0.5)

# Sを接続した時に必要な置換数を求める
s3 = S*3
cnt_1 = 0
for i in range((len(s3)*3)-1):
    if s3[i] == s3[i+1]:
        cnt_1 += 0.5
    else:
        cnt_1 = int(cnt_1+0.5)
cnt_1 = int(cnt_1+0.5)

# 単体と接続時に必要な置換数の合計を求める
ans = int((cnt_1-cnt*3)/2*(K-1)+0.5)+cnt*K
print(ans)

Submission status: Runtime Error

Solution code:
s = list(input())
k = int(input())
cnt=[]
tmp=1
n=len(s)
for i in range(1,n):
    if s[i]==s[i-1]:
        tmp+=1
    else:
        cnt.append((s[i-1], tmp))
        tmp=1
    if i==n-1:
        cnt.append((s[i], tmp))

ans=0
if len(cnt)==1:
    ans=cnt[0][1]*k//2
elif cnt[0][0]!=cnt[-1][0]:
    for x,y in cnt:
        ans+=y//2
    ans*=k
else:
    for x,y in cnt:
        ans+=y//2
    ans*=k
    if (cnt[0][1]+cnt[-1][1])%2==0:
        ans+=k-1
print(ans)

Submission status: Runtime Error

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

def co(x):
    X = []
    for i in range(len(x)):
        X.append(x[i])
    n = 0
    for i in range(len(x)-1):
        if X[i] == X[i+1]:
            X[i+1] = 0
            n += 1
    return n

S = co(s)
D = co(s + s)
K = 2*S-D

f = 1
h = -2

if s[0] == s[-1]:
    while True:
        if s[0] == s[f]:
            f+= 1
if s[0] == s[-1]:
    while True:
        if s[-1] == s[h]:
            f-= 1

f -= 1
h += 1

if s[0] != s[-1]:
    ans = S * k
else:
    d = (f+h)//2 - f//2 - h//2
    ans = S * k - d *(k-1)

print(ans)

Submission status: Runtime Error

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

ans = 0

if S[0] == S[-1]:
    if S[-2] == S[-1]:
        ans = K
        S[-1] = -1
    else:
        ans = K - 1
        S[-1] = -1

j = 0
like = []

for i in range(len(S) - 1):
    if S[i] == S[i + 1]:
        if len(like) == j:
            like.append(2)
        else:
            like[j] += 1
    else:
        like.append(1)
        j += 1

for l in like:
    ans += l // 2 * K

print(ans)

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())
def seqcnt(s):
    cnt=1
    ans=0
    bk=''
    for c in s+' ':
        print(f'bk={bk} c={c}')
        if bk==c:
            cnt+=1
        else:
            ans+=(cnt)//2
            cnt=1
        bk=c
    return ans

cnt=seqcnt(s)
cnt2=seqcnt(s*2)    
print(f'cnt={cnt} cnt2={cnt2}')
if len(set(list(s)))==1:
    if len(s)%2==0:
        print(cnt*k)
    else:
        print(cnt*(k//2)+(cnt2-cnt)*(k-k//2))
else:
    print(cnt+(cnt2-cnt)*(k-1))

Submission status: Runtime Error

Solution code:
A=list(input())
n=int(input())
tmp=0
for i in range(len(A)-1):
   if A[i] == A[i+1]:
      A[i+1]=1
      tmp+=1
if A[0] == A[-1]:
   tmp2=n-1
print(tmp*n+tmp2)

Submission status: Runtime Error

Solution code:
def warshall_floyd(g):
    for k in range(n):
        for i in range(n):
            for j in range(n):
                g[i][j] = min(g[i][j], g[i][k] + g[k][j])

n = int(input())
s = [[int(i) for i in list(input())] for _ in range(n)]
graph = [[float("inf")]*n for _ in range(n)]
for i in range(n):
    for j in range(n):
        if s[i][j] == 1:
            graph[i][j] = 1
        if i == j:
            graph[i][j] = 0

warshall_floyd(graph)
d = 0
#print(graph)
for i in range(n-1):
    for j in range(i+1,n):
        if graph[i][j] != float("inf"):
            if d < graph[i][j]:
                start = i
                end = j
                d = graph[i][j]

v = [[] for _ in range(d+1)]
for i in range(n):
    l = graph[start]
    v[l[i]].append(i)
#print(v)
#print(start,end)

for l in v:
    m = len(l)
    for i in range(m-1):
        for j in range(i+1,m):
            if s[l[i]][l[j]] == 1:
                print(-1)
                quit()
print(d+1)

Submission status: Runtime Error

Solution code:
S =  list(input())
K = int(input())
#print(S)
N=len(S)

#if K==1:
kosuu=[]
cnt=1
for i in range(N-1):
    if S[i]==S[i+1]:
        cnt+=1
        if i==N-2:
            kosuu.append(cnt)
    elif S[i]!=S[i+1]:
        kosuu.append(cnt)
        cnt=1
#print('kosuu', kosuu)
dammy=0
for i in range(len(kosuu)):
    dammy+=kosuu[i]//2

#else:
if K==1:
    out=dammy

elif S[0]!=S[-1]:
    out = dammy * K

else:
    #kotei=kosuu[1:-1]  #少ないとき
    kotei = dammy - kosuu[0]//2 - kosuu[-1]//2
    aida = (kosuu[0] + kosuu[-1]) // 2
    #print('aida',aida)
    out = kosuu[0]//2 + (kotei*K) + aida*(K-1) + kosuu[-1]//2

print(out)

Submission status: Runtime Error

Solution code:
s=list(input())
s2=s+s
count1=0
count2=0
k=int(input())
for i in range(len(s)-1):
  if s[i]==s[i+1]:
    s[i+1]="X"
    count1+=1
for i in range(len(s2)-1):
  if s2[i]==s2[i+1]:
    s2[i+1]="X"
    count2+=1
if len(set(s))==1:
  print(int(s*k/2))
else:
  print(count1+(k-1)*(count2-count1))

Submission status: Runtime Error

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

C0 = []
cnt = 1
for i in range(1,N):
    if S[i]==S[i-1]:
        cnt+=1
        if i==N-1:
            C0.append(cnt)
    else:
        C0.append(cnt)
        cnt = 1
out0 = 0
for m in C0:
    out0 += m//2

if S[0]!=S[-1]:
    out = out0*K
else:
    X = (C0[0] + C0[-1])//2
    Y = C0[0]//2 + C0[-1]//2
    out = (out0-Y)*K
    out += X*(K-1)
    out += Y
print(out)

Submission status: Runtime Error

Solution code:
s = list(input())
k = int(input())
if not s[0]==s[-1]:
  count1=0
  for i in range(len(s)-1):
    if s[i]==s[i+1]:
      s[i+1]=1
      count1+=1
  print(k*count1)
else:
  s[-1]=0
  count2=0
  for i in range(len(s)-1):
    if s[i]==s[i+1]:
      s[i+12]=1
      count2+=1
  print(k*count2+k)

Submission status: Runtime Error

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

def f(s):
    prev = ''
    ct = 0
    for i in s:
        if i == prev:
            ct += 1
            prev = ''
        else:
            prev = i
    return ct
        

if len(set(s)) == 1:
    print(len(s) *n //2)
elif s[0]!=s[-1]:
    print(f(t) * n)
else:
    ct = f(s*2)
    
    ans = ct * (n//2) if n % 2 == 0 else ct*(n//2) + f(s)
    
    print(ans)

Submission status: Runtime Error

Solution code:
S = list(input())
K = int(input())
 
ans = 0
#２つ並べて一般性を確かめる
S2 = S*2

for i in range(1,len(S2)):
    if S2[i-1]== S2[i]:
        s_d[i] = '@'
        #2回目以降はk-1回任意の@へ直す
        if i>=len(S2)//2:
            ans+= K-1
        else:
            ans+=1

print(ans)

Submission status: Runtime Error

Solution code:
s = str(input())
k = int(input())
flag = [0 for _ in range(len(s))]
count = 0
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        if not flag[i]:
            flag[i+1] = 1
            count += 1
if s[0] == s[-1] and s[-1] != s[-2]:
    count += 1
print(k*count)

Submission status: Runtime Error

Solution code:
s=input()
n=int(input())
ans=0
flg=0
if len(s) == 1:
  print(-(-n//2))
else:
  for i in range(1,len(s)):
    if flg == 0 and s[i] == s[i-1]:
      flg = 1
    elif flg != 0 and s[i] == s[i-1]:
      flg += 1
    elif flg != 0:
      ans += -(-flg//2)
      flg = 0
ans += -(-flg//2)
ans *= n
if s[-1] != s[-2] and s[0] == s[-1]:
  ans += -(-n//2)
print(ans)

Submission status: Runtime Error

Solution code:
from collections import Counter

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

ans = 0
s_d = s*2
for i in range(1,len(s_d)):
    if s[i-1]==s[i]:
        s[i] = '_'
        if i>=len(s)//2:
            ans+=k-1
        else:
            ans+=1
s_s = set(s)
if len(s_s)==1 and len(s)%2!=0:
    ans = len(s)*(k//2)+(k%2==1)*(len(s)//2)
print(ans)

Submission status: Runtime Error

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

cnt = 0
t_cnt = 1
tmp = ''
for i in range(len(s)):
    if tmp==s[i]:
        t_cnt += 1
    else:
        cnt += t_cnt//2
        tmp = s[i]
        t_cnt = 1
cnt += t_cnt//2

top_cnt = 0
char = s[0]
for i in range(len(s)):
    if char == s[i]:
        top_cnt += 1
    else:
        break

bottom_cnt = 0
char = s[-1]
for i in range(1, len(s)+1):
    if char == s[-i]:
        bottom_cnt += 1
    else:
        break
        
if set(s)==1:
    print(len(s)*k//2)
else:
    if s[1]!=s[-1]:
        print(cnt*k)
    else:
        print(cnt*k-(top_cnt//2+bottom_cnt//2-(top_cnt+bottom_cnt)//2)*(k-1))

Submission status: Runtime Error

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

ans = 0
for i in range(1, len(S)):
  pre_s = S[i-1]
  s = S[i]
  if s == pre_s:
    S[i] = '#'
    ans += 1

if S[1] == '#' and len(S) >= 3 and S[0] != S[2]:
  S[0], S[1] = S[1], S[0]

ans *= K
if S[0] == S[-1]:
  ans += K - 1

print(ans)

Submission status: Runtime Error

Solution code:
s = input()
K = int(input())
s = list(s)
S = []
S.extend(s)
S.extend(s)

num = 0
for i in range(len(S)-1):
    if S[i] == S[i+1]:
        S[i+1] = 'A'
        num += 1

if S[0] == S[-1]:
    S[-1] == 'A'
    num += 1
    k = 1


n = 0
if K % 2 == 1:
    k = 0
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            s[i+1] = 'A'
            n += 1
        if s[0] == S[-1]:
            n += 1



result = num * (K//2) + n - k

print(result)

Submission status: Runtime Error

Solution code:
from itertools import groupby

def solve():
    S = input()
    K = int(input())
    a = [len(tuple(s)) for _, s in groupgy(S)]
    
    if S[0] == S[-1] and K > 1:
        if len(a) == 1:
            return a[0] * K // 2
        else:
            ans = a[0] // 2 + a[-1] // 2 + (a[0]+a[-1]) // 2 * (K-1)
            ans += sum(n // 2 for n in a[1:-1]) * K
            return ans
    else:
        return sum(n // 2 for n in a) * K
    
print(solve())

Submission status: Runtime Error

Solution code:
def solve():
    S = input()
    K = int(input())
    a = [len(tuple(s)) for _, s in groupgy(S)]
    
    if S[0] == S[-1] and K > 1:
        if len(a) == 1:
            return a[0] * K // 2
        else:
            ans = a[0] // 2 + a[-1] // 2 + (a[0]+a[-1]) // 2 * (K-1)
            ans += sum(n // 2 for n in a[1:-1]) * K
            return ans
    else:
        return sum(n // 2 for n in a) * K
    
print(solve())

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import collections
import itertools as it
import math
#import numpy as np
import re
 
s = input()
k = int(input())
#  = map(int, input().split())
#  = list(map(int, input().split()))
#  = [int(input()) for i in range(N)]
#
c = collections.Counter(s)

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():]

ans = 0

chains = list(nchars(s, 2))

if len(c) == 1:
    print(len(s) * k // 2)
else:
    #print(chains)
    #print(len(chains[0]), len(chains[-1]), k)
    for r in chains:
        ans += len(r) // 2

    print(ans * k + (k - 1 if chains[0] == chains[-1] else 0))

Submission status: Runtime Error

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

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

A = []
cnt = 1
C = []
for i in range(1,len(S)):
    if S[i-1] == S[i]:
        cnt += 1
    else:
        C.append([S[i-1], cnt])
        cnt = 1
C.append([S[i], cnt])

ans = 0
S2 = []
for c in C:
    ans += (c[1])//2
    if c[1] == 1:
        S2.append(False)
    else:
        for _ in range(c[1]):
            S2.append(True)

ans *= K

ans2 = 0
if S[0] == S[-1]:
    if S2[-1] == False:
        ans2 += 1

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

Submission status: Runtime Error

Solution code:
i = [input() for i in range(2)]

s = i[0] * int(i[1])

a = [i for i in s]
b = 0
c = 0
for i in range(len(a)):
  if a[i] != a[i-1]:
  	b += 1
  else:
  	b += 1
  	c += 1
print(c)

Submission status: Runtime Error

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

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

if len(S)%2 == 0:
    ans = 0
    tmp = ""
    for s in S:
        if s == tmp:
            ans += 1
            tmp = ""
            continue
        else:
            tmp = s
    if S[0] == tmp:
        ans += 1
    ans *= K
    print(ans)
    exit()
else:
    len1 = len(S)-1
    S = S*2
    ans = 0
    tmp = ""
    for i in range(len(S)):
        if S[i] == tmp:
            ans += 1
            tmp = ""
            continue
        else:
            tmp = S[i]
        if i == len1:
            num = ans
    if S[0] == tmp:
        ans += 1
    d,m = divmod(K,2)
    ans = ans*d + num*m
    print(ans)
    exit()

Submission status: Runtime Error

Solution code:
s = list(input())
k = int(input())
cou = 0
p = 0
if s[0] == s[-1]:
    p = 1
    
for i in range(1,len(s)-2):
    if s[i] == s[i+1]:
        cou += 1
        s[i+1] = "V"
if s[-2] == s[-1]:
    cou += 1
    if p == 1:
        p = 0


print(cou*k + p * (k-1))

Submission status: Runtime Error

Solution code:
s = list(input())
k = int(input())
cou = 0
for i in range(len(s)-1):
  if s[i]==s[i+1]:
    cou += 1
    s[i+1] = '-1'
if s[0]!=s[-1]:
  print(cou*k)
else:
  if s[-1]==s[-2]:
    print(cou*k)
  else:
    print((cou+1)*k-1)

Submission status: Runtime Error

Solution code:
import sys
import re
import math
import collections
import decimal
import bisect

# import copy
# import heapq
# from collections import deque
# import decimal

# sys.setrecursionlimit(100001)
INF = sys.maxsize
# MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    s = list(input())
    k = ni()

    cnt = 0
    for i in range(len(s)):
        next = i + 1 if i < len(s) - 1 else 0
        if s[i] == s[next]:
            s[i+1] = "@"
            cnt+=1

    print(cnt*k)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
S = input()
K = int(input())
L = len(S)
count = 0
changed = [0]*L

i = 0
while i < L-1:
    if S[i] == S[i+1]:
        changed[i+1] = 1
        i += 2
        count += 1
    else:
        i += 1

if changed[1] == 0 and changed[L-1] == 0 and S[0] == S[L-1]:
    ans = count * K + K -1
else:
    ans = count * K


print(ans)

Submission status: Runtime Error

Solution code:
s = list(input())

k = int(input())

n = len(s)

if(len(set(s)) == 1):
    print(n*k//2)
else:
    if(s[0] != s[-1]):
        print(n//2*k)
    else:
        print(n//2*k - (a//2 + b//2 - (a+b)//2) * (k-1))