ZhenlongDai/ACPR huggingface dataset:
Problem: p02913
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
from collections import *
def z(s):
    n = len(s)
    a = [0]*n
    a[0] = n
    i, j = 1, 0
    while i < n:
        while i+j<n and s[j]==s[i+j]:
            j += 1
        a[i] = j
        if j == 0: 
            i += 1
            continue
        k = 1
        while i+k<n and k+a[k]<j:
            a[i+k] = a[k]
            k += 1
        i += k
        j -= k
    return a

n = int(input())
s = input()
r = 0
for i in range(1, n):
    a = z(s[i:] + s)
    c = Counter(a)
    for k in c:
        if c[k] >= 2 and r < k:
            r = k
print(r)

Failing test case input:
5
bc`ba

Expected output:
1


Actual output:
0


Fixed submission (verdict: Accepted):
def z(s):
    n = len(s)
    a = [0]*n
    a[0] = n
    i, j = 1, 0
    while i < n:
        while i+j<n and s[j]==s[i+j]:
            j += 1
        a[i] = j
        if j == 0:
            i += 1
            continue
        k = 1
        while i+k<n and k+a[k]<j:
            a[i+k] = a[k]
            k += 1
        i += k
        j -= k
    return a
n = int(input())
s = input()
r = 0
for i in range(n):
    a = z(s[i:])
    for j in range(len(a)):
        if a[j] <= j:
            r = max(r, a[j])
print(r)

Problem: p02913
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
N = int(input())
S = input()
dp = [[0]*(N+1) for _ in range(N+1)]
res = 0
for i in range(N-1,-1,-1):
  for j in range(N-1,i,-1):
    if S[i]==S[j]:
      dp[i][j]=max(dp[i+1][j+1]+1,dp[i][j])
    res = max(res,min(j-1,dp[i][j]))
print(res)

Failing test case input:
5
abab`

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
N = int(input())
S = input()
dp = [[0]*(N+1) for _ in range(N+1)]
res = 0
for i in range(N-1,-1,-1):
  for j in range(N-1,i,-1):
    if S[i]==S[j]:
      dp[i][j]=max(dp[i+1][j+1]+1,dp[i][j])
    res = max(res,min(j-i,dp[i][j]))
print(res)
Problem: p02913
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):

n = int(input())
ab = str(input())

ans = set([0])
lenab = len(ab)
for h in range(lenab):
    abc = ab[h:]
    lenabc = lenab - h
    A = [0] * lenabc
    i = 1; j = 0
    midlen = lenabc
    while i < lenabc:
        while i+j < lenabc and abc[j] == abc[i+j] and j < midlen:
            j += 1
        if not j:
            i += 1
            continue
        A[i] = j
        ans.add(j)
        k= 1
        while lenabc-i > k < j - A[k]:
            A[i+k] = A[k]
            k += 1
        i += k; j -= k

print(max(ans))


Failing test case input:
5
ababa

Expected output:
2

Actual output:
3


Fixed submission (verdict: Accepted):

n = int(input())
ab = str(input())

ans = set([0])
lenab = len(ab)

for h in range(lenab):
    abc = ab[h:]
    lenabc = len(abc)
    i = 1; j = 0
    A = [0]*lenabc
    A[0] = lenabc
    while i < lenabc:
        while i+j < lenabc and abc[j] == abc[i+j] and j < i:
            j += 1
        if j == 0:
            i += 1
        else:
            A[i] = j
            ans.add(j)
            k = 1
            while lenabc - i > k < j - A[k] and j < i - k :
                A[i + k] = A[k]
                k += 1
            i += k;
            j -= k

print(max(ans))

Problem: p02913
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
S = input()
dp = [0]*N
j = 1

for i in range(N):
    while S[i:j] in S[j:N]:
        j += 1
    dp[i] = j - i
print(max(dp))


Failing test case input:
2
y

Expected output:
0


Actual output:
1


Fixed submission (verdict: Accepted):
n = int(input())
s = input()
dp = [0]*n
j = 1

for i in range(n):
    while s[i:j] in s[j:n]:
        j += 1
    dp[i] = j-i-1
print(max(dp))

Problem: p02913
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
def main():
  n=int(input())
  s=input()
  dp=[[0]*(n+1) for i in range(n+1)]
  for i in range(n-1,-1,-1):
    for j in range(n-1,-1,-1):
      if s[i]==s[j]:
        dp[i][j]=dp[i+1][j+1]+1
  
  
  ans=0
  for i in range(n):
    for j in range(n):
      if i<j:
        t=min(j-1,dp[i][j])
        ans=max(ans,t)
  print(ans)      
        

if __name__=='__main__':
  main()

Failing test case input:
5
abab`

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
def main():
  n=int(input())
  s=input()
  dp=[[0]*(n+1) for i in range(n+1)]
  for i in range(n-1,-1,-1):
    for j in range(n-1,i,-1):
      if s[i]==s[j]:
        dp[i][j]=dp[i+1][j+1]+1
  
  
  
  ans=0
  for i in range(n-1):
    for j in range(i+1,n):
      ans=max(ans,min(j-i,dp[i][j]))
  print(ans)      
        

if __name__=='__main__':
  main()
Problem: p02913
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
n = int(input())
s = input()
dp = [[0]*(n) for _ in range(n)]

for i in range(n-1, -1, -1):
    for j in range(i, -1, -1):
        if i == (n-1):
            if s[i] == s[j]:
                dp[i][j] += 1
            continue

        if s[i] == s[j]:
            dp[i][j] = dp[i+1][j+1] + 1        
        else:
            dp[i][j] = dp[i+1][j+1]

ans = 0
for i in range(n-1, -1, -1):
    for j in range(i, -1, -1):
        ans = max(ans, min(abs(j-i), dp[i][j]))
print(ans)

Failing test case input:
13
renaofunparfd

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
n = int(input())
s = input()
dp = [[0]*(n) for _ in range(n)]

for i in range(n-1, -1, -1):
    for j in range(i-1, -1, -1):
        if i == (n-1):
            if s[i] == s[j]:
                dp[i][j] += 1
            continue

        if s[i] == s[j]:
            dp[i][j] = dp[i+1][j+1] + 1        

ans = 0
for i in range(n-1, -1, -1):
    for j in range(i, -1, -1):
        ans = max(ans, min(abs(j-i), dp[i][j]))
print(ans)

Problem: p02913
Buggy submission (Python (3.4.3), verdict: Wrong Answer):

tmp = input()
my_str = input()

start = 0
end = 1
max_len = 0

while (end + max_len + 1 < len(my_str)):
    if my_str[start:end] in my_str[end:]:
        end += 1
        if end - start > max_len:
            max_len = end - start
    else:
        start += 1

print(max_len)


Failing test case input:
13
qfoaodvopaqff

Expected output:
2


Actual output:
3


Fixed submission (verdict: Accepted):

tmp = input()
my_str = input()

start = 0
end = 1
max_len = 0

while (end < len(my_str)):
    if my_str[start:end] in my_str[end:]:
        if end - start > max_len:
            max_len = end - start
        end += 1
    else:
        start += 1

print(max_len)

Problem: p02913
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
N = int(input())
s = input()

dp = [[0 for _ in range(N+1)] for _ in range(N+1)]

for i in range(N-1, 0, -1):
  for j in range(N-1, 0, -1):
    if s[i] != s[j]:
      dp[i][j] = 0
    else:
      dp[i][j] = dp[i+1][j+1] + 1
    
ans = 0
for i in range(N):
  for j in range(N):
    if i >= j:
      continue
    now = min(dp[i][j], j - i)
    ans = max(ans, now)
print(ans)


Failing test case input:
5
_b]_b

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
N = int(input())
s = input()

dp = [[0 for _ in range(N+1)] for _ in range(N+1)]

for i in range(N-1, -1, -1):
  for j in range(N-1, -1, -1):
    if s[i] != s[j]:
      dp[i][j] = 0
    else:
      dp[i][j] = dp[i+1][j+1] + 1
    
ans = 0
for i in range(N):
  for j in range(N):
    if i >= j:
      continue
    now = min(dp[i][j], j - i)
    ans = max(ans, now)
print(ans)
Problem: p02913
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):

n=int(input())
s=input()
def Zalgo(s):
    ans=0
    L=len(s)
    Z=[0]*L
    Z[0]=L
    i=1;j=0
    while i<L:
        while i+j < L and s[j]==s[i+j]:j+=1
        Z[i]=j
        if Z[i] > ans and Z[i]<i:
            ans=Z[i]
        if j == 0:
            i+=1
            continue
        k=1
        while k<j and k+Z[k]<j:
            Z[i+k]=Z[k]
            k+=1
        i+=k
        j-=k
    return ans
ans=0
for i in range(n):
    p=Zalgo(s[i:])
    if p > ans:
        ans=p
print(ans)

Failing test case input:
5
ababa

Expected output:
2

Actual output:
1


Fixed submission (verdict: Accepted):

n=int(input())
s=input()
def Zalgo(s):
    ans=0
    L=len(s)
    Z=[0]*L
    Z[0]=L
    i=1;j=0
    while i<L:
        while i+j < L and s[j]==s[i+j]:j+=1
        Z[i]=j
        if Z[i] > ans and Z[i]<=i:
            ans=Z[i]
        if j == 0:
            i+=1
            continue
        k=1
        while k<j and k+Z[k]<j:
            Z[i+k]=Z[k]
            k+=1
        i+=k
        j-=k
    return ans
ans=0
for i in range(n):
    p=Zalgo(s[i:])
    if p > ans:
        ans=p
print(ans)

Problem: p02913
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
def solve():
    N = int(input())
    S = input()
    next_end = 1
    max_length = 0
    for start in range(N-1):
        for end in range(next_end, N):
            text = S[start:end]
            if text in S[end:]:
                max_length = len(text)
                continue
            else:
                if start + 1 == end:
                    next_end = end + 1
                else:
                    next_end = end
            break
    
    print(max_length)

if __name__ == '__main__':
    solve()

Failing test case input:
13
ffqapovdoaofq

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
def solve():
    N = int(input())
    S = input()
    next_end = 1
    max_length = 0
    for start in range(N-1):
        for end in range(next_end, N):
            text = S[start:end]
            if text in S[end:]:
                max_length = max(len(text), max_length)
                continue
            else:
                if start + 1 == end:
                    next_end = end + 1
                else:
                    next_end = end
            break
    
    print(max_length)

if __name__ == '__main__':
    solve()

Problem: p02913
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
N = int(input())
S = input()
dp = [[0] * (N+2) for _ in range(N+2)]
ans = 0
for i in range(1,N+1):
    for d in range(N-i):
        if S[i] == S[i+d]:
            dp[i][d] = min(d, dp[i-1][d]+1)
            ans = max(ans, dp[i][d])
print(ans)

Failing test case input:
5
_b]_b

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
N = int(input())
S = input()
dp = [[0] * (N+2) for _ in range(N+2)]
ans = 0
for i in range(1,N):
    for d in range(1,N+1-i):
        if S[i-1] == S[i-1+d]:
            dp[i][d] = min(d, dp[i-1][d]+1)
            ans = max(ans, dp[i][d])
print(ans)
Problem: p02913
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
print("speed test")

N=int(input())
S=input()
 
dp=[[0 for i in range(N + 1)] for j in range(N + 1)]
 
for i in range(N):
  for j in range(i + 1, N):
    if S[i] == S[j]:
      dp[i][j] = dp[i - 1][j - 1] + 1

 
answer = 0
for i in range(N):
  for j in range(i + 1, N):
    if answer < dp[i][j] and j - i >= dp[i][j]:
      answer = dp[i][j]
print(answer)

Failing test case input:
2
{

Expected output:
0


Actual output:
speed test
0


Fixed submission (verdict: Accepted):
N = int(input())
S = input()

T = [[0 for _ in range(N)] for _ in range(N)]

for i in range(N):
  for j in range(i + 1, N):
    if S[i] == S[j]:
      T[i][j] += T[i - 1][j - 1] + 1

answer = 0
for i in range(N):
  for j in range(N):
    if T[i][j] and j - i >= T[i][j]:
      answer = max(answer, T[i][j])
print(answer)

Problem: p02913
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
S = input()


r = 0
ans = 0
for l in range(N):

    while True:
        print(S.rfind(S[l:r+1]), S[l:r+1], l, r)
        
        if S.rfind(S[l:r+1]) >= r+1:
            r += 1
            ans = max(ans, r-l)
        else:
            break

print(ans)


Failing test case input:
5
ab`cb

Expected output:
1


Actual output:
0 a 0 0
5  1 0
4 b 1 1
1 b` 1 2
2 ` 2 2
5  3 2
3 c 3 3
5  4 3
4 b 4 4
1


Fixed submission (verdict: Accepted):
N = int(input())
S = input()


r = 0
ans = 0
for l in range(N):

    while True:
        
        
        if S.rfind(S[l:r+1]) >= r+1:
            r += 1
            ans = max(ans, r-l)
        else:
            break

print(ans)

Problem: p02913
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
def main():
    N = int(input())
    S = input()
    ans = 0

    for i in range(N):
        s1 = S[0:i+1]
        s2 = S[i+1:N]
        for j in range(i):
            s = s1[j:]
            if s in s2:
                ans = len(s)
                break
        if len(s2) < ans and len(s1) > len(s2):
            break

    print(ans)

if __name__ == '__main__':
    main()


Failing test case input:
2
zz

Expected output:
1


Actual output:
0


Fixed submission (verdict: Accepted):
def main():
    N = int(input())
    S = input()
    ans = 0
    index = 0

    for i in range(N):
        s1 = S[index:i+1]
        s2 = S[i+1:]
        if s1 in s2:
            ans = len(s1)
        else:
            index += 1

    print(ans)

if __name__ == '__main__':
    main()

Problem: p02913
Buggy submission (Python (3.4.3), verdict: Wrong Answer):

tmp = input()
my_str = input()

start = 0
end = 1
max_len = 0

while (end < len(my_str)):
    if my_str[start:end] in my_str[end:]:
        end += 1
        if end - start > max_len:
            max_len = end - start
    else:
        start += 1

print(max_len)


Failing test case input:
2
y}

Expected output:
0


Actual output:
1


Fixed submission (verdict: Accepted):

tmp = input()
my_str = input()

start = 0
end = 1
max_len = 0

while (end < len(my_str)):
    if my_str[start:end] in my_str[end:]:
        if end - start > max_len:
            max_len = end - start
        end += 1
    else:
        start += 1

print(max_len)
Problem: p02913
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
N = int(input())
S = input()

ans = 0
left, right = 0, 1
while right < N:
    s = S[left:right]
    if s in S[left:]:
        ans = max(ans, len(s))
        right += 1
    else:
        left += 1

print(ans)


Failing test case input:
13
deqapovfo`nfr

Expected output:
1


Actual output:
12


Fixed submission (verdict: Accepted):
N = int(input())
S = input()

ans = 0
left, right = 0, 1
while right < N:
    s = S[left:right]
    if s in S[right:]:
        ans = max(ans, len(s))
        right += 1
    else:
        left += 1

print(ans)
Problem: p02891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import sys
import math
stdin = sys.stdin
mod = 10**9 + 7

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
sa = lambda h: [list(map(int, stdin.readline().split())) for i in range(h)]

s = ns()
k = ni()

w = s[0]
a = []
count = 1
for i in s[1:]:
    if w == i:
        count += 1
    else:
        a.append(count)
        count = 1
        w = i
if s.count(s[0]) == len(s):
    ans = len(s) * k 
elif w != s[0]:
    a.append(count)
    ans = 0
    for i in a:
        ans += i 
    ans = ans * k
else:
    al = a[0]
    ar = count
    ans = 0
    for i in a[1:]:
        ans += i 
    ans = ans * k
    ans += al 
    ans += (al + ar) * (k -1)
print(ans)

Failing test case input:
shrih
6

Expected output:
0


Actual output:
30


Fixed submission (verdict: Accepted):
import sys
import math
stdin = sys.stdin
mod = 10**9 + 7

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
sa = lambda h: [list(map(int, stdin.readline().split())) for i in range(h)]

s = ns()
k = ni()

w = s[0]
a = []
count = 1
for i in s[1:]:
    if w == i:
        count += 1
    else:
        a.append(count)
        count = 1
        w = i
if s.count(s[0]) == len(s):
    ans = (len(s) * k) 
elif w != s[0]:
    a.append(count)
    ans = 0
    for i in a:
        ans += i 
    ans = ans * k
else:
    al = a[0]
    ar = count
    ans = 0
    for i in a[1:]:
        ans += i 
    ans = ans * k
    ans += al 
    ans += ((al + ar)
print(ans)
Problem: p02891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
s = list(input())
k = int(input())

ss = s + s

def counter(string):
    count = 0
    answer = 0
    prev = ""
    for index,sss in enumerate(string):
        if prev == "" or sss == prev:
            count+=1
            if count == 2: 
                answer+=1
                count=0
                string[index] = "1"
        else:
            count=1
        prev = sss
    return answer,string

kk = k

if k%2 == 0: 
    answer, string = counter(ss)
    if string[0] == string[-1]:
        print(answer*kk+k-1)
    else:
        print(answer*kk)
else:
    answer, string = counter(s)
    answer2, string2 = counter(ss)

    if string2[0] == string2[-1]:
        print(answer2*kk + answer + k-1)
    else:
        print(answer2*kk + answer)


Failing test case input:
ndeemenpcedrtmefpptfmonf
8560

Expected output:
17120


Actual output:
34240


Fixed submission (verdict: Accepted):
import sys
s = list(input())
k = int(input())

if len(set(s))==1:
	print((len(s)*k)
	sys.exit()
    
def counter(string):
    count = 0
    answer = 0
    prev = ""
    for index,sss in enumerate(string):
        if prev == "" or sss == prev:
            count+=1
            if count == 2: 
                answer+=1
                count=0
                string[index] = "1"
        else:
            count=1
        prev = sss
    return answer,string

answer, string = counter(s.copy())

if string[0] != string[-1]:
    print(answer*k)
else:
    count = 0
    l = len(s)
    for i in range(l):
        if s[i] == s[0]:
            count += 1
        else:
            break
    for i in range(l):
        if s[-1-i] == s[0]:
        	count += 1
        else:
        	break

    if count % 2 ==0:
        print(answer*k + k-1)
    else:
        print(answer*k)
Problem: p02891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
s = list(input())
k = int(input())
count = 0
import copy
s1 = list(set(s))
if len(s1) == 1:
    count = int((k * len(s)) / 2)
elif k >= 3:
    t1 = s * 5
    for i in range(len(t1) - 1):
        if t1[i] == t1[i + 1]:
            t1.pop(i + 1)
            t1.insert(i + 1, "")
            count += 1
    if count % 5 == 0:
        count = (count / 5) * k
    else:
        count = ((count + 2) / 5) * k - 2
else:
    t1 = s * k
    for i in range(len(t1) - 1):
        if t1[i] == t1[i + 1]:
            t1.pop(i + 1)
            t1.insert(i + 1, "")
            count += 1
print(int(count))


Failing test case input:
hssih
3

Expected output:
5


Actual output:
4


Fixed submission (verdict: Accepted):
s = list(input())
k = int(input())
count = 0
s1 = list(set(s))
if len(s1) == 1:
    count = int((k * len(s)) / 2)
elif k >= 3:
    t1 = s * 5
    for i in range(len(t1) - 1):
        if t1[i] == t1[i + 1]:
            t1.pop(i + 1)
            t1.insert(i + 1, "")
            count += 1
    if count % 5 == 0:
        count = (count / 5) * k
    elif (count + 1) % 5 == 0:
        count = ((count + 1) / 5) * k - 1
    else:
        count = ((count + 2) / 5) * k - 2
else:
    t1 = s * k
    for i in range(len(t1) - 1):
        if t1[i] == t1[i + 1]:
            t1.pop(i + 1)
            t1.insert(i + 1, "")
            count += 1
print(int(count))

Problem: p02891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
s = input()
k = int(input())
n = len(s)
c = 0
x = []
a = 1

for i in range(n):
    if i<n-1 and s[i]==s[i+1]:
        a += 1
    else:
        c += a
        x.append(a)
        a = 1

c2 = 0
if s[0]==s[-1]:
    c2 = x[0]

print(c*k-c2*(k-1))

Failing test case input:
fnouftppgemmceerpndmeedm
313

Expected output:
1252


Actual output:
7512


Fixed submission (verdict: Accepted):
s = input()
k = int(input())
n = len(s)
c = 0
x = []
a = 1

for i in range(n):
    if i<n-1 and s[i]==s[i+1]:
        a += 1
    else:
        c += a
        x.append(a)
        a = 1

c2 = 0
if s[0]==s[-1]:
    c2 = x[0]

print(len(s)*k
Problem: p02891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
S = list(input())
K = int(input())



i = 0
sum = 1
total_sum = 0
sum_ketu = 0
j = 0
add_sum = 1
if len(S) == 1:
    print( K -1)
    exit()
    
while(1):
    if i == len(S)-1:
        if S[i] == S[0]:
            while( j <= len(S)-2):
                if S[j] == S[j+1]:
                    add_sum += 1
                    j += 1
                else:
                    break
        if (sum % 2 == 1) and (add_sum % 2 == 1):
            sum += 1
            sum_ketu = -1
        break
    if S[i] == S[i+1]:
     
     
        sum += 1
     
        i += 1
    else:
        total_sum += sum 
        sum = 1
        i += 1
        
total_sum += sum 



print(total_sum * K + sum_ketu)



Failing test case input:
ndeemenpcedrtmefpptfmonf
8560

Expected output:
17120


Actual output:
213999


Fixed submission (verdict: Accepted):
S = list(input())
K = int(input())

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

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

sum += count




j = 0 
a = 1 
while(1):
    if S[j] == S[j+1]:
        a += 1
        j += 1
    else:
        break
    if j == len(S) -1:
        break



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 a == len(S): 
    print((len(S)* K)
elif S[len(S)-1] == S[0]:
    print( sum * K + ((a+b)
else:
    print( sum * K)

Problem: p02891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
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 
    
    return r

if K == 1:
    ans = count(S)
elif len(S) == 1:
    ans = (K + 1) 
elif K == 2:
    ans = count(S*2)
else:
    a1 = count(S*2)
    a2 = count(S*3)
    ans = a1 + (a2-a1) * (K-2)

print(ans)





Failing test case input:
fnomftppfemurdecpndmeedm
1185

Expected output:
2370


Actual output:
4740


Fixed submission (verdict: Accepted):
S = input()
K = int(input())

def count(s):
    
    a = []
    b = ''
    c = 1
    for si in s:
        if b == si:
            c += 1
        else:
            a.append(c)
            c = 1
        b = si
    a.append(c)

    r = 0
    for ai in a:
        r += ai 
    
    return r

if K == 1:
    ans = count(S)
elif len(S) == 1:
    ans = K 
elif len(set(S)) == 1:
    c = count(S*2)
    if K % 2 == 0:
        ans = c * (K
    else:
        diff = count(S*3) - count(S*2)
        ans = c * (K
else:
    a1 = count(S*2)
    a2 = count(S*3)
    ans = a1 + (a2-a1) * (K-2)

print(ans)
Problem: p02891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import sys
import math
stdin = sys.stdin
mod = 10**9 + 7

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
sa = lambda h: [list(map(int, stdin.readline().split())) for i in range(h)]

s = ns()
k = ni()

w = s[0]
a = []
count = 1
for i in s[1:]:
    if w == i:
        count += 1
    else:
        a.append(count)
        count = 1
        w = i
if s.count(s[0]) == len(s):
    ans = len(s) * k 
elif w != s[0]:
    ans = 0
    for i in a:
        ans += i 
    ans = ans * k
else:
    al = a[0]
    ar = count
    ans = 0
    for i in a[1:]:
        ans += i 
    ans = ans * k
    ans += al 
    ans += (al + ar) * (k -1)
print(ans)

Failing test case input:
cnonooooopntedeeeeeedest
17669775

Expected output:
88348875


Actual output:
406404825


Fixed submission (verdict: Accepted):
import sys
import math
stdin = sys.stdin
mod = 10**9 + 7

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
sa = lambda h: [list(map(int, stdin.readline().split())) for i in range(h)]

s = ns()
k = ni()

w = s[0]
a = []
count = 1
for i in s[1:]:
    if w == i:
        count += 1
    else:
        a.append(count)
        count = 1
        w = i
if s.count(s[0]) == len(s):
    ans = (len(s) * k) 
elif w != s[0]:
    a.append(count)
    ans = 0
    for i in a:
        ans += i 
    ans = ans * k
else:
    al = a[0]
    ar = count
    ans = 0
    for i in a[1:]:
        ans += i 
    ans = ans * k
    ans += al 
    ans += ((al + ar)
print(ans)

Problem: p02891
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
s = input()
k = int(input())
n = len(s)
l = []

x = 1
for i in range(n):
    if i == n-1:
        if s[i] == s[0]:
            l.append(x)
        continue
    
    if s[i] == s[i+1]:
        x += 1
    else:
        l.append(x)
        x = 1

ans = 0
for i in l:
    ans += i 
ans *= k
if l[0] % 2 and l[-1] % 2 and s[0] == s[-1]:
    ans += k-1

print(ans)

Failing test case input:
rr
538

Expected output:
538


Actual output:
1076


Fixed submission (verdict: Accepted):
s = input()
k = int(input())
n = len(s)
l = []

t = set(list(s))
if len(t) == 1:
    print(n * k 
    exit()

x = 1
for i in range(n):
    if i == n-1:
        l.append(x)
        continue
    
    if s[i] == s[i+1]:
        x += 1
    else:
        l.append(x)
        x = 1

ans = 0
for i in l:
    ans += i 
ans *= k
if l[0] % 2 and l[-1] % 2 and s[0] == s[-1]:
    ans += k-1

print(ans)
Problem: p02891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
S = str(input())
K = int(input())
tmp = 1
cnt1 = 0
cnt2 = 0
cnt3 = 0
for i in range(1, len(S)):
    if S[i-1] == S[i]:
        tmp += 1
    else:
        cnt1 += tmp 
        tmp = 1
if tmp > 1:
    cnt1 += tmp
S2 = S + S
tmp = 1
for i in range(1, len(S2)):
    if S2[i-1] == S2[i]:
        tmp += 1
    else:
        cnt2 += tmp 
        tmp = 1
if tmp > 1:
    cnt2 += tmp
S3 = S2 + S
tmp = 1
for i in range(1, len(S3)):
    if S3[i-1] == S3[i]:
        tmp += 1
    else:
        cnt3 += tmp 
        tmp = 1
if tmp > 1:
    cnt3 += tmp
if K == 1:
    print(cnt1)
elif K == 2:
    print(cnt2)
else:
    print((cnt3 - cnt2) * (K - 2) + cnt2)

Failing test case input:
tredmdepdedetndmopnnfond
14180048

Expected output:
14180048


Actual output:
340321151


Fixed submission (verdict: Accepted):
S = str(input())
K = int(input())
tmp = 1
cnt1 = 0
cnt2 = 0
for i in range(1, len(S)):
    if S[i-1] == S[i]:
        tmp += 1
    else:
        cnt1 += tmp 
        tmp = 1
if tmp == len(S):
        print(len(S) * K 
        exit()
if tmp > 1:
    cnt1 += tmp
S2 = S + S
tmp = 1
for i in range(1, len(S2)):
    if S2[i-1] == S2[i]:
        tmp += 1
    else:
        cnt2 += tmp 
        tmp = 1
if tmp > 1:
    cnt2 += tmp
if K == 1:
    print(cnt1)
else:
    print((cnt2 - cnt1) * (K-2) + cnt2)

Problem: p02891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
S = input()
N = len(S)
K = int(input())

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

left = 0
right = 1

while left < N and S[left] == S[0]:
    left += 1
while right <= N and S[-right] == S[-1]:
    right += 1

change = 0
isChanged = False
T = list(S[left: N - right + 1])

for i, t in enumerate(T[1:], start=1):
    if t == T[i - 1]:
        if not isChanged:
            change += 1
            isChanged = True
            continue
    isChanged = False

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

Failing test case input:
issii
2

Expected output:
4

Actual output:
3


Fixed submission (verdict: Accepted):
S = input()
N = len(S)
K = int(input())

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

left = 0
right = 1

while left < N and S[left] == S[0]:
    left += 1
while right <= N and S[-right] == S[-1]:
    right += 1
right -= 1

change = 0
isChanged = False
T = list(S[::])

for i, t in enumerate(T[1:], start=1):
    if t == T[i - 1]:
        if not isChanged:
            change += 1
            isChanged = True
            continue
    isChanged = False

ans = change * K

if S[0] == S[-1]:
    ans -= (left 
print(ans)
Problem: p02891
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):



import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

s = input()
k = I()
n = len(s)
res = 0
i = 0
while i < n:
    tmp = s[i]
    i += 1
    cnt = 0
    while i < n and tmp == s[i]:
        cnt += 1
        i += 1
    res += (cnt+1)
print(res*k)

Failing test case input:
enomntppfentrdecpfemeedn
740263

Expected output:
1480526


Actual output:
17766312


Fixed submission (verdict: Accepted):



import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

s = input()
k = I()
n = len(s)
res = 0
i = 0
lst = deque()
while i < n:
    tmp = s[i]
    i += 1
    cnt = 1
    while i < n and tmp == s[i]:
        cnt += 1
        i += 1
    lst.append(cnt)
if len(lst) >= 2:
    if s[0] == s[-1] and k >= 2:
        x = lst.popleft()
        y = lst.pop()
        res = 0
        for i in lst:
            res += (i
        res += (x
        res += ((x+y)
        print(res)
    else:
        res = 0
        for i in lst:
            res += (i
        print(res)
else:
    print((n*k)
Problem: p02891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
S = input()
K = int(input())

def count(s):
    
    a = []
    b = ''
    c = 1
    for si in s:
        if b == si:
            c += 1
        else:
            a.append(c)
            c = 1
        b = si
    a.append(c)

    r = 0
    for ai in a:
        r += ai 
    
    return r

if K == 1:
    ans = count(S)
elif len(S) == 1:
    
    ans = K 
elif K == 2:
    ans = count(S*2)
else:
    a1 = count(S*2)
    a2 = count(S*3)
    ans = a1 + (a2-a1) * (K-2)

print(ans)





Failing test case input:
tredmdepdedetndmopnnfond
8171470

Expected output:
8171470


Actual output:
196115281


Fixed submission (verdict: Accepted):
S = input()
K = int(input())

def count(s):
    
    a = []
    b = ''
    c = 1
    for si in s:
        if b == si:
            c += 1
        else:
            a.append(c)
            c = 1
        b = si
    a.append(c)

    r = 0
    for ai in a:
        r += ai 
    
    return r

if K == 1:
    ans = count(S)
elif len(S) == 1:
    ans = K 
elif len(set(S)) == 1:
    c = count(S*2)
    if K % 2 == 0:
        ans = c * (K
    else:
        diff = count(S*3) - count(S*2)
        ans = c * (K
else:
    a1 = count(S*2)
    a2 = count(S*3)
    ans = a1 + (a2-a1) * (K-2)

print(ans)

Problem: p02891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):

def func():
    if len(S) == 1:
        return int(K/2)
    
    cnt = 1
    pre_s = S[0]
    tup_list = []
    for s in S[1:]:
        if pre_s == s:
            cnt += 1
        else:
            tup_list.append([pre_s, cnt])
            cnt = 1
        pre_s = s
    else:
        tup_list.append([pre_s, cnt])

    if len(tup_list) == 1:
        return int(cnt* K/2)
        
    if tup_list[0][0] == tup_list[-1][0]:
        tup_list[0][1] += tup_list.pop()[1]
    res = 0
    for _,cnt in tup_list:
        res += int(cnt/2) * K
    return res

S = input()
K = int(input())
print(func())

Failing test case input:
hsrih
6

Expected output:
5


Actual output:
6


Fixed submission (verdict: Accepted):

def func():
    if len(S) == 1:
        return int(K/2)
    
    cnt = 1
    pre_s = S[0]
    tup_list = []
    for s in S[1:]:
        if pre_s == s:
            cnt += 1
        else:
            tup_list.append([pre_s, cnt])
            cnt = 1
        pre_s = s
    else:
        tup_list.append([pre_s, cnt])

    if len(tup_list) == 1:
        return int(cnt* K/2)
    
    ext = 0
    if tup_list[0][0] == tup_list[-1][0]:
        a = tup_list.pop()[1]
        b = tup_list[0][1]
        ext = int(a/2) + int(b/2) - int((a+b)/2)
        tup_list[0][1] += a
        
    res = 0
    for _,cnt in tup_list:
        res += int(cnt/2) * K
    return res + ext
    
S = input()
K = int(input())
print(func())
Problem: p02891
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
def count_changes(s, k, prev=None):
    count = 0
    for _ in range(k):
        for cur in s:
            if cur == prev:
                count += 1
                prev = None
            else:
                prev = cur
    return count


if __name__ == '__main__':
    s = input()
    k = int(input())
    if k <= 2:
        print(count_changes(s, k))
    else:
        base_count = count_changes(s, 3)
        d, rest = divmod(k, 3)
        res = base_count * d
        if rest:
            res += count_changes(s, rest, s[-1])
        if s[0] == s[-1]:
            res += d - 1
        print(res)


Failing test case input:
rr
552

Expected output:
552


Actual output:
735


Fixed submission (verdict: Accepted):
def count_changes(s, k, prev=None):
    count = 0
    for _ in range(k):
        for cur in s:
            if cur == prev:
                count += 1
                prev = None
            else:
                prev = cur
    return count, prev


if __name__ == '__main__':
    s = input()
    k = int(input())
    if len(set(s)) == 1:
      print((len(s) * k) 
      exit()
    if k <= 10 ** 8:
        print(count_changes(s, k)[0])
    else:
        base_count, prev = count_changes(s, 3)
        d, rest = divmod(k, 3)
        res = base_count * d
        if rest:
            res += count_changes(s, rest, prev)[0]
        elif s[0] == prev:
            res += (d - 1)
        print(res)

Problem: p02891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import re
 
s = input()
k = int(input())
 
frag = False
 
def nchars(s, n):
    assert n > 0
    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():]
        
s_ = list(nchars(s,1))
 
for i in s_[0]:
  s1 = i
for i in s_[-1]:
  s2 = i
 
if s1 == s2:
  frag = True
 
l = [len(i) for i in s_]
 
c = l[0]
 
c1 = 0
c2 = 0
 
for i in range(len(l)-1):
  if i != len(l)-1:
    c1 += l[i+1]
  else:
    if frag:
      c1 += l[i] 
    else:
      c2 = (l[0]+l[i])
  
if not frag:
  print(c1*k)
else:
  print(c1)

Failing test case input:
fnomftppfemurdecpndmeedm
1185

Expected output:
2370


Actual output:
27255


Fixed submission (verdict: Accepted):
import re
 
s = input()
k = int(input())
 
frag = False
 
def nchars(s, n):
    assert n > 0
    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():]
        
s_ = list(nchars(s,1))
 
for i in s_[0]:
  s1 = i
for i in s_[-1]:
  s2 = i
 
if s1 == s2:
  frag = True
 
l = [len(i) for i in s_]
 
c = l[0]
 
c1 = 0
c2 = 0
c3 = 0

for i in range(len(l)):
  if i != len(l)-1:
    c1 += l[i]
  else:
    if not frag:
      c1 += l[i] 
    else:
      c2 = (l[0]+l[i])
      c3 = l[i]

if len(l) == 1:
  print((l[0]*k)

elif not frag:
  print(c1*k)
else:
  print(c + (c1-c)*k + c2*(k-1) + c3)
Problem: p02891
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
s = input()
k = int(input())

press = []
temp = s[0]
cnt = 0
for i in range(len(s)):
    if temp == s[i]:cnt += 1
    else:
        press.append((temp, cnt))
        cnt = 1
        temp = s[i]
press.append((temp, cnt))

count = 0
for i, l in enumerate(press):
    str, num = l
    if num > 1:
        if i != 0 and i != len(s) - 1:
            count += num 
        else:
            count += (num - 1) 

if s[0] != s[-1]:
    print(count * k)
else:
    if press[0][1] % 2 == 0 and press[-1][1] % 2 == 0:
        count += 1
    print(count * k)

Failing test case input:
rr
757

Expected output:
757


Actual output:
1514


Fixed submission (verdict: Accepted):
s = input()
k = int(input())
if len(s) == 1:
    print(k 
    exit()

press = []
temp = s[0]
cnt = 0
for i in range(len(s)):
    if temp == s[i]:cnt += 1
    else:
        press.append((temp, cnt))
        cnt = 1
        temp = s[i]
press.append((temp, cnt))

if len(press) == 1:
    print(len(s) * k 
    exit()

count = 0
for str, num in press:
    if num > 1:
        count += num 

count *= k
if s[0] == s[-1]:
    count -= (press[0][1] 
print(count)

Problem: p02891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
s = list(input())
k = int(input())

ss=[]
if k%2 != 0:
    ss = s + s

def counter(string):
    count = 0
    answer = 0
    prev = ""
    for index,sss in enumerate(string):
        if prev == "" or sss == prev:
            count+=1
            if count == 2: 
                answer+=1
                count=0
                string[index] = "1"
        else:
            count=1
        prev = sss
    return answer,string

if not ss: 
    answer, string = counter(s)
    if string[0] == string[-1]:
        print(answer*k+k-1)
    else:
        print(answer*k)
else:
    kk = k
    answer, string = counter(s)
    answer2, string2 = counter(ss)

    if string2[0] == string2[-1]:
        print(answer2*kk + answer + k-1)
    else:
        print(answer2*kk + answer)

Failing test case input:
tredmdepdedetndmopnnfond
14475413

Expected output:
14475413


Actual output:
28950827


Fixed submission (verdict: Accepted):
import sys
s = list(input())
k = int(input())

if len(set(s))==1:
	print((len(s)*k)
	sys.exit()
    
def counter(string):
    count = 0
    answer = 0
    prev = ""
    for index,sss in enumerate(string):
        if prev == "" or sss == prev:
            count+=1
            if count == 2: 
                answer+=1
                count=0
                string[index] = "1"
        else:
            count=1
        prev = sss
    return answer,string

answer, string = counter(s.copy())

if string[0] != string[-1]:
    print(answer*k)
else:
    count = 0
    l = len(s)
    for i in range(l):
        if s[i] == s[0]:
            count += 1
        else:
            break
    for i in range(l):
        if s[-1-i] == s[0]:
        	count += 1
        else:
        	break

    if count % 2 ==0:
        print(answer*k + k-1)
    else:
        print(answer*k)
Problem: p02891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
S = input()
K = int(input())

def count(s):
    
    a = []
    b = ''
    c = 1
    for si in s:
        if b == si:
            c += 1
        else:
            a.append(c)
            c = 1
        b = si
    a.append(c)

    r = 0
    for ai in a:
        r += ai 
    
    return r

if K == 1:
    ans = count(S)
elif len(S) == 1:
    ans = (K + 1) 
elif K == 2:
    ans = count(S*2)
else:
    a1 = count(S*2)
    a2 = count(S*3)
    ans = a1 + (a2-a1) * (K-2)

print(ans)





Failing test case input:
rr
597

Expected output:
597


Actual output:
1195


Fixed submission (verdict: Accepted):
S = input()
K = int(input())

def count(s):
    
    a = []
    b = ''
    c = 1
    for si in s:
        if b == si:
            c += 1
        else:
            a.append(c)
            c = 1
        b = si
    a.append(c)

    r = 0
    for ai in a:
        r += ai 
    
    return r

if K == 1:
    ans = count(S)
elif len(S) == 1:
    ans = K 
elif len(set(S)) == 1:
    c = count(S*2)
    if K % 2 == 0:
        ans = c * (K
    else:
        diff = count(S*3) - count(S*2)
        ans = c * (K
else:
    a1 = count(S*2)
    a2 = count(S*3)
    ans = a1 + (a2-a1) * (K-2)

print(ans)

Problem: p02891
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
S=input()
N=len(S)
K=int(input())
s="".join((S,S))
cnt1=0
cnt2=0
buf=0
piv=""
for i in range(N):
    if S[i]==piv:
        buf=buf+1
    else:
        piv=S[i]
        cnt1=cnt1+(buf+1)
        buf=0
cnt1=cnt1+(buf+1)
buf=0
piv=""
for i in range(2*N):
    if s[i]==piv:
        buf=buf+1
    else:
        piv=s[i]
        cnt2=cnt2+(buf+1)
        buf=0
    
cnt2=cnt2+(buf+1)
x=cnt2-cnt1
print(cnt1+(K-1)*x)

Failing test case input:
cooooooooonteeeeeeeedest
999993333

Expected output:
7999946664


Actual output:
23999839993


Fixed submission (verdict: Accepted):
S=input()
N=len(S)
K=int(input())
s="".join((S,S))
cnt1=0
cnt2=0
buf=0
piv=""
if len(set(S))!=1:
    for i in range(N):
        if S[i]==piv:
            buf=buf+1
        else:
            piv=S[i]
            cnt1=cnt1+(buf+1)
            buf=0
    cnt1=cnt1+(buf+1)
    buf=0
    piv=""
    for i in range(2*N):
        if s[i]==piv:
            buf=buf+1
        else:
            piv=s[i]
            cnt2=cnt2+(buf+1)
            buf=0
    
    cnt2 = cnt2 + (buf + 1) 
    x = cnt2 - cnt1
    print(cnt1 + (K - 1) * x)
else:
    print((len(S)*K)
Problem: p02891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import sys
import os
S = input()
K = int(input())
if len(S) == 1:
    print(0)
    sys.exit()
cnt0 = 1
cntL = 1
cnt = 1
ans = 0
i = 0
while i < len(S)-1:
    for j in range(i+1,len(S)):
        if S[i] == S[j]:
            cnt +=1
        else:
            if i == 0:
                cnt0 = cnt
            i = j
            break
    if cnt > 1:
        ans += cnt
        cnt = 1
        i = j

for i in range(len(S)-1,0,-1):
    if S[i] == S[i-1]:
        cntL +=1
    else:
        break
ans = K*ans
if S[0] == S[-1] and cnt0%2 != 0 and cntL %2 != 0:
    ans += K-1

print(ans)


Failing test case input:
enomftppfentrdecpnemeedn
1594731

Expected output:
3189462


Actual output:
6378924


Fixed submission (verdict: Accepted):
import sys
S = input()
K = int(input())
if len(S) == 1:
    print(K
    sys.exit()
cnt0 = 1
cntL = 1
cnt = 1
ans = 0
i = 0
while i < len(S)-1:
    for j in range(i+1,len(S)):
        if S[i] == S[j]:
            cnt +=1
        else:
            if i == 0:
                cnt0 = cnt
            i = j
            break
    if cnt > 1:
        ans += cnt
        cnt = 1
        i = j

for i in range(len(S)-1,0,-1):
    if S[i] == S[i-1]:
        cntL +=1
    else:
        break
ans = K*ans
if S[0] == S[-1] and cnt0%2 != 0 and cntL %2 != 0:
    ans += K-1
if cntL == len(S):
    if (K*cntL)%2==0:
        print((K*cntL)
    else:
        print((K*cntL-1)
    sys.exit()
print(ans)
Problem: p02891
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
import sys
import itertools

import time
import math
from heapq import heappop, heappush
from collections import defaultdict
from collections import Counter
from collections import deque
from itertools import permutations
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


S = input()
K = int(input())
ans = 0

S = list(S)
def f(T):
    ret = 0
    l = 0
    r = 1
    n = len(T)
    while l < n:
        while r < n and T[l] == T[r]:
            r += 1
        c = r - l
        ret += c 
        l = r
        r = l + 1
    return ret

first = f(S)
second = f(S + S)
diff = second - first

print(first + diff * (K - 1))

Failing test case input:
tsedmeepdedetndoopoofond
30581139

Expected output:
91743417


Actual output:
733947336


Fixed submission (verdict: Accepted):
import sys
import itertools

import time
import math
from heapq import heappop, heappush
from collections import defaultdict
from collections import Counter
from collections import deque
from itertools import permutations
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


S = input()
K = int(input())
ans = 0

S = list(S)
def f(T):
    ret = 0
    l = 0
    r = 1
    n = len(T)
    while l < n:
        while r < n and T[l] == T[r]:
            r += 1
        c = r - l
        ret += c 
        l = r
        r = l + 1
    return ret

first = f(S)
second = f(S + S)
diff = second - first

if len(set(S)) == 1:
    print(len(S) * K 
else:
    print(first + diff * (K - 1))
Problem: p02891
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
import string


def count_changes(s, k, pool, prev=None):
    if k == 0:
        return 0, None, None
    t = s * k
    count = 0
    i = 0
    for cur in t:
        if cur == prev:
            count += 1
            prev = pool[i]
            i = (i + 1) % length
        else:
            prev = cur
    return count, i, prev


if __name__ == '__main__':
    s = input()
    k = int(input())
    pool = list(set(string.ascii_lowercase) - set(s))
    length = len(pool)
    if k <= 2:
        print(count_changes(s, k, pool)[0])
    else:
        base_count, i, prev = count_changes(s, 3, pool)
        d, rest = divmod(k, 3)
        print(base_count * d + count_changes(s, rest, pool, prev)[0])


Failing test case input:
hirsh
6

Expected output:
5


Actual output:
4


Fixed submission (verdict: Accepted):
def count_changes(s, k, prev=None):
    count = 0
    for _ in range(k):
        for cur in s:
            if cur == prev:
                count += 1
                prev = None
            else:
                prev = cur
    return count, prev


if __name__ == '__main__':
    s = input()
    k = int(input())
    if len(set(s)) == 1:
      print((len(s) * k) 
      exit()
    if k <= 10 ** 8:
        print(count_changes(s, k)[0])
    else:
        base_count, prev = count_changes(s, 3)
        d, rest = divmod(k, 3)
        res = base_count * d
        if rest:
            res += count_changes(s, rest, prev)[0]
        elif s[0] == prev:
            res += (d - 1)
        print(res)

Problem: p02891
Buggy submission (Python (3.8.2), verdict: Wrong Answer):


def main():
    S = input()
    K = int(input())

    cnt = 0
    ans = 0
    s_temp = S[0]
    fcnt = 0
    for s in S:
        if s_temp == s:
            cnt += 1
            s_temp = s
        else:
            ans += cnt 
            if fcnt == 0:
                fcnt = cnt
            cnt = 1
            s_temp = s
    ans += cnt 
    ans *= K
    if S[0] == S[-1] and cnt % 2 == 1 and fcnt % 2 == 1:
        ans += K - 1
    if len(S) == 1:
        if K % 2 == 0:
            ans = K 
        else:
            ans = K 

    print(ans)


if __name__ == "__main__":
    main()


Failing test case input:
hssih
3

Expected output:
5


Actual output:
17


Fixed submission (verdict: Accepted):


def main():
    S = input()
    K = int(input())

    cnt = 0
    ans = 0
    s_temp = S[0]
    fcnt = 0
    for s in S:
        if s_temp == s:
            cnt += 1
            s_temp = s
        else:
            ans += cnt 
            if fcnt == 0:
                fcnt = cnt
            cnt = 1
            s_temp = s
    if fcnt == 0:
        fcnt = cnt
    ans += cnt 
    ans *= K
    if len(S) == fcnt:
        ans = (len(S) * K) 
    elif S[0] == S[-1]:
        ans -= (fcnt 

    print(ans)


if __name__ == "__main__":
    main()