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:
S = input()
K = int(input())

def count(s):
    """何文字変更が必要か
    """
    a = []
    b = ''
    c = 1
    for si in s:
        if b == si:
            c += 1
        else:
            if c != 1:
                a.append(c)
            c = 1
        b = si
    if c != 1:
        a.append(c)

    r = 0
    for ai in a:
        r += ai // 2
    # print(s, a, r)
    return r

if K == 1:
    ans = count(S)
elif len(S) == 1:
    ans = (K + 1) // 2
elif K == 2:
    ans = count(S*2)
else:
    c1 = count(S)

    concat2 = S * 2
    c2 = count(concat2)

    concat3 = S * 3
    c3 = count(concat3)
    diff = c3 - c2
    # print(c1, diff, K)
    ans = c1 + (diff * (K-1))

print(ans)

Submission status: Wrong Answer

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



def count(s):
    """何文字変更が必要か
    """
    a = []
    b = ''
    c = 1
    for si in s:
        if b == si:
            c += 1
        else:
            if c != 1:
                a.append(c)
            c = 1
        b = si
    if c != 1:
        a.append(c)

    r = 0
    for ai in a:
        r += ai // 2
    # print(s, a, r)
    return r

if K == 1:
    ans = count(S)
elif len(S) == 1:
    ans = (K + 1) // 2
elif K == 2:
    ans = count(S*2)
else:
    c1 = count(S)

    concat2 = S * 2
    c2 = count(concat2)

    concat3 = S * 3
    c3 = count(concat3)
    diff = c3 - c2
    print(c1, diff, K)
    ans = c1 + (diff * (K-1))

print(ans)

Submission status: Wrong Answer

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



def count(s):
    """何文字変更が必要か
    """
    a = []
    b = ''
    c = 1
    for si in s:
        if b == si:
            c += 1
        else:
            if c != 1:
                a.append(c)
            c = 1
        b = si
    if c != 1:
        a.append(c)

    r = 0
    for ai in a:
        r += ai // 2
    # print(s, a, r)
    return r

if K == 1:
    ans = count(S)
elif K == 2:
    ans = count(S*2)
else:
    c1 = count(S)

    concat2 = S * 2
    c2 = count(concat2)

    concat3 = S * 3
    c3 = count(concat3)
    diff = c3 - c2
    ans = c1 + (diff * (K-1))

print(ans)

Submission status: Wrong Answer

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



def count(s):
    """何文字変更が必要か
    """
    a = []
    b = ''
    c = 1
    for si in s:
        if b == si:
            c += 1
        else:
            if c != 1:
                a.append(c)
            c = 1
        b = si
    if c != 1:
        a.append(c)

    r = 0
    for ai in a:
        r += ai // 2
    # print(s, a, r)
    return r

if K == 1:
    ans = count(S)
else:
    concat2 = S * 2
    c2 = count(concat2)

    concat3 = S * 3
    c3 = count(concat3)
    diff = c3 - c2
    ans = diff * K

print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
n=len(s)
k=int(input())
if k==1:
  if n==1:
    print(0)
  else:
    i=1
    cnt=0
    while i<n:
      if s[i-1]==s[i]:
        cnt+=1
        i+=1
      i+=1
    print(cnt)
else:
  single=True
  for c in s:
    if c!=s[0]:
      single=False
      break
  if single:
    print(n*k//2)
  elif s[0]!=s[-1]:
    i=1
    cnt=0
    while i<n:
      if s[i-1]==s[i]:
        cnt+=1
        i+=1
      i+=1
    print(cnt*k)
  else:
    i=1
    while s[i]==s[0]:
      i+=1
    k=i
    j=-1
    while s[j]==s[0]:
      j-=1
    num=i-j-1
    cnt=0
    i+=1
    while i<=n+j:
      if s[i-1]==s[i]:
        cnt+=1
        i+=1
      i+=1
    print(cnt*k+num//2*(k-1)+k//2+(-j-1)//2)

Submission status: Wrong Answer

Solution code:
s=input()
n=len(s)
k=int(input())
if k==1:
  if n==1:
    print(0)
  else:
    i=1
    cnt=0
    while i<n:
      if s[i-1]==s[i]:
        cnt+=1
        i+=1
      i+=1
    print(cnt)
else:
  single=True
  for c in s:
    if c!=s[0]:
      single=False
      break
  if single:
    print(n*k//2)
  elif s[0]!=s[-1]:
    i=1
    cnt=0
    while i<n:
      if s[i-1]==s[i]:
        cnt+=1
        i+=1
      i+=1
    print(cnt*k)
  else:
    i=1
    cnt=0
    flag=True
    while i<n:
      if s[i-1]==s[i]:
        cnt+=1
        if i==n-1:
          flag=False
        i+=1
      i+=1
    if flag:
      print(cnt*k+k-1)
    else:
      print(cnt*k)

Submission status: Wrong Answer

Solution code:
s=input()
n=len(s)
k=int(input())
if k==1:
  if n==1:
    print(0)
  else:
    i=1
    cnt=0
    while i<n:
      if s[i-1]==s[i]:
        cnt+=1
        i+=1
      i+=1
    print(cnt)
else:
  if n==1:
    print(k//2)
  elif s[0]!=s[-1]:
    i=1
    cnt=0
    while i<n:
      if s[i-1]==s[i]:
        cnt+=1
        i+=1
      i+=1
    print(cnt*k)
  else:
    i=1
    cnt=0
    flag=True
    while i<n:
      if s[i-1]==s[i]:
        cnt+=1
        if i==n-1:
          flag=False
        i+=1
      i+=1
    if flag:
      print(cnt*k+k-1)
    else:
      print(cnt*k)

Submission status: Wrong Answer

Solution code:
s=input()
n=len(s)
k=int(input())
if k==1:
  if n==1:
    print(0)
  else:
    i=1
    cnt=0
    while i<n:
      if s[i-1]==s[i]:
        cnt+=1
        i+=1
      i+=1
    print(cnt)
else:
  if n==1:
    print(k-1)
  elif s[0]!=s[-1]:
    i=1
    cnt=0
    while i<n:
      if s[i-1]==s[i]:
        cnt+=1
        i+=1
      i+=1
    print(cnt*k)
  else:
    i=1
    cnt=0
    flag=True
    while i<n:
      if s[i-1]==s[i]:
        cnt+=1
        if i==n-1:
          flag=False
        i+=1
      i+=1
    if flag:
      print(cnt*k+k-1)
    else:
      print(cnt*k)

Submission status: Wrong Answer

Solution code:
s = str(input())
k = int(input())
arr = []
for i in range(len(s) - 1):
    count = 1
    if s[i] == s[i + 1]:
        count += 1
        if i == len(s) - 2:
            arr.append(count)
    else:
        arr.append(count)
        if i == len(s) - 2:
            arr.append(1)
if len(arr) == 0 or len(arr) == 1:
    print(len(s) * k // 2)
    exit()
ans = 0
for i in range(len(arr) - 2):
    ans += arr[i + 1] // 2
if s[0] == s[-1]:
    ans += (arr[0] + arr[-1]) // 2 * (k - 1)
else:
    ans += arr[0] // 2 + arr[-1] // 2
    ans *= k
print(ans)

Submission status: Wrong Answer

Solution code:
s = str(input())
k = int(input())
arr = []
for i in range(len(s) - 1):
    count = 1
    if s[i] == s[i + 1]:
        count += 1
        if i == len(s) - 2:
            arr.append(count)
    else:
        arr.append(count)
        if i == len(s) - 2:
            arr.append(1)
if len(arr) == 0 or len(arr) == 1:
    print(len(s) * k // 2)
    exit()
ans = 0
for i in range(len(arr) - 2):
    ans += arr[i + 1] // 2
ans = ans * k + arr[0] // 2 + arr[-1] // 2
if s[0] == s[-1]:
    ans += (arr[0] + arr[-1]) * (k - 1)
print(ans)

Submission status: Wrong Answer

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

if len(set(S)) == 1:
  print(len(S)*K//2)
  exit()
  
if S[0] != S[-1]:
  cnt = 1
  s = S[0]
  l = []
  for i in range(1,len(S)):
    if S[i] == s:
      cnt += 1
    else:
      l.append(cnt//2)
      cnt = 1
      s = S[i]
  l.append(cnt//2)
  print(sum(l)*K)
else:
  cnt = 1
  s = S[0]
  l = []
  for i in range(1, len(S)):    
    if S[i] == s:
      cnt += 1
    else:
      l.append(cnt)
      cnt = 1
      s = S[i]
  l.append(cnt)
  num = l[0]+l[-1]
  ans = num*(K-1)//2
  for i in range(len(l)):
    if i == 0 or i == len(l)-1:
      if l[i] > 1:
        ans += l[i]//2
    else:
      ans += (l[i]//2)*K
  print(ans)

Submission status: Wrong Answer

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

if len(set(S)) == 1:
  print(len(S)*K//2)
  exit()
  
if S[0] != S[-1]:
  cnt = 1
  s = S[0]
  l = []
  for i in range(1,len(S)):
    if S[i] == s:
      cnt += 1
    else:
      l.append(cnt//2)
      cnt = 1
      s = S[i]
  l.append(cnt//2)
  print(sum(l)*K)
else:
  cnt = 1
  s = S[0]
  l = []
  for i in range(1, len(S)):    
    if S[i] == s:
      cnt += 1
    else:
      l.append(cnt)
      cnt = 1
      s = S[i]
  l.append(cnt)
  num = l[0]+l[-1]
  ans = num*(K-1)//2
  for i in range(len(l)):
    if i == 0 or i == len(l)-1:
      if l[i] > 1:
        ans += l[i]//2
    else:
      ans += l[i]*K//2
  print(ans)

Submission status: Wrong Answer

Solution code:
def runlen(a):
    n = len(a)
    result = []
    count = 1
    for i in range(n):
        if i == n - 1 or a[i] != a[i + 1]:
            result.append((count, a[i]))
            count = 1
        else:
            count += 1
    return result

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

rl = runlen(S)

if len(rl) == 1:
    result = (len(S) * K) // 2
else:
    result = rl[0][0] // 2 + rl[-1][0] // 2 + K * sum(map(lambda i: rl[i][0] // 2, range(1, len(rl) - 1)))
    if rl[0][1] == rl[-1][1]:
        result += (K - 1) * ((rl[0][0] + rl[-1][0]) // 2)

print(result)

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
if len(S)==1:
  print(K//2)
  exit()
if len(S)==2:
  if S[0]==S[1]:
    print(K)
  else:
    print(0)
  exit()
ans=0
# 内部
cnt=1
fro=0
flag=1
for i in range(len(S)-1):
  if S[i+1]==S[i]:
    cnt+=1
  else:
    if flag: 
      fro=cnt
      flag=0
    ans+=cnt//2
    cnt=1
ans+=cnt//2
ans*=K
#接続部
if S[0]==S[-1]:
  if fro%2==1 and cnt%2==1:
    ans+=K-1
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    string = input()
    repeat = int(input())
    count_succession = 0
    before = ""
    can_change = True
    answer = 0
    for i in range(len(string)):
        if before == string[i] and can_change:
            count_succession += 1
            can_change = False
        else:
            before = string[i]
            can_change = True
    if string[0] == string[-1]:
        if len(string) > 1 and not string[-1] == string[-2]:
            answer += repeat - 1
        elif len(string) == 1:
            answer += repeat // 2
    answer += count_succession * repeat
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    string = input()
    repeat = int(input())
    count_succession = 0
    before = ""
    can_change = True
    answer = 0
    for i in range(len(string)):
        if before == string[i] and can_change:
            count_succession += 1
            can_change = False
        else:
            before = string[i]
            can_change = True
    if len(set(string)) == 1 and len(string) > 2:
        answer += repeat // 2
    answer += count_succession * repeat
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    s = input()
    k = int(input())
    n = len(s)

    if s == s[0]*n:
        print( n*k//2 )
        return 0

    cnt, t = 0, 0
    for key, value in enumerate(s):
        if key == 0: continue
        if value == s[key-1]:
            t += 1
        else:
            cnt += t // 2
            t = 1
    cnt += t//2
    left, right = 0, len(s)-1
    a, b = 0, 0
    while s[left] == s[0]:
        left += 1
        a += 1
    while s[right] == s[-1]:
        right -= 1
        b += 1

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

main()

Submission status: Wrong Answer

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

    if s == s[0]*len(s):
        print( len(s)*k//2 )
        return 0

    cnt, t = 0, 0
    for key, value in enumerate(s):
        if key == 0: continue
        if value == s[key-1]:
            t += 1
        else:
            cnt += t // 2
            t = 1
    cnt += t//2
    left, right = 0, len(s)-1
    a, b = 1, 1
    while s[left] == s[0]:
        left += 1
        a += 1
    while s[right] == s[-1]:
        right -= 1
        b += 1

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

main()

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
cnt=0
if len(set(s))==1:
    print(len(s)*k//2)
elif s[0]!=s[-1]:
    tmp=1
    for i in range(1, len(s)):
        if s[i]==s[i-1]:
            tmp+=1
        else:
            cnt+=tmp//2
            tmp=1
    cnt+=tmp//2
    print(cnt*k)
else:
    tmp=1
    for i in range(1, len(s)):
        if s[i]==s[i-1]:
            tmp+=1
        else:
            cnt+=tmp//2
            tmp=1
    cnt+=tmp//2

    a,b=1,1
    i=0
    while s[i]==s[i-1]:
        a+=1
        i+=1
    i=0
    while s[-1-i]==s[-2-i]:
        b+=1
        i+=1
    ans=cnt*k-(a//2+b//2-(a+b)//2)*(k-1)
    print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
cnt=0
tmp=1
if len(s)==1:
    print(k//2)
    exit()

for i in range(1,len(s)):
    if s[i]==s[i-1]:
        tmp+=1
    else:
        cnt+=tmp//2
        tmp=1
cnt+=tmp//2
if k==1:
    print(cnt)
    exit()

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

Submission status: Wrong Answer

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

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

print(cnt*k)

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
cnt=0
for i in range(1,len(s)):
    if s[i]==s[i-1]:
        s=s[:i]+'?'+s[i+1:]
        cnt+=1
if s[0]==s[-1]:
    cnt+=1

print(cnt*k)

Submission status: Wrong Answer

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

cnt = 0
for i in range(n):
  if (s[i]==s[(i-1)%n] and s[i]==s[(i-2)%n]):
    cnt += 1
    s = s[:(i-1)%n] + '0' + s[(i-1)%n+1:]
  if (s[i]!='0') and (s[i]==s[(i-1)%n]):
    cnt += 1
    s = s[:i] + '0' + s[i+1:]

cnt *= k

for i in [0,-1]:
  if (s[i]=='0' and s[(i-1)%n]!=s[(i+1)%n]):
    cnt -= 1
    continue

print(cnt)

Submission status: Wrong Answer

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

cnt = 0
for i in range(n):
  if (s[i]==s[(i-1)%n] and s[i]==s[(i-2)%n]):
    cnt += 1
    s = s[:(i-1)%n] + '0' + s[(i-1)%n+1:]
  if (s[i]!='0') and (s[i]==s[(i-1)%n]):
    cnt += 1
    s = s[:i] + '0' + s[i+1:]
  # print(s,cnt)

cnt *= k
print(cnt)

Submission status: Wrong Answer

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

cnt = 0
for i in range(n):
  if (s[i]==s[(i-1)%n] and s[i]==s[(i-2)%n]):
    cnt += 1
    s = s[:(i-1)%n] + '0' + s[i:]
  if (s[i]==s[(i-1)%n]):
    cnt += 1
    s = s[:i] + '0' + s[i+1:]

cnt *= k
print(cnt)

Submission status: Wrong Answer

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

ans = 0

count = 1
past = ''

for i in s:
    if i == past:
        count += 1
    else:
        ans += count // 2
        count = 1
    past = i

ans += count // 2
ans *= n

if s[0] == s[-1]:
    past = s[0]
    idx = 1
    begin = 1

    while idx < len(s) and past == s[idx] :
        begin += 1
        idx += 1

    past = s[-1]
    idx = -1
    end = 1

    while idx > 0 and past == s[idx] :
        end += 1
        idx -= 1

    if begin %2 != 0 and count %2 != 0:
        ans += n - 1

print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
if s[0]*len(s) == s and len(s) % 2 == 1:
    print(len(s)*k//2)
    exit()
s = [s_i for s_i in s]
c = a = 0
s_ = s[0]
idx = 1
while idx < len(s):
    if s_ == s[idx]:
        c += 1
    else:
        s_ = s[idx]
        c = 0
    a += c % 2
    idx += 1
a *= k
if s[-1] == s[0]:
    if (c > 0 and c % 2 == 0) or c == 0: a += k - 1
print(a)

Submission status: Wrong Answer

Solution code:
s = [s_i for s_i in input()]
k = int(input())
c = a = 0
s_ = s[0]
idx = 1
while idx < len(s):
    if s_ == s[idx]:
        c += 1
    else:
        s_ = s[idx]
        c = 0
    a += c % 2
    idx += 1
a *= k
if s[-1] == s[0]:
    if (c > 0 and c % 2 == 0) or c == 0: a += k - 1
print(a)

Submission status: Wrong Answer

Solution code:
s = [s_i for s_i in input()]
k = int(input())
c = a = 0
s_ = s[0]
idx = 1
while idx < len(s):
    if s_ == s[idx]:
        c += 1
    else:
        s_ = s[idx]
        c = 0
    a += c % 2
    idx += 1
a *= k
if c != 0 and c % 2 == 0 and s[-1] == s[0]: a -= 1
print(a)

Submission status: Wrong Answer

Solution code:
def solve():
    s = list(input())
    k = int(input())
    ans = 0
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            ans += 1
            s[i+1] = '0'
    return ans * k

print(solve())

Submission status: Wrong Answer

Solution code:
# AGC039a
def main():
    import distutils.sysconfig
    # map(int, input().split())

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

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

    ans = 0
    targ = ''
    cou = 1
    flag = True
    for i in range(len(s)):
        if s[i] == targ:
            cou += 1
            if i == len(s) - 1 and cou % 2 == 0:
                flag = False
            continue
        ans += cou // 2
        cou = 1
        targ = s[i]
    ans += cou // 2
    cou = 1

    ans *= k

    if s2[0] == s2[-1]:
        f = s2[0]
        a = -1
        for i in s2:
            if i != f:
                break
            a += 1
        f = s2[-1]
        b = -1
        for i in s2[::-1]:
            if i != f:
                break
            b += 1
        ans -= (a//2 + b//2)*(k-1)
        ans += (a+b)//2*(k - 1)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# AGC039a
def main():
    import distutils.sysconfig
    # map(int, input().split())

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

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

    ans = 0
    targ = ''
    cou = 1
    flag = True
    for i in range(len(s)):
        if s[i] == targ:
            cou += 1
            if i == len(s) - 1 and cou % 2 == 0:
                flag = False
            continue
        ans += cou // 2
        cou = 1
        targ = s[i]
    ans += cou // 2
    cou = 1
    f = s2[0]
    a = -1
    for i in s2:
        if i != f:
            break
        a += 1
    f = s2[-1]
    b = -1
    for i in s2[::-1]:
        if i != f:
            break
        b += 1
    ans *= k
    # if s2[0] == s2[-1]:
    #    ans -= (a // 2 + b // 2 - (a + b) // 2) * (k - 1)
    if flag and s2[0] == s2[-1]:
        ans += k - 1
    #print(a, b, *s2)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# AGC039a
def main():
    import distutils.sysconfig
    # map(int, input().split())

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

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

    ans = 0
    targ = ''
    cou = 1
    for i in range(len(s)):
        if s[i] == targ:
            cou += 1
            continue
        ans += cou // 2
        cou = 1
        targ = s[i]
    ans += cou // 2
    cou = 1
    f = s2[0]
    a = -1
    for i in s2:
        if i != f:
            break
        a += 1
    f = s2[-1]
    b = -1
    for i in s2[::-1]:
        if i != f:
            break
        b += 1
    ans *= k
    if s2[0] == s2[-1]:
        ans -= (a // 2 + b // 2 - (a + b) // 2) * (k - 1)
    if ans == 0 and s2[0] == s2[-1]:
        ans += k - 1
    #print(a, b, *s2)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# AGC039a
def main():
    import distutils.sysconfig
    # map(int, input().split())

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

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

    ans = 0
    for i in range(1, len(s)):
        if s[i] == s[i - 1]:
            ans += 1
            s[i] = ''
    f = s2[0]
    a = -1
    for i in s2:
        if i != f:
            break
        a += 1
    f = s2[-1]
    b = -1
    for i in s2[::-1]:
        if i != f:
            break
        b += 1
    ans *= k
    if s2[0] == s2[-1]:
        ans -= (a // 2 + b // 2 - (a + b) // 2) * (k - 1)
    if ans == 0 and s2[0] == s2[-1]:
        ans += k-1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# AGC039a
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**6)
    # map(int, input().split())

    s = input()[:-1]
    k = int(input())
    s2 = s
    s = list(s)
    s2 = list(s2)

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

    ans = 0
    for i in range(1, len(s)):
        if s[i] == s[i - 1]:
            ans += 1
            s[i] = ''
    f = s2[0]
    a = -1
    for i in s2:
        if i != f:
            break
        a += 1
    f = s2[-1]
    b = -1
    for i in s2[::-1]:
        if i != f:
            break
        b += 1
    ans *= k
    if s2[0] == s2[-1]:
        ans -= (a//2+b//2-(a+b)//2)*(k-1)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# AGC039a
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**6)
    # map(int, input().split())

    s = list(input()[:-1])
    k = int(input())

    ans = 0
    alphabet = list('abcdefghijklmnopqrstuvwxyz')

    for i in range(1, len(s)):
        if s[i-1] == s[i]:
            ans += 1
            s[i] = alphabet[(alphabet.index(s[i]) + 1) % len(alphabet)]
            if s[i] == s[(i + 1) % len(s)]:
                s[i] = alphabet[(alphabet.index(s[i]) + 1) % len(alphabet)]
    if s[0] == s[-1]:
        ans += 1
    print(ans*k)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

# abbbbbbcccd -> abxbxbxcxcd 2文字続いたら1count
# K=3 ddaad -> ddaadddaadddaad とかになったら、
# dddの真ん中、すなわち頭を変換したほうが効率が良い…

def counting(S):
  cnt = 0
  i = 1
  while i < len(S):
    if S[i-1] == S[i]:
      cnt += 1
      i += 2
    else:
      i += 1
  return cnt

#for i in range(1,K+1):
#  print(counting(S*i))
if K <= 100:
  print(counting(S*K))
  exit(0)
if K % 2 == 1:
  A3 = counting(S*3)
  A1 = counting(S)
  print( (A3-A1) * (K//2) + A1 )
else:
  A2 = counting(S*2)
  print( A2 * (K//2) )

Submission status: Wrong Answer

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

# abbbbbbcccd -> abxbxbxcxcd 2文字続いたら1count
# K=3 ddaad -> ddaadddaadddaad とかになったら、
# dddの真ん中、すなわち頭を変換したほうが効率が良い…

def counting(S):
  cnt = 0
  i = 1
  while i < len(S):
    if S[i-1] == S[i]:
      cnt += 1
      i += 2
    else:
      i += 1
  return cnt

#for i in range(1,K+1):
#  print(counting(S*i))
if K % 2 == 1:
  A3 = counting(S*3)
  A1 = counting(S)
  print( (A3-A1) * (K//2) + A1 )
else:
  A2 = counting(S*2)
  print( A2 * (K//2) )

Submission status: Wrong Answer

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

# abbbbbbcccd -> abxbxbxcxcd 2文字続いたら1count
# K=3 ddaad -> ddaadddaadddaad とかになったら、
# dddの真ん中、すなわち頭を変換したほうが効率が良い…

def counting(S):
  cnt = 0
  i = 1
  while i < len(S):
    if S[i-1] == S[i]:
      cnt += 1
      i += 2
    else:
      i += 1
  return cnt

#for i in range(1,K+1):
#  print(counting(S*i))
print(counting(S)*K)

Submission status: Wrong Answer

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

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

count_list.append(count)

if len(count_list) == 1:
    print((len(s) * repeat_num ) // 2)
else:
    if s[0] != s[-1]:
        print(count_list)
        single = sum(list(map(lambda x: x//2, count_list)))
        print(single * repeat_num)
    else:
        # s[0] == s[-1] 繰り返しによってつながる
        single = sum(list(map(lambda x: x//2, count_list[1:-1])))
        print(single * repeat_num
            + (count_list[0]) // 2
            + (count_list[-1]) // 2
            + (count_list[0] + count_list[-1]) // 2 * (repeat_num - 1))

Submission status: Wrong Answer

Solution code:
def RanLengthEncoding(S):
    N = len(S)
    RLE = []
    now_char = S[0]
    now_cnt = 1
    for i in range(1, N):
        if now_char == S[i]:
            now_cnt += 1
        else:
            RLE.append([now_char, now_cnt])
            now_char = S[i]
            now_cnt = 1
    RLE.append([now_char, now_cnt])
    return RLE

S = input()
K = int(input())
rle = RanLengthEncoding(S)
if len(rle) == 1:
    ans = rle[0][-1] // 2
    ans *= K
    print(ans)
    exit()
cnt = 0
for i in range(1, len(rle)-1):
    cnt += rle[i][1] // 2

if rle[0][0] != rle[-1][0]:
    cnt += rle[0][1] // 2
    cnt += rle[-1][1] // 2
    ans = cnt * K
else:
    tmp = (rle[0][1] + rle[-1][1]) // 2
    ans = cnt * K
    ans += tmp * (K-1)
    ans += rle[0][1] // 2
    ans += rle[-1][1] // 2
print(ans)

Submission status: Wrong Answer

Solution code:
def RanLengthEncoding(S):
    N = len(S)
    RLE = []
    now_char = S[0]
    now_cnt = 1
    for i in range(1, N):
        if now_char == S[i]:
            now_cnt += 1
        else:
            RLE.append([now_char, now_cnt])
            now_char = S[i]
            now_cnt = 1
    RLE.append([now_char, now_cnt])
    return RLE

S = input()
K = int(input())
rle = RanLengthEncoding(S)
if len(rle) == 1:
    ans = rle[0][-1] // 2
    ans *= K
    print(ans)
    exit()
cnt = 0
for i in range(len(rle)-1):
    cnt += rle[i][1] // 2

if rle[0][0] != rle[-1][0]:
    cnt += rle[-1][1] // 2
    ans = cnt * K


else:
    tmp = (rle[0][1] + rle[-1][1]) // 2
    ans = cnt * K
    ans += tmp * (K-1)
    ans += rle[-1][1] // 2
print(ans)

Submission status: Wrong Answer

Solution code:
def RanLengthEncoding(S):
    N = len(S)
    RLE = []
    now_char = S[0]
    now_cnt = 1
    for i in range(1, N):
        if now_char == S[i]:
            now_cnt += 1
        else:
            RLE.append([now_char, now_cnt])
            now_char = S[i]
            now_cnt = 1
    RLE.append([now_char, now_cnt])
    return RLE

S = input()
K = int(input())
rle = RanLengthEncoding(S)
if len(rle) == 1:
    ans = rle[0][-1] // 2
    ans *= K
    print(ans)
    exit()
cnt = 0
for i in range(len(rle)-1):
    cnt += rle[i][1] // 2

if rle[0][0] != rle[-1][0]:
    cnt += rle[-1][1] // 2
    ans = cnt * K
    print(ans)

else:
    tmp = (rle[0][1] + rle[-1][1]) // 2
    ans = cnt * K
    ans += tmp * (K-1)
    ans += rle[-1][1] // 2
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import copy
s = list(input())
k = int(input())
t = copy.deepcopy(s)
ans = 0
for i in range(1,len(s)-2):
    if s[i] == s[i+1]:
        ans += 1
        s[i+1] = '_'
if s[0] == s[-1]:
    ans += 1
ans *= k
print(ans)

Submission status: Wrong Answer

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

ans = 0
tmax = len(t)
r = 0

for l in range(tmax-1):
    if t[l] == t[l+1]:
        while r < (tmax - 1 ) and t[l] == t[r+1]:
            r += 1
        ans += (r+1-l)//2
        l = r + 1
    else:
        continue

print(abs(ans*k))

Submission status: Wrong Answer

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

count=0
lit=s[0]
memo=0

for m in s[1:]:
  if m==lit:
    if memo==0:
      # need to change
      memo=1
      count+=1
    else:
      # not change
      memo=0
  else:
    # reset
    memo=0
    lit=m

if s[0]==s[-1] and memo==0:
  count+=1
  
print(count*n)

Submission status: Wrong Answer

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

count=0
lit=s[0]
memo=0

for m in s[1:]:
  if m==lit:
    if memo==0:
      memo=1
      count+=1
    else:
      memo=0
  else:
    memo=0
  lit=m

if s[0]==s[-1] and memo==0:
  count+=1
  
print(count*n)

Submission status: Wrong Answer

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

count=0
lit=s[0]
memo=0

for m in s[1:]:
  if m==lit:
    if memo==0:
      memo=1
      count+=1
    else:
      memo=0
  lit=m
  
if s[0]==s[-1] and memo==0:
  count+=1
  
print(count*n)

Submission status: Wrong Answer

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

count=0
lit=s[0]
memo=0

for m in s[1:]:
  if m==lit:
    if memo==0:
      memo=1
      count+=1
    else:
      memo=0
  lit=m
  
if s[0]==s[-1]:
  count+=1
  
print(count*n)

Submission status: Wrong Answer

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

count=0
lit=s[0]
memo=0

for m in s[1:]:
  if m==lit and memo==0:
    memo=1
    count+=1
  elif m==lit and memo==1:
    memo=0
  lit=m
  
if s[0]==s[-1]:
  count+=1
  
print(count*n)

Submission status: Wrong Answer

Solution code:
s = input()
N = len(s)
K = int(input())
if s[0] != s[-1]:
    count = 1
    ans = 0
    now = s[0]
    for i in range(1,len(s)):
        if now == s[i]:
            count += 1
        else:
            now = s[i]
            ans += count//2
            count = 1
    if count != 1:
        ans += count//2
    print(ans*K)
else:
    ans = 0
    fcount = 1
    bcount = 1
    now = s[0]
    start = N
    for i in range(1,N):
        if s[i] == now:
            fcount += 1
        else:
            start = i
            break
    if start == N:
        print(K*((fcount+1)//2))
    else:
        now = s[-1]
        end= 0
        for i in range(N-2,-1,-1):
            if s[i] == now:
                bcount += 1
            else:
                end = i
                break   
        ans = fcount//2 + bcount//2 + ((fcount+bcount)//2)*(K-1)
        now = s[start]
        count = 1
        mcount = 0
        for i in range(start+1,end+1):
            if s[i] == now:
                count += 1
            else:
                mcount += count//2
                count = 1
                now = s[i]
        if count != 1:
            mcount += count//2
        print(ans + mcount*K)

Submission status: Wrong Answer

Solution code:
s = input()
N = len(s)
K = int(input())
if s[0] != s[-1]:
    count = 1
    ans = 0
    now = s[0]
    for i in range(1,len(s)):
        if now == s[i]:
            count += 1
        else:
            now = s[i]
            ans += count//2
            count = 1
    if count != 1:
        ans += count//2
    print(ans*K)
else:
    ans = 0
    fcount = 1
    bcount = 1
    now = s[0]
    start = N-1
    for i in range(1,N):
        if s[i] == now:
            fcount += 1
        else:
            start = i
            break
    if start == N-1:
        print(K*((fcount+1)//2))
    else:
        now = s[-1]
        end= 0
        for i in range(N-2,-1,-1):
            if s[i] == now:
                bcount += 1
            else:
                end = i
                break   
        ans = fcount//2 + bcount//2 + ((fcount+bcount)//2)*(K-1)
        now = s[start]
        count = 1
        mcount = 0
        for i in range(start+1,end+1):
            if s[i] == now:
                count += 1
            else:
                mcount += count//2
                count = 1
                now = s[i]
        if count != 1:
            mcount += count//2
        print(ans + mcount*K)