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

cnt=0
l=len(s)
i=0
flag=1
while(i<l-1):
    if s[i]==s[i+1]:
        cnt+=1
        i+=2
    else:
        i+=1

cnt2=cnt

if s[0]==s[1]:
    i=1
    cnt2=1
    while(i<l-1):
        if s[i]==s[i+1]:
            if i+1==l-1:
                flag=0
            cnt2+=1
            i+=2
        else:
            i+=1


if s[0]==s[-1]:
    ans=cnt*(k//2+k%2)+cnt2*(k//2)
else:
    ans=cnt*k
        
print(ans)

Submission status: Runtime Error

Solution code:
S = input()
K = int(input())
cnt = 0
for i in range(len(S)-1):
    if S[i]==S[i+1]&&S[i-1]!=S[i]:
        cnt += K
print(cnt)

Submission status: Runtime Error

Solution code:
s = list(input())
k = int(input())
ans = 0
for i in range(0,len(s)-1):
    if s[i]==s[i+1]:
        ans += k
        s[i+1] = "0"
if s[-2]!=s[-1] and s[0] == s[-1]:
    ans += k-1
print(ans)

Submission status: Runtime Error

Solution code:
from collections import defaultdict
if len(set(string)) ==1:
    print(K*len(string)//2)
else:
    if(string[0]==string[-1]):
        ## 両端が同じ文字なら…
        start = string[0]
        startFlag = True
        cnt = defaultdict(int)
        length = 0
        
        for char in string:
            #print(char,start,length)
            if char == start:
                length +=1
                
            else:
                if startFlag:
                    startlen = length
                    startFlag = False
                cnt[length] +=1
                start = char
                length = 1
        else:
            ## 両端をカウントしない
            cnt[startlen] -= 1
            cnt[startlen +length] += 1
        print(sum([(key//2)*value*K for key,value in cnt.items()])-(startlen+length)//2+startlen//2+length//2)
    else:
        ## 両端が同じ文字なら…
        start = string[0]
        cnt = defaultdict(int)
        length = 0
        for char in string:
            #print(char,start,length)
            if char == start:
                length +=1
                
            else:
                cnt[length] +=1
                start = char
                length = 1
        else:
            ## 両端をカウントしない
            cnt[length] += 1
        print(sum([(key//2)*value*K for key,value in cnt.items()]))

Submission status: Runtime Error

Solution code:
import sys

def input(): return sys.stdin.readline()[:-1]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def MATINT(h): return [list(map(int, input().split())) for _ in range(h)]
def MATSTR(h): return [input() for _ in range(h)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
inf = float('inf')
mod = 10 ** 9 + 7

S = input()
K = INT()

N = len(S)
numlist = []
count = 1
for i in range(1, N):
    if S[i] == S[i - 1]:
        count += 1
    else:
        numlist.append(count)
        count = 1
numlist.append(count)
flag = 0
if S[0] == S[-1] and len(numlist) > 2:
    center = numlist[0] + numlist[-1]
    ans = (center // 2) * (K - 1)
    ans += numlist[0] // 2
    ans += numlist[-1] // 2
    for i in range(1, len(numlist)-1):
        ans += (numlist[i] // 2) * K
    import pdb; pdb.set_trace()
else:
    ans = 0
    for i in range(len(numlist)):
        ans += (numlist[i] // 2) * K
print(ans)

Submission status: Runtime Error

Solution code:
word = list(input())
num = int(input())
p = []
count = 1
for i in range(len(word)-1):
  if word[i] == word[i+1]:
    count += 1
    if i == len(word)-2:
      p.append(count)
  else:
    p.append(count)
    count = 1

point = 0
for i in p:
  point += i // 2
if word[0] == word[-1] and p[-1] % 2 == 1:
  point += 1
  
print(point * num)

Submission status: Runtime Error

Solution code:
S = input()
K = int(input())
lst = []

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

ans = 0
for i in lst:
  ans += i // 2
print(ans*K)

Submission status: Runtime Error

Solution code:
S=input()
K=int(input())
count=0
for time in range(0,len(K*len(S))):
  if time[i]==time[i-1]:
    count+=1
print(count)

Submission status: Runtime Error

Solution code:
S=input()
K=int(input())
count=0
for time in range(1,len(K*len(S))):
  if time[i]==time[i-1]:
    count+=1
print(count)

Submission status: Runtime Error

Solution code:
s = list(input())
n = len(s)
t = s + s
k = int(input())
tmp1 = 0
for i in range(n * 2 - 2):
    if t[i] == t[i + 1]:
        tmp1 += 1
        t[i + 1] = 'X'
if t[-1] == t[-2]:
    tmp1 += 1
tmp2 = 0
for i in range(n - 2):
    if s[i] == s[i + 1]:
        tmp2 += 1
        s[i + 1] = 'X'
if s[-1] == s[-2]:
    tmp2 += 1
tmp3 = tmp1 - tmp2 * 2
print(tmp3 * (k - 1) + tmp2 * k)

Submission status: Runtime Error

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

# now_s = S[0]
# for i in range(1, len(S)):
#     if now_s == S[i]:
#         cnt += 1
#     now_s = S[i]

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

for i in range(len(ruisekiwa)):
    cnt += ruisekiwa[i]//2

cnt = cnt * K

if S[0] == S[-1] and S[-2] != S[-1]:
    cnt += K-1

print(cnt)

Submission status: Runtime Error

Solution code:
string s,t=""
s=input()
int k
k=input()
for i in range(k):
  t+=s
int c=0
for j in range(len(t)-1):
  if t[j]==t[j+1]:
    c++
print(c)

Submission status: Runtime Error

Solution code:
def count(st):
    n=len(st)
    tmp=1
    res=[]
    for i in range(n-1):
        if st[i]==st[i+1]:
            tmp+=1
        else:
            res.append(tmp)
            tmp=1
    res.append(tmp)
    return res
S=input()
K=int(input())
T=S*3
N=len(S)

if K<=2:
    Y=count(S*K)
    ans=0
    for i in Y:
        ans+=i//2
    print(ans)
    exit()
X=[]
if S[0]==S[-1]:
    for j in range(1,N):
        if S[j-1]!=S[j]:
            break
    tmp=1
    for i in range(j,j+N-1):
        if T[i]==T[i+1]:
            tmp+=1
        else:
            X.append(tmp)
            tmp=1
    X.append(tmp)
else:
    X=count(S)

cnt=0
for i in X:
    cnt+=i//2
Y=count(T)
cnt2=0
for i in Y:
    cnt2+=i//2
print(cnt*(K-3)+cnt2)

Submission status: Runtime Error

Solution code:
S=input()
K=int(input())
count=0
for time in range(1,len(K)):
  if time[i]==time[i-1]:
    count+=1
print(count)

Submission status: Runtime Error

Solution code:
s=input()
k=int(input())
ans=0
chg=0
for i in range(1,len(s)):
  if chg==0 and s[i-1]==s[i]:
    ans+=1
    chg=1
  else:
    chg=0
ans2=1
chg2=1
for i in range(1,len(s)):
  if chg==0 and s[i-1]==s[i]:
    ans2+=1
    chg2=1
  else:
    chg2=0
if s[0]!=s[1]:
  print(k*ans)
else:
  print(k*min(ans,ans2))

Submission status: Runtime Error

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

L = []
temp = S[0]
c = 1
for i in range(1, len(S)):
    if S[i] == temp:
        c += 1
    else:
        L.append(c)
        c = 1
        temp = S[i]
    if i == len(S)-1:
        L.append(c)
        
ans = 0
if S[0] != S[-1]:
    for i in range(len(L)):
        ans += (L[i]//2)
    ans *= K
else:
    if len(L) == 1:
        ans = K*L[0]//2
    else:
        for i in range(1, len(L)-1):
            ans += (L[i]//2)
        ans *= K
        ans += (L[0]+L[-1])//2*K
        
print(ans)

Submission status: Runtime Error

Solution code:

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

count = 0

for i in range(len(s)-1):
    if s[i] == s[i+1]:
        count += 1
        p = i % 10
        s[i+1] = str(p)

if s[0] == s[-1]:
    count += 1
    p = i % 10
    s[-1] = str(p)

print(int(count*k))

Submission status: Runtime Error

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]

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

N = len(S)
ans = 0
last_same = False
if S[-1]!=S[-2]:
    if S[0]==S[-1]:
        ans += 1
        last_same = True
pre = S[0]
p = False
for i in range(1,N):
    if p:
        pre = S[i]
        p = False
        continue
    s = S[i]
    if pre==s:
        ans += 1
        p = True
    pre = s
ANS = ans*K
if last_same:
    ANS -= 1
print(ANS)

Submission status: Runtime Error

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

cnt = 0
p = 1
while p < len(s):
  if s[p-1] == s[p]:
    cnt += 1
    p += 1
  p += 1

cnt2 = 0
if s[0] == s[-1]:
  cnt2 += 1
  
cnt3 = 0
if s[-2] == s[-1]:
  cnt3 += 1

ans = k*cnt + (k - 1)*cnt2 - (k - 1)*cnt3
print(ans)

Submission status: Runtime Error

Solution code:
s=input()
k=int(input())
a=0
i=0
while True:
    if s[i]==s[i+1]:
        a+=1
        i+=1
    i+=1
    if i>=len(s)-1:
        break
print(a*k)

Submission status: Runtime Error

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

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


count = 0
tmp = S[0]
for i in range(1, len(S)):
    if S[i] == tmp:
        count += 1
        tmp = ""
    else:
        tmp = S[i]

if S[0] == S[-1] and S[-2] != S[-1]:
    count += 1
print(count * K)

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())
cnt = 0
i = 0
while i < len(s):
  if s[i] == s[i+1]:
    i += 2
    cnt += 1
  else:
    i += 1


print(cnt*k)

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())
cnt = 0
i = 0
c0 = 0
c1 = 0
while i < len(s) - 1:
    if s[i] == s[i + 1]:
        cnt += 1
        if i == 0:
            c0 = 1
        if i == len(s) - 2:
            c1 = 1

        i += 1
    i += 1

cnt *= k
if c0 == 0 and c1 == 0:
    if s[1] == s[0]:
        cnt += k - 1

print(cnt)

Submission status: Runtime Error

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

if len(S) == 1:
    print(0)
else:
    c = 0
    flg = False
    for i in range(len(S)-1):
        if S[i] == S[i+1] and flg is False:
            c += 1
            flg = True
        else:
            flg = False

    if flg is False and S[-1] == S[0]:
        j = 1
    else:
        j = 0

print(c*K+j*(K-1))
# print(c, j)