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:
#!/usr/bin/env python3
def solve():
    ans=0
    f=0
    s=input()
    n=len(s)
    k=int(input())
    if len(s)==1:
        return k-1
    count=1
    c1=0
    tmp=s[0]
    if s[-1]==tmp:
        j=1
        for j in range(n-1,1,-1):
            if s[j]!=tmp:
                break
            c1+=1
        n=j+1
        f=1

    for j in range(1,n):
        if s[j]!=tmp:
            break
        count+=1
    if f:
        ans+=(count+c1)//2*(k-1)
        ans+=c1//2*(k-1)
        ans+=count//2
    else:
        ans+=count//2*(k-1)
        
        
    i=1
    while i<n-1:
    # for i in range(1,n-1):
        count=1
        tmp=s[i]
        for j in range(i+1,n):
            if s[j]!=tmp:
                break
            count+=1
        i=j
        # if i==0 and f:
        #     ans+=count//2
        #     count+=1
        #     ans+=count//2*(k-1)
        # else:
        # countd=min(n-i,count)
        # ans+=countd//2
        ans+=count//2*(k)
        # print(count)
    return ans

print(solve())

Submission status: Wrong Answer

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

ans=0

a=s+s
a+='A'
cnt=0
tmp=1
ad=0

bng=1

for i in range(0, len(a)-2):
    if a[i]==a[i+1]:
        bng+=1

if bng==len(a)-1:
    print(int(len(s)*k/2))

else:
    for i in range(0, len(a)-1):
        if a[i]==a[i+1]:
            tmp+=1
        if a[i]!=a[i+1]:
            cnt+=int(tmp/2)
            tmp=1

    v=0
    tmpv=0
    for i in range(1,len(s)-2):
        if s[i]==s[i+1]:
            tmpv+=1
        if s[i]!=s[i+1]:
            v+=int(tmpv/2)
            tmpv=1


    if s[0]==s[len(s)-1]:
        v+=1

    if a[0]==a[len(a)-2]:
        ad=1

    if k%2==0:
        ans=ad*(int(k/2)-1)+cnt*(int(k/2))

    else :
        ans=ad*(int(k/2)-1)+cnt*(int(k/2))+ad+v

    print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
cnt = 0
cur = s[0]
rec = 1
recf = 0
for i in range(1,len(s)):
  if s[i] == cur:
    rec += 1
  else:
    cnt += rec//2
    if recf == 0:
      recf = rec
    rec = 1
    cur = s[i]
cnt += rec//2
recl = rec
if s[0] == s[-1]:
  if (recf >= 2 and recf % 2 == 0) or \
    (recl >= 2 and recl % 2 == 0):
    print(cnt*k)
  else:
    print(cnt*k+k-1)
else:
  print(cnt*k)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
S=list(input())
K=int(input())
S2 = S*2
tmp=0
tmp2=0
for i in range(1,len(S)):
    if S[i-1]==S[i]:
        S[i]="0"
        tmp += 1

for i in range(1,len(S2)):
    if S2[i-1]==S2[i]:
        S2[i]="0"
        tmp2 += 1
if all([[S[i] == S[0]] for i in range(1,len(S))]):
    print((len(S)*K)//2)
    exit()
print(tmp+(tmp2-tmp)*(K-1))

Submission status: Wrong Answer

Solution code:
def calc(S, T):
    U = [(S[0], 1)]
    un = 0
    for i in range(1, len(S)):
        if U[un][0] == S[i]:
            U[un] = (U[un][0], U[un][1] + 1)
        else:
            U += [(S[i], 1)]
            un += 1
    U = [u for u in U if u[1] > 1]
    un = len(U) - 1
    if not (U):
        return 0
    elif un == 0:
        return (U[0][1] * T) // 2
    else:
        if U[0][0] != U[un][0]:
            return sum([u[1] // 2 for u in U]) * T
        else:
            return U[0][1] // 2 + U[un][1] // 2 \
                + (U[0][1] + U[un][1]) // 2 * (T - 1) \
                + sum([u[1] // 2 for u in U[1:un]]) * T


S = input()
T = int(input())
print(calc(S, T))

Submission status: Wrong Answer

Solution code:
S,K=open(0);K=int(K);s=1;l=[]
for c,d in zip(S,S[1:]):
 if c==d:s+=1
 else:l+=[s];s=1
print((len(l)<2)*l[0]*K//2or(S[0]!=S[-2])*K*sum(i//2for i in l)or K*sum(i//2for i in[l[0]+l[-1]]+l[1:-1])-(l[0]&l[-1]&1))

Submission status: Wrong Answer

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

text = a[0]
iter = int(a[1])

text_list = list(text)
ren = []
start = text_list[0]
a = 0
for i in range(1,len(text_list)):
  if text_list[i] == start:
    a += 1
    if i == len(text_list)-1:
      ren.append(a)
  else:
    start = text_list[i]
    ren.append(a)
    a = 0

for i in range(len(ren)):
  ren[i] += 1

if ren == []:
  ren = [1]
  
ans = 0
if text_list[0] == text_list[-1]:
  if ren[0] % 2 == 1 and ren[-1] % 2 == 1:
    for r in ren:
      ans += (r//2)
    ans *= iter
    ans += iter-1
    if iter > 2:
      ans -= 1
  else:
    for r in ren:
      ans += (r//2)
    ans *= iter    
else:
  for r in ren:
    ans += (r//2)
  ans *= iter

print(ans)

Submission status: Wrong Answer

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

text = a[0]
iter = int(a[1])

text_list = list(text)
ren = []
start = text_list[0]
a = 0
for i in range(1,len(text_list)):
  if text_list[i] == start:
    a += 1
    if i == len(text_list)-1:
      ren.append(a)
  else:
    start = text_list[i]
    ren.append(a)
    a = 0

for i in range(len(ren)):
  ren[i] += 1

if ren == []:
  ren = [1]
  
ans = 0
if text_list[0] == text_list[-1]:
  if ren[0] % 2 == 1 and ren[-1] % 2 == 1:
    for r in ren:
      ans += (r//2)
    ans *= iter
    ans += iter-1
    if iter > 2:
      ans += 1
  else:
    for r in ren:
      ans += (r//2)
    ans *= iter    
else:
  for r in ren:
    ans += (r//2)
  ans *= iter

print(ans)

Submission status: Wrong Answer

Solution code:
S,K=open(0);K=int(K);s=1;l=[]
for c,d in zip(S,S[1:]):
 if c==d:s+=1
 else:l+=[s];s=1
print((len(l)==1)*l[0]*K//2or(S[0]!=S[-2])*K*sum(i//2for i in l))or K*sum(i//2for i in[l[0]+l[-1]]+l[1:-1])-(l[0]&l[-1]&1)

Submission status: Wrong Answer

Solution code:
S,K=open(0);K=int(K);s=1;l=[]
for c,d in zip(S,S[1:]):
 if c==d:s+=1
 else:l+=[s];s=1
print((len(l)>1)*(S[0]==S[-2])*(K*sum(i//2for i in[l[0]+l[-1]]+l[1:-1])-(l[0]&l[-1]&1))or K*sum(i//2for i in l))

Submission status: Wrong Answer

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

text = a[0]
iter = int(a[1])

text_list = list(text)
ren = []
start = text_list[0]
a = 0
for i in range(1,len(text_list)):
  if text_list[i] == start:
    a += 1
    if i == len(text_list)-1:
      ren.append(a)
  else:
    start = text_list[i]
    ren.append(a)
    a = 0

for i in range(len(ren)):
  ren[i] += 1

if ren == []:
  ren = [1]
  
ans = 0
if text_list[0] == text_list[-1]:
  if ren[0] % 2 == 1 and ren[-1] % 2 == 1:
    for r in ren:
      ans += (r//2)
    ans *= iter
    ans += iter-1
  else:
    for r in ren:
      ans += (r//2)
    ans *= iter    
else:
  for r in ren:
    ans += (r//2)
  ans *= iter

print(ans)

Submission status: Wrong Answer

Solution code:
S=list(input())
K=int(input())
S2 = S*2
tmp=0
tmp2=0
for i in range(1,len(S)):
    if S[i-1]==S[i]:
        S[i]="0"
        tmp += 1

for i in range(1,len(S2)):
    if S2[i-1]==S2[i]:
        S2[i]="0"
        tmp2 += 1
if all(S) == S[0]:
    print((len(S)*K)//2)
    exit()
print(tmp+(tmp2-tmp)*(K-1))

Submission status: Wrong Answer

Solution code:
S=list(input())
K=int(input())
S2 = S*2
tmp=0
tmp2=0
for i in range(1,len(S)):
    if S[i-1]==S[i]:
        S[i]="0"
        tmp += 1

for i in range(1,len(S2)):
    if S2[i-1]==S2[i]:
        S2[i]="0"
        tmp2 += 1
if all(S) == S[0]:
    print((S*K)//2)
    exit()
print(tmp+(tmp2-tmp)*(K-1))

Submission status: Wrong Answer

Solution code:
import sys

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


countA = 0
for i in range(len(S) - 1):
    if S[i] == S[i + 1]:
        countA += 1
        S[i + 1] = 'A'
 
# 2bai
countB = 0
for i in range(len(T) - 1):
    if T[i] == T[i + 1]:
        countB += 1
        T[i + 1] = 'A'
tmp = countB - (2*countA)
print((countA * K) + (tmp * (K - 1)))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    s = S()
    k = II()
    ans = 0
    lis = []
    i = 0
    tmp = 1
    while len(s) > i + 1:
        if s[i] == s[i + 1]:
            tmp += 1
        else:
            lis.append((s[i], tmp))
            tmp = 1
        i += 1
    lis.append((s[i], tmp))
    if lis[0][0] == lis[-1][0]:
        if len(lis) == 1:
            ans += k // 2 * lis[0][1]
            print(ans)
            return
        ans -= (lis[0][1] // 2 + lis[-1][1] // 2) * (k - 1)
        ans += (lis[0][1] + lis[-1][1]) // 2 * (k - 1)
    for _, b in lis:
        ans += b // 2 * k
    print(ans)
    

    return

#B
def B():
    return

#C
def C():
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == '__main__':
    A()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    s = S()
    k = II()
    ans = 0
    lis = []
    i = 0
    tmp = 1
    while len(s) > i + 1:
        if s[i] == s[i + 1]:
            tmp += 1
        else:
            lis.append((s[i], tmp))
            tmp = 1
        i += 1
    lis.append((s[i], tmp))
    if lis[0][0] == lis[-1][0]:
        if len(lis) == 1:
            ans += (lis[0][1] // 2) * k
            print(ans)
            return
        ans -= (lis[0][1] // 2 + lis[-1][1] // 2) * (k - 1)
        ans += (lis[0][1] + lis[-1][1]) // 2 * (k - 1)
    for _, b in lis:
        ans += b // 2 * k
    print(ans)
    

    return

#B
def B():
    return

#C
def C():
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == '__main__':
    A()

Submission status: Wrong Answer

Solution code:
def calc(S, T):
    if T == 0:
        return 0
    if T == 1:
        s = list(S)
        cnt = 0
        for i in range(1, len(s)):
            if s[i - 1] == s[i]:
                s[i] = '*'
                cnt += 1
        return cnt
    else:
        a = T // 2
        b = T % 2
        return calc(S + S, 1) * a + calc(S, b)


S = input()
T = int(input())
print(calc(S, T))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    s = S()
    k = II()
    ans = 0
    lis = []
    i = 0
    tmp = 1
    while len(s) > i + 1:
        if s[i] == s[i + 1]:
            tmp += 1
        else:
            lis.append((s[i], tmp))
            tmp = 1
        i += 1
    lis.append((s[i], tmp))
    if lis[0][0] == lis[-1][0]:
        if len(lis) == 1:
            ans += (lis[0][1] // 2) * k
            print(ans)
            return
        ans -= (lis[0][1] + lis[-1][1])
        ans += (lis[0][1] + lis[-1][1]) // 2
    for i in range(len(lis)):
        ans += (lis[i][1] // 2) * k
    print(ans)
    

    return

#B
def B():
    return

#C
def C():
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == '__main__':
    A()

Submission status: Wrong Answer

Solution code:
s = str(input())
k = int(input())
if len(s)==1:
    print((len(s)*k)//2)
    exit()

t = [1]
for i in range(1, len(s)):
    if s[i-1]==s[i]:
        t[-1]+=1
    else:
        t.append(1)

a = 0
for i in t:
    a+=(i//2)*k
if s[0]==s[-1]:
    if t[0]%2==t[-1]%2==1:
        a += k-1

print(a)

Submission status: Wrong Answer

Solution code:
import re

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


if __name__ == "__main__":
    s = input()
    K = int(input())

    if len(s) == 1:
        print(K // 2)
        exit()
    
    ans1 = 0
    if len(s) >= 3:
        if s[0] != s[1] and s[-1] != s[-2] and s[0] == s[-1]:
            ans1 = K-1

    t = list(main(s))

    if len(t) == 1 and len(t[0]) == len(s):
        print((len(s) * K) // 2)
        exit()

    ans2 = 0
    for i in t:
        ans2 += len(i) // 2
    
    ans = ans1 + ans2 * K
    print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

# coding: utf-8
# Your code here!

s=input()
K=int(input())
S=list(s) #文字を置き換えるためリストに変換

def monomer(A): #文字列内の置換回数を数える関数。引数がリスト。
    N=0
    for i in range(len(A)-1):
        if A[i]==A[i+1]:
            A[i+1]="?"
            N+=1
    return N

 #文字列S単体内での隣接重複文字、重複を置き換える
if S[0]!=S[-1]: #モノマー内の置き換え後に最初と最後が同じでなければそのまま掛け算でOK
    print(monomer(S)*K)

#置き換え後も最初と最後が同じ場合は接続ダブりを考慮
else:
    S1=list(s)
    n=monomer(S1) #初項の置換
    WS=list(s*2) #pppのような、偶数次と奇数次で置換文字が交互に変わるパターン対策
    WS.insert(0,WS[-1]) #ppapのような2次以降に最初の文字を置換した方がいいパターン対策
    wn=monomer(WS)
    S1.insert(0,S[-1])
    nn=monomer(S1)
    print(n+wn*((K-1)//2)+nn*((K-1)%2)) #最初のループ、途中のループ、最後に余ったループでの置き換え回数

Submission status: Wrong Answer

Solution code:
import re

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


if __name__ == "__main__":
    s = input()
    K = int(input())

    if len(s) == 1:
        print(K // 2)
        exit()
    
    ans1 = 0
    if len(s) >= 3:
        if s[0] != s[1] and s[-1] != s[-2] and s[0] == s[-1]:
            ans1 = (K-1)

    s = list(main(s))
    ans2 = 0
    for i in s:
        ans2 += len(i) // 2
    
    ans = ans1 + ans2 * K
    print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    s = S()
    k = II()
    ans = 0
    lis = []
    i = 0
    tmp = 1
    while len(s) > i + 1:
        if s[i] == s[i + 1]:
            tmp += 1
        else:
            lis.append((s[i], tmp))
            tmp = 1
        i += 1
    lis.append((s[i], tmp))
    if lis[0][0] == lis[-1][0]:
        if len(lis) == 1:
            ans += (lis[0][1] // 2) * (k - 1)
        else:
            ans += ((lis[0][1] + lis[-1][1]) // 2) * (k - 1)
    for i in range(len(lis)):
        if i == 0 or len(lis) - 1 == i:
            ans += lis[i][1] // 2
        else:
            ans += (lis[i][1] // 2) * k
    print(ans)
    

    return

#B
def B():
    return

#C
def C():
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == '__main__':
    A()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

# coding: utf-8
# Your code here!

s=input()
K=int(input())
S=list(s) #文字を置き換えるためリストに変換

def monomer(A): #文字列内の置換回数を数える関数。引数がリスト。
    N=0
    for i in range(len(A)-1):
        if A[i]==A[i+1]:
            A[i+1]="?"
            N+=1
    return N

 #文字列S単体内での隣接重複文字、重複を置き換える
if S[0]!=S[-1]: #モノマー内の置き換え後に最初と最後が同じでなければそのまま掛け算でOK
    print(monomer(S)*K)

#置き換え後も最初と最後が同じ場合は接続ダブりを考慮
else:
    S1=list(s)
    del S1[-1]
    n=monomer(S1)
    WS=list(s*2) #pppのような、偶数次と奇数次で置換文字が交互に変わるパターン対策
    WS.insert(0,WS[-1]) #ppapのような2次以降に最初の文字を置換した方がいいパターン対策
    wn=monomer(WS)
    S1.insert(0,S[-1])
    nn=monomer(S1)
    print(n+wn*((K-1)//2)+nn*((K-1)%2)) #最初のループ、途中のループ、最後に余ったループでの置き換え回数

Submission status: Wrong Answer

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

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

count = 1
i = 0
sum = 0
while(1):
    if S[i] == S[i+1]:
        count += 1
        i += 1
    else:
        sum += count//2
        count = 1
        i += 1
    if i == len(S)-1:
        break

sum += count//2 #最後のカウントの加算

#print("check1",sum)

#先頭の連続数をカウント
j = 0 # リストカウント
a = 1 # 連続数カウント
while(1):
    if S[j] == S[j+1]:
        a += 1
        j += 1
    else:
        break
    if j == len(S) -1:
        break
#print("check")

#末尾の連続数をカウント
m = len(S)-1 # リストカウント
b = 1 # 連続数カウント
while(1):
    if S[m] == S[m-1]:
        m -= 1
        b += 1
    else:
        break
    if m == 0:
        break

if S[len(S)-1] == S[0]:
    print( sum * K + ((a+b)//2 - a//2 - b //2 ) * ( K -1))
else:
    print( sum * K)

Submission status: Wrong Answer

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

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

count = 1
i = 0
sum = 0
while(1):
    if S[i] == S[i+1]:
        count += 1
        i += 1
    else:
        sum += count//2
        count = 1
        i += 1
    if i == len(S)-1:
        break
if sum != 1:
    sum += count//2
#print("check1",sum)

#先頭の連続数をカウント
j = 0 # リストカウント
a = 1 # 連続数カウント
while(1):
    if S[j] == S[j+1]:
        a += 1
        j += 1
    else:
        break
    if j == len(S) -1:
        break
#print("check")

#末尾の連続数をカウント
m = len(S)-1 # リストカウント
b = 1 # 連続数カウント
while(1):
    if S[m] == S[m-1]:
        m -= 1
        b += 1
    else:
        break
    if m == 0:
        break

if S[len(S)-1] == S[0]:
    print( sum * K + ((a+b)//2 - a//2 - b //2 ) * ( K -1))
else:
    print( sum * K)

Submission status: Wrong Answer

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

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

count = 1
i = 0
sum = 0
while(1):
    if S[i] == S[i+1]:
        count += 1
        i += 1
    else:
        sum += count//2
        count = 1
        i += 1
    if i == len(S)-1:
        break
sum += count//2
#print("check1",sum)
#先頭の連続数をカウント
j = 0 # リストカウント
a = 1 # 連続数カウント
while(1):
    if S[j] == S[j+1]:
        a += 1
        j += 1
    else:
        break
    if j == len(S) -1:
        break
#print("check")
#末尾の連続数をカウント
m = len(S)-1 # リストカウント
b = 1 # 連続数カウント
while(1):
    if S[m] == S[m-1]:
        m -= 1
        b += 1
    else:
        break
    if m == 0:
        break

if S[len(S)-1] == S[0]:
    print( sum * K + ((a+b)//2 - a//2 - b //2 ) * ( K -1))
else:
    print( sum * K)

Submission status: Wrong Answer

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

if len(S) == 1:
    print(K -1)
    exit()

count = 1
i = 0
sum = 0
while(1):
    if S[i] == S[i+1]:
        count += 1
        i += 1
    else:
        sum += count//2
        count = 1
        i += 1
    if i == len(S)-1:
        break
sum += count//2
#print("check1",sum)
#先頭の連続数をカウント
j = 0 # リストカウント
a = 1 # 連続数カウント
while(1):
    if S[j] == S[j+1]:
        a += 1
        j += 1
    else:
        break
    if j == len(S) -1:
        break
#print("check")
#末尾の連続数をカウント
m = len(S)-1 # リストカウント
b = 1 # 連続数カウント
while(1):
    if S[m] == S[m-1]:
        m -= 1
        b += 1
    else:
        break
    if m == 0:
        break

if S[len(S)-1] == S[0]:
    print( sum * K + ((a+b)//2 - a//2 - b //2 ) * ( K -1))
else:
    print( sum * K)

Submission status: Wrong Answer

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

if len(S) == 1:
    print(0)
    exit()

count = 1
i = 0
sum = 0
while(1):
    if S[i] == S[i+1]:
        count += 1
        i += 1
    else:
        sum += count//2
        count = 1
        i += 1
    if i == len(S)-1:
        break
sum += count//2
#print("check1",sum)
#先頭の連続数をカウント
j = 0 # リストカウント
a = 1 # 連続数カウント
while(1):
    if S[j] == S[j+1]:
        a += 1
        j += 1
    else:
        break
    if j == len(S) -1:
        break
#print("check")
#末尾の連続数をカウント
m = len(S)-1 # リストカウント
b = 1 # 連続数カウント
while(1):
    if S[m] == S[m-1]:
        m -= 1
        b += 1
    else:
        break
    if m == 0:
        break

if S[len(S)-1] == S[0]:
    print( sum * K + ((a+b)//2 - a//2 - b //2 ) * ( K -1))
else:
    print( sum * K)

Submission status: Wrong Answer

Solution code:
def calc(S, T):
    s = list(S + S[0])
    cnt = 0
    for i in range(1, len(s)):
        if s[i - 1] == s[i]:
            s[i] = '*'
            cnt += 1
    return cnt * T


S = input()
T = int(input())
print(calc(S, T))

Submission status: Wrong Answer

Solution code:
import math
S=list(input())
K=int(input())
count = 0
ans = 0
flag = False
firstCount = 0

for i in range(1,len(S)):
    if S[i-1] == S[i]:
      count += 1
    else:
      if flag == False:
        firstCount = count
        flag = True
      ans += ((count+1)//2)*K
      count = 0
ans += ((count+1)//2)*K
if S[0]==S[len(S)-1]:
  ans -= ((count+firstCount+2)//2)*(K-1)

print(ans)

Submission status: Wrong Answer

Solution code:
S=list(input())
K=int(input())
S2 = S*2
tmp=0
tmp2=0
for i in range(1,len(S)):
    if S[i-1]==S[i]:
        S[i]="0"
        tmp += 1

for i in range(1,len(S2)):
    if S2[i-1]==S2[i]:
        S2[i]="0"
        tmp2 += 1
print(tmp+(tmp2-tmp)*(K-1))

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
l = len(S)
s_before = ""
ans = [0]*3
cnt = 0
if K ==1:
  for s in S:
    if s ==s_before:
      ans[0]+= 1
      s_before = ""
    else:
      s_before = s
  print(ans[0])
else:
  for s in S*3:
    if 0 <= cnt <= l-1:
      if s ==s_before:
        ans[0]+= 1
        s_before = ""
      else:
        s_before = s
      cnt +=1
    elif l <= cnt <= l*2-1:
      if s ==s_before:
        ans[1]+= 1
        s_before = ""
      else:
        s_before = s
      cnt += 1
    else:
      if s ==s_before:
        ans[2]+= 1
        s_before = ""
      else:
        s_before = s
      cnt += 1
  print(ans[0]+(K-2)*ans[1]+ans[2])

Submission status: Wrong Answer

Solution code:
S_list = [s for s in input()]
S_double_list = S_list + S_list
K = int(input())
first_count = 0
second_count = 0
count = 0
first_last_flag = 0
second_last_flag = 0
if len(set(S_list)) == 1:
    print((len(S_list) * K) // 2)

else:
    S_double_list.append(S_list[0])

    for idx, s in enumerate(S_double_list):
        if idx == len(S_double_list) - 1:
            break
        if S_double_list[idx + 1] == s:
            if idx < len(S_list):
                first_count += 1
                S_double_list[idx + 1] = "*"
                if idx == len(S_list) - 1:
                    first_last_flag = 1
            else:
                second_count += 1
                S_double_list[idx + 1] = "*"
                if idx == len(S_double_list) - 2:
                    second_last_flag = 1
    if K % 2 == 0 and second_last_flag == 1 :
        print(first_count * (K - (K // 2)) + second_count * ((K // 2)) - 1)
    elif K % 2 == 1 and first_last_flag == 1:
        print(first_count * (K - (K // 2)) + second_count * ((K // 2)) - 1)
    else:
        print(first_count * (K - (K // 2)) + second_count * ((K // 2)))

Submission status: Wrong Answer

Solution code:
S_list = [s for s in input()]
S_double_list = S_list + S_list
K = int(input())
first_count = 0
second_count = 0
count = 0
first_last_flag = 0
second_last_flag = 0
if len(set(S_list)) == 1:
    print((len(S_list) * K) // 2)

elif K > 1:
    S_double_list.append(S_list[0])

    for idx, s in enumerate(S_double_list):
        if idx == len(S_double_list) - 1:
            break
        if S_double_list[idx + 1] == s:
            if idx < len(S_list):
                first_count += 1
                S_double_list[idx + 1] = "*"
                if idx == len(S_list) - 1:
                    first_last_flag = 1
            else:
                second_count += 1
                S_double_list[idx + 1] = "*"
                if idx == len(S_double_list) - 2:
                    second_last_flag = 1
    if K % 2 == 0 and second_last_flag == 1 :
        print(first_count * (K - (K // 2)) + second_count * ((K // 2)) - 1)
    elif K % 2 == 1 and first_last_flag == 1:
        print(first_count * (K - (K // 2)) + second_count * ((K // 2)) - 1)
    else:
        print(first_count * (K - (K // 2)) + second_count * ((K // 2)))

else:
    for idx, s in enumerate(S_list):
        if idx == len(S_list) - 1:
            break
        if S_list[idx + 1] == s:
            count += 1
            S_list[idx + 1] = "*"
    print(count * K)

Submission status: Wrong Answer

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

if len(set(list(s)))==1:print((len(s)*k)//2);exit()

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


print(a*k-c)

Submission status: Wrong Answer

Solution code:
S_list = [s for s in input()]
S_double_list = S_list + S_list
K = int(input())
first_count = 0
second_count = 0
first_last_flag = 0
second_last_flag = 0
if len(set(S_list)) == 1:
    print((len(S_list) * K) // 2)

elif K > 1:
    S_double_list.append(S_list[0])

    for idx, s in enumerate(S_double_list):
        if idx == len(S_double_list) - 1:
            break
        if S_double_list[idx + 1] == s:
            if idx < len(S_list):
                first_count += 1
                S_double_list[idx + 1] = "*"
                if idx == len(S_list) - 1:
                    first_last_flag = 1
            else:
                second_count += 1
                S_double_list[idx + 1] = "*"
                if idx == len(S_double_list) - 2:
                    second_last_flag = 1
    if K % 2 == 0 and second_last_flag == 1 :
        print(first_count * (K - (K // 2)) + second_count * ((K // 2)) - 1)
    elif K % 2 == 1 and first_last_flag == 1:
        print(first_count * (K - (K // 2)) + second_count * ((K // 2)) - 1)
    else:
        print(first_count * (K - (K // 2)) + second_count * ((K // 2)))

else:
    for idx, s in enumerate(S_list):
        if idx == len(S_list) - 1:
            break
        if S_list[idx + 1] == s:
            count += 1
            S_list[idx + 1] = "*"
    print(count * K)

Submission status: Wrong Answer

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

if len(s)==1:
  print(k//2)
  import sys 
  sys.exit()

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


print(a*k-c)

Submission status: Wrong Answer

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

li = list(S)
last = 0

if li[0] == li[-1]:
    last = 1
cnt = 0
sum = 0
for i in li:
    if cnt == 0:
        next
    elif i == li[cnt-1]:
        sum += 1
    cnt += 1
total = sum * K + last * (K-1)
print(total)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
ans = []
seqlen = 1
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        seqlen += 1
    else:
        ans.append(seqlen)
        seqlen = 1
ans.append(seqlen)
key = 0
key0 = 0
key1 = 0
if s[-1] == s[0]:
    key0, key1 = ans[0], ans[-1]
    ans[0] = ans[0]+ans[-1]
    ans.pop()
    key = 1
for i in range(len(ans)):
    ans[i] = ans[i] // 2
answer = sum(ans)
if key0:
    print(answer*k+key0//2+key1//2-(key0+key1)//2)
else:
    print(answer*k)