Problem:


Score : 500 points

Problem Statement

Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists.

Let s' be the concatenation of 10^{100} copies of s. t is a subsequence of the string {s'}_1{s'}_2\ldots{s'}_i (the first i characters in s').

Notes

A subsequence of a string a is a string obtained by deleting zero or more characters from a and concatenating the remaining characters without changing the relative order. For example, the subsequences of contest include net, c, and contest.

Constraints

1 \leq |s| \leq 10^5

1 \leq |t| \leq 10^5

s and t consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s
t

Output

If there exists an integer i satisfying the following condition, print the minimum such i; otherwise, print -1.

Sample Input 1

contest
son

Sample Output 1

10

t = son is a subsequence of the string contestcon (the first 10 characters in s' = contestcontestcontest...), so i = 10 satisfies the condition.

On the other hand, t is not a subsequence of the string contestco (the first 9 characters in s'), so i = 9 does not satisfy the condition.

Similarly, any integer less than 9 does not satisfy the condition, either. Thus, the minimum integer i satisfying the condition is 10.

Sample Input 2

contest
programming

Sample Output 2

-1

t = programming is not a substring of s' = contestcontestcontest.... Thus, there is no integer i satisfying the condition.

Sample Input 3

contest
sentence

Sample Output 3

33

Note that the answer may not fit into a 32-bit integer type, though we cannot put such a case here.



Sample input:
contest
son


Sample output:
10

Submission status: Time Limit Exceeded

Solution code:
S = list(map(lambda x : ord(x)-ord('a'), input().strip()))
T = list(map(lambda x : ord(x)-ord('a'), input().strip()))

L = len(S)

occ = [[] for i in range(26)]
for i, c in enumerate(S):
    occ[c].append(i)

for c in T:
    if occ[c] == []:
        print(-1)
        quit()

total = 0
cur = -1

def getLowest(bound, lo, hi, lst):
    # Return index of lowest element in list greater than bound
    if lo >= hi:
        if lst[lo] > bound:
            return lo
        else:
            return 0
        
    mid = lo + (hi - lo) // 2
    if lst[mid] > bound:
        return getLowest(bound, lo, mid, lst)
    else:
        return getLowest(bound, mid+1, hi, lst)

for i, char in enumerate(T):
    newpos = occ[char][getLowest(cur, 0, len(occ[char])-1, occ[char])]
    if cur == newpos:
        total += L
    elif newpos < cur:
        total += L - cur + newpos
    else:
        total += newpos - cur
    cur = newpos
print(total)

Submission status: Time Limit Exceeded

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

x = [[] for _ in range(0,26)]
lenS = len(s)

for i in range(lenS):
  x[ord(s[i]) - 97].append(i)

cnt = 0
cnt2 = 0
idx = -1
while len(t) > 0:  
  if len(x[ord(t[0]) - 97]) == 0:
    cnt = -1 
    break
    
  flag = True
  for xx in x[ord(t[0]) - 97]:
    if xx > idx:
      cnt += xx - idx
      idx = xx
      flag = False
      t = t[1:len(t)]
      break
  if flag:
    cnt2 += 1
    cnt = cnt2*lenS
    idx = -1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import collections

def main():
    s=input()
    t=input()
    n = len(s)
    cs = collections.Counter(s)
    ct = collections.Counter(t)

    for sct in ct.keys():
        if cs[sct] == 0:
            return -1
    l = 0
    lc = 0
    for c in t:
        while c != s[l]:
            l += 1
            if l == n:
                l = 0
                lc += 1
        l += 1
        if l == n:
            l = 0
            lc += 1

    return lc * n + l

print(main())

Submission status: Time Limit Exceeded

Solution code:
s=input()
t=input()
T=list(t)

import sys
mojis=set(list(s))
mojit=set(list(t))
if sum([1 if i not in s else 0 for i in t])>=1:
    print(-1)
    sys.exit()

p=0
a=''
i=0
while p<len(t):
    a+=s[i]
    i+=1
    if i==len(s):
        i=0
    if s[i-1]==t[p]:
        p+=1

print(len(a))

Submission status: Time Limit Exceeded

Solution code:
import bisect
S=list(input())
T=list(input())
N=len(S)
TI={}
for i in range(N):
  s=S[i]
  if s in set(T):
    if s in TI:
      TI[s].append(i)
    else:
      TI[s]=[i]

i=-1
for t in T:
  if t not in TI:
    print(-1)
    exit()
  else:
    ni=TI[t][(bisect.bisect_right(TI[t],i%N))%len(TI[t])]
    if ni>i%N:
      i+=ni-i%N
    else:
      i+=N-i%N+ni
print(i+1)

Submission status: Time Limit Exceeded

Solution code:
import bisect
S=list(input())
T=list(input())
N=len(S)
TI={}
for i in range(N):
  s=S[i]
  if s in T:
    if s in TI:
      TI[s].append(i)
    else:
      TI[s]=[i]

i=-1
for t in T:
  if t not in TI:
    print(-1)
    exit()
  else:
    ni=TI[t][(bisect.bisect_right(TI[t],i%N))%len(TI[t])]
    if ni>i%N:
      i+=ni-i%N
    else:
      i+=N-i%N+ni
print(i+1)

Submission status: Time Limit Exceeded

Solution code:
def counter():
  s = input()
  t = input()
  N = len(s)
  if not set(t) <= set(s):
    return -1
  else:
    count = 0
    for tempstar in t:
      while True:
        if tempstar == s[count%N]:
          count +=1
          break
        count +=1
  return count

print(counter())

Submission status: Time Limit Exceeded

Solution code:
S=list(input())
T=list(input())
N=len(S)

i=-1
for t in T:
  if t in set(S[(i+1)%N:]):
    i+=S[(i+1)%N:].index(t)+1
  elif t in set(S[:(i+1)%N]):
    i+=N-1-i%N+S.index(t)+1
  else:
    print(-1)
    exit()

print(i+1)

Submission status: Time Limit Exceeded

Solution code:
S=list(input())
T=list(input())
N=len(S)

i=-1
for t in T:
  if t in set(S[(i+1)%N:]):
    i+=S[(i+1)%N:].index(t)+1
  elif t in set(S[:(i+1)%N]):
    i+=len(S[(i+1)%N:])+S.index(t)+1
  else:
    print(-1)
    exit()

print(i+1)

Submission status: Time Limit Exceeded

Solution code:
S=list(input())
T=list(input())
N=len(S)

i=-1
for t in T:
  if t in set(S[(i+1)%N:]):
    i+=S[(i+1)%N:].index(t)+1
  elif t in set(S[:(i+1)%N]):
    i+=len(S[(i+1)%N:])+S.index(t)+1
  else:
    print(-1)
    exit()
print(i+1)

Submission status: Time Limit Exceeded

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

ls=len(s)
lt=len(t)

for i in range(lt):
  if (t[i] in s) == False:
    print(-1)
    exit()

si,ti=0,0
while ti < lt:
  if s[si%ls]==t[ti]:
    ti += 1
  si += 1
print(si)

Submission status: Time Limit Exceeded

Solution code:
import sys
import copy
def input():
    return sys.stdin.readline()[:-1]
inf=float("inf")
s=input()
t=input()
dic={}
for i in range(len(s)):
    dic[s[i]]=1
for i in range(len(t)):
    if t[i] not in dic:
        print(-1)
        quit()

li=[{} for i in range(len(s)+1)] 

for i in range(len(s)):
    li[-2-i]=copy.deepcopy(li[-1-i])
    li[-2-i][s[-1-i]]=len(s)-i
# print(li)
ans=0
i=0
now=0
while i<len(t):
    if t[i] in li[now]:
        ans+=li[now][t[i]]-now
        now=li[now][t[i]]
        i+=1
        if now==len(s):
            now=0
    else:
        ans+=len(s)-now
        now=0
print(ans)

Submission status: Time Limit Exceeded

Solution code:

s=input()
t=input()
sl=list(set(s))
tl=list(set(t))
c=0
ch=0
sc=s
l=0
for i in tl:
    if(i not in sl):
        c=-1
        break
if(c==-1):
    print(c)
else:
    while True:
        for i in range(l,len(sc)):
            temp=t[ch]
            if(temp==sc[i]):
                ch+=1
            c+=1
            if(ch>=len(t)):
                print(c)
                break
        if(ch>=len(t)):
            break
        sc+=s
        l+=len(s)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
t = list(input())

f = True
for i in t:
    if(not i in s):
        f = False
i = 0
if(not f):
    print(-1)
else:
    for x in t:
         
        while(s[i % len(s)] != x):
            i+=1
        i+=1
    print(i)

Submission status: Time Limit Exceeded

Solution code:
s=input()
t=input()
sl=list(set(s))
tl=list(set(t))
c=0
ch=0
sc=s
l=0
for i in tl:
    if(i not in sl):
        c=-1
        break
if(c==-1):
    print(c)
else:
    while True:
        for i in range(l,len(sc)):
            temp=t[ch]
            if(temp==sc[i]):
                ch+=1
            c+=1
            if(ch>=len(t)):
                break
        if(ch>=len(t)):
            break
        sc+=s
        l+=len(s)
    print(c)

Submission status: Time Limit Exceeded

Solution code:
import sys
import queue
s = list(map(str,input()))
t = list(map(str,input()))

if len(set(s) & set(t)) < len(set(t)):
    print(-1)
    sys.exit()

q = queue.Queue()
for i in range(len(s)):
    q.put(s[i])
w = t[::-1]

ans = 0
while len(w) > 0:
    a = q.get()
    b = w[len(w)-1]
    if a == b:
        w.pop()
    q.put(a)
    ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import queue
s = list(map(str,input()))
t = list(map(str,input()))

if len(set(s) & set(t)) < len(set(t)):
    print(-1)
    sys.exit()

q = queue.Queue()
for i in range(len(s)):
    q.put(s[i])
w = t[::-1]

ans = 0
while len(w) > 0:
    a = q.get()
    b = w.pop()
    if a != b:
        w.append(b)
    q.put(a)
    ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = input()
    t = list(reversed(list(input())))

    for T in t:
        if T not in s:
            print(-1)
            return

    for loop in range(10**100):
        for idx in range(len(s)):
            if s[idx] == t[-1]:
                t.pop()
                if len(t) == 0:
                    print(idx+1+loop*len(s))
                    return

    print(-1)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import string

s = input()
t = input()

doubles = s + s

next_distance = [[-1 for i in range(30)] for j in range(len(s) + 1)]
alphas = list(string.ascii_lowercase)
#print(alphas)

for alpidx in range(len(alphas)):
	alp = alphas[alpidx]
	lastpos = -1
	for idx in range(len(doubles) - 1, -1, -1):
		if(doubles[idx] == alp):
			lastpos = idx
		if(idx < len(s)):
			next_distance[idx][alpidx] = lastpos - idx

#print(next_distance)
		
pos = 0
for ch in t:
	index = ord(ch) - 97
	mod = pos%len(s)
	if(next_distance[mod][index] < 0):
		print(-1)
		exit(0)
	distance = next_distance[mod][index]
	pos += distance + 1
print(pos)

Submission status: Time Limit Exceeded

Solution code:
from copy import*
from collections import*

d=defaultdict(deque)
s=input()
x=len(s)
t=input()
for k,v in enumerate(s):
    d[v].append(k)
dsub=deepcopy(d)
#print(*d)
if (set(s)>=set(t)):
#    print(d)
    pre=-1
    ans=0
    for i in t:
        if len(d[i])==0:
            d=deepcopy(dsub)
            pre=d[i].popleft()
            ans+=1
        else:
            p=d[i].popleft()
            if p>pre:
                pre=p
            else:
                d=deepcopy(dsub)
                ans+=1
                pre=p
#        print(d.values())
    print(ans*x+pre+1)
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
s = str(input())
t = str(input())

if set(t).issubset(set(s)):
    p = -1
    r = 0
    
    for i in t:
        p = s.find(i, p+1)
        if p == -1:
            r += 1
            p = s.find(i)
        
    print(p + 1 + len(s)*r)
    
else:
    print(-1)

Submission status: Time Limit Exceeded

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

l = len(s)
d = dict()
for i in range(l):
    if s[i] in d.keys():
        d[s[i]].append(i+1)
    else:
        d[s[i]] = [i+1]

ans = 0
now = 0
for i in t:
    if i not in d.keys():
        print(-1)
        exit()

    for j in d[i]:
        if j > now:
            now = j
            break
    else:
        now = d[i][0]
        ans += l

print(ans + now)

Submission status: Time Limit Exceeded

Solution code:

if __name__ == "__main__":
    s = input()
    alphabet = set(s)
    t = input()
    impossible = False
    for i in range(len(t)):
        if t[i] not in alphabet:
            impossible = True
    if not impossible:
        i = 0
        found = 0
        for i in range(len(s) * (10**100)):
            if (found >= len(t)):
                break
            if s[i % len(s)] == t[found]:
                found += 1
        print(i)
    else:
        print("-1")

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
t = list(input())

tmp = 0
sets = set(s)
# print (sets)
for T in t:
    if T not in sets:
        print (-1)
        exit()

T = 0
while T < len(t):
    if s[tmp % len(s)] == t[T]:
        T += 1
    tmp += 1

print (tmp)

Submission status: Time Limit Exceeded

Solution code:

# import sys
# input = sys.stdin.readline
from collections import Counter


def main():
    s = input()
    t = input()
    ts = Counter(s)
    i = 0
    pos = 0

    while i < len(t):
        cur = pos % len(s)
        # print(len(t), i, cur, t[i], s[cur])

        if t[i] not in ts:
            pos = -1
            break

        if t[i] == s[cur]:
            i += 1
        # else:
        #     pos = -1
        #     break
        pos += 1

    print(pos)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = input()
    t = list(input())
    N = len(s)

    for tt in t:
        if tt not in s:
            ans = -1
            print(ans)
            exit()

    i = 0
    ans = 0
    r = s * 2
    while t:
        tt = t.pop(0)
        r = r[i::] + s
        cnt = 0
        for rr in r:
            cnt += 1
            if tt == rr:
                break
        ans += cnt
        i = cnt
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = input()
    t = list(input())
    N = len(s)

    for tt in t:
        if tt not in s:
            ans = -1
            print(ans)
            exit()

    i = 0
    ans = 0
    r = s * 2
    while t:
        tt = t.pop(0)
        r = r[i::] + s
        for rr in r:
            if tt == rr:
                i = r.index(rr)
                break
        ans += (i + 1)
        i += 1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import defaultdict
from copy import deepcopy


s = input()
t = input()

d_original = defaultdict(list)

for i,e in enumerate(s):
    d_original[e].append(i+1)

counter = 0

positions = [-1]
d = deepcopy(d_original)

for e in t:
    if len(d[e]) == 0:
        print(-1)
        sys.exit()

i = 0
while i < len(t):
    e = t[i]
    if len(d[e]) == 0:
        d = deepcopy(d_original)
        counter += 1
    value = d[e].pop(0)+len(s)*counter
    if value > positions[-1]:
        positions.append(value)
        i += 1
print(positions[-1])

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import defaultdict
from copy import deepcopy


s = input()
t = input()

d_original = defaultdict(list)

for i,e in enumerate(s):
    d_original[e].append(i+1)

counter = 0

positions = [-1]
d = deepcopy(d_original)

for e in t:
    if len(d[e]) == 0:
        print(-1)
        sys.exit()


i = 0
while i < len(t):
    e = t[i]
    if len(d[e]) == 0:
        d = deepcopy(d_original)
        counter += 1
    value = d[e].pop(0)+len(s)*counter
    if value > positions[-1]:
        positions.append(value)
        i += 1
    else:
        d = deepcopy(d_original)
        counter += 1



print(positions[-1])

Submission status: Time Limit Exceeded

Solution code:
import bisect
s = input()
t = input()

pos = [0]*len(t)
for i, c in enumerate(t):
    ind = [ii for ii, v in enumerate(s) if v == c]
    if not ind:
        print(-1)
        exit()
    pos[i] = ind

lens = len(s)
last = pos[0][0]
res = last + 1
addval = 0
for vals in pos[1:]:
    if vals[-1] <= last:
        addval = (lens - last - 1) + (vals[0] + 1)
        res += addval
        last = vals[0]
    val = vals[bisect.bisect_left(vals, last)]
    addval = val - last
    res += addval
    last = val

print(res)

Submission status: Time Limit Exceeded

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

pos = [0]*len(t)
for i, c in enumerate(t):
    ind = [ii for ii, v in enumerate(s) if v == c]
    if not ind:
        print(-1)
        exit()
    pos[i] = ind

lens = len(s)
last = pos[0][0]
res = last + 1
addval = 0
for vals in pos[1:]:
    for i, val in enumerate(vals):
        if last < val:
            addval = val - last
            res += addval
            last = val
            break
        else:
            if i==len(vals)-1:
                addval = (lens - last - 1) + (val + 1)
                res += addval
                last = val
                break
            else:
                continue

print(res)

Submission status: Time Limit Exceeded

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

pos = [0]*len(t)
for i, c in enumerate(t):
    ind = [ii for ii, v in enumerate(s) if v == c]
    if not ind:
        print(-1)
        exit()
    pos[i] = ind

lens = len(s)
last = pos[0][0]
res = last + 1
addval = 0
for vals in pos[1:]:
    for i, val in enumerate(vals):
        if last < val:
            addval = val - last
            res += addval
            last = val
            break
        else:
            if i==len(vals)-1:
                addval = (lens - last - 1) + (val + 1)
                res += addval
                last = val
                break
            else:
                continue

print(res)

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    s = str(input())
    t = list(input())
    if set(t) <= set(s):
        t.append('7')
        n = len(s)
        result = 0
        point = 0
        counter = 0
        i = t.pop(0)
        while i != '7':
            if i in s[point:]:
                x = s[point:].index(i)
                result += x + 1
                point += x + 1
                i = t.pop(0)
                counter = 0
            else:
                result += n - point
                point = 0
                counter += 1
        print(result)
    else:
        print(-1)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    s = str(input())
    t = list(input())
    t.append('777')
    n = len(s)
    result = 0
    point = 0
    counter = 0
    i = t.pop(0)
    while i != '777':
        if i in s[point:]:
            x = s[point:].index(i)
            result += x + 1
            point += x + 1
            i = t.pop(0)
            counter = 0
        else:
            result += n - point
            point = 0
            counter += 1
            if counter >= 2:
                print(-1)
                sys.exit()
    print(result)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    import sys
    s = str(input())
    t = str(input())
    s += s
    n = len(s)
    m = len(t)
    result = 0
    point = 0
    counter = 0
    i = t[0]
    c = 1
    while c <= m:
        if i in s[point:]:
            x = s[point:].index(i)
            result += x + 1
            point += x + 1
            if c < m:
                i = t[c]
            c += 1
            counter = 0
        else:
            result += n - point
            point = 0
            counter += 1
            if counter >= 2:
                print(-1)
                sys.exit()
    print(result)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
s = input()
ss = ''.join([s,s])
t = input()
ans = 0
count = 0
dp = [0 for _ in range(len(t))]
slength = len(s)

for i in range(len(t)):
    for j in range(len(s)):
        if ss[j+(dp[i-1]%slength)]==t[i]:
            if i==0:
                dp[i] = j+1
                break
            else:
                dp[i] = dp[i-1] + j + 1
                break
        if j==slength-1:
            ans = -1
            break
            
if ans == -1:
    print(ans)
else:
    print(dp[len(t)-1])
    #print(dp)

Submission status: Time Limit Exceeded

Solution code:
s = input()
ss = ''.join([s,s])
t = input()
ans = 0
count = 0
dp = [0 for _ in range(len(t))]
slength = len(s)

for i in range(len(t)):
    if (t[i] in s):
        for j in range(len(s)):
#            print('i:',i)
#            print('j:',j)
            if ss[j+(dp[i-1]%slength)]==t[i]:
                if i==0:
                    dp[i] = j+1
                    break
                else:
                    dp[i] = dp[i-1] + j + 1
                    break
    else:
        ans = -1
        break
if ans == -1:
    print(ans)
else:
    print(dp[len(t)-1])
    #print(dp)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
s = input()
t = input()
ss = set(s)
tt = set(t)
count = 0
for i in tt:
  if not i in ss:
    print(-1)
    break
else:
  dic = defaultdict(list)
  for i in range(len(s)):
    dic[s[i]] += [i]
  ans = 0
  for i in range(len(t)):
    x = ans%len(s)
    for j in dic[t[i]]:
      count += 1
      if j+1>x:
        ans += j+1-x
        break
    else:
      ans += len(s) - x + dic[t[i]][0] + 1
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = raw_input()
t = raw_input()
p, e, n = 0, 1, len(s)
ss = set(s)
for tt in t:
    q = p % n
    if not tt in ss:
        e = 0
        break
    if tt in s[q:]: p += s[q:].index(tt) + 1 
    else:   p += s.index(tt) + len(s[q:]) + 1
print((p+1)*e-1)

Submission status: Time Limit Exceeded

Solution code:
alpha = list("abcdefghijklmnopqrstuvwxyz")

s = list(str(input()))
t = list(str(input()))

info = [[0] * len(s) for _ in range(26)]

ss = s[:] * 2

for i in range(26):
    last = -1
    for j in range(len(ss)-1,-1,-1):
        info[i][j%(len(s))] = last
        if ss[j] ==  alpha[i]:
            last = j % (len(s))
#print(info)

count = 0
num = 0
place = -1

while num < len(t):
    x = info[alpha.index(t[num])][place]
    if x > place:
        count += x - place
    else:
        count += x - place + len(s)
    place = x
    num += 1

s_ = [0] * 26
t_ = [0] * 26
for lets in s:
    s_[alpha.index(lets)] = 1
for lett in t:
    t_[alpha.index(lett)] = 1
for i in range(26):
    if s_[i] < t_[i]:
        count = -1
        break

print(count)

Submission status: Time Limit Exceeded

Solution code:
slist = input()
tlist = input()
scounter = 0
slength=len(slist)
for tchar in tlist:
    Sstart=scounter%slength
    while True:
        # print(tchar,slist[scounter%slength],scounter)
        if tchar == slist[scounter%slength]:
            scounter += 1
            break
        scounter += 1
        if scounter%slength==Sstart:
            scounter=-1
            break
    if scounter==-1:
        break
print(scounter)

Submission status: Time Limit Exceeded

Solution code:
slist = input()
tlist = input()
# 番兵設定

scounter = 0
slength=len(slist)

for tchar in tlist:
    Slist=[]
    for schar in slist[scounter%slength:]:
        Slist.append(schar)
    for schar in slist[:scounter%slength]:
        Slist.append(schar)
    Slist.append("aa")
    for schar in Slist:
        # print(schar,tchar,scounter)
        if schar == "aa":
            scounter = -1
            tcharcheckflag=True
            break
        if tchar == schar:
            scounter += 1
            tcharcheckflag=True
            break
        scounter+=1
    if scounter == -1:
        break

print(scounter)

Submission status: Time Limit Exceeded

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

slist = list(s)
tlist = list(t)
# 番兵設定

scounter = 0
slength=len(slist)

for tchar in tlist:
    Slist=[]
    for schar in slist[scounter%slength:]:
        Slist.append(schar)
    for schar in slist[:scounter%slength]:
        Slist.append(schar)
    Slist.append("aa")
    for schar in Slist:
        # print(schar,tchar,scounter)
        if schar == "aa":
            scounter = -1
            tcharcheckflag=True
            break
        if tchar == schar:
            scounter += 1
            tcharcheckflag=True
            break
        scounter+=1
    if scounter == -1:
        break

print(scounter)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
import numpy as np


s = input()
t = input()
l = len(s)

idx_dict = defaultdict(list)

#各文字の index を取得
for i, v in enumerate(s):
    idx_dict[v].append(i)

#リストを ndarray に変換
for key, value in idx_dict.items():
    idx_dict[key] = np.array(value)

l = len(s)
rnd = 0
idx = 0
ans = 0
p_v = ''
# index のリストを取り出し、現在の idx と比べる
# idx より大きい値がない場合は一周ふやす
for v in t:
    # idx_dict の key に　v が存在しなかったら -1
    if v not in idx_dict:
        ans = -1
        break
    # 同じ文字が続いたら idx を 1 増やす
    if v == p_v:
        idx += 1
    arr = idx_dict[v]
    ar = arr[arr >= idx]

    if ar.size > 0:
        idx = ar[0]
    else:
        idx = arr[0]
        rnd += 1
    p_v = v

if not ans:
        ans = ((rnd * l) + idx) + 1
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
T = input()
D = [[] for _ in range(26)]
for i in range(26):
  D[i] = [0] * S.count(chr(97 + i))
P = [0] * 26
for i, s in enumerate(S):
  n = ord(s) - 97
  D[n][P[n]] = i + 1
  P[n] += 1
def bsearch(t, pos, loop):
  d = D[ord(t) - 97]
  l = len(d)
  if l < 1:
    return -1, 0
  if pos < d[0]:
    return d[0], loop
  if d[l - 1] <= pos:
    loop += 1
    return d[0], loop
  low = 0
  high = l
  while high - low > 1:
    mid = (high - low) // 2
    if pos < d[mid]:
      high = mid
    else:
      low = mid
  return d[high], loop
pos = 0
loop = 0
for t in T:
  pos, loop = bsearch(t, pos, loop)
  if pos == -1:
    print(-1)
    break
else:
  print(len(S) * loop + pos)

Submission status: Time Limit Exceeded

Solution code:
S = input()
T = input()
D = [[] for _ in range(26)]
for i, s in enumerate(S):
  D[ord(s) - 97].append(i + 1)
def bsearch(t, pos, loop):
  d = D[ord(t) - 97]
  l = len(d)
  if l < 1:
    return -1, 0
  if pos < d[0]:
    return d[0], loop
  if d[l - 1] <= pos:
    loop += 1
    return d[0], loop
  low = 0
  high = l
  while high - low > 1:
    mid = (high - low) // 2
    if pos < d[mid]:
      high = mid
    else:
      low = mid
  return d[high], loop
pos = 0
loop = 0
for t in T:
  pos, loop = bsearch(t, pos, loop)
  if pos == -1:
    print(-1)
    break
else:
  print(len(S) * loop + pos)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
S = input()
T = input()
D = defaultdict(list)
L = [0] * 26
for i, s in enumerate(S):
  D[s].append(i + 1)
for i in range(26):
  c = chr(97 + i)
  L[i] = len(D[c])
def bsearch(t, pos, loop):
  d = D[t]
  l = L[ord(t) - 97]
  if l < 1:
    return -1, 0
  if pos < d[0]:
    return d[0], loop
  if d[l - 1] <= pos:
    loop += 1
    return d[0], loop
  low = 0
  high = l
  while high - low > 1:
    mid = (high - low) // 2
    if pos < d[mid]:
      high = mid
    else:
      low = mid
  return d[high], loop
pos = 0
loop = 0
for t in T:
  pos, loop = bsearch(t, pos, loop)
  if pos == -1:
    print(-1)
    break
else:
  print(len(S) * loop + pos)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
S = input()
T = input()
D = defaultdict(list)
for i, s in enumerate(S):
  D[s].append(i + 1)
def bsearch(d, pos, loop):
  l = len(d)
  if l < 1:
    return -1, 0
  if pos < d[0]:
    return d[0], loop
  if d[l - 1] <= pos:
    loop += 1
    return d[0], loop
  low = 0
  high = l
  while high - low > 1:
    mid = (high - low) // 2
    if pos < d[mid]:
      high = mid
    else:
      low = mid
  return d[high], loop
pos = 0
loop = 0
for t in T:
  pos, loop = bsearch(D[t], pos, loop)
  if pos == -1:
    print(-1)
    break
else:
  print(len(S) * loop + pos)

Submission status: Time Limit Exceeded

Solution code:
def main():
  s = list(map(lambda x:ord(x)-97, list(input())))
  ls = len(s)
  ss = set(s)
  t = list(map(lambda x:ord(x)-97, list(input())))
  lt = len(t)
  st = set(t)
  td = [list() for _ in range(26)]
  if not st.issubset(ss):
    print(-1)
    exit()
  for i in range(lt-1):
    td[t[i]].append(t[i+1])
  md = [None for _ in range(ls)]
  cd = [0]*26
  for i in range(ls-1,-1,-1):
    cd[s[i]] = i + ls
  for i in range(ls-1,-1,-1):
    md[i] = {x:cd[x] for x in td[s[i]]}
    cd[s[i]] = i
  ans = 0
  cur = cd[t[0]]
  for i in range(lt-1):
    cur = md[cur][t[i+1]]
    ans += (cur//ls)
    cur %= ls
  print(1 + ans*ls+cur)

if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

Solution code:
def main():
  s = input()
  ls = len(s)
  ss = set(s)
  t = input()
  lt = len(t)
  st = set(t)
  td = {x:list() for x in ss}
  if not st.issubset(ss):
    print(-1)
    exit()
  for i in range(lt-1):
    td[t[i]].append(t[i+1])
  md = [0]*ls
  cd = dict()
  for i in range(ls-1,-1,-1):
    cd[s[i]] = i + ls
  for i in range(ls-1,-1,-1):
    md[i] = {x:k for x,k in cd.items() if x in td[s[i]]}
    cd[s[i]] = i
  ans = 0
  cur = cd[t[0]]
  for i in range(lt-1):
    cur = md[cur][t[i+1]]
    ans += (cur//ls)
    cur %= ls
  print(1 + ans*ls+cur)

if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

Solution code:
s = input()
ls = len(s)
ss = set(s)
t = input()
lt = len(t)
st = set(t)
td = {x:list() for x in st}
if not st.issubset(ss):
  print(-1)
  exit()
for i in range(lt-1):
  td[t[i]].append(t[i+1])
md = [dict() for _ in range(ls)]
cd = dict()
for i in range(ls-1,-1,-1):
  cd[s[i]] = i + ls
for i in range(ls-1,-1,-1):
  if s[i] not in td:
    cd[s[i]] = i
    continue
  for x in td[s[i]]:
    md[i][x] = cd[x]
  cd[s[i]] = i
ans = 0
cur = cd[t[0]]
for i in range(lt-1):
  cur = md[cur][t[i+1]]
  ans += (cur//ls)
  cur %= ls
print(1 + ans*ls+cur)

Submission status: Time Limit Exceeded

Solution code:
s = input()
ls = len(s)
t = input()
lt = len(t)
td = dict()
for x in t:
  if x not in s:
    print(-1)
    exit(0)
for i in range(lt-1):
  if t[i] not in td:
    td[t[i]] = list()
  td[t[i]].append(t[i+1])
md = [dict() for _ in range(ls)]
cd = dict()
for i in range(ls-1,-1,-1):
  cd[s[i]] = i + ls
for i in range(ls-1,-1,-1):
  if s[i] not in td:
    cd[s[i]] = i
    continue
  for x in td[s[i]]:
    md[i][x] = cd[x]
  cd[s[i]] = i
ans = 0
cur = cd[t[0]]
for i in range(lt-1):
  cur = md[cur][t[i+1]]
  ans += (cur//ls)
  cur %= ls
print(1 + ans*ls+cur)

Submission status: Time Limit Exceeded

Solution code:
s = input()
ls = len(s)
t = input()
lt = len(t)
td = dict()
for x in t:
  if x not in s:
    print(-1)
    exit(0)
for i in range(lt-1):
  if t[i] not in td:
    td[t[i]] = list()
  td[t[i]].append(t[i+1])
md = [dict() for _ in range(ls)]
cd = dict()
for i in range(ls-1,-1,-1):
  cd[s[i]] = i + ls
for i in range(ls-1,-1,-1):
  if s[i] not in td:
    cd[s[i]] = i
    continue
  for x in td[s[i]]:
    md[i][x] = cd[x]
  cd[s[i]] = i
ans = 0
cur = cd[t[0]]
for i in range(lt-1):
  cur = md[cur][t[i+1]]
  ans += (cur//ls)
  cur %= ls
print(1 + ans*ls+cur)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
import bisect
s = input()
t = input()



count = 0
s_len = len(s)
s_list = np.zeros(len(s))
t_list = []*len(t)
indexes = []

for i in range(26):
  
  indexes.append([j for j, x in enumerate(s) if x == chr(i + ord('a')) ])
  


for i,k in enumerate(t):

  index = indexes[ord(k)-ord('a')]
  
  if len(index) == 0:
    print(-1)
    sys.exit(0)
  else:
    t_list.insert(i,index)
    


pre = -1
for i in range(len(t)):
  
  high = [j  for j in t_list[i] if j > pre]
  #high = bisect.bisect_left(t_list[i], pre+1) 

  if len(high)==0:
    
    count += s_len - pre  + t_list[i][0]
    pre = t_list[i][0] 
  else:
    
    #count += t_list[i][high] - pre 
    count += high[0] - pre
    pre = high[0]

print(count)