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:
import sys
import itertools
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def main():

    S = in_s()
    K = in_n()

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

    S += S[0]
    gr = itertools.groupby(S)

    cnt = 0
    flag = False
    for i, (k, v) in enumerate(gr):
        n = len(list(v))
        if i == 0 and n >= 2 and n % 2 == 0:
            flag = True
        cnt += n // 2
    else:
        if i != 0 and flag and n >= 2 and n % 2 == 0:
            cnt -= 1

    print(cnt * K)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import itertools
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def main():

    S = in_s()
    K = in_n()

    S += S[0]
    gr = itertools.groupby(S)

    cnt = 0
    flag = False
    for i, (k, v) in enumerate(gr):
        n = len(list(v))
        if i == 0 and n >= 2 and n % 2 == 0:
            flag = True
        cnt += n // 2
    else:
        if i != 0 and flag and n >= 2 and n % 2 == 0:
            cnt -= 1
        if i == 0:
            if n <= 2:
                cnt = 1
            else:
                cnt = (n - 2) // 2 + 1

    print(cnt * K)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import itertools
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def main():

    S = in_s()
    K = in_n()

    S += S[0]
    gr = itertools.groupby(S)

    cnt = 0
    flag = False
    for i, (k, v) in enumerate(gr):
        n = len(list(v))
        if i == 0 and n >= 2 and n % 2 == 0:
            flag = True
        cnt += n // 2
    else:
        if i != 0 and flag and n >= 2 and n % 2 == 0:
            cnt -= 1
            pass

    print(cnt * K)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import itertools
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def main():

    S = in_s()
    K = in_n()

    S += S[0]
    gr = itertools.groupby(S)

    cnt = 0
    flag = False
    for i, (k, v) in enumerate(gr):
        n = len(list(v))
        cnt += n // 2
        if i == 0 and n >= 2 and n % 2 == 0:
            flag = True
    else:
        if flag and n >= 2 and n % 2 == 0:
            cnt -= 1
            pass

    print(cnt * K)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import itertools
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def main():

    S = in_s()
    K = in_n()

    S += S[0]
    gr = itertools.groupby(S)

    cnt = 0
    flag = False
    for i, (k, v) in enumerate(gr):
        n = len(list(v))
        cnt += n // 2
        if i == 0 and n >= 2:
            flag = True
    else:
        if i != 0 and flag:
            cnt -= 1

    print(cnt * K)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

# from numba import njit

# input = stdin.readline

def countProcedure(string):
  res = 0
  strCount = 1
  prev = "" # 1つまえの文字
  for i in range(len(string)):
    if string[i] == prev:
      strCount += 1
    else:
      res += strCount // 2
      strCount = 1
    prev = string[i]

  if strCount != 1:
    res += strCount // 2
  return res

def solve(s,k):
  if s[0] != s[-1]:
    return countProcedure(s)*k
  if all(x == s[0] for x in s):
    return countProcedure(s)*k
  else:
    leftLength = max(i for i in range(1,len(s)) if s[:i]== s[0]*i)
    rightLength = max(i for i in range(1,len(s)) if s[::-1][:i] == s[-1]*i)
    left = s[0]*leftLength
    center = s[leftLength:-rightLength]
    right = s[-1]*rightLength
    ans = countProcedure(left) + countProcedure(center)*k + countProcedure(right) + countProcedure("".join([right,left]))*(k-1)
    return ans


def main():
  S = input()
  K = int(input())
  # N,M = map(int,input().split())
  # a = list(map(int,input().split()))
  print(solve(S,K))
  
  
if __name__ == '__main__':
  main()

Submission status: Wrong Answer

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

# from numba import njit

# input = stdin.readline

def countProcedure(string):
  res = 0
  strCount = 1
  prev = "" # 1つまえの文字
  for i in range(len(string)):
    if string[i] == prev:
      strCount += 1
    else:
      res += strCount // 2
      strCount = 1
    prev = string[i]

  if strCount != 1:
    res += strCount // 2
  return res

def solve(s,k):
  if s[0] != s[-1]:
    return countProcedure(s)*k
  if all(x == s[0] for x in s):
    return countProcedure(s)*k
  else:
    leftLength = max(i for i in range(1,len(s)) if s[:i]== s[0]*i)
    rightLength = max(i for i in range(1,len(s)) if s[::-1][:i] == s[-1]*i)
    left = s[0]*leftLength
    center = s[leftLength:-rightLength]
    right = s[-1]*rightLength
    ans = countProcedure(left) + countProcedure(center)*k + countProcedure(right) + countProcedure("".join([right,left]))*(k-1)
    return ans


def main():
  S = input()
  K = int(input())
  # N,M = map(int,input().split())
  # a = list(map(int,input().split()))
  
  
if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
import sys
s = input()
k = int(input())
n = len(s)
all_same = True
for i in range(n-1):
    if s[i] != s[i+1]:
        all_same = False
if all_same:
    print((n*k)//2)
    sys.exit()
head_same = 1
for i in range(n-1):
    if s[i] == s[i+1]:
        head_same += 1
    else:
        break
tail_same = 1
for i in range(n-1,0,-1):
    if s[i] == s[i-1]:
        tail_same += 1
    else:
        break
head_tail_same = 0
if s[0] == s[-1]:
    head_tail_same = head_same + tail_same
def return_internal_same(ls):
    i = 0
    same_count = 1
    return_value = 0
    while i < len(ls)-1:
        if ls[i] != ls[i+1]:
            i += 1
            return_value += same_count // 2
            same_count = 1
            continue
        else:
            same_count += 1
            i += 1
    return_value += same_count // 2
    return return_value
ans = head_same//2 + tail_same//2
ans += (k-1) * (head_tail_same//2)
ans += k*(return_internal_same(s[head_same:n-tail_same]))
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
s = input()
k = int(input())
n = len(s)
all_same = True
for i in range(n-1):
    if s[i] != s[i+1]:
        all_same = False
if all_same:
    print(n*k//2)
    sys.exit()
head_same = 1
for i in range(n-1):
    if s[i] == s[i+1]:
        head_same += 1
    else:
        break
tail_same = 1
for i in range(n-1,0,-1):
    if s[i] == s[i-1]:
        tail_same += 1
    else:
        break
head_tail_same = 0
if s[0] == s[-1]:
    head_tail_same = head_same + tail_same
internal_same = []
def return_internal_same(ls):
    i = 0
    same_count = 1
    return_value = 0
    while i < len(ls)-1:
        if ls[i] != ls[i+1]:
            i += 1
            if same_count > 0:
                return_value += same_count // 2
                same_count = 1
            continue
        else:
            same_count += 1
            i += 1
    return_value += same_count // 2
    return return_value
ans = head_same//2 + tail_same//2
ans += (k-1) * (head_tail_same//2)
ans += k*(return_internal_same(s[head_same:n-tail_same]))
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
s = input()
n = len(s)
k = int(input())
if s.count(s[0]) == n:
    print(n * k // 2)
    exit()
# pre,post を数えた上で前から連続しているやつの長さを数えて和をとる
ans = 0  # k倍する前をおく
cnt = 1
pre, post = 0, 0
pre_bit = 1
for i in range(n - 1):
    if s[i] == s[i + 1]:
        cnt += 1
    else:
        ans += cnt // 2
        if pre_bit:
            pre = cnt
        cnt = 1

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

Submission status: Wrong Answer

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

c1 = 0
now = 0
for i in range(1,n):
    if s[i] == s[i-1]:
        now += 1
    else:
        c1 += (now+1)//2
        now = 0
c1 += now//2
if now == n-1:
    print(n*k//2)
    exit()
if s[0] != s[-1]:
    print(c1*k)
    exit()
s.append("1")
c2 = 0
now = 0
for i in range(1,2*n+1):
    if s[i] == s[i-1]:
        now += 1
    else:
        c2 += (now+1)//2
        now = 0

print(c1+(k-1)*(c2-c1))

Submission status: Wrong Answer

Solution code:
import copy
s = list(input())
n = len(s)
s2 = copy.deepcopy(s)
la = s[-1]
for i in range(n):
    if s[i] == la:
        s2.append(la)
    else:
        break

n2 = len(s2)
k = int(input())
if n2 == 2*n:
    print(n*k//2)
    exit()
count = 0
for i in range(n2-n+1,n2):
    if s2[i] == s2[i-1]:
        s2[i] = "1"
        count += 1
ans = k*count

c = 0

for i in range(n-1,-1,-1):
    if la == s[i]:
        c += 1
    else:
        break

#if n2 > n and (n2-n)%2 == 0 and c > 1 and c%2:
 #   ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
import copy
s = list(input())
n = len(s)
s2 = copy.deepcopy(s)
la = s[-1]
for i in range(n):
    if s[i] == la:
        s2.append(la)
    else:
        break

n2 = len(s2)
k = int(input())
if n2 == 2*n:
    print(n*k//2)
    exit()
count = 0
for i in range(n2-n+1,n2):
    if s2[i] == s2[i-1]:
        s2[i] = "1"
        count += 1
ans = k*count

c = 0

for i in range(n-1,-1,-1):
    if la == s[i]:
        c += 1
    else:
        break

if n2 > n and (n2-n)%2 == 0 and c > 1 and c%2:
    ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
import copy
s = list(input())
n = len(s)
s2 = copy.deepcopy(s)
la = s[-1]
for i in range(n):
    if s[i] == la:
        s2.append(la)
    else:
        break

n2 = len(s2)
k = int(input())
if n2 == 2*n:
    print(n*k//2)
    exit()
count = 0
for i in range(n2-n+1,n2):
    if s2[i] == s2[i-1]:
        s2[i] = "1"
        count += 1
ans = k*count

c = 0

for i in range(n-1,-1,-1):
    if la == s[i]:
        c += 1
    else:
        break

if n2 > n and c > 1 and c%2:
    ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
import copy
s = list(input())
n = len(s)
s2 = copy.deepcopy(s)
la = s[-1]
for i in range(n):
    if s[i] == la:
        s2.append(la)
    else:
        break

n2 = len(s2)
k = int(input())
if n2 == 2*n:
    print(n*k//2)
    exit()
count = 0
for i in range(n2-n+1,n2):
    if s2[i] == s2[i-1]:
        s2[i] = "1"
        count += 1
ans = k*count

c = 0

for i in range(n-1,-1,-1):
    if la == s[i]:
        c += 1

if n2 > n and c > 1 and c%2:
    ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
import copy
s = list(input())
n = len(s)
s2 = copy.deepcopy(s)
la = s[-1]
for i in range(n):
    if s[i] == la:
        s2.append(la)
    else:
        break

n2 = len(s2)
k = int(input())
if n2 == 2*n:
    print(n*k//2)
    exit()
count = 0
for i in range(n2-n+1,n2):
    if s2[i] == s2[i-1]:
        s2[i] = "1"
        count += 1
ans = k*count

c = 1

for i in range(n-2,-1,-1):
    if s[i] == s[i+1]:
        c += 1
    else:
        break

if n2 > n and (n2-n+c)%2 and c > 1 and c%2:
    ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
import copy
s = list(input())
n = len(s)
s2 = copy.deepcopy(s)
la = s[-1]
for i in range(n):
    if s[i] == la:
        s2.append(la)
    else:
        break

n2 = len(s2)
k = int(input())
if n2 == 2*n:
    print(n*k//2)
    exit()
count = 0
for i in range(n2-n+1,n2):
    if s2[i] == s2[i-1]:
        s2[i] = "1"
        count += 1
ans = k*count

c = 1

for i in range(n-2,-1,-1):
    if s[i] == s[i+1]:
        c += 1
    else:
        break

if n2 > n and c > 1 and c%2:
    ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
import copy
s = list(input())
n = len(s)
s2 = copy.deepcopy(s)
la = s[-1]
for i in range(n):
    if s[i] == la:
        s2.append(la)
    else:
        break

n2 = len(s2)
k = int(input())
if n2 == 2*n:
    print(n*k//2)
    exit()
count = 0
for i in range(n2-n+1,n2):
    if s2[i] == s2[i-1]:
        s2[i] = "1"
        count += 1
ans = k*count

c = 1

for i in range(n-2,-1,-1):
    if s[i] == s[i+1]:
        c += 1
    else:
        break

if c > 1 and c%2:
    ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
import copy
s = list(input())
n = len(s)
s2 = copy.deepcopy(s)
la = s[-1]
for i in range(n):
    if s[i] == la:
        s2.append(la)
    else:
        break

n2 = len(s2)
k = int(input())
if n2 == 2*n:
    print(n*k//2)
    exit()
count = 0
for i in range(n2-n+1,n2):
    if s2[i] == s2[i-1]:
        s2[i] = "1"
        count += 1
ans = k*count
c = 1
for i in range(n-2,-1,-1):
    if s[i] == s[i+1]:
        c += 1

if c > 1 and c%2:
    ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
n = len(s)
count = 0
c = 1
now = 1
while now < n:
    if s[now] == s[now-1]:
        c += 1
    else:
        break
    now += 1
if now == n:
    print(n*k//2)
    exit()
count += c//2
now2 = n-2
c2 = 1
while now2 >= 0:
    if s[now2] == s[now2+1]:
        c2 += 1
    else:
        break
    now2 -= 1
count += c2//2
if s[0] == s[-1]:
    count += ((c+c2)//2)*(k-1)
c3 = 0

for i in range(now,now2):
    if s[i] == s[i-1]:
        c3 += 1
        s[i] = "1"
count += c3*k

print(count)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
n = len(s)
count = 0
c = 1
now = 1
while now < n:
    if s[now] == s[now-1]:
        c += 1
    else:
        break
    now += 1
if now == n:
    print(n*k//2)
    exit()
count += c//2
now2 = n-2
c2 = 1
while now2 >= 0:
    if s[now2] == s[now2+1]:
        c2 += 1
    else:
        break
    now2 -= 1
count += c2//2
if s[0] == s[-1]:
    count += ((c+c2)//2)*(k-1)
c3 = 0

for i in range(now+1,now2+1):
    if s[i] == s[i-1]:
        c3 += 1
        s[i] = "1"
count += c3*k

print(count)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
n = len(s)
count = 0
c = 1
now = 1
while now < n:
    if s[now] == s[now-1]:
        c += 1
    else:
        break
    now += 1
if now == n:
    print(n*k//2)
    exit()
count += c//2
now2 = n-2
c2 = 1
while now2 >= 0:
    if s[now2] == s[now2+1]:
        c2 += 1
    else:
        break
    now2 -= 1
count += c2//2
if s[0] == s[-1]:
    count += (c+c2)//2*(k-1)
c3 = 0

for i in range(now,now2+1):
    if s[i] == s[i-1]:
        c3 += 1
        s[i] = "1"
count += c3*k
print(count)

Submission status: Wrong Answer

Solution code:
def solve(S):
    pre = S[0]
    flag = False
    ans = 0
    cnt = 0
    for s in S[1:]:
        if flag:
            if pre == s:
                cnt += 1
            else:
                ans += cnt // 2
                cnt = 0
                flag = False
        else:
            if pre == s:
                cnt += 2
                flag = True
        pre = s
    if flag:
        ans += cnt // 2
    return ans


S = input()
K = int(input())
if len(S) == 1:
    print(K // 2)
else:
    if K == 1:
        print(solve(S))
    elif K == 2:
        print(solve(S + S))
    else:
        ans2 = solve(S + S)
        ans3 = solve(S + S + S)
        print(ans2 + (ans3-ans2)*(K - 2))

Submission status: Wrong Answer

Solution code:
s = list(input())
n = len(s)
s2 = s
la = s[-1]
for i in range(n):
    if s[i] == la:
        s2.append(la)
    else:
        break

n2 = len(s2)
k = int(input())
if n2 == 2*n:
    print(n*k//2)
    exit()
count = 0
for i in range(n2-n+1,n2):
    if s2[i] == s2[i-1]:
        s2[i] = "1"
        count += 1


print(k*count)

Submission status: Wrong Answer

Solution code:
s = list(input())
n = len(s)
s2 = s
la = s[-1]
for i in range(n):
    if s[i] == la:
        s2.append(la)
    else:
        break

n2 = len(s2)
count = 0
for i in range(n2-n+1,n2):
    if s2[i] == s2[i-1]:
        s2[i] = "1"
        count += 1

k = int(input())
print(k*count)

Submission status: Wrong Answer

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

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


cnt = 0
i = 0
while i < len(S) - 1:
    if S[i] == S[i + 1]:
        cnt += 1
        i += 1
    i += 1

ans = K * cnt
if len(S) >= 3 and S[0] == S[-1] and S[0] != S[1] and S[-1] != S[-2]:
    ans += K - 1
print(ans)

Submission status: Wrong Answer

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


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


cnt = 0
i = 0
while i < len(S) - 1:
    if S[i] == S[i + 1]:
        cnt += 1
        i += 1
    i += 1

ans = K * cnt
if len(S) >= 3 and S[0] == S[-1] and S[0] != S[1] and S[-1] != S[-2]:
    ans += K - 1
print(ans)

Submission status: Wrong Answer

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


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


cnt = 0
i = 0
while i < len(S) - 1:
    if S[i] == S[i + 1]:
        cnt += 1
        i += 1
    i += 1

length = -1
if S[0] == S[-1] and len(S) >= 2:
    length = 0
    x = S[0]
    for s in S:
        if s == x:
            length += 1
        else:
            break
    for s in S[::-1]:
        if s == x:
            length += 1
        else:
            break

print(K * cnt + (K - 1) * (length == 2))

Submission status: Wrong Answer

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

ans = 0
d = 0
for i in range(len(S)-1):
    if S[i] == S[i+1]:
        d += 1
        continue
    if d > 0:
        ans += (d+1) // 2
        d = 0

if d > 0:
    ans += (d+1) // 2

print(ans*K)

Submission status: Wrong Answer

Solution code:
S = list(input())
N = len(S)
K = int(input())
if len(set(S)) == 1:
    print((N*K)//2)
    exit()

cnt = 0
tmpS = S[:]
for i in range(N-1):
    if tmpS[i] == tmpS[i+1]:
        cnt += 1
        tmpS[i+1] = '!'

if S[N-1] == S[0]:
    a = 1
    while a <= N-1 and S[a-1] == S[a]:
        a += 1
    b = 1
    while b <= N-1 and S[N-b] == S[N-b-1]:
        b += 1
    print(cnt*K+(a//2+b//2-(a+b)//2)*(K-1))
else:
    print(cnt*K)

Submission status: Wrong Answer

Solution code:
S = list(input())
N = len(S)
K = int(input())
if len(set(S)) == 1:
    print(N*K//2)
    exit()

cnt = 0
tmpS = S[:]
for i in range(N-1):
    if tmpS[i] == tmpS[i+1]:
        cnt += 1
        tmpS[i+1] = '!'

if S[N-1] == S[0]:
    a = 1
    while a <= N-1 and S[a-1] == S[a]:
        a += 1
    b = 1
    while b <= N-1 and S[N-b] == S[N-b-1]:
        b += 1
    print(cnt*K+(a//2+b//2-(a+b)//2)*(K-1))
else:
    print(cnt*K)

Submission status: Wrong Answer

Solution code:
S = list(input())
N = len(S)
K = int(input())
if len(set(S)) == 1:
    print(N*K//2)
    exit()

cnt = 0
tmpS = S*1
for i in range(N-1):
    if tmpS[i] == tmpS[i+1]:
        cnt += 1
        tmpS[i+1] = '!'

if S[N-1] == S[0]:
    a = 1
    while a <= N-1 and S[a-1] == S[a]:
        a += 1
    b = 1
    while b <= N-1 and S[N-b] == S[N-b-1]:
        b += 1
    print(cnt*K+(a//2+b//2-(a+b)//2)*(K-1))
else:
    print(cnt*K)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

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

if len(S)==1:
    print(K//2)
    exit()
s = S[0]
for s2 in S[1:]:
    if not s==s2:
        break
    s = s2
else:
    print((len(S)*K-1)//2)
    exit()
ans = 0
S2 = S[:]
if S[0]==S[-1]:
    S2[-1]='#'
    ans += K-1
leng = len(S)
last = '.'
cnt = 0
for i in range(leng):
    s = S[i]
    if last==s:
        cnt += 1
        last = '.'
    else:
        last = s
last = '.'
cnt2 = 0
for i in range(leng):
    s = S2[i]
    if last==s:
        cnt2 += 1
        last = '.'
    else:
        last = s
ans += cnt+cnt2*(K-1)
print(ans)

Submission status: Wrong Answer

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

if len(set(S)) == 1:
    print((K * len(S)) // 2)
    exit()
SS = S + S
cnt_SS = 0
for i in range(len(SS) - 1):
    if SS[i] == SS[i + 1]:
        SS[i + 1] = 'X'
        cnt_SS += 1

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

if S_origin[0] == S_origin[-1]:
    cnt_S += 1

if K % 2 == 0:
    print(K // 2 * cnt_SS)
else:
    print(K // 2 * cnt_SS + cnt_S)

Submission status: Wrong Answer

Solution code:
S = list(input())
N = len(S)
K = int(input())
if len(set(S)) == 1:
    print(N*K//2)
    exit()

cnt = 0
tmpS = S*1
for i in range(N-1):
    if tmpS[i] == tmpS[i+1]:
        cnt += 1
        tmpS[i+1] = '!'

if S[N-1] == S[0]:
    a = 1
    while S[a-1] == S[a] and a <= N-1:
        a += 1
    b = 1
    while S[N-b] == S[N-b-1] and b <= N-1:
        b += 1
    print(cnt*K+a//2+b//2 - (a+b)//2 * (K-1))
else:
    print(cnt*K)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

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

if len(S)==1:
    print(K//2)
    exit()
s = S[0]
for s2 in S[1:]:
    if not s==s2:
        break
    s = s2
else:
    print(len(S)*K//2)
    exit()
ans = 0
S2 = S[:]
if S[0]==S[-1]:
    S2[-1]='#'
    ans += K-1
leng = len(S)
last = '.'
cnt = 0
for i in range(leng):
    s = S[i]
    if last==s:
        cnt += 1
        last = '.'
    else:
        last = s
last = '.'
cnt2 = 0
for i in range(leng):
    s = S2[i]
    if last==s:
        cnt2 += 1
        last = '.'
    else:
        last = s
ans += cnt+cnt2*(K-1)
print(ans)

Submission status: Wrong Answer

Solution code:
S = list(input())
N = len(S)
K = int(input())
if N == 1:
    print(K//2)
    exit()

cnt = 0
tmpS = S*1
for i in range(N-1):
    if tmpS[i] == tmpS[i+1]:
        cnt += 1
        tmpS[i+1] = '!'

if S[N-1] == S[0]:
    a = 1
    while a <= N-1 and S[a-1] == S[a]:
        a += 1
    b = 1
    while b <= N-1 and S[N-b] == S[N-b-1]:
        b += 1
    print(cnt*K+(a//2+b//2-(a+b)//2)*(K-1))
else:
    print(cnt*K)

Submission status: Wrong Answer

Solution code:
S = list(input())
N = len(S)
K = int(input())
if N == 1:
    print(K//2)
    exit()

cnt = 0
tmpS = S
for i in range(N-1):
    if tmpS[i] == tmpS[i+1]:
        cnt += 1
        tmpS[i+1] = '!'

if tmpS[N-1] == tmpS[0]:
    a = 1
    while S[a-1] == S[a] and a <= N-1:
        a += 1
    b = 1
    while S[N-b] == S[N-b-1] and b <= N-1:
        b += 1
    print(cnt*K+a//2+b//2 - (a+b)//2 * (K-1))
else:
    print(cnt*K)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

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

if len(S)==1:
    print(K//2)
    exit()
ans = 0
S2 = S[:]
if S[0]==S[-1]:
    S2[-1]='#'
    ans += K-1

leng = len(S)
last = '.'
cnt = 0
for i in range(leng):
    s = S2[i]
    if last==s:
        cnt += 1
        last = '.'
    else:
        last = s
last = '.'
cnt2 = 0
for i in range(leng):
    s = S[i]
    if last==s:
        cnt2 += 1
        last = '.'
    else:
        last = s

ans += cnt*(K-1)+cnt2
print(ans)

Submission status: Wrong Answer

Solution code:
S = list(input())
flag = False
N = len(S)
K = int(input())
if N == 1:
    print(K//2)
    exit()

cnt = 0
tmpS = S
for i in range(N-1):
    if tmpS[i] == tmpS[i+1]:
        cnt += 1
        tmpS[i+1] = '!'

if tmpS[N-1] == tmpS[0]:
    if S[0] == S[1]:
        print(cnt*K)
    else:
        print(cnt*K+(K-1))
else:
    print(cnt*K)

Submission status: Wrong Answer

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

ans = 0

sw = 0
for i in range(26):
    if s.count(chr(ord("a")+i)) == len(s):
        sw = 1
        break

for i in range(len(s)-1):
    if s[i] == 0:
        pass
    elif s[i] == s[i+1]:
        s[i+1] = 0
        ans += 1
ans2 = 0
if s[0] == s[-1]:
    ans2 = 1
    s[-1] = 0
if len(s) == 1:
    print(k//2)
else:
    if sw == 0:
        print(ans*k + ans2*(k-1))
    else:
        print(len(s)*k//2)

Submission status: Wrong Answer

Solution code:
S = input().strip()
K = int(input())
if len(S)==1:
    print(K//2)
else:
    x = list(S+S)
    cnt = 0
    for i in range(1,len(x)):
        if x[i]==x[i-1]:
            x[i]=0
            cnt += 1
    k = len(x)
    d = 0
    x = x+list(S)
    for i in range(k,len(x)):
        if x[i]==x[i-1]:
            x[i]=0
            d += 1
    if K%2==0:
        print(K//2*cnt)
    else:
        print(K//2*cnt+d)

Submission status: Wrong Answer

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

# 1回
T = S*1
for i in range(N-1):
    if T[i] == T[i+1]:
        cnt += 1
        T[i+1] = '!'

# 2回
U = S*2
cnt2 = 0
for i in range(2*N-1):
    if U[i] == U[i+1]:
        cnt2 += 1
        U[i+1] = '!'

diff = cnt2-cnt
print(cnt + (K-1)*diff)
#print(T,U,cnt,cnt2)

Submission status: Wrong Answer

Solution code:
S = input().strip()
K = int(input())
if len(S)==1:
    print(K//2)
else:
    x = list(S+S)
    cnt = 0
    for i in range(1,len(x)):
        if x[i]==x[i-1]:
            x[i]=0
            cnt += 1
    k = len(x)
    d = 0
    x = x+list(S)
    for i in range(k,len(x)):
        if x[i]==x[i-1]:
            x[i]=0
            d += 1
    print(cnt+d*(K-2))

Submission status: Wrong Answer

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

ans = 0

for i in range(len(s)-1):
    if s[i] == 0:
        pass
    elif s[i] == s[i+1]:
        s[i+1] = 0
        ans += 1
ans2 = 0
if s[0] == s[-1]:
    ans2 = 1
    s[-1] = 0
if len(s) == 1:
    print(k//2)
else:
    print(ans*k + ans2*(k-1))

Submission status: Wrong Answer

Solution code:
S = input().strip()
K = int(input())
x = list(S+S)
cnt = 0
for i in range(1,len(x)):
    if x[i]==x[i-1]:
        x[i]=0
        cnt += 1
k = len(x)
d = 0
x = x+list(S)
for i in range(k,len(x)):
    if x[i]==x[i-1]:
        x[i]=0
        d += 1
print(cnt+d*(K-2))

Submission status: Wrong Answer

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

for i in range(N-1):
    if S[i] == S[i+1]:
        cnt += 1
        S[i+1] = '!'

if S[N-1] == S[0]:
    print(cnt*K+(K-1))
else:
    print(cnt*K)

Submission status: Wrong Answer

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

SS = S + S
cnt_SS = 0
for i in range(len(SS) - 1):
    if SS[i] == SS[i + 1]:
        SS[i + 1] = 'X'
        cnt_SS += 1

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

if K % 2 == 0:
    print(K // 2 * cnt_SS)
else:
    print(K // 2 * cnt_SS + cnt_S)

Submission status: Wrong Answer

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

ans = 0

for i in range(len(s)-1):
    if s[i] == 0:
        pass
    elif s[i] == s[i+1]:
        s[i+1] = 0
        ans += 1
ans2 = 0
if s[0] == s[-1]:
    ans2 = 1
    s[-1] = 0
print(ans*k + ans2*(k-1))

Submission status: Wrong Answer

Solution code:
s=list(input())

k=int(input())

if list(s).count(s[0])==len(s):
    print(len(s)*k//2)
    exit()

count = 0
for i in range(1,len(s)):
    if s[i-1] == s[i]:
        s[i] = '!'
        count += 1

if s[0] == s[-1]:
    s[-1] = '!'
    count += 1

if s[-1] == '?':
    print(count*k-1)
else:
    print(count*k)

Submission status: Wrong Answer

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

SS = S + S
cnt_SS = 0
for i in range(len(SS) - 1):
    if SS[i] == SS[i + 1]:
        SS[i + 1] = 'X'
        cnt_SS += 1

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

if K % 2 == 0:
    print(K // 2 * cnt_SS)
else:
    print(K // 2 * cnt_SS + cnt_S)

Submission status: Wrong Answer

Solution code:
s=list(input())

k=int(input())

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

count = 0
for i in range(1,len(s)):
    if s[i-1] == s[i]:
        s[i] = '!'
        count += 1

if s[0] == s[-1]:
    s[-1] = '!'
    count += 1

if s[-1] == '?':
    print(count*k-1)
else:
    print(count*k)

Submission status: Wrong Answer

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

ans = 0
if s[0] == s[-1]:
    ans += 1
    s[-1] = 0
for i in range(len(s)-1):
    if s[i] == 0:
        pass
    elif s[i] == s[i+1]:
        s[i+1] = 0
        ans += 1
print(ans*k)

Submission status: Wrong Answer

Solution code:
S=[c for c in input()]
K=int(input())

ans=0

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

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

Submission status: Wrong Answer

Solution code:
s=input()
ss=s*2
k=int(input())
a=[False for i in ss]
for i in range(len(ss)-1):
    if ss[i]==ss[i+1] and a[i]==False:
        a[i+1]=True
print(min(sum(a)*(k//2)+(k%2)*sum(a[:len(s)]),sum(a)*(k//2)+(k%2)*sum(a[:len(s)])))

Submission status: Wrong Answer

Solution code:
s=input()
ss=s*2
k=int(input())
a=[False for i in ss]
for i in range(len(ss)-1):
    if ss[i]==ss[i+1] and a[i]==False:
        a[i+1]=True
print(sum(a[len(s):])*(k-1)+sum(a[:len(s)]))