ZhenlongDai/ACPR huggingface dataset:
Problem: p01102
Buggy submission (Python3, verdict: Wrong Answer):
results=[]
while True:
    a=input().split("\"")
    if a[0]==".":
        break
    b=input().split("\"")
    count1=0
    count2=0
    if len(a) != len(b):
        results.append(2)
    else:
        for i in range(len(a)):
            if(i%2==1):
                if len(a[i]) == 0 and len(b[i]) != 0:
                    count1+=1
                elif len(a[i]) != 0 and len(b[i]) == 0:
                    count1+=1 
                else:
                    for k,l in zip(a[i],b[i]):
                        if(k!=l):
                            count1+=1
            else:
                if len(a[i]) == 0 and len(b[i]) != 0:
                    count2+=1
                elif len(a[i]) != 0 and len(b[i]) == 0:
                    count2+=1 
                else:
                    for k,l in zip(a[i],b[i]):
                        if(k!=l):
                            count2+=1           
        if count1==0 and count2==0:
            results.append(0)
        elif count1==1 and count2==0:
            results.append(1)
        else:
            results.append(2)
for i in results:
    if i==0:
        print("IDENTICAL")
    if i==1:
        print("CLOSE")
    if i==2:
        print("DIFFERENT")


Failing test case input:
print"hello";print133
print"hello";qrinu123
read"B1input";somve;output;
;tuptuo;evlos;"2B"daer
read"C1";solve;output"C1ans";
read"C2";solve;output"C2ans";
""""""""
"""52"!#""
slow"program"
fast"code"
"super"fast"program"
"super"faster"prohram"
X""
Y
I"S""CREAM"
I"CE""BREAM"
11"12"11
13"3"111
.

Expected output:
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT


Actual output:
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
CLOSE
DIFFERENT
DIFFERENT
DIFFERENT


Fixed submission (verdict: Accepted):
results=[]
while True:
    a=input().split("\"")
    if a[0]==".":
        break
    b=input().split("\"")
    count1=0
    count2=0
    if len(a) != len(b):
        results.append(2)
    else:
        for i in range(len(a)):
            if(i%2==1):
                if len(a[i]) != len(b[i]):
                    count1+=1
                else:
                    for k,l in zip(a[i],b[i]):
                        if(k!=l):
                            count1+=1
            else:
                if len(a[i]) != len(b[i]):
                    count2+=1
                else:
                    for k,l in zip(a[i],b[i]):
                        if(k!=l):
                            count2+=1           
        if count1==0 and count2==0:
            results.append(0)
        elif count1==1 and count2==0:
            results.append(1)
        else:
            results.append(2)
for i in results:
    if i==0:
        print("IDENTICAL")
    if i==1:
        print("CLOSE")
    if i==2:
        print("DIFFERENT")
Problem: p01102
Buggy submission (Python3, verdict: Wrong Answer):
def p(A,s):
    i=0
    j=0
    s=[]
    while(len(A)>i):
        if A[i]=='"':
            list1=A.partition('"')
            s.append(list1[0])
            A=list1[2]
            i=0
            j+=1
        else:
            i+=1
            if len(A)==i:
                s.append(A)
    return s
A=input()
s1=[]
s2=[]
O=[]
while 1:
    i=0
    cnt=0
    B=input()
    s1=p(A,s1)
    s2=p(B,s2)
    l1=len(s1)
    l2=len(s2)    
    while i<l1 and i<l2:
        if s1[i]!=s2[i]:
            cnt+=1
        i+=1
    if cnt==0:
        O.append("IDENTICAL")
    elif cnt==1:
        O.append("CLOSE")
    else:
        O.append("DIFFERENT")
    A=input()
    if A==".":
        break
    
for i in range(len(O)):
    print(O[i])



Failing test case input:
print"hello";print133
print"hello";qrinu123
read"B1input";somve;output;
;tuptuo;evlos;"2B"daer
;"sna0C"tuptuo;evlos;"1C"daer
read"C2";solve;output"C2ans";
""""""""
""#!"25"""
slow"program"
fast"code"
"super"fast"program"
"super"faster"prohram"
X""
Y
I"S""CREAM"
I"CE""BREAM"
11"12"11
13"3"111
.

Expected output:
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT


Actual output:
CLOSE
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
CLOSE
DIFFERENT
DIFFERENT


Fixed submission (verdict: Accepted):
s1=[]
s2=[]
O=[]
while 1:
    A=input().split('"')
    if A==["."]:
        break
    i=0
    cnt=0
    B=input().split('"')
    l1=len(A)
    l2=len(B)
    if l1==l2:   
        while i<l1 and i<l2:
            if A[i]!=B[i]:
                cnt+=1
                if i%2==0:
                    cnt+=2
            i+=1
        if cnt==0:
            O.append("IDENTICAL")
        elif cnt==1:
            O.append("CLOSE")
        else:
            O.append("DIFFERENT")
    else:
        O.append("DIFFERENT")
for i in range(len(O)):
    print(O[i])
Problem: p01102
Buggy submission (Python3, verdict: Wrong Answer):
while 1:
    s1 = input().split('\"')
    l1 = len(s1)
    if s1[0] == ".": break
    s2 = input().split('\"')
    l2 = len(s2)
    if l1 != l2:
        print("DIFFERENT")
        continue
    i = 0
    cnt = 0
    while 1:
        if i > l1-2:
            if cnt == 1:
                print("CLOSE")
                break
            else:
                print("IDENTICAL")
                break
        if s1[i] != s2[i]:
            print("DIFFERENT")
            break
        if s1[i+1] != s2[i+1]:
            if cnt == 1:
                print("DIFFERENT")
                break
            else: cnt = 1
        i += 2



Failing test case input:
print"hello";print133
print"hello";print123
read"B1input";somve;output;
;tuptuo;evlos;"2B"daer
read"C1";solve;output"C1ans";
read"C2";solve;output"C2ans";
"!""""""
"""52""#""
slow"program"
fast"code"
"super"fast"program"
"super"faster"prohram"
X""
Y
I"S""CREAM"
I"CE""BREAM"
11"22"11
13"3"111
.

Expected output:
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT


Actual output:
IDENTICAL
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT


Fixed submission (verdict: Accepted):
while 1:
    s1 = input().split('"')
    l1 = len(s1)
    if s1[0] == ".": break
    s2 = input().split('"')
    l2 = len(s2)
    if l1 != l2:
        print("DIFFERENT")
        continue
    i = 0
    cnt = 0

    while 1:
        if s1[i] != s2[i]:
            print("DIFFERENT")
            break
        if i > l1-2:
            if cnt == 1:
                print("CLOSE")
                break
            else:
                print("IDENTICAL")
                break
        if s1[i+1] != s2[i+1]:
            if cnt == 1:
                print("DIFFERENT")
                break
            else: cnt = 1
        i += 2
Problem: p01102
Buggy submission (Python3, verdict: Wrong Answer):
while True:
    s1 = input()
    if s1 == '.':
        break
    s2 = input()

    if s1 == s2:
        print("IDENTICAL")
        continue

    s2 = s2.split('"')
    s1 = s1.split('"')
    
    
    count = 0
    isTmp = False
    if s1[0] == '"' and s2[0] == '"':
        isTmp = True

    for i in range(len(s1)):
        if len(s1) != len(s2):
            print("DIFFERENT")
            break
        if count > 1:
            print("DIFFERENT")
            break

        if not isTmp:
            if i % 2 == 0:
                if s1[i] != s2[i]:
                    print("DIFFERENT");
                    break
            if i % 2 == 1:
                if s1[i] != s2[i]:
                    count += 1
        else:
            if i+1 % 2 == 0:
                if s1[i] != s2[i]:
                    print("DIFFERENT");
                    break
            if i+1 % 2 == 1:
                if s1[i] != s2[i]:
                    count += 1

    if count == 1:
        print("CLOSE")



Failing test case input:
print"hello";print133
print"helln";print123
read"B1input";solve;output;
;tuptuo;evmos;"2B"daer
read"C1";solve;output"C1ans";
read"C2";solve;output"C2ans";
""""""""
"""42""#""
slow"program"
fast"code"
"super"fast"program"
"super"faster"program"
X#"
Y
I"S""CREAM"
I"CE""CREAM"
11"22"11
1"33"111
.

Expected output:
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
CLOSE
DIFFERENT


Actual output:
DIFFERENT
CLOSE
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
CLOSE
DIFFERENT


Fixed submission (verdict: Accepted):
while True:
    s1 = input()
    if s1 == '.':
        break
    s2 = input()

    if s1 == s2:
        print("IDENTICAL")
        continue

    s2 = s2.split('"')
    s1 = s1.split('"')
    
    
    count = 0
    isTmp = False
    if s1[0] == '"' and s2[0] == '"':
        isTmp = True

    hoge = True
    for i in range(len(s1)):
        if len(s1) != len(s2):
            print("DIFFERENT")
            hoge = False
            break
        if count > 1:
            print("DIFFERENT")
            hoge = False
            break

        if not isTmp:
            if i % 2 == 0:
                if s1[i] != s2[i]:
                    print("DIFFERENT");
                    hoge = False
                    break
            if i % 2 == 1:
                if s1[i] != s2[i]:
                    count += 1
        else:
            if i+1 % 2 == 0:
                if s1[i] != s2[i]:
                    print("DIFFERENT");
                    hoge = False
                    break
            if i+1 % 2 == 1:
                if s1[i] != s2[i]:
                    count += 1

    if count == 1 and hoge:
        print("CLOSE")
Problem: p01102
Buggy submission (Python3, verdict: Wrong Answer):
A=[]
while True:	
	a=input().split('"')
	if a[0]==".":
		break
	b=input().split('"')
	
	cnt=0

	if len(a)!=len(b):
		A.append(-1)
	else:
		for i in range(len(a)):
			if(a[i]==b[i]):
				pass
			else:
				cnt+=1
		print(cnt)
		if cnt>=2:
			A.append(-1)
		if cnt==1:
			A.append(0)
		if cnt==0:
			A.append(1)

A.reverse()

for i in range(len(A)):
	tmp=A.pop()
	if tmp==-1:
		print("DIFFERENT")
	elif tmp==0:
		print("CLOSE")
	else:
		print("IDENTICAL")


Failing test case input:
print"hello";print133
321tnirp;"olleh"tnirp
read"e1input";solvB;output;
;tuptuo;evlos;"2B"daer
reae"C1";solve;output"C1ans";
read"C2";solve;output"C2ans";
""""""""
"""52""#""
slow"program"
fast"code"
"super"fast"program"
"super"faster"qrogram"
X""
Y
I"S""CREAM"
I"CE""BREAM"
11"22"11
1"33"112
.

Expected output:
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT


Actual output:
3
3
3
2
2
2
3
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT


Fixed submission (verdict: Accepted):
A=[]
while True:	
	a=input().split('"')
	if a[0]==".":
		break
	b=input().split('"')
	
	cnt=0

	if len(a)!=len(b):
		A.append(-1)
	else:
		for i in range(len(a)):
			if(a[i]==b[i]):
				pass
			else:
				if i%2==1:
					cnt+=1
				else:
					cnt+=2
		if cnt>=2:
			A.append(-1)
		if cnt==1:
			A.append(0)
		if cnt==0:
			A.append(1)

A.reverse()

for i in range(len(A)):
	tmp=A.pop()
	if tmp==-1:
		print("DIFFERENT")
	elif tmp==0:
		print("CLOSE")
	else:
		print("IDENTICAL")

Problem: p01102
Buggy submission (Python3, verdict: Wrong Answer):
a1 = input()
a2 = input()
b1 = a1.split('"')
b2 = a2.split('"')
cnt = 0
if len(b1)==len(b2):
    for i in range(0,len(b1)):
        if i%2==0:
            if b1[i]!=b2[i]:
                cnt+=10000
                break
        if i%2!=0:
            if b1[i]!=b2[i]:
                cnt+=1
else:
    cnt+=10000

if cnt>1:
    print("DIFFERENT")
if cnt==1:
    print("CLOSE")
if cnt==0:
    print("IDENTICAL")


Failing test case input:
print"hello";irpnt123
print"hello";print123
read"B1input";solve;output;
read"B2";stlve;ovtpuo;
read"C1";solve;output"C1ans";
read"C2";solve;output"C2ans";
""""""""
"""""24"""
slwo"program"
"edoc"tsaf
"super"fast"program"
"super"faster"program"
""X
X
I"S""CREAM"
I"CE""CREAM"
11"22"11
1"33"111
.

Expected output:
DIFFERENT
DIFFERENT
DIFFERENT
CLOSE
DIFFERENT
DIFFERENT
DIFFERENT
CLOSE
DIFFERENT


Actual output:
DIFFERENT


Fixed submission (verdict: Accepted):
st = []
while(1):
    a1 = input()
    if a1 =='.':
        break
    a2 = input()
    b1 = a1.split('"')
    b2 = a2.split('"')
    cnt = 0
    if len(b1)==len(b2):
        for i in range(0,len(b1)):
            if i%2==0:
                if b1[i]!=b2[i]:
                    cnt+=10000
                    break
            if i%2!=0:
                if b1[i]!=b2[i]:
                    cnt+=1
    else:
        cnt+=10000
    
    if cnt>1:
        st.append("DIFFERENT")
    if cnt==1:
        st.append("CLOSE")
    if cnt==0:
        st.append("IDENTICAL")

for i in range(len(st)):
    print(st[i])
Problem: p01102
Buggy submission (Python3, verdict: Wrong Answer):
while 1:
    s1=input().split('"')
    if s1[0]==".":
        break
    s2=input().split('"')
     
    flag2=0
    cnt2=0
    flag=0
     
    if len(s1)!=len(s2):
        print("DIFFERENT")
    else:
        i=0
        while i<len(s1):
            if s1[i]==s2[i]:
                flag2=1
            elif i%2==0:
                print("DIFFERENT")
                flag=1
                break
            else:
                cnt2+=1
                if cnt2>1:
                    print("DIFFERENT")
                    break
            i+=1
                 
        if flag==0 and cnt2==1:
            print("CLOSE")
        if flag2==1:
            print("IDENTICAL")


Failing test case input:
print"hello";print123
print"hello";print123
read"B1input";solve;output;
read"B2";stlve;ovtpuo;
read"C1";solve;output"C1ans";
read"C2";solve;output"C2ans";
""""""""
"""""24"""
slwo"program"
fast"code"
"super"fast"program"
"super"faster"program"
""X
X
I"S""CREAM"
I"CE""CREAM"
11"22"11
1"33"111
.

Expected output:
IDENTICAL
DIFFERENT
DIFFERENT
CLOSE
DIFFERENT
DIFFERENT
DIFFERENT
CLOSE
DIFFERENT


Actual output:
IDENTICAL
DIFFERENT
IDENTICAL
DIFFERENT
IDENTICAL
CLOSE
IDENTICAL
DIFFERENT
DIFFERENT
IDENTICAL
DIFFERENT
CLOSE
IDENTICAL
DIFFERENT


Fixed submission (verdict: Accepted):
while 1:
    s1=input().split('"')
    if s1[0]==".":
        break
    s2=input().split('"')
     
    cnt1=0
    cnt2=0
    flag=0
     
    if len(s1)!=len(s2):
        print("DIFFERENT")
    else:
        i=0
        while i<len(s1):
            if s1[i]==s2[i]:
                cnt1+=1
            elif i%2==0:
                print("DIFFERENT")
                flag=1
                break
            else:
                cnt2+=1
                if cnt2>1:
                    print("DIFFERENT")
                    break
            i+=1
                 
        if flag==0 and cnt2==1:
            print("CLOSE")
        if cnt1==len(s1):
            print("IDENTICAL")
Problem: p01102
Buggy submission (Python3, verdict: Wrong Answer):
ans=[]
while 1:
    s1=input().split('"')
    if s1==["."]: break
    s2=input().split('"')
    if len(s1)!=len(s2):
        ans.append("DIFFERENT")
        continue
    else:
        cnt=0
        for i in range(len(s1)):
            if s1[i]!=s2[i]:
                cnt+=1
            else: pass
        if cnt==0:
            ans.append("IDENTICAL")
        elif cnt==1:
            ans.append("CLOSE")
        else: 
            ans.append("DIFFERENT")
for i in range(len(ans)):
    print(ans[i])



Failing test case input:
print"hello";print133
print"hello";qrint123
read"B1input";somve;output;
;tuptuo;evlos;"2B"daer
read"C1";solve;output"C1ans";
read"C2";solve;output"C2ans";
""""""""
""#""25"!"
slow"progr"ma
fast"cod"e
"surer"fast"progpam"
"super"faster"prohram"
X""
Y
I"S""CREAM"
I"CE""BREAM"
11"12"11
13"3"111
.

Expected output:
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT


Actual output:
CLOSE
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT
DIFFERENT


Fixed submission (verdict: Accepted):
ans=[]
while 1:
    s1=input().split('"')
    if s1==["."]: break
    s2=input().split('"')
    if len(s1)!=len(s2):
        ans.append("DIFFERENT")
        continue
    else:
        cnt=0
        flag=0
        for i in range(len(s1)):
            if s1[i]!=s2[i]:
                if i%2==0:
                    ans.append("DIFFERENT")
                    flag=1
                    break
                else:
                    cnt+=1
            else: pass
        if flag==1:
            continue
        else: pass
        if cnt==0:
            ans.append("IDENTICAL")
        elif cnt==1:
            ans.append("CLOSE")
        else: 
            ans.append("DIFFERENT")

for i in range(len(ans)):
    print(ans[i])
Problem: p03558
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from collections import deque
k=int(input())
t=[0]*k
t[1]=1
q=deque()
q.append(1)
while q:
    p=q.popleft()
    if p==0:
        print(t[0])
        break
    if not t[(p*10)%k]:
        t[(p*10)%k]=t[p]
        q.appendleft((p*10)%k)
    if not t[(p+1)%k]:
        t[(p+1)%k]=t[p]+1
        q.append((p+1)%k)

Failing test case input:
3197

Expected output:
2


Actual output:
4


Fixed submission (verdict: Accepted):
from collections import deque
k=int(input())
t=[1]*k
q=deque()
q.append((1,1))
while q:
    p,r=q.popleft()
    if p==0:
        print(r)
        break
    if t[p]:
        t[p]=0
        q.appendleft(((p*10)%k,r))
        q.append(((p+1)%k,r+1))

Problem: p03558
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from collections import deque


def solve(K):
    s = 1
    q = deque()
    q.append(s)
    dists = {}
    dists[s] = 1
    while len(q) > 0:
        n = q.pop()
        if n == 0:
            break
        m = (n*10)%K
        if m not in dists:
            dists[m] = dists[n]
            q.append(m)
        if dists[m] > dists[n]:
            dists[m] = dists[n]
        if n % 10 != 9:
            m = (n+1)%K
            if m not in dists:
                dists[m] = dists[n] + 1
                q.appendleft(m)
            if dists[m] > dists[n] + 1:
                dists[m] = dists[n] + 1
    return dists[0]


def main():
    K = int(input())
    print(solve(K))


if __name__ == '__main__':
    main()


Failing test case input:
1246

Expected output:
3


Actual output:
4


Fixed submission (verdict: Accepted):
from collections import deque


def solve(K):
    s = 1
    q = deque()
    q.append(s)
    dists = {}
    dists[s] = 1
    while len(q) > 0:
        n = q.pop()
        if n == 0:
            break
        m = (n*10)%K
        if m not in dists:
            dists[m] = dists[n]
            q.append(m)
        elif dists[m] > dists[n]:
            dists[m] = dists[n]
            q.append(m)
        if n % 10 != 9:
            m = (n+1)%K
            if m not in dists:
                dists[m] = dists[n] + 1
                q.appendleft(m)
            elif dists[m] > dists[n] + 1:
                dists[m] = dists[n] + 1
                q.appendleft(m)
    return dists[0]


def main():
    K = int(input())
    print(solve(K))


if __name__ == '__main__':
    main()
Problem: p03558
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):











import sys
input=sys.stdin.readline
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [input() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    from collections import deque
    K = I()
    edge = [[]for _ in range(K)]
    for i in range(K):
        edge[i].append((i * 10) % K)
        edge[i].append((i + 1)%K)
        
    zero_one_deque = deque([1])
    node = [inf] * K
    node[1]=1
    for i in range(K+1):
        tmp = deque()
        while zero_one_deque:
            from_ = zero_one_deque.popleft()
            for i, to in enumerate(edge[from_]):
                if node[to] != inf:
                    continue
                node[to] = node[from_]
                if i == 0:
                    tmp.appendleft(to)
                else:   
                    node[to] += 1
                    tmp.append(to)
        zero_one_deque = tmp
    print(node[0])
                    

            
    
if __name__ == '__main__':
    main()

Failing test case input:
1246

Expected output:
3


Actual output:
6


Fixed submission (verdict: Accepted):











import sys
input=sys.stdin.readline
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [input() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    from collections import deque
    K = I()
    edge = [[]for _ in range(K)]
    for i in range(K):
        edge[i].append((i * 10) % K)
        edge[i].append((i + 1)%K)
        
    zero_one_deque = deque([(1, 1)])
    node = [inf] * K
    
    node[1] = 1
    searched = set()
    while zero_one_deque:
        from_ , digit_sum = zero_one_deque.popleft()
        if from_ in searched:
            continue
        else:
            searched.add(from_)
            node[from_] = digit_sum
            
        for i, to in enumerate(edge[from_]):
            if i == 0:
                zero_one_deque.appendleft((to,node[from_]))
            else:   
                zero_one_deque.append((to, node[from_] + 1))
    print(node[0])
                    

if __name__ == '__main__':
    main()
Problem: p03558
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from collections import deque
K = int(input())
E = [[((i + 1) % K, 1)] for i in range(K)]
for i in range(1, K):
	E[i].append((i * 10 % K, 0))
Q = deque([(1, 1)])
reached = set([1])
while 1:
	tmp_node, tmp_cost = Q.popleft()
	if tmp_node == 0:
		ans = tmp_cost
		break
	for nxt, dist in E[tmp_node]:
		if nxt in reached:
			continue
		reached.add(nxt)
		if dist == 0:
			Q.appendleft((nxt, tmp_cost))
		else:
			Q.append((nxt, tmp_cost + 1))
print(ans)


Failing test case input:
3197

Expected output:
2


Actual output:
4


Fixed submission (verdict: Accepted):
from collections import deque
K = int(input())
E = [[((i + 1) % K, 1)] for i in range(K)]
for i in range(1, K):
	E[i].append((i * 10 % K, 0))
Q = deque([(1, 1)])
reached = set()
while 1:
	tmp_node, tmp_cost = Q.popleft()
	if tmp_node == 0:
		ans = tmp_cost
		break
	if tmp_node in reached:
		continue
	reached.add(tmp_node)
	for nxt, dist in E[tmp_node]:
		if dist == 0:
			Q.appendleft((nxt, tmp_cost))
		else:
			Q.append((nxt, tmp_cost + 1))
print(ans)

Problem: p03558
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from collections import deque
K = int(input())
V = list(range(K))
E = [[(i*10)%K,(i+1)%K] for i in range(K)] 
data = [-1] * (K)
data[1] = 1
q = deque([1])
i = 1
while i != 0:
    i = q.popleft()
    if data[E[i][0]] == -1:
        q.appendleft(E[i][0])
        data[E[i][0]] = data[i]
    if data[E[i][1]] == -1:
        q.append(E[i][1])
        data[E[i][1]] = data[i]+1
print(data[i])

Failing test case input:
551

Expected output:
3


Actual output:
4


Fixed submission (verdict: Accepted):
from collections import deque
K = int(input())
E = [[(i*10)%K,(i+1)%K] for i in range(K)] 
INF = K*10
V = [INF] * (K)
V[1] = 1
q = deque([1])
i = 1
while q:
    i = q.popleft()
    if V[E[i][0]] > V[i]:
        q.appendleft(E[i][0])
        V[E[i][0]] = V[i]
    if V[E[i][1]] > V[i]+1:
        q.append(E[i][1])
        V[E[i][1]] = V[i]+1
print(V[0])

Problem: p03558
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from collections import deque
K = int(input())
dist = {1: 1}

deq = deque([1])
while deq:
    v = deq.popleft()
    
    w = (10 * v) % K
    if w not in dist:
        dist[w] = dist[v]
        deq.appendleft(w)
    
    w = (v + 1) % K
    if w not in dist:
        dist[w] = dist[v] + 1
        deq.append(w)
print(dist[0])


Failing test case input:
551

Expected output:
3


Actual output:
4


Fixed submission (verdict: Accepted):
from collections import deque
K = int(input())
dist = {1: 1}

deq = deque([1])
while deq:
    v = deq.popleft()
    
    w = (10 * v) % K
    if dist[v] < dist.get(w, 10**9):
        dist[w] = dist[v]
        deq.appendleft(w)
    
    w = (v + 1) % K
    if dist[v] + 1 < dist.get(w, 10**9):
        dist[w] = dist[v] + 1
        deq.append(w)
print(dist[0])
Problem: p03558
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):











import sys
input=sys.stdin.readline
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [input() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    from collections import deque
    K = I()
    edge = [[]for _ in range(K)]
    for i in range(K):
        edge[i].append((i * 10) % K)
        edge[i].append((i + 1)%K)
        
    zero_one_deque = deque([1])
    node = [inf] * K
    
    node[1] = 1
    while zero_one_deque:
        from_ = zero_one_deque.popleft()
        for i, to in enumerate(edge[from_]):
            if node[to] != inf:
                continue
            node[to] = node[from_]
            if i == 0:
                zero_one_deque.appendleft(to)
            else:   
                node[to] += 1
                zero_one_deque.append(to)
    print(node[0])
                    

if __name__ == '__main__':
    main()

Failing test case input:
1246

Expected output:
3


Actual output:
4


Fixed submission (verdict: Accepted):











import sys
input=sys.stdin.readline
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [input() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    from collections import deque
    K = I()
    edge = [[]for _ in range(K)]
    for i in range(K):
        edge[i].append((i * 10) % K)
        edge[i].append((i + 1)%K)
        
    zero_one_deque = deque([(1, 1)])
    node = [inf] * K
    
    node[1] = 1
    searched = set()
    while zero_one_deque:
        from_ , digit_sum = zero_one_deque.popleft()
        if from_ in searched:
            continue
        else:
            searched.add(from_)
            node[from_] = digit_sum
            
        for i, to in enumerate(edge[from_]):
            if i == 0:
                zero_one_deque.appendleft((to,node[from_]))
            else:   
                zero_one_deque.append((to, node[from_] + 1))
    print(node[0])
                    

if __name__ == '__main__':
    main()
Problem: p03558
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from collections import deque
n = int(input())
mins = [100 for i in range(n+1)]
used = [0 for i in range(n+1)]
deq = deque()
deq.append((1,1))
while deq:
  x,cnt = deq.popleft()
  mins[x] = cnt
  used[x] = 1
  if used[10*x%n] == 0:
    deq.appendleft((10*x%n,cnt))
  if used[(x+1)%n] == 0:
    deq.append(((x+1)%n,cnt+1))
print(mins[0])

Failing test case input:
551

Expected output:
3


Actual output:
4


Fixed submission (verdict: Accepted):
from collections import deque
n = int(input())
mins = [1000 for i in range(n+1)]
used = [0 for i in range(n+1)]
deq = deque()
deq.append((1,1))
while deq:
  x,cnt = deq.popleft()
  mins[x] = min(mins[x],cnt)
  used[x] = 1
  if used[10*x%n] == 0:
    deq.appendleft((10*x%n,cnt))
  if used[(x+1)%n] == 0:
    deq.append(((x+1)%n,cnt+1))
print(mins[0])

Problem: p03558
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
K = int(input())

L = {i: -1 for i in range(K)}
L[1] = 1
biq = [1]

while len(biq) > 0:
    i = biq.pop(0)
    j = (i * 10) % K
    if L[j] < 0:
        L[j] = L[i]
        biq.insert(0, j)
    k = 0 if i == K - 1 else i + 1
    if L[k] < 0:
        L[k] = L[i] + 1
        biq.insert(-1, k)

print(L[0])


Failing test case input:
9736

Expected output:
2


Actual output:
3


Fixed submission (verdict: Accepted):
from collections import deque
K = int(input())

L = {i: 10**6 for i in range(K)}
L[1] = 1
biq = deque()
biq.append(1)

while len(biq) > 0:
    i = biq.popleft()
    if i == 0:
        break
    j = (i * 10) % K
    if L[j] > L[i]:
        L[j] = L[i]
        biq.appendleft(j)
    k = 0 if i == K - 1 else i + 1
    if L[k] > L[i]:
        L[k] = L[i] + 1
        biq.append(k)

print(L[0])

Problem: p03558
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from collections import defaultdict, deque

INF = float('inf')
K = int(input())
G = defaultdict(list)

for v in range(K):
    G[v].append((0, (v * 10) % K))
    G[v].append((1, (v + 1) % K))

def zero_one_bfs_shortest_path(s):
    d = [INF] * K
    d[s] = 0
    dq = deque()
    dq.appendleft(s)
    while len(dq) != 0:
        u = dq.popleft()
        for w, v in G[u]:
            if d[v] != INF:continue
            d[v] = d[u] + w
            if w == 0: dq.appendleft(v)
            else: dq.append(v)
    return d

print(zero_one_bfs_shortest_path(1)[0] + 1)

Failing test case input:
379

Expected output:
2


Actual output:
5


Fixed submission (verdict: Accepted):
from collections import defaultdict, deque

INF = float('inf')
K = int(input())

def zero_one_bfs_shortest_path(s, K):
    G = defaultdict(list)
    for v in range(K):
        G[v].append((0, (v * 10) % K))
        G[v].append((1, (v + 1) % K))
    d = [INF] * K
    d[s] = 0
    dq = deque()
    dq.appendleft(s)
    while len(dq) != 0:
        u = dq.popleft()
        for w, v in G[u]:
            if d[v] > d[u] + w:
                d[v] = d[u] + w
                if w == 0: dq.appendleft(v)
                else: dq.append(v)
    return d

print(zero_one_bfs_shortest_path(1, K)[0] + 1)
Problem: p02589
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):

import sys
input = sys.stdin.readline

class RollingHash():
    def __init__(self, s):
        length = len(s)
        base = 1009
        self.mod = (1 << 61) - 1
        self.hash = [0] * (length + 2)
        self.pow = [1] * (length + 2)

        for i in range(length):
            self.hash[i+1] = (self.hash[i] + s[i]) * base % self.mod
            self.pow[i+1] = self.pow[i] * base % self.mod
        self.pow[length+1] = self.pow[length] * base % self.mod 

    def get(self, l, r):
        t = self.hash[r] - self.hash[l] * self.pow[r-l] % self.mod
        return t % self.mod

ss = []
orda = ord("a")
n = int(input())
for _ in range(n):
    ss.append([ord(item) - orda for item in input().rstrip()])
ss.sort(key=lambda x: len(x), reverse=True)

words = dict()
ans = 0
for s in ss:
    word_len = len(s)
    s_rev = s[::-1]
    RH = RollingHash(s_rev)
    rh = RH.get(0, word_len-1)
    if rh in words:
        ans += words[rh][s_rev[-1]]
    initial = set()
    for i, ch in enumerate(s):
        initial.add(ch)
        for key in initial:
            rh = RH.get(0, word_len-i-1)
            if rh in words:
                words[rh][key] += 1
            else:
                words[rh] = [0] * 26
                words[rh][key] += 1
print(ans)

Failing test case input:
3
ayxyad
byw
baa

Expected output:
0


Actual output:
1


Fixed submission (verdict: Accepted):

import sys
input = sys.stdin.readline
from collections import defaultdict

def empty_list():
    return [0] * 26

class RollingHash():
    def __init__(self, s):
        length = len(s)
        base = 1009
        
        self.mod = 67280421310721
        self.hash = [0] * (length + 2)
        self.pow = [1] * (length + 2)

        for i in range(length):
            self.hash[i+1] = (self.hash[i] + s[i]) * base % self.mod
            self.pow[i+1] = self.pow[i] * base % self.mod
        self.pow[length+1] = self.pow[length] * base % self.mod 

    def get(self, l, r):
        t = self.hash[r] - self.hash[l] * self.pow[r-l] % self.mod
        return t % self.mod

ss = []
n = int(input())
for _ in range(n):
    ss.append([ord(item) for item in input().rstrip()])
ss.sort(key=lambda x: len(x), reverse=True)

orda = ord("a")
words = defaultdict(empty_list)
ans = 0
for s in ss:
    word_len = len(s)
    s_rev = s[::-1]
    RH = RollingHash(s_rev)
    rh = RH.get(0, word_len-1)
    ans += words[rh][s_rev[-1] - orda]
    initial = [0] * 26 
    for i, ch in enumerate(s):
        initial[ch - orda] = 1
        for j, is_initial in enumerate(initial):
            if is_initial:
                rh = RH.get(0, word_len-i-1)
                words[rh][j] += 1
print(ans)
Problem: p02589
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
import sys
input = lambda: sys.stdin.readline().rstrip()
from random import randint
mod=1370757747362922367
rr=randint(2,mod-2)
n=int(input())
ss=[input()[::-1]for _ in range(n)]
ss.sort(key=len)
d={}
ans=0
for s in ss:
  a=ord(s[-1])-97
  r=1
  h=0
  y=[0]*26
  for t in s[:-1]:
    b=ord(t)-97
    if h in d:
      for i in range(26):y[i]+=d[h][i]
    ans+=y[b]
    y[b]=0
    h=(h+b*r)%mod
    r=r*rr%mod
  ans+=y[a]
  if h not in d:d[h]=[0]*26
  d[h][a]+=1
print(ans)

Failing test case input:
3
dabxxy
czw
baa

Expected output:
0


Actual output:
1


Fixed submission (verdict: Accepted):
import sys
input = lambda: sys.stdin.readline().rstrip()
m=2**150-1
n=int(input())
ss=[input()[::-1]for _ in range(n)]
ss.sort(key=len)
d={}
ans=0
for s in ss:
  a=ord(s[-1])-97
  r=1
  h=0
  y=[0]*26
  for t in s[:-1]:
    b=ord(t)-97
    if h in d:
      for i in range(26):y[i]+=d[h][i]
    ans+=y[b]
    y[b]=0
    h=(h*71+b+135)&m
  ans+=y[a]
  if h not in d:d[h]=[0]*26
  d[h][a]+=1
print(ans)

Problem: p02589
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
import sys
input = sys.stdin.readline

base = 1999849216943526259
mod = (1 << 61) - 1

N = int(input())
S = [input().rstrip()[::-1] for _ in range(N)]
S.sort(key=lambda s: -len(s))
cnt = {}
ans = 0
one = [0] * 26
for s in S:
    n = len(s)
    if n == 1:
        ans += one[ord(s[0]) - ord('a')]
        continue
    hashes = [0] * (n - 1)
    hashes[0] = ord(s[0]) - ord('a')
    for i in range(1, n - 1):
        hashes[i] = (hashes[i-1] * base + ord(s[i]) - ord('a')) % mod
    if hashes[-1] in cnt:
        ans += cnt[hashes[-1]][ord(s[-1]) - ord('a')]
    found = [0] * 26
    found[ord(s[-1]) - ord('a')] = 1
    for i in range(n - 2, 0, -1):
        found[ord(s[i]) - ord('a')] = 1
        if hashes[i-1] not in cnt:
            cnt[hashes[i-1]] = [0] * 26
        for c in range(26):
            cnt[hashes[i-1]][c] += found[c]
    found[ord(s[0]) - ord('a')] = 1
    for c in range(26):
        one[c] += found[c]
print(ans)


Failing test case input:
3
dxyayb
ycw
aba

Expected output:
0


Actual output:
1


Fixed submission (verdict: Accepted):
import sys
input = sys.stdin.readline

base = 1999849216943526259
mod = (1 << 61) - 1

N = int(input())
S = [input().rstrip()[::-1] for _ in range(N)]
S.sort(key=lambda s: -len(s))
cnt = {}
ans = 0
one = [0] * 26
for s in S:
    n = len(s)
    if n == 1:
        ans += one[ord(s[0]) - ord('a')]
        continue
    hashes = [0] * (n - 1)
    hashes[0] = ord(s[0])
    for i in range(1, n - 1):
        hashes[i] = (hashes[i-1] * base + ord(s[i])) % mod
    if hashes[-1] in cnt:
        ans += cnt[hashes[-1]][ord(s[-1]) - ord('a')]
    found = [0] * 26
    found[ord(s[-1]) - ord('a')] = 1
    for i in range(n - 2, 0, -1):
        found[ord(s[i]) - ord('a')] = 1
        if hashes[i-1] not in cnt:
            cnt[hashes[i-1]] = [0] * 26
        for c in range(26):
            cnt[hashes[i-1]][c] += found[c]
    found[ord(s[0]) - ord('a')] = 1
    for c in range(26):
        one[c] += found[c]
print(ans)
Problem: p02589
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
import sys
input = sys.stdin.readline

base = 100
mod = (1 << 61) - 1

N = int(input())
S = [input().rstrip()[::-1] for _ in range(N)]
S.sort(key=lambda s: -len(s))
cnt = {}
ans = 0
one = [0] * 26
for s in S:
    n = len(s)
    if n == 1:
        ans += one[ord(s[0]) - ord('a')]
        continue
    hashes = [0] * (n - 1)
    hashes[0] = ord(s[0]) - ord('a')
    for i in range(1, n - 1):
        hashes[i] = (hashes[i-1] * base + ord(s[i]) - ord('a')) % mod
    if hashes[-1] in cnt:
        ans += cnt[hashes[-1]][ord(s[-1]) - ord('a')]
    found = [0] * 26
    found[ord(s[-1]) - ord('a')] = 1
    for i in range(n - 2, 0, -1):
        found[ord(s[i]) - ord('a')] = 1
        if hashes[i-1] not in cnt:
            cnt[hashes[i-1]] = [0] * 26
        for c in range(26):
            cnt[hashes[i-1]][c] += found[c]
    found[ord(s[0]) - ord('a')] = 1
    for c in range(26):
        one[c] += found[c]
print(ans)


Failing test case input:
3
dayxyb
ycw
aba

Expected output:
0


Actual output:
1


Fixed submission (verdict: Accepted):
import sys
input = sys.stdin.readline

base = 1999849216943526259
mod = (1 << 61) - 1

N = int(input())
S = [input().rstrip()[::-1] for _ in range(N)]
S.sort(key=lambda s: -len(s))
cnt = {}
ans = 0
one = [0] * 26
for s in S:
    n = len(s)
    if n == 1:
        ans += one[ord(s[0]) - ord('a')]
        continue
    hashes = [0] * (n - 1)
    hashes[0] = ord(s[0])
    for i in range(1, n - 1):
        hashes[i] = (hashes[i-1] * base + ord(s[i])) % mod
    if hashes[-1] in cnt:
        ans += cnt[hashes[-1]][ord(s[-1]) - ord('a')]
    found = [0] * 26
    found[ord(s[-1]) - ord('a')] = 1
    for i in range(n - 2, 0, -1):
        found[ord(s[i]) - ord('a')] = 1
        if hashes[i-1] not in cnt:
            cnt[hashes[i-1]] = [0] * 26
        for c in range(26):
            cnt[hashes[i-1]][c] += found[c]
    found[ord(s[0]) - ord('a')] = 1
    for c in range(26):
        one[c] += found[c]
print(ans)
Problem: p02589
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
import sys
input = sys.stdin.readline
from collections import defaultdict
N = int(input())
T = [input()[::-1].strip() for i in range(N)]






class TrieTree:
    class Node:
        def __init__(self):
            self.ch = defaultdict(TrieTree.Node)
            self.cnt = 0
            self.end = False

    def __init__(self):
        self.root = self.Node()

    def register(self,word):
        node = self.root
        for w in word:
            node.cnt += 1
            node = node.ch[w]
        node.cnt += 1
        node.end = True
    
    def prefix(self, word):
        node = self.root
        for w in word:
            if w not in node.ch:
                return False
            else:
                node = node.ch[w]
        return node

tt = TrieTree()
T = sorted(T,reverse=True)
ans = 0
for word in T:
    tt.register(word)
    pre = word[:-1]
    last = word[-1]
    node = tt.prefix(pre)
    if not node:
        continue
    df = [node]
    while df:
        node = df.pop()
        for c, nnode in node.ch.items():
            if c == last:
                ans += node.ch[last].cnt
            else:
                df.append(nnode)
    ans -= 1
    
print(ans)


Failing test case input:
3
xbcayx
cyx
bba

Expected output:
1


Actual output:
0


Fixed submission (verdict: Accepted):
import sys
import string
input = sys.stdin.readline
from collections import defaultdict
N = int(input())
T = [input()[::-1].strip() for i in range(N)]







class TrieTree:
    class Node:
        def __init__(self):
            
            self.clue = [0]*26
            self.ch = defaultdict(TrieTree.Node)
            self.cnt = 0
            self.end = False

    def __init__(self):
        self.root = self.Node()

    def register(self,word):
        node = self.root
        for w in word:
            node.cnt += 1
            node = node.ch[w]
        node.cnt += 1
        node.end = True
    
    def prefix(self, word):
        node = self.root
        for w in word:
            if w not in node.ch:
                return False
            else:
                node = node.ch[w]
        return node

tt = TrieTree()
for word in T:
    tt.register(word)

route = []

st = [tt.root]
while st:
    node = st.pop()
    route.append(node)
    st.extend(node.ch.values())

f = lambda c : ord(c)-97
for node in route[::-1]:
    for c,nnode in node.ch.items():
        for i,v in enumerate(nnode.clue):
            node.clue[i] += v
        node.clue[f(c)] += nnode.cnt - nnode.clue[f(c)]

ans = 0
for word in T:
    pre = word[:-1]
    last = word[-1]
    node = tt.prefix(pre)
    if not node:
        continue
    ans += node.clue[f(last)] - 1
    
print(ans)
Problem: p02589
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
import sys
input = lambda: sys.stdin.readline().rstrip()
P = 10 ** 30 + 57
N = int(input())
X = sorted([input()[::-1] for _ in range(N)], key = len)

D = {}
ans = 0
for x in X:
    L = [0] * 26
    T, a = x[:-1], ord(x[-1]) - 97
    h = 0
    for t in T:
        if h in D:
            for i in range(26):
                L[i] += D[h][i]
        b = ord(t) - 97
        ans += L[b]
        L[b] = 0
        
        h = (h + b) * 7 % P
    ans += L[a]
    if h in D:
        D[h][a] += 1
    else:
        D[h] = [0] * 26
        D[h][a] = 1
    
print(ans)

Failing test case input:
3
dayxyb
ycw
baa

Expected output:
0


Actual output:
1


Fixed submission (verdict: Accepted):
import sys
input = lambda: sys.stdin.readline().rstrip()
P = 10 ** 40 + 121
N = int(input())
X = sorted([input()[::-1] for _ in range(N)], key = len)

D = {}
ans = 0
for x in X:
    L = [0] * 26
    T, a = x[:-1], ord(x[-1]) - 97
    h = 0
    for t in T:
        if h in D:
            for i in range(26):
                L[i] += D[h][i]
        b = ord(t) - 97
        ans += L[b]
        L[b] = 0
        
        h = (h + b + 135) * 71 % P
    ans += L[a]
    if h in D:
        D[h][a] += 1
    else:
        D[h] = [0] * 26
        D[h][a] = 1
    
print(ans)
Problem: p02589
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
import sys
input = sys.stdin.readline

base = 26
mod = (1 << 61) - 1

N = int(input())
S = [input().rstrip()[::-1] for _ in range(N)]
S.sort(key=lambda s: -len(s))
cnt = {}
ans = 0
one = [0] * 26
for s in S:
    n = len(s)
    if n == 1:
        ans += one[ord(s[0]) - ord('a')]
        continue
    hashes = [0] * (n - 1)
    hashes[0] = ord(s[0]) - ord('a')
    for i in range(1, n - 1):
        hashes[i] = (hashes[i-1] * base + ord(s[i]) - ord('a')) % mod
    if hashes[-1] in cnt:
        ans += cnt[hashes[-1]][ord(s[-1]) - ord('a')]
    found = [0] * 26
    found[ord(s[-1]) - ord('a')] = 1
    for i in range(n - 2, 0, -1):
        found[ord(s[i]) - ord('a')] = 1
        if hashes[i-1] not in cnt:
            cnt[hashes[i-1]] = [0] * 26
        for c in range(26):
            cnt[hashes[i-1]][c] += found[c]
    found[ord(s[0]) - ord('a')] = 1
    for c in range(26):
        one[c] += found[c]
print(ans)


Failing test case input:
3
cybxxa
cyw
baa

Expected output:
0


Actual output:
1


Fixed submission (verdict: Accepted):
import sys
input = sys.stdin.readline

base = 1999849216943526259
mod = (1 << 61) - 1

N = int(input())
S = [input().rstrip()[::-1] for _ in range(N)]
S.sort(key=lambda s: -len(s))
cnt = {}
ans = 0
one = [0] * 26
for s in S:
    n = len(s)
    if n == 1:
        ans += one[ord(s[0]) - ord('a')]
        continue
    hashes = [0] * (n - 1)
    hashes[0] = ord(s[0])
    for i in range(1, n - 1):
        hashes[i] = (hashes[i-1] * base + ord(s[i])) % mod
    if hashes[-1] in cnt:
        ans += cnt[hashes[-1]][ord(s[-1]) - ord('a')]
    found = [0] * 26
    found[ord(s[-1]) - ord('a')] = 1
    for i in range(n - 2, 0, -1):
        found[ord(s[i]) - ord('a')] = 1
        if hashes[i-1] not in cnt:
            cnt[hashes[i-1]] = [0] * 26
        for c in range(26):
            cnt[hashes[i-1]][c] += found[c]
    found[ord(s[0]) - ord('a')] = 1
    for c in range(26):
        one[c] += found[c]
print(ans)
Problem: p02589
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
import sys;input=sys.stdin.readline
N, = map(int, input().split())
MOD=998244353
S = []
for _ in range(N):
    s = input().strip()
    S.append(s)
S.sort(key=lambda x:len(x))
D = dict()
dd = set()
R = 0
for s in S:
    l = len(s)
    d = [0]*26
    for c in dd:
        d[c] += 1
    tmp=0
    for i in range(l-1, -1, -1):
        cc = ord(s[i]) - 97
        tmp=(100*tmp+cc)%MOD
        R += d[cc]
        d[cc] = 0
        if tmp in D:
            for c in D[tmp]:
                d[c] += 1
        if i == 1:
            nn = tmp
    if l != 1:
        if nn not in D:
            D[nn] = set()
        D[nn].add(cc)
    else:
        dd.add(cc)
print(R)


Failing test case input:
3
dayxyb
ycw
aba

Expected output:
0


Actual output:
1


Fixed submission (verdict: Accepted):
import sys;input=sys.stdin.readline
N, = map(int, input().split())
MOD=67280421310721
S = []
for _ in range(N):
    s = input().strip()
    S.append(s)
S.sort(key=lambda x:len(x))
D = dict()
dd = set()
R = 0
for s in S:
    l = len(s)
    d = [0]*26
    for c in dd:
        d[c] += 1
    tmp=0
    for i in range(l-1, -1, -1):
        ee = ord(s[i]) - 97
        tmp = 100*tmp+(ee+1)
        tmp %= MOD
        R += d[ee]
        d[ee] = 0
        if tmp in D:
            for c in D[tmp]:
                d[c] += 1
        if i == 1:
            nn = tmp
    if l != 1:
        if nn not in D:
            D[nn] = set()
        D[nn].add(ee)
    else:
        dd.add(ee)
print(R)

Problem: p02589
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
import sys
input = lambda: sys.stdin.readline().rstrip()
P = 10 ** 9 + 7
N = int(input())
X = sorted([input()[::-1] for _ in range(N)], key = len)

D = {}
ans = 0
for x in X:
    L = [0] * 26
    T, a = x[:-1], ord(x[-1]) - 97
    h = 0
    for t in T:
        if h in D:
            for i in range(26):
                L[i] += D[h][i]
        b = ord(t) - 97
        ans += L[b]
        L[b] = 0
        
        h = (h + b) * 7 % P
    ans += L[a]
    if h in D:
        D[h][a] += 1
    else:
        D[h] = [0] * 26
        D[h][a] = 1
    
print(ans)

Failing test case input:
3
dabxxy
czw
baa

Expected output:
0


Actual output:
1


Fixed submission (verdict: Accepted):
import sys
input = lambda: sys.stdin.readline().rstrip()
P = 10 ** 40 + 121
N = int(input())
X = sorted([input()[::-1] for _ in range(N)], key = len)

D = {}
ans = 0
for x in X:
    L = [0] * 26
    T, a = x[:-1], ord(x[-1]) - 97
    h = 0
    for t in T:
        if h in D:
            for i in range(26):
                L[i] += D[h][i]
        b = ord(t) - 97
        ans += L[b]
        L[b] = 0
        
        h = (h + b + 135) * 71 % P
    ans += L[a]
    if h in D:
        D[h][a] += 1
    else:
        D[h] = [0] * 26
        D[h][a] = 1
    
print(ans)