Problem p02937 (python)

Fixed submission:
s=input()
t=input()

pos=[[] for _ in range(26)]

len_s=len(s)
len_t=len(t)


for i in range(len_s):
    idx=ord(s[i])-97
    pos[idx].append(i)

ans=[]
tmp=-1
cnt=0
for i in range(len_t):
    idx=ord(t[i])-97
    if len(pos[idx])==0:
        print(-1)
        exit()

    if tmp<pos[idx][-1]:
        l=-1
        r = len(pos[idx]) - 1
        while l + 1 < r:
            mid = (l + r) // 2
            if pos[idx][mid] <= tmp:
                l = mid
            else:
                r=mid
        ans.append(pos[idx][r])
        tmp=pos[idx][r]
    else:
        cnt+=1
        ans.append(pos[idx][0])
        tmp=pos[idx][0]


print(cnt*len_s+ans[-1]+1)

Buggy submission (verdict: Runtime Error):
s=input()
t=input()

pos=[[] for _ in range(26)]

len_s=len(s)
len_t=len(t)


for i in range(len_s):
    idx=ord(s[i])-97
    pos[idx].append(i)

ans=[]
tmp=0
for i in range(len_t):
    idx=ord(t[i])-97
    if len(pos[idx])==0:
        print(-1)
        exit()
    if tmp>pos[idx][-1]:
        ans.append(pos[idx][0])
        tmp=pos[idx][0]
    elif tmp<=pos[idx][0]:
        ans.append(pos[idx][0])
        tmp=pos[idx][0]
    else:
        l=0
        r=len(pos[idx])-1
        while l+1<r:
            mid=(l+r)//2
            if pos[idx][mid]<=ans:
                l=mid
            else:
                r=mid
        ans.append(pos[idx][r])
        tmp=pos[idx][r]
cnt=0
for i in range(1,len_t):
    if ans[i-1]>=ans[i]:
        cnt+=1

print(cnt*len_s+ans[-1]+1)

Buggy submission (verdict: Runtime Error):
s=input()
t=list(input())

from collections import Counter

S=Counter(s)
T=Counter(t)

for a in T.keys():
    if not a in S.keys():
        print(-1)
        exit()

Ls=len(s)
Lt=len(t)

from collections import deque

abc="abcdefghjijklmnopqrstuvwxyz"
pos=[[] for _ in range(26)]
for i in range(Ls):
    ch=s[i]
    idx=abc.index(ch)
    pos[idx].append(i)

Ns=[]
now=0
for i in range(Lt):
    ch=t[i]
    idx=abc.index(ch)
    if pos[idx][-1]<now:
        Ns.append(pos[idx][0])
        now=pos[idx][0]
        continue
    l=0
    r=len(pos[idx])
    while l+1<r:
        mid=(l+r)//2
        if pos[idx][mid]<=now:
            l=mid
        else:
            r=mid
    Ns.append(pos[idx][l])
    now=pos[idx][l]

cnt=0
for i in range(1,Lt):
    if Ns[i-1]>=Ns[i]:
        cnt+=1
print(cnt*Ls+Ns[-1]+1)

Buggy submission (verdict: Runtime Error):
s=input()
t=list(input())

from collections import Counter

S=Counter(s)
T=Counter(t)

for a in T.keys():
    if not a in S.keys():
        print(-1)
        exit()

Ls=len(s)
Lt=len(t)

from collections import deque

abc="abcdefghjijklmnopqrstuvwxyz"
pos=[[] for _ in range(26)]
for i in range(Ls):
    ch=s[i]
    idx=abc.index(ch)
    pos[idx].append(i)

Ns=[]
now=0
for i in range(Lt):
    ch=t[i]
    idx=abc.index(ch)
    if pos[idx][-1]<now:
        Ns.append(pos[idx][0])
        now=pos[idx][0]
        continue
    l=-1
    r=len(pos[idx])
    while l+1<r:
        mid=(l+r)//2
        if pos[idx][mid]<now:
            l=mid
        else:
            r=mid
    Ns.append(pos[idx][r])
    now=pos[idx][r]

cnt=0
for i in range(1,Lt):
    if Ns[i-1]>=Ns[i]:
        cnt+=1
print(cnt*Ls+Ns[-1]+1)