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:
# -*- coding: utf-8 -*-

def main():
    s = input()
    t = input()
    if judgeMate(s, t):
        print(countMakeTbyS(s, t))
    else:
        print("-1")

def judgeMate(s, t):
    ans = True
    for i in range(len(t)):
        if t[i] in s:
            continue
        else:
            ans = False
            break
    return ans

def countMakeTbyS(s, t):
    count = 0
    p_t = 0
    try:
        while(True):
            for i in range(len(s)):
                x = t[p_t]
                y = s[i]
                if x == y:
                    p_t += 1
                count += 1
    except:
        return count


main()

Submission status: Time Limit Exceeded

Solution code:
import sys

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

string = s
length = 1

for i in range(len(t)):
	if s.find(t[i])==-1:
		print(-1)
		sys.exit()
	if string.find(t[i])==-1:
		string = string + s
		length += 1
	string = string[string.find(t[i])+1 : ]
	
print(length*len(s)-len(string))

Submission status: Time Limit Exceeded

Solution code:
import sys

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

string = s
length = 1

for i in t:
	if s.find(i)==-1:
		print(-1)
		sys.exit()
	if string.find(i)==-1:
		string = string + s
		length += 1
	string = string[string.find(i)+1 : ]
	
print(length*len(s)-len(string))

Submission status: Time Limit Exceeded

Solution code:
import sys 
input_a = list(input())
input_b = list(input())
count = 0
judge = 0
for i in input_b:
    if input_a.count(i) == 0:
        judge = 1
if judge == 1:
    print(-1)
    sys.exit()
while input_b != []:
    for j in input_a:
        count += 1
        if input_b[0] == j:
           input_b.pop(0)
        if input_b == []:
            break
print(count)

Submission status: Time Limit Exceeded

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

import sys
import math
import bisect
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()
t = input()
n = len(s)
m = len(t)
f = [False]*26
f2 = [False]*26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(n):
    f[alpha.index(s[i])] = True
for i in range(m):
    f2[alpha.index(t[i])] = True
for i in range(26):
    if not f[i] and f2[i]:
        print(-1)
        quit()
ans = 0
pre = s.index(t[0])
ans += pre+1
for i in range(1,m):
    s = s[pre+1:] + s[:pre+1]
    inde = s.index(t[i])
    ans += inde+1
    pre = inde
print(ans)

Submission status: Time Limit Exceeded

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

p = 0
roop = 0
dic = [['' for i in range(len(s))] for j in range(26)]

for i in range(len(t)):
    dis = 0
    if dic[ord(t[i])-97][p] == '':
        for j in range(p,len(s)):
            if t[i] == s[j]:
                for k in range(p,j+1):
                    dic[ord(t[i])-97][k] = j
                dis = 1
                break
        if dis == 0:
            for j in range(0,p):
                if t[i] == s[j]:
                    for k in range(p,len(s)):
                        dic[ord(t[i])-97][k] = j
                    for k in range(0,j+1):
                        dic[ord(t[i])-97][k] = j
                    dis = 1
                    break
        if dis == 0:
            dis = 2
            break
    if p > dic[ord(t[i])-97][p]:
        roop += 1
    if dic[ord(t[i])-97][p] == len(s)-1:
        roop += 1        
        p = 0
    else:
        p = dic[ord(t[i])-97][p] + 1

if dis == 2:
    print(-1)
else:
    print(roop*len(s)+p)

Submission status: Time Limit Exceeded

Solution code:
import bisect

s=input()
t=input()

index=list()
for a in range(len(t)):
 index.append([i for i, x in enumerate(s) if x == t[a]])

b=0
loop=0
condition=0

for j in range(len(t)):
  #print(j,index[j],b,c,loop)
  if len(index[j])<1:
    print(-1)
    condition=1
    break
  c=bisect.bisect(index[j],b)
  if j<1:
    b=index[0][0]    
  elif c == len(index[j]): 
     loop +=1
     b=index[j][0]
  else:
     b=index[j][c]
     #print(j,b,loop)


if condition==0:
 print(b+loop*len(s)+1)

Submission status: Time Limit Exceeded

Solution code:
import bisect

s=input()
t=input()

index=list()
for a in range(len(t)):
 index.append([i for i, x in enumerate(s) if x == t[a]])

b=0
loop=0
condition=0

for j in range(len(t)):
  #print(j,index[j],b,c,loop)
  if len(index[j])<1:
    print(-1)
    condition=1
    break
  c=bisect.bisect_left(index[j],b)
  if j<1:
    b=index[0][0]    
  elif c == len(index[j]) or c==index[j][len(index[j])-1]: 
     loop +=1
     b=index[j][0]
  else:
     b=index[j][c]
     #print(j,b,loop)


if condition==0:
 print(b+loop*len(s)+1)

Submission status: Time Limit Exceeded

Solution code:
import bisect

s=input()
t=input()

index=list()
for a in range(len(t)):
 index.append([i for i, x in enumerate(s) if x == t[a]])

b=0
loop=0
condition=0

for j in range(len(t)):
  c=bisect.bisect_left(index[j],b)
  #print(j,index[j],b,c,loop)
  if len(index[j])<1:
    print(-1)
    condition=1
    break
  elif j<1:
    b=index[0][0]    
  elif c<len(index[j])  and c != index[j][len(index[j])-1]:
     b=index[j][c]
     #print(j,b,loop)
  else:
     loop +=1
     b=index[j][0]
     #if loop == len(s)*(10**100):
     #  print(-1)
     #  break

if condition==0:
 print(b+loop*len(s)+1)

Submission status: Time Limit Exceeded

Solution code:
import bisect

s=input()
t=input()

condition=0

index=list()
for a in range(len(t)):
 index.append([i for i, x in enumerate(s) if x == t[a]])

b=0
loop=0
condition=0

for j in range(len(t)):
  c=bisect.bisect_left(index[j],b)
  #print(j,index[j],b,c,loop)
  if len(index[j])<1:
    print(-1)
    condition=1
    break
  elif j==0:
    b=index[0][0]    
  elif c<len(index[j])  and c != index[j][len(index[j])-1]:
     b=index[j][c]
     #print(j,b,loop)
  else:
     loop=loop+len(s)
     b=index[j][0]
     if loop == len(s)*(10**100):
       print(-1)
       break

if condition==0:
 print(b+loop+1)

Submission status: Time Limit Exceeded

Solution code:
import bisect

s=input()
t=input()

condition=0

index=list()
for a in range(len(t)):
 index.append([i for i, x in enumerate(s) if x == t[a]])

for i in range(len(t)):
 if index[i]==[]:
   condition=1
   break

if condition==1:
  print(-1)
else:
  #print(index)
  b=index[0][0]
  j=1
  loop=0
  if len(t)==1:
    print(b+1)
  else:
   for j in range(1,len(t)):
     c=bisect.bisect_left(index[j],b)
     #print(j,index[j],b,c,loop)
     if c<len(index[j]) and c != index[j][len(index[j])-1]:
        b=index[j][c]
        #print(j,b,loop)
     else:
        loop=loop+len(s)
        b=index[j][0]
        if loop == len(s)*(10**100):
           print(-1)
           break
   print(b+loop+1)

Submission status: Time Limit Exceeded

Solution code:
import bisect

s=input()
t=input()

condition=0

index=list()
for a in range(len(t)):
 index.append([i for i, x in enumerate(s) if x == t[a]])

for i in range(len(t)):
 if index[i]==[]:
   condition=1
   break

if condition==1:
  print(-1)
else:
  #print(index)
  b=index[0][0]
  j=1
  loop=0
  if len(t)==1:
    print(b+1)
  else:
   while j <= len(t)-1:
     c=bisect.bisect_left(index[j],b)
     #print(j,index[j],b,c,loop)
     if c<len(index[j]) and c != index[j][len(index[j])-1]:
        b=index[j][c]
        #print(j,b,loop)
        j=j+1
        if j==len(t):
          print(b+loop+1)
          break
     else:
        loop=loop+len(s)
        b=index[j][0]
        j=j+1
        if j==len(t):
          print(b+loop+1)
          break
        if loop == len(s)*(10**100):
           print(-1)
           break

Submission status: Time Limit Exceeded

Solution code:
s = input()
t= input()
if not set(t)<=set(s):
    print(-1)
else:
    ans=0
    for i in range(len(t)):
        n=int(s.index(t[i]))+1
        ans=ans+n
        s=s[n:]+s[:n]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect

s=input()
t=input()

#print(index)
condition=0
#t2=list(set(t))
#print(t2)

#for i in range(len(t)):
# if s.count(t[i])==0:
#   condition=1
#   break
index=list()
for a in range(len(t)):
 index.append([i for i, x in enumerate(s) if x == t[a]])

for i in range(len(t)):
 if index[i]==[]:
   condition=1
   break

if condition==1:
  print(-1)
else:
  #print(index)
  b=index[0][0]
  j=1
  loop=0
  count=0
  if len(t)==1:
    print(b+1)
  else:
   while j <= len(t)-1:
     c=bisect.bisect_left(index[j],b)
     #print(j,index[j],b,c,loop)
     if c < len(index[j]) and c != index[j][len(index[j])-1]:
        b=index[j][c]
        #print(j,b,loop)
        j=j+1
        if j==len(t):
          print(b+loop+1)
          break
     else:
        loop=loop+len(s)
        b=index[j][0]
        j=j+1
        if j==len(t):
          print(b+loop+1)
          break
        if loop == len(s)*(10**100):
           print(-1)
           break

Submission status: Time Limit Exceeded

Solution code:
import bisect

s=input()
t=input()

#print(index)
condition=0
t2=list(set(t))
#print(t2)

for i in range(len(t2)):
 if s.count(t2[i])==0:
   condition=1
   break

if condition==1:
  print(-1)
else:
  index=list()
  for a in range(len(t)):
   index.append([i for i, x in enumerate(s) if x == t[a]])
  
  #print(index)
  b=index[0][0]
  j=1
  loop=0
  count=0
  if len(t)==1:
    print(b+1)
  else:
   while j <= len(t)-1:
     c=bisect.bisect_left(index[j],b)
     #print(j,index[j],b,c,loop)
     if c < len(index[j]) and c != index[j][len(index[j])-1]:
        b=index[j][c]
        #print(j,b,loop)
        j=j+1
        if j==len(t):
          print(b+loop+1)
          break
     else:
        loop=loop+len(s)
        b=index[j][0]
        j=j+1
        if j==len(t):
          print(b+loop+1)
          break
        if loop == len(s)*(10**100):
           print(-1)
           break

Submission status: Time Limit Exceeded

Solution code:
import bisect

s=input()
t=input()

#print(index)
condition=0
t2=list(set(t))
#print(t2)

for i in range(len(t2)):
 if s.count(t2[i])==0:
   condition=1
   break

if condition==1:
  print(-1)
else:
  index=list()
  for a in range(len(t)):
   index.append([i for i, x in enumerate(s) if x == t[a]])
  
  #print(index)
  b=index[0][0]
  j=1
  loop=0
  count=0
  if len(t)==1:
    print(b+1)
  else:
   while j <= len(t)-1:
     c=bisect.bisect_left(index[j],b-loop)
     #print(j,index[j],b,c,loop)
     if c < len(index[j]) and c != index[j][len(index[j])-1]:
        b=index[j][c]+loop
        #print(j,b,loop)
        j=j+1
        if j==len(t):
          print(b+1)
          break
     else:
        loop=loop+len(s)
        b=index[j][0]+loop
        j=j+1
        if j==len(t):
          print(b+1)
          break
        if loop == len(s)*(10**100):
           print(-1)
           break

Submission status: Time Limit Exceeded

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



#print(index)
condition=0
t2=list(set(t))
#print(t2)

for i in range(len(t2)):
 if s.count(t2[i])==0:
   condition=1
   break

if condition==1:
  print(-1)
else:
  index=list()
  for a in range(len(t)):
   index.append([i for i, x in enumerate(s) if x == t[a]])
  
  #print(index)
  b=index[0][0]
  j=1
  loop=0
  count=0
  if len(t)==1:
    print(b+1)
  else:
   while j <= len(t)-1:
     cond=0
     for k in range(len(index[j])):
       if index[j][k]+loop >b:
         b=index[j][k]+loop
         #print(b)
         j=j+1
         cond=1
         if j==len(t):
           print(b+1)
         break
       if cond==0:   
         loop=loop+len(s)
         if loop == len(s)*(10**100):
            print(-1)
            break

Submission status: Time Limit Exceeded

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

def solve():
    l = list()
    for i in range(len(t)):
        l.append([j for j, x in enumerate(s) if x == t[i]])
    cnt = 0
    now = -1
    for i in range(len(l)):
        if any([now < j for j in l[i]]):
            now = min([e for e in l[i] if e > now])
        else:
            now = min(l[i])
            cnt += 1
    print(cnt*len(s)+now+1)

if __name__ == "__main__":
    if set(s) >= set(t):
        solve()
    else:
        print(-1)

Submission status: Time Limit Exceeded

Solution code:
NUM_LETTER = 26

def solve():
    s = list(raw_input())
    t = list(raw_input())
    Ns = len(s)
    # Create charDict
    charDict = [[] for i in range(NUM_LETTER)]
    for i in range(Ns):
        charDict[ord(s[i]) - ord('a')].append(i)
    # print charDict
    # Search
    ret = 0
    pk_1 = -1
    for i in range(len(t)):
        hashCharDict = ord(t[i]) - ord('a')
        # print "hashCharDict: " + str(hashCharDict)
        if len(charDict[hashCharDict]) == 0:
            ret = -1
            break
        else:
            ind = 0
            while charDict[hashCharDict][ind] <= pk_1:
                ind += 1
                if ind >= len(charDict[hashCharDict]):
                    break
            if ind >= len(charDict[hashCharDict]):
                ret += Ns - pk_1
                pk_1 = charDict[hashCharDict][0]
                ret += pk_1
                # ret = ((ret + 1) // Ns + 1) * Ns + pk_1
            else:
                ret += charDict[hashCharDict][ind] - pk_1
                pk_1 = charDict[hashCharDict][ind]
                # ret = ((ret + 1) // Ns) * Ns + pk_1
        # print "ret: " + str(ret)
    print ret

if __name__ == '__main__':
    solve()

Submission status: Time Limit Exceeded

Solution code:
s = input()
t = input()
s_len = len(s)
t_len = len(t)
s_list = [s[i] for i in range(s_len)]
num = 0
str_num = 0
for i in range(t_len):
  try:
    num = s_list.index(t[i])
    str_num += num+1
    s_list = s_list[num+1:] + s_list[:num+1]
  except:
    str_num = -1
    break
print(str_num)

Submission status: Time Limit Exceeded

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

ans = 0
now = 0
temp = s
flag = True

i=0

while i < len(t):
  if t[i] in s:
    now = temp.index(t[i])
    temp = temp[now+1:]+temp[:now+1]
    ##print(now+1)
    ##print(temp)
    ans += now + 1
    i+=1
  else:
    print(-1)
    flag = False
    break
if flag:
  print(ans)

Submission status: Time Limit Exceeded

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

ans = 0
now = 0
temp = s
flag = True

i=0

while i < len(t):
  if t[i] in s:
    now = temp.find(t[i])
    temp = temp[now+1:]+temp[:now+1]
    ##print(now+1)
    ##print(temp)
    ans += now + 1
    i+=1
  else:
    print(-1)
    flag = False
    break
if flag:
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-


def main():
    s = input().splitlines()[0]
    t = input()

    Slen = len(s)
    Index = -1
    Loop = 0
    LUT = [[] for i in range(26)]
    Cur = [0 for i in range(26)]

    for i, c in enumerate(s):
        v = ord(c) - ord('a')
        LUT[v].append(i)

    for c in t:
        v = ord(c) - ord('a')
        l = len(LUT[v])
        if l == 0:
            print(-1)
            return

    for c in t:
        v = ord(c) - ord('a')
        l = len(LUT[v])
        b = -1
        for a in range(Cur[v],l):
            if LUT[v][a] > Index:
                b = a
                break
        if b < 0:
            Loop += 1
            Cur = [0 for i in range(26)]
            b = 0
        Index = LUT[v][b]

    print(Loop*Slen+Index+1)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
s=input()
t=input()
exi={chr(97+i): False for i in range(26)}
jump={chr(97+i): [] for i in range(26)}
length=len(s)
pointer=0
flag=True
for i,ch in enumerate(s*2):
    exi[ch]=True
    jump[ch].append(i)
for i in range(26):
    if jump[chr(97+i)] !=[]:
        buf=[min(filter(lambda x:x>=j,jump[chr(97+i)]))-j+1 for j in range(length)]
    else:
        buf=0
    jump[chr(97+i)]=buf
for ch in t:
    if exi[ch]:
        pointer+=jump[ch][pointer%length]
    else:
        flag=False
        break
if flag:
    print(pointer)
else:
    print(-1)

Submission status: Time Limit Exceeded

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

ans = 0
now = 0
temp = s
flag = True


for i in range(len(t)):
  if t[i] in s:
    now = temp.find(t[i])
    temp = temp[now+1:]+temp[:now+1]
    ##print(now+1)
    ##print(temp)
    ans += now + 1
  else:
    print(-1)
    flag = False
    break
if flag:
  print(ans)

Submission status: Time Limit Exceeded

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

import sys
import math
import bisect
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()
t = input()
n = len(s)
m = len(t)
f = [False]*26
f2 = [False]*26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(n):
    f[alpha.index(s[i])] = True
for i in range(m):
    f2[alpha.index(t[i])] = True
for i in range(26):
    if not f[i] and f2[i]:
        print(-1)
        quit()
x = []
pre = s.index(t[0])
x.append(pre+1)
ss = s
for i in range(1,m):
    ss = ss[pre+1:] + ss[:pre+1]
    inde = ss.index(t[i])
    x.append(inde+1)
    pre = inde
print(sum(x))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

s = input().splitlines()[0]
t = input()

Slen = len(s)
Index = -1
Loop = 0
LUT = [[] for i in range(26)]
Cur = [0 for i in range(26)]
for i, c in enumerate(s):
    v = ord(c) - ord('a')
    LUT[v].append(i)

NotFound = False
for c in t:
    v = ord(c) - ord('a')
    l = len(LUT[v])
    if l == 0:
        NotFound = True
        break
    if LUT[v][Cur[v]] > Index:
        Index = LUT[v][Cur[v]]
        Cur[v] += 1
        if Cur[v] >= l:
            Cur[v] = 0
    else:    
        Index = LUT[v][0]
        for i in range(26):
            Cur[i] = 0
            if len(LUT[i]) > 0:
                for j,k in enumerate(LUT[i]):
                    if k <= Index:
                        Cur[i] = j
        Loop += 1

if NotFound:
    print(-1)
else:
    print(Loop*Slen+Index+1)

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
t = list(input())
i = 0
j = 0
k = 0

set_s = set(s)
set_t = set(t)

if set_t - set_s != set():
    k = -1
else:
    while True:
        k += 1
        if t[j] == s[i]:
            j += 1
            if j == len(t):
                break
        i += 1
        if i == len(s):
            i = 0

print(k)

Submission status: Time Limit Exceeded

Solution code:
def main():
    s = input()
    t = input()
    sletters = set()
    for i in s:
        sletters.add(i)

    for i in t:
        if i not in sletters:
            print(-1)
            return

    i = 0
    j = 0
    ans = 0
    while i < len(s) and j < len(t):
        if s[i] == t[j]:
            j += 1
        i += 1
        ans += 1
        if i == len(s):
            i = 0

    print(ans)


main()

Submission status: Time Limit Exceeded

Solution code:
S = input()
T = input()

S_map = {}
len_S = len(S)
len_T = len(T)
for si in range(len(S)):
    if S_map.get(S[si]) is None:
        S_map[S[si]] = [si+1 + len_S*i for i in range(len_T)]
    else:
        S_map[S[si]] += [si+1 + len_S*i for i in range(len_T)]

for s_key in S_map.keys():
    S_map[s_key].sort()

# print(S_map)
i = -1
for t in T:
    try:
        for si in S_map[t]:
            if si > i:
                i = si
                break
    except Exception:
        print(-1)
        exit()
else:
    print(si)

Submission status: Time Limit Exceeded

Solution code:
import sys
# 10倍速いらしい
input = sys.stdin.readline

INF = float('inf')
MOD = 10 ** 9 + 7
EPS = 10 ** -7
sys.setrecursionlimit(1000000)

S = input().rstrip()
T = input().rstrip()

sn = len(S)
M = [dict() for _ in range(sn)]
for i, s in enumerate(S):
    for j in range(sn):
        M[(i - j + sn) % sn][s] = min(M[(i - j + sn) % sn].get(s, INF), j)
    for j in range(sn):
        if M[(i - j + sn) % sn][s] == 0:
            M[(i - j + sn) % sn][s] = M[(i - j + sn + 1) % sn][s] + 1


ans = 0
idx = sn - 1
for t in T:
    if M[idx].get(t, INF) == INF:
        print(-1)
        sys.exit()
    ans += M[idx][t]
    idx = (idx + M[idx][t]) % sn

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
t = input()
l = len(s)
s2 = s*2
if not set(s) >= set(t) :
    print(-1)
else :
    lowers = sorted(set(t))
    ldict = {v:i for i, v in enumerate(lowers)}
    dist = [[0 for j in range(l)] for i in range(len(lowers))]
    for j in range(l) :
        for i, b in enumerate(lowers) :
            k = s2.find(b, j+1)
            dist[i][j] = k-j
    ans = 0
    last = -1
    for c in t :
        diff = dist[ldict[c]][last]
        ans += diff
        last = (last+diff)%l
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
abc = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
s = input()
x = [[-1 for i in range(26)] for j in range(len(s))]
for k in range(2):
    for i in range(len(s)-1,-1,-1):
        if i == 0:
            ib = len(s)-1
        else:
            ib = i-1
        for j in range(26):
            if j == ord(s[i])-97:
                x[ib][j] = i
            else:
                x[ib][j] = x[i][j]
t = input()
i = len(s)-1
count = 0
for j in range(len(t)):
    nxt = x[i][ord(t[j])-97]
    if nxt == -1:
        print(-1)
        exit()
    elif nxt <= i:
        count += 1
        i = nxt
    else:
        i = nxt
print(len(s)*(count-1)+i+1)

Submission status: Time Limit Exceeded

Solution code:
s = input()
t = input()
l = len(s)
s2 = s*2
if not set(s) >= set(t) :
    print(-1)
else :
    lowers = sorted(set(t))
    ldict = {v:i for i, v in enumerate(lowers)}
    dist = [[0 for j in range(l)] for i in range(len(lowers))]
    for j in range(l) :
        for i, b in enumerate(lowers) :
            for k, c in enumerate(s2[j+1:]) :
                if c == b :
                    dist[i][j] = 1+k
                    break
    ans = 0
    last = -1
    for c in t :
        diff = dist[ldict[c]][last]
        ans += diff
        last = (last+diff)%l
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
s = input()
t = input()
s = s[:-1]
t = t[:-1]
len_s = len(s)
len_t = len(t)
# print(len_s)
# print(len_t)
a = [-100]
ans = 0
ng_count = 0
s_raw = s
d = []
for i in range(len_t):
    b = s.find(t[i])
    # print(b)
    if b == -1:
        e = s_raw.find(t[i])
        if e!=-1:
            d.append(e)
        else:
            print(-1)
            exit()
    else:
        if b!=len_t:
            s = s[:b] + "?" + s[b+1:]
        else:
            s = s[:b] + "?"
        # print(t)
        d.append(b)
        # print(d)
if len(d) == 1:
    print(d[0]+1)
    exit()
else:
    for i in range(1,len_t):
        if d[i-1] >= d[i]:
            ng_count += 1
        ans = ng_count*len_s + d[i]
print(ans+1)

Submission status: Time Limit Exceeded

Solution code:
import collections, heapq
s = tuple(input())
t = tuple(input())
ls = len(s)
idx = collections.defaultdict(list)
dct = collections.defaultdict(list)
ln = -1
for i, v in enumerate(s):
    idx[v].append(i)
for k in idx:
    heapq.heapify(idx[k])
for i in t:
    if idx[i]:
        while True:
            p = heapq.heappop(idx[i])
            heapq.heappush(idx[i], p + ls)
            if p > ln:
                ln = p
                break
    else:
        print(-1)
        exit()
print(ln + 1)

Submission status: Time Limit Exceeded

Solution code:
S = input()
T = input()

length = len(S)

ans = 0
pre = -1

for t in T:
    x = S.find(t, pre+1, length)
    if x != -1:
        ans += x - pre
        pre = x
    else:
        x = S.find(t, 0, pre+1)
        if x != -1:
            ans += length + x - pre
            pre = x
        else:
            ans = -1
            break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s=input()
t=input()
l=list()
for _ in list(t):
  if [i for i,x in enumerate(list(s)) if x==_] != list():
    l.append([i for i,x in enumerate(list(s)) if x==_])
  else:
    print("-1")
    exit()
if len(t)==1:
  print(s.index(t)+1)
  exit()
ed=l[0][0]
cd=0
for i in range(len(t))[1:-1]:
  k=l[i]
  if max(k+[ed])==ed :
    cd+=1
    ed=k[0]
  else:
    ex=sorted(k+[ed])
    exex=ex.index(ed)
    ed=k[exex]
k=l[len(t)-1]
if max(k+[ed])==ed :
  print((cd+1)*len(s)+min(k)+1)
else:
  ex=sorted(k+[ed])
  exex=ex.index(ed)
  ed=k[exex]
  print((cd)*len(s)+ed+1)

Submission status: Time Limit Exceeded

Solution code:
s = input()
t = input()
a = 0
b = 0
j = 0
for i in t:
    if  s.count(i) == 0:
        print('-1')
        exit()
    else:
        a += 1
if a >= len(t):
    for i in t:
        j += (s[b:]+s).index(i)+1
        b = j%len(s)
    print(j)

Submission status: Time Limit Exceeded

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

ans = 0
temp = s
flag = True

for i in range(len(t)):
  if t[i] in s:
    ans += temp.find(t[i])+1
    if len(temp[temp.find(t[i])+1:]) < len(s):
      temp = temp[temp.find(t[i])+1:] +s
    else:
      temp = temp[temp.find(t[i])+1:]
  else:
    print(-1)
    flag = False
    break

if flag:
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
t = input()
a = 0
b = 0
j = 0
for i in t:
    if  s.count(i) == 0:
        print('-1')
        break
    else:
        a += 1
if a >= len(t):
    for i in t:
        j += (s[b:]+s).index(i)+1
        b = j%len(s)
    print(j)

Submission status: Time Limit Exceeded

Solution code:
s = input()
t = input()
flag = True
i = 0 # s
j = 0 # t
loop = 0
while(flag):
    if s[i]==t[j]:
        j+=1
        if j==len(t):
            print(i+loop*len(s)+1)
            flag=False
    i+=1
    if i == len(s):
        if j > 0:
            loop+=1
            i = 0
        else:
            print(-1)
            flag=False

Submission status: Time Limit Exceeded

Solution code:
S = input()
T = input()

S_map = {}
for si in range(len(S)):
    if S_map.get(S[si]) is None:
        S_map[S[si]] = [si+1]
    else:
        S_map[S[si]] += [si+1]


# print(S_map)
ans = 0
i = -1
len_S = len(S)
for t in T:
    try:
        for si in S_map[t]:
            if si > i:
                # print(t, si, ans+si)
                i = si
                break
        else:
            ans += len_S
            i = -1
            for si in S_map[t]:
                if si > i:
                    # print(t, si, ans+si, 'laped')
                    i = si
                    break
    except Exception:
        print(-1)
        exit()
else:
    ans += si

print(ans)

Submission status: Time Limit Exceeded

Solution code:
s = input()
t = input()
len_s = len(s)
len_t = len(t)
a = [-100]
ans = 0
ng_count = 0
s_raw = s
d = []
for i in range(len_t):
    b = s.find(t[i])
    # print(b)
    if b == -1:
        e = s_raw.find(t[i])
        if e!=-1:
            d.append(e)
        else:
            print(-1)
            exit()
    else:
        if b!=len_t:
            s = s[:b] + "?" + s[b+1:]
        else:
            s = s[:b] + "?"
        # print(t)
        d.append(b)
        # print(d)
if len(d) == 1:
    print(d[0]+1)
    exit()
else:
    for i in range(1,len_t):
        if d[i-1] >= d[i]:
            ng_count += 1
        ans = ng_count*len_s + d[i]
print(ans+1)

Submission status: Time Limit Exceeded

Solution code:

s = input()
t = input()

ans = 0
temp = s
flag = True

for i in range(len(t)):
  if t[i] in s:
    ans += temp.find(t[i])+1
    temp = temp[temp.find(t[i])+1:] + s
    
  else:
    print(-1)
    flag = False
    break

if flag:
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import defaultdict

def main():
    input = sys.stdin.readline
    S = str(input().strip())
    T = str(input().strip())

    if not set(T) <= set(S):
        return -1

    n = len(S)
    d = defaultdict(list)
    for i, s in enumerate(S, start=1):
        d[s].append(i)

    cnt = 0
    pos = 0
    for t in T:
        if pos >= max(d[t]):
            cnt += 1
            pos = d[t][0]
        else:
            left = 0
            right = len(d[t])
            while left < right:
                mid = (left + right) // 2
                np = d[t][mid]
                if np <= pos:
                    left = mid + 1
                else:
                    right = mid
            pos = d[t][left]

    return n * cnt + pos


if __name__ == '__main__':
    print(main())

Submission status: Time Limit Exceeded

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

results = {}
for i in range(len(s)):
    results.setdefault(s[i], []).append(i)

ans=0
start=-1
hoge=0
for i in t:
    if i in results:
            if start<max(results[i]):
                count =0
                while results[i][count] <= start:
                    count += 1
                hoge = results[i][count]
                ans += hoge-start
            else:
                hoge = results[i][0]
                ans+= (len(s)-start) + hoge
    else:
        ans = -1
        break
    start = hoge

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

# input
s = input()
t = input()

sl = list(s)
tl = list(t)

flag = 1

for tn in tl:
    for y, sn in enumerate(sl):
        if tn == sn:
            break
        if y == (len(sl) - 1):
            print(int(-1))
            flag = 0
            break
    if flag == 0:
        break

#print("There ans")

count = 0
pos = 0
flag2 = 0
while True:
    for y, sn in enumerate(sl):
        count += 1
        if tl[pos] == sn:
            #print("letter")
            #print(tl[pos])
            #print("count")
            #print(count)
            pos += 1
        if pos == len(tl):
            print(count)
            flag2 = 1
            break
    if flag2 == 1:
        break