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.

Solution (Python):
import sys
import bisect
from collections import deque
input = lambda: sys.stdin.readline().rstrip()


def solve():
    s = list(map(lambda x: ord(x) - ord('a'), input()))
    t = list(map(lambda x: ord(x) - ord('a'), input()))
    n = len(s)
    a_list = [[] for _ in range(26)]

    for i in range(n):
        a_list[s[i]].append(i)

    for tt in list(set(t)):
        if len(a_list[tt]) == 0:
            print(-1)
            exit()

    now_i = -1
    count = 0
    t = deque(t)
    ts = t.popleft()
    while True:
        index = bisect.bisect_right(a_list[ts], now_i)
        if index == len(a_list[ts]):
            count += 1
            now_i = -1
        else:
            now_i = a_list[ts][index]
            if not t:
                break
            ts = t.popleft()

    print(n * count + now_i + 1)


if __name__ == '__main__':
    solve()

Solution (Python):
import sys
S = list(input())
T = list(input())
C = {}
for i in range(97, 97+26):
    C[chr(i)] = -1

k = len(T)
for i in range(len(S)):
    if C[S[i]] == -1:
        C[S[i]] = [i]
    else:
        C[S[i]].append(i)

p = 0
now = -1
ans = 0
used = {}
L = {}
for i in range(97, 97+26):
    used[chr(i)] = 0
    if C[chr(i)] == -1:
        L[chr(i)] = 0
    else:
        L[chr(i)] = len(C[chr(i)])

while(True):
    n = T[p]
    if C[n] == -1:
        print(-1)
        sys.exit()
    else:
        if used[n] >= L[n]:
            p -= 1
            for i in range(97, 97 + 26):
                used[chr(i)] = 0
            ans += len(S) - now - 1
            now = -1
        elif C[n][used[n]] >= now:
            ans += C[n][used[n]] - now
            now = C[n][used[n]]
            used[n] += 1
        else:
            used[n] += 1
            p -= 1
    p += 1
    if p == k:
        print(ans)
        break

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

from collections import defaultdict
pos = defaultdict(list)
for i, char in enumerate(s):
    pos[char].append(i)

from bisect import bisect_left

ans = 0
before = -1
flag = True
for char in t:
    if pos[char] == []:
        flag = False
        break

    if before > pos[char][-1]:
        ans += len(s)
        before = pos[char][0] + 1
    else:
        idx = bisect_left(pos[char], before)
        before = pos[char][idx] + 1

if flag:
    print(ans + before)
else:
    print(-1)

Solution (Python):
import collections
s = raw_input()
t = raw_input()
sb = s + s
h = {chr(ord('a') + i):-1 for i in range(26)}
dp = []
for j in range(len(sb)-1,-1,-1):
	dp.append({k:h[k] for k in h})
	h[sb[j]] = j
special = h
dp = dp[::-1]
i = -1
j = 0
c = 0
while(j < len(t)):
	ha = dp[i] if i >= 0 else special
	letter = t[j]
	if ha[letter] != -1:
		i = ha[letter]
		j += 1
	else:
		j = -1
		break

	if i >= len(s):
		i %= len(s)
		c+=1

print (c * len(s) + i + 1) if j != -1 else -1

Solution (Python):
import sys
readline = sys.stdin.readline

S = readline().rstrip()
T = readline().rstrip()

if len(set(T) - set(S)) > 0:
  print(-1)
  exit(0)
  
import bisect
S2 = S * 2
from collections import defaultdict
dic = defaultdict(list)

for i in range(len(S2)):
  dic[S2[i]] += [i]

ind = -1
ans = 0
for t in T:
  targets = dic[t]
  newind = targets[bisect.bisect_left(targets, ind + 1)]
  ans += (newind - ind)
  if newind >= len(S):
    newind -= len(S)
  ind = newind

print(ans)

Solution (Python):
import sys
readline = sys.stdin.readline

S = readline().rstrip()
T = readline().rstrip()

if len(set(T) - set(S)) > 0:
  print(-1)
  exit(0)
  
import bisect

S2 = S * 2
from collections import defaultdict

dic = defaultdict(list)

for i in range(len(S2)):
  dic[S2[i]] += [i]

ind = -1
ans = 0
for t in T:
  
  targets = dic[t]

  newind = targets[bisect.bisect_left(targets, ind + 1)]

  ans += (newind - ind)
  if newind >= len(S):
    newind -= len(S)
  ind = newind

print(ans)

Solution (Python):
def main():
    from bisect import bisect_left

    S = input()
    T = input()

    L = len(S)

    pos = tuple(list() for _ in range(26))
    orda = ord('a')
    for i, c in enumerate(S):
        j = ord(c) - orda
        pos[j].append(i)

    rep = 0
    curr = 0
    for c in T:
        j = ord(c) - orda

        if not pos[j]:
            print(-1)
            return

        i = bisect_left(pos[j], curr)
        if i == len(pos[j]):
            rep += 1
            i = 0  
        curr = pos[j][i] + 1  

    print(rep * L + curr)


if __name__ == '__main__':
    main()

Solution (Python):
import bisect

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

if not set(t).issubset(set(s)):
    print(-1)
    exit()

indices = [[] for _ in range(26)]
idx = [0]*26

for i in range(len(s)):
    indices[ord(s[i]) - 97].append(i)

now = 0
rep = 1
for i in range(len(t)):
    x = ord(t[i]) - 97
    y = bisect.bisect_left(indices[x], now)
    if y == len(indices[x]):
        rep += 1
        now = indices[x][0] + 1
    else:
        now = indices[x][y] + 1

print(len(s)*(rep-1) + now)

Solution (Python):
import bisect
import collections
import copy
import functools
import heapq
import math
import sys
from collections import deque
from collections import defaultdict
input = sys.stdin.readline
MOD = 10**9+7

s = list(input().rstrip())
t = list(input().rstrip())

d = {}
N = len(s)
for i in range(N):
    if s[i] not in d:
        d[s[i]] = [i]
    else:
        d[s[i]].append(i)

now = -1
c = 0
for i in range(len(t)):
    if t[i] in d:
        next = bisect.bisect_right(d[t[i]],now)
        if next < len(d[t[i]]):
            nnum = d[t[i]][next]
            now = nnum
        else:
            c += 1
            now = -1
            next = bisect.bisect_right(d[t[i]],now)
            nnum = d[t[i]][next]
            now = nnum
    else:
        print(-1)
        break
else:
    print(c*N+now+1)

Solution (Python):
import copy
import bisect
S=input()
T=input()

sbasic=[[] for _ in range(26)]
for i,s in enumerate(S):
  sbasic[ord(s)-ord("a")].append(i)

for t in set(T):
    if len(sbasic[ord(t)-ord("a")])==0:
        print(-1)
        exit()

slist=copy.deepcopy(sbasic)
index=-1
roop=0
for t in T:
  if len(sbasic[ord(t)-ord("a")])==0:
    roop+=1
    
    index=-1
  pos=bisect.bisect_right(sbasic[ord(t)-ord("a")],index)
  if pos==len(sbasic[ord(t)-ord("a")]):
    roop+=1
    
    index=-1
    pos=0
  index=sbasic[ord(t)-ord("a")][pos]
  
print(len(S)*roop+index+1)

Solution (Python):

 
import sys

from heapq import heappushpop, heapify, heapreplace
from collections import defaultdict

 
S = input()
T = input()
lenS = len(S)
lenT = len(T)
F1 = [False]*26
F2 = [False]*26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(lenS):
    F1[alpha.index(S[i])] = True
for i in range(lenT):
    F2[alpha.index(T[i])] = True
for i in range(26):
    if not F1[i] and F2[i]:
        print(-1)
        sys.exit()
 
ind = S.index(T[0])
ans = ind+1
for i in range(1,lenT):
    S = S[ind+1:] + S[:ind+1]
    ind = S.index(T[i])
    ans += ind+1
 
print(ans)

Solution (Python):

 
import sys

from heapq import heappush, heappop, heappushpop
from collections import defaultdict

 
input = sys.stdin.readline
 
S = input().rstrip()
T = input().rstrip()
lenS = len(S)
lenT = len(T)
F1 = [False]*26
F2 = [False]*26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(lenS):
    F1[alpha.index(S[i])] = True
for i in range(lenT):
    F2[alpha.index(T[i])] = True
for i in range(26):
    if not F1[i] and F2[i]:
        print(-1)
        sys.exit()
 
ind = S.index(T[0])
ans = ind+1
for i in range(1,lenT):
    S = S[ind+1:] + S[:ind+1]
    ind = S.index(T[i])
    ans += ind+1
 
print(ans)

Solution (Python):

import sys
input = sys.stdin.readline









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


if not set(T) <= set(S):
    print(-1)
    sys.exit()

ind = -1
cnt = 0
for t in T:
    
    ind = S.find(t,ind+1)
    
    if ind == -1:
        cnt += 1
        ind = S.find(t)

print(len(S) * cnt + ind + 1)

Solution (Python):
A = tuple(range(10**5))

S = input()
T = input()
lenS = len(S)
lenT = len(T)
F1 = [False]*26
F2 = [False]*26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(lenS):
    F1[alpha.index(S[i])] = True
for i in range(lenT):
    F2[alpha.index(T[i])] = True
for i in range(26):
    if not F1[i] and F2[i]:
        print(-1)
        exit()

ind = S.index(T[0])
ans = ind+1
for i in range(1,lenT):
    S = S[ind+1:] + S[:ind+1]
    ind = S.index(T[i])
    ans += ind+1

print(ans)

Solution (Python):
import heapq
import bisect
import collections

S = input()
T = input()
lenS = len(S)
lenT = len(T)
F1 = [False]*26
F2 = [False]*26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(lenS):
    F1[alpha.index(S[i])] = True
for i in range(lenT):
    F2[alpha.index(T[i])] = True
for i in range(26):
    if not F1[i] and F2[i]:
        print(-1)
        exit()

ind = S.index(T[0])
ans = ind+1
for i in range(1,lenT):
    S = S[ind+1:] + S[:ind+1]
    ind = S.index(T[i])
    ans += ind+1

print(ans)

Solution (Python):
import math
import heapq
import bisect
import collections

S = input()
T = input()
lenS = len(S)
lenT = len(T)
F1 = [False]*26
F2 = [False]*26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(lenS):
    F1[alpha.index(S[i])] = True
for i in range(lenT):
    F2[alpha.index(T[i])] = True
for i in range(26):
    if not F1[i] and F2[i]:
        print(-1)
        exit()

ind = S.index(T[0])
ans = ind+1
for i in range(1,lenT):
    S = S[ind+1:] + S[:ind+1]
    ind = S.index(T[i])
    ans += ind+1

print(ans)

Solution (Python):
import sys
import math
import heapq
import bisect
import collections

S = input()
T = input()
lenS = len(S)
lenT = len(T)
F1 = [False]*26
F2 = [False]*26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(lenS):
    F1[alpha.index(S[i])] = True
for i in range(lenT):
    F2[alpha.index(T[i])] = True
for i in range(26):
    if not F1[i] and F2[i]:
        print(-1)
        sys.exit()

ind = S.index(T[0])
ans = ind+1
for i in range(1,lenT):
    S = S[ind+1:] + S[:ind+1]
    ind = S.index(T[i])
    ans += ind+1

print(ans)

Solution (Python):
import sys
import math
import bisect
import heapq
import collections

S = input()
T = input()
lenS = len(S)
lenT = len(T)
F1 = [False]*26
F2 = [False]*26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(lenS):
    F1[alpha.index(S[i])] = True
for i in range(lenT):
    F2[alpha.index(T[i])] = True
for i in range(26):
    if not F1[i] and F2[i]:
        print(-1)
        sys.exit()

ind = S.index(T[0])
ans = ind+1
for i in range(1,lenT):
    S = S[ind+1:] + S[:ind+1]
    ind = S.index(T[i])
    ans += ind+1

print(ans)

Solution (Python):
import math
import heapq
import collections

S = input()
T = input()
lenS = len(S)
lenT = len(T)
F1 = [False]*26
F2 = [False]*26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(lenS):
    F1[alpha.index(S[i])] = True
for i in range(lenT):
    F2[alpha.index(T[i])] = True
for i in range(26):
    if not F1[i] and F2[i]:
        print(-1)
        exit()

ind = S.index(T[0])
ans = ind+1
for i in range(1,lenT):
    S = S[ind+1:] + S[:ind+1]
    ind = S.index(T[i])
    ans += ind+1

print(ans)

Solution (Python):
import sys
import math
import bisect
import heapq
import collections

S = input()
T = input()
lenS = len(S)
lenT = len(T)
F1 = [False]*26
F2 = [False]*26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(lenS):
    F1[alpha.index(S[i])] = True
for i in range(lenT):
    F2[alpha.index(T[i])] = True
for i in range(26):
    if not F1[i] and F2[i]:
        print(-1)
        sys.exit()

ind = S.index(T[0])
ans = ind+1
for i in range(1,lenT):
    S = S[ind+1:] + S[:ind+1]
    ind = S.index(T[i])
    ans += ind+1

print(ans)

Solution (Python):
import heapq

S = input()
T = input()
lenS = len(S)
lenT = len(T)
F1 = [False]*26
F2 = [False]*26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(lenS):
    F1[alpha.index(S[i])] = True
for i in range(lenT):
    F2[alpha.index(T[i])] = True
for i in range(26):
    if not F1[i] and F2[i]:
        print(-1)
        exit()

ind = S.index(T[0])
ans = ind+1
for i in range(1,lenT):
    S = S[ind+1:] + S[:ind+1]
    ind = S.index(T[i])
    ans += ind+1

print(ans)

Solution (Python):
import sys
import math
import bisect
import heapq
from collections import defaultdict

S = input()
T = input()
lenS = len(S)
lenT = len(T)
F1 = [False]*26
F2 = [False]*26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(lenS):
    F1[alpha.index(S[i])] = True
for i in range(lenT):
    F2[alpha.index(T[i])] = True
for i in range(26):
    if not F1[i] and F2[i]:
        print(-1)
        sys.exit()

ind = S.index(T[0])
ans = ind+1
for i in range(1,lenT):
    S = S[ind+1:] + S[:ind+1]
    ind = S.index(T[i])
    ans += ind+1

print(ans)

Solution (Python):
import sys
import math
import bisect
from heapq import heappush
from collections import defaultdict

S = input()
T = input()
lenS = len(S)
lenT = len(T)
F1 = [False]*26
F2 = [False]*26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(lenS):
    F1[alpha.index(S[i])] = True
for i in range(lenT):
    F2[alpha.index(T[i])] = True
for i in range(26):
    if not F1[i] and F2[i]:
        print(-1)
        sys.exit()

ind = S.index(T[0])
ans = ind+1
for i in range(1,lenT):
    S = S[ind+1:] + S[:ind+1]
    ind = S.index(T[i])
    ans += ind+1

print(ans)

Solution (Python):
import sys
import math
import bisect
from heapq import heappush, heappop,heappushpop
from collections import defaultdict

S = input()
T = input()
lenS = len(S)
lenT = len(T)
F1 = [False]*26
F2 = [False]*26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(lenS):
    F1[alpha.index(S[i])] = True
for i in range(lenT):
    F2[alpha.index(T[i])] = True
for i in range(26):
    if not F1[i] and F2[i]:
        print(-1)
        sys.exit()

ind = S.index(T[0])
ans = ind+1
for i in range(1,lenT):
    S = S[ind+1:] + S[:ind+1]
    ind = S.index(T[i])
    ans += ind+1

print(ans)

Solution (Python):

 

import numpy
import itertools


S = input()
T = input()
lenS = len(S)
lenT = len(T)
F1 = [False]*26
F2 = [False]*26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(lenS):
    F1[alpha.index(S[i])] = True
for i in range(lenT):
    F2[alpha.index(T[i])] = True
for i in range(26):
    if not F1[i] and F2[i]:
        print(-1)
        exit()

ind = S.index(T[0])
ans = ind+1
for i in range(1,lenT):
    S = S[ind+1:] + S[:ind+1]
    ind = S.index(T[i])
    ans += ind+1

print(ans)

Solution (Python):
from collections import defaultdict
import bisect
def main():
    s = list(input())
    t = list(input())
    dic_s = defaultdict(list)
    for i in range(len(s)):
        dic_s[s[i]].append(i)
    x,y = 0,-1
    for i in range(len(t)):
        if dic_s[t[i]] == []:
            print(-1)
            return
        next_y = bisect.bisect_left(dic_s[t[i]],y+1)
        if next_y == len(dic_s[t[i]]):
            x += 1
            y = dic_s[t[i]][0]
        else:
            y = dic_s[t[i]][next_y]
    print(x*len(s)+y+1)

if __name__ == "__main__":
    main()

Solution (Python):

 

import math
from heapq import heappush, heappop,heappushpop
from collections import defaultdict


S = input()
T = input()
lenS = len(S)
lenT = len(T)
F1 = [False]*26
F2 = [False]*26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(lenS):
    F1[alpha.index(S[i])] = True
for i in range(lenT):
    F2[alpha.index(T[i])] = True
for i in range(26):
    if not F1[i] and F2[i]:
        print(-1)
        exit()

ind = S.index(T[0])
ans = ind+1
for i in range(1,lenT):
    S = S[ind+1:] + S[:ind+1]
    ind = S.index(T[i])
    ans += ind+1

print(ans)

Solution (Python):

 
import sys
import math

from heapq import heappush, heappop,heappushpop
from collections import defaultdict


S = input()
T = input()
lenS = len(S)
lenT = len(T)
F1 = [False]*26
F2 = [False]*26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(lenS):
    F1[alpha.index(S[i])] = True
for i in range(lenT):
    F2[alpha.index(T[i])] = True
for i in range(26):
    if not F1[i] and F2[i]:
        print(-1)
        sys.exit()

ind = S.index(T[0])
ans = ind+1
for i in range(1,lenT):
    S = S[ind+1:] + S[:ind+1]
    ind = S.index(T[i])
    ans += ind+1

print(ans)

Solution (Python):

 
import sys
import math

from heapq import heappush, heappop,heappushpop
from collections import defaultdict


S = input()
T = input()
lenS = len(S)
lenT = len(T)
F1 = [False]*26
F2 = [False]*26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(lenS):
    F1[alpha.index(S[i])] = True
for i in range(lenT):
    F2[alpha.index(T[i])] = True
for i in range(26):
    if not F1[i] and F2[i]:
        print(-1)
        sys.exit()

ind = S.index(T[0])
ans = ind+1
for i in range(1,lenT):
    S = S[ind+1:] + S[:ind+1]
    ind = S.index(T[i])
    ans += ind+1

print(ans)

Solution (Python):

 
import sys

from heapq import heappush, heappop,heappushpop
from collections import defaultdict

 
S = input()
T = input()
lenS = len(S)
lenT = len(T)
F1 = [False]*26
F2 = [False]*26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(lenS):
    F1[alpha.index(S[i])] = True
for i in range(lenT):
    F2[alpha.index(T[i])] = True
for i in range(26):
    if not F1[i] and F2[i]:
        print(-1)
        sys.exit()
 
ind = S.index(T[0])
ans = ind+1
for i in range(1,lenT):
    S = S[ind+1:] + S[:ind+1]
    ind = S.index(T[i])
    ans += ind+1
 
print(ans)

Solution (Python):

 
import sys

from heapq import heappush, heappop,heappushpop
from collections import defaultdict

 
S = input()
T = input()
lenS = len(S)
lenT = len(T)
F1 = [False]*26
F2 = [False]*26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(lenS):
    F1[alpha.index(S[i])] = True
for i in range(lenT):
    F2[alpha.index(T[i])] = True
for i in range(26):
    if not F1[i] and F2[i]:
        print(-1)
        sys.exit()
 
ind = S.index(T[0])
ans = ind+1
for i in range(1,lenT):
    S = S[ind+1:] + S[:ind+1]
    ind = S.index(T[i])
    ans += ind+1
 
print(ans)

Solution (Python):

 
import sys

from heapq import heappush, heappop,heappushpop
from collections import defaultdict


S = input()
T = input()
lenS = len(S)
lenT = len(T)
F1 = [False]*26
F2 = [False]*26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(lenS):
    F1[alpha.index(S[i])] = True
for i in range(lenT):
    F2[alpha.index(T[i])] = True
for i in range(26):
    if not F1[i] and F2[i]:
        print(-1)
        sys.exit()

ind = S.index(T[0])
ans = ind+1
for i in range(1,lenT):
    S = S[ind+1:] + S[:ind+1]
    ind = S.index(T[i])
    ans += ind+1

print(ans)

Solution (Python):

 
import sys
import math

from heapq import heappush, heappop,heappushpop
from collections import defaultdict


S = input()
T = input()
lenS = len(S)
lenT = len(T)
F1 = [False]*26
F2 = [False]*26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(lenS):
    F1[alpha.index(S[i])] = True
for i in range(lenT):
    F2[alpha.index(T[i])] = True
for i in range(26):
    if not F1[i] and F2[i]:
        print(-1)
        sys.exit()

ind = S.index(T[0])
ans = ind+1
for i in range(1,lenT):
    S = S[ind+1:] + S[:ind+1]
    ind = S.index(T[i])
    ans += ind+1

print(ans)

Solution (Python):

 
import sys
import math
import bisect

from heapq import heappush, heappop,heappushpop
from collections import defaultdict


S = input()
T = input()
lenS = len(S)
lenT = len(T)
F1 = [False]*26
F2 = [False]*26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(lenS):
    F1[alpha.index(S[i])] = True
for i in range(lenT):
    F2[alpha.index(T[i])] = True
for i in range(26):
    if not F1[i] and F2[i]:
        print(-1)
        sys.exit()

ind = S.index(T[0])
ans = ind+1
for i in range(1,lenT):
    S = S[ind+1:] + S[:ind+1]
    ind = S.index(T[i])
    ans += ind+1

print(ans)

Solution (Python):

 
import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict

S = input()
T = input()
lenS = len(S)
lenT = len(T)
F1 = [False]*26
F2 = [False]*26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(lenS):
    F1[alpha.index(S[i])] = True
for i in range(lenT):
    F2[alpha.index(T[i])] = True
for i in range(26):
    if not F1[i] and F2[i]:
        print(-1)
        sys.exit()

ind = S.index(T[0])
ans = ind+1
for i in range(1,lenT):
    S = S[ind+1:] + S[:ind+1]
    ind = S.index(T[i])
    ans += ind+1

print(ans)

Solution (Python):

 
import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate

S = input()
T = input()
lenS = len(S)
lenT = len(T)
F1 = [False]*26
F2 = [False]*26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(lenS):
    F1[alpha.index(S[i])] = True
for i in range(lenT):
    F2[alpha.index(T[i])] = True
for i in range(26):
    if not F1[i] and F2[i]:
        print(-1)
        sys.exit()

ind = S.index(T[0])
ans = ind+1
for i in range(1,lenT):
    S = S[ind+1:] + S[:ind+1]
    ind = S.index(T[i])
    ans += ind+1

print(ans)

Solution (Python):

 
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

S = input()
T = input()
lenS = len(S)
lenT = len(T)
F1 = [False]*26
F2 = [False]*26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(lenS):
    F1[alpha.index(S[i])] = True
for i in range(lenT):
    F2[alpha.index(T[i])] = True
for i in range(26):
    if not F1[i] and F2[i]:
        print(-1)
        sys.exit()

ind = S.index(T[0])
ans = ind+1
for i in range(1,lenT):
    S = S[ind+1:] + S[:ind+1]
    ind = S.index(T[i])
    ans += ind+1

print(ans)

Solution (Python):

 
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

S = input()
T = input()
lenS = len(S)
lenT = len(T)
F1 = [False]*26
F2 = [False]*26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(lenS):
    F1[alpha.index(S[i])] = True
for i in range(lenT):
    F2[alpha.index(T[i])] = True
for i in range(26):
    if not F1[i] and F2[i]:
        print(-1)
        sys.exit()

ind = S.index(T[0])
ans = ind+1
for i in range(1,lenT):
    S = S[ind+1:] + S[:ind+1]
    ind = S.index(T[i])
    ans += ind+1

print(ans)

Solution (Python):

 
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()
lenS = len(S)
lenT = len(T)
F1 = [False]*26
F2 = [False]*26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(lenS):
    F1[alpha.index(S[i])] = True
for i in range(lenT):
    F2[alpha.index(T[i])] = True
for i in range(26):
    if not F1[i] and F2[i]:
        print(-1)
        sys.exit()

ind = S.index(T[0])
ans = ind+1
for i in range(1,lenT):
    S = S[ind+1:] + S[:ind+1]
    ind = S.index(T[i])
    ans += ind+1

print(ans)

Solution (Python):



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
inde = s.index(t[0])
ans += inde+1
for i in range(1,m):
    s = s[inde+1:] + s[:inde+1]
    inde = s.index(t[i])
    ans += inde+1
print(ans)

Solution (Python):
import bisect

S = input().strip()
T = input().strip()

x = [[] for j in range(26)]

for i, s in enumerate(S):
    x[ord(s) - ord('a')].append(i)

sett = list(set(T))

for t in sett:
    if len(x[ord(t) - ord('a')]) == 0:
        print(-1)
        exit()

c = 0
z = 0

for t in T:
    y = bisect.bisect_left(x[ord(t) - ord('a')], c)
    if y == len(x[ord(t) - ord('a')]):
        z += 1
        c = x[ord(t) - ord('a')][0] + 1
    else:
        c = x[ord(t) - ord('a')][y] + 1

print(z * len(S) + c)

Solution (Python):
import sys
import bisect

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7
alphabet = [chr(i) for i in range(97, 97 + 26)]


def resolve():
    S = input()
    T = input()

    n = len(S)
    m = len(T)

    idx = [[] for _ in range(26)]
    for i in range(n):
        idx[alphabet.index(S[i])].append(i)

    for i in range(n):
        idx[alphabet.index(S[i])].append(i + n)

    res = 0
    pos = 0
    for i in range(m):
        c = alphabet.index(T[i])
        if len(idx[c]) == 0:
            print(-1)
            break
        pos = idx[c][bisect.bisect_left(idx[c], pos)] + 1
        if pos >= n:
            pos -= n
            res += n
    else:
        res += pos
        print(res)


if __name__ == '__main__':
    resolve()

Solution (Python):
import bisect
from collections import Counter
from collections import defaultdict
s=input()
t=input()


sd=Counter(s)
td=Counter(t)
N=len(t)
Ns=len(s)
for ts in td:
    if ts not in sd:
        print(-1)
        exit()
D=defaultdict(list)
for i in range(Ns):
    D[s[i]].append(i)
ans=0
for i in range(N):
    if i==0:
        pre=min(D[t[i]])
    else:
        L=D[t[i]]
        idx=bisect.bisect(L,pre)
        if idx==len(L):
            ans+=Ns
            pre=min(L)
        else:
            pre=L[idx]
    if i==N-1:
        ans+=pre+1
print(ans)

Solution (Python):
S = list(input())
T = list(input())

S = [ord(s)-97 for s in S]
T = [ord(t)-97 for t in T]

N = len(S)

dp = [[-1 for j in range(26)] for i in range(N)]

for i,s in enumerate(S):
    if dp[N-1][s] == -1:
        dp[N-1][s] = i

for i in range(N-2,-1,-1):
    for j in range(26):
        dp[i][j] = dp[i+1][j]
    dp[i][S[i+1]] = i+1

M = len(T)
ans = dp[N-1][T[0]] + 1
now = dp[N-1][T[0]]
if now == -1:
    print(-1)
    exit()
for i in range(1,M):
    t = T[i]
    if dp[now][t] == -1:
        print(-1)
        exit()
    if now < dp[now][t]:
        ans += dp[now][t] - now
    else:
        ans += N + dp[now][t] - now
    now = dp[now][t]

print(ans)

Solution (Python):
S = list(input())
T = list(input())

S = [ord(s)-97 for s in S]
T = [ord(t)-97 for t in T]

N = len(S)

dp = [[-1 for j in range(26)] for i in range(N)]

for i,s in enumerate(S):
    if dp[N-1][s] == -1:
        dp[N-1][s] = i

for i in range(N-2,-1,-1):
    for j in range(26):
        dp[i][j] = dp[i+1][j]
    dp[i][S[i+1]] = i+1

M = len(T)
ans = dp[N-1][T[0]] + 1
now = dp[N-1][T[0]]
if now == -1:
    print(-1)
    exit()
for i in range(1,M):
    t = T[i]
    if dp[now][t] == -1:
        print(-1)
        exit()
    if now < dp[now][t]:
        ans += dp[now][t] - now
    else:
        ans += N + dp[now][t] - now
    now = dp[now][t]

print(ans)

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

ans=0
s_inx=0
t_inx=0
s_l=len(s)
t_l=len(t)

c=0
while t_inx<=t_l-1:
  try:
    temp_inx=s[s_inx:].index(t[t_inx])
    s_inx+=temp_inx+1
    t_inx+=1
    c=0
  except:
    c+=1
    s_inx=0
    ans+=s_l

  if c==2:
    print(-1)
    exit()
else:
  ans+=s_inx

print(ans)

Solution (Python):
from bisect import bisect_left

S = input()
T = input()

if not set(list(T)) <= set(list(S)):
    print(-1)
    exit()

sToI = {s: [] for s in set(list(S))}
for i, s in enumerate(S):
    sToI[s].append(i)

now = 0
R = 0
for t in T:
    I = sToI[t]
    nx = bisect_left(I, now)

    if nx == len(I):
        nx = 0
        R += 1

    now = I[nx] + 1

print(now + R * len(S))

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]

s=input()
t=input()
import collections
dic={}
lim={}
mod=len(s)
for ind,i in enumerate(s):
    if i not in dic:
        dic[i] = []
        lim[i] = 0
    dic[i].append(ind)
    lim[i] += 1
now=0
cnt=0
ok = True

import bisect
for i in t:
    if i not in dic:
        ok=False
        break
    ed = bisect.bisect_left(dic[i],now)
    if ed == lim[i]:
        cnt+=1
        now = dic[i][0]+1
    else:
        now = dic[i][ed]+1
    
if ok==True:
    print(mod*cnt+now)
else:
    print(-1)

Solution (Python):
from collections import defaultdict
import bisect

S = input()
T = input()
d = defaultdict(list)
for i, s in enumerate(S):
    d[s].append(i)

x = -1
cnt = 0

for t in T:
    if len(d[t]) == 0:
        print(-1)
        exit()
    y = bisect.bisect_right(d[t], x)
    if y == len(d[t]):
        cnt += 1
        x = d[t][0]
    else:
        x = d[t][y]

ans = len(S) * cnt + x + 1
print(ans)

Solution (Python):
import bisect

s = input()
t = input()
ls = len(s)
sets = set(s)
sett = set(t)
ind = [[] for _ in range(26)]
temp = 0
ans = 0
if sett.issubset(sets) == False:
    print('-1')
else:
    for i in range(ls):
        ind[ord(s[i])-97].append(i+1)
    for x in t:
        if temp >= ind[ord(x)-97][-1]:
            ans += ls - temp + ind[ord(x)-97][0]
            temp = ind[ord(x)-97][0]
        else:
            bs = bisect.bisect_right(ind[ord(x)-97],temp)
            ans += ind[ord(x)-97][bs] - temp
            temp = ind[ord(x)-97][bs]
    print(ans)