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:
#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
import heapq
#from fractions  import gcd
#input=sys.stdin.readline
import bisect
def my_index(l, x, default=-1):
    if x in l:
        return l.index(x)
    else:
        return default
s=list(input())
n=len(s)
t=input()
m=len(t)

b=0
ans=0
rs=set(s)
for i in range(m):
    if t[i] not in rs:
        print(-1)
        exit()
    ind=my_index(s[b:],t[i])
    if ind==-1:
        ind=my_index(s,t[i])+1
        ans+=n-b+ind
        b=ind
    else:
        ans+=ind+1
        b+=ind+1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
import heapq
#from fractions  import gcd
#input=sys.stdin.readline
import bisect
def my_index(l, x, default=-1):
    if x in l:
        return l.index(x)
    else:
        return default
s=list(input())
n=len(s)
t=input()
m=len(t)
dic={}
for i in range(len(s)):
    if s[i] not in dic:
        dic[s[i]]={i}
    else:
        dic[s[i]].add(i)
b=0
ans=0
for i in range(m):
    if t[i] not in dic:
        print(-1)
        exit()
    ind=my_index(s[b:],t[i])
    if ind==-1:
        ind=my_index(s,t[i])+1
        ans+=n-b+ind
        b=ind
    else:
        ans+=ind+1
        b+=ind+1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left

s = input()
t = input()
n = len(s)

dict_s = {}
for i, c in enumerate(s * 2):
    dict_s[c] = dict_s.get(c, []) + [i]


dict_next_i = {
    c: [dict_s[c][bisect_left(dict_s[c], i + 1)] for i in range(n)] for c in dict_s.keys()
}


def next_i(i, c):
    if c not in dict_s.keys():
        return -1
    else:
        return n * (i // n) + dict_next_i[c][i % n]


def answer():
    i = -1
    for c in t:
        i = next_i(i, c)
        if i == -1:
            return -1
    return i + 1  # 1-origin


print(answer())

Submission status: Time Limit Exceeded

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

dict_s = {}
for i, c in enumerate(s * 2):
    dict_s[c] = dict_s.get(c, []) + [i]


# dict_next_i = {
#     c: [min(j for j in dict_s[c] if j > i) for i in range(n)] for c in dict_s.keys()
# }
dict_next_i = {c: [None] * n for c in dict_s.keys()}

for c in dict_s.keys():
    for i in range(n):
        for j in dict_s[c]:
            if j > i:
                dict_next_i[c][i] = j
                break


def next_i(i, c):
    if c not in dict_s.keys():
        return -1
    else:
        return n * (i // n) + dict_next_i[c][i % n]


def answer():
    i = -1
    for c in t:
        i = next_i(i, c)
        if i == -1:
            return -1
    return i + 1  # 1-origin


print(answer())

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
from collections import Counter

s=input()
t=input()


ds=Counter(s)
dt=Counter(t)

for k in dt.keys():
    if not k in ds.keys():
        print(-1)
        exit()

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


T=deque(list(t))

cnt=0
idx=0
while T:
    t1=T.popleft()
    temp=""
    for i in range(Ls):
        if t1==s[i]:
            temp+=t1
            idx+=1
            if idx==Lt:
                print(cnt*Ls+i+1)
                exit()
            else:
                t1=T.popleft()
    cnt+=1
    T.appendleft(t1)

Submission status: Time Limit Exceeded

Solution code:
import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
# from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
# import numpy as np


mod = int(pow(10, 9) + 7)
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def L(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]

try:
    # sys.setrecursionlimit(int(pow(10,6)))
    sys.stdin = open("input.txt", "r")
    # sys.stdout = open("out.txt", "w")
except:
    pass


# @lru_cache(None)

s=input()
t=input()
j=0
i=0
c=0
T=list(t)
S=list(s)
x=len(set(T))
y=len((set(T)).intersection(set(S)))
if x!=y:
    print(-1)
    exit()

while(j!=len(t)):

    c+=1
    if s[i]==t[j]:
        j+=1
    i+=1
    i%=len(s)
print(c)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left as bisect
from collections import defaultdict


s, t = input(), input()

if not all(i in s for i in t):
    print(-1)
    exit()

alp = defaultdict(list)
for i, si in enumerate(s):
    alp[si].append(i)

ans, cnt = 1 - len(s), 0
for i in t:
    l = alp[i]
    ans += len(s) * (cnt == 0)
    num = bisect(l, cnt)
    ans += len(s) * (num == len(l))
    cnt = (l[num % len(l)] + 1) % len(s)
print(ans + (cnt - 1) % len(s))

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left as bisect

s, t = input(), input()
lens = len(s)


def index(I, CNT, ANS):
    LIST = alp[ord(I) - 97]
    NUM = bisect(LIST, CNT)
    ANS += (NUM == len(LIST)) * lens
    CNT = (LIST[NUM % len(LIST)] + 1) % lens
    return ANS, CNT

if not all(i in s for i in t):
    print(-1)
    exit()

alp = [[] for i in range(26)]
for i, si in enumerate(s):
    alp[ord(si) - 97].append(i)

ans, cnt = 1 - lens, 0
for i in t:
    ans += (cnt == 0) * lens
    ans, cnt = index(i, cnt, ans)
print(ans + (cnt - 1) % lens)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left as bisect

def index(I, CNT, ANS):
    LIST = alp[ord(I) - 97]
    NUM = bisect(LIST, CNT)
    ANS += (NUM == len(LIST)) * len(s)
    CNT = (LIST[NUM % len(LIST)] + 1) % len(s)
    return ANS, CNT

s, t = input(), input()

if not all(i in s for i in t):
    print(-1)
    exit()

alp = [[] for i in range(26)]
for i, si in enumerate(s):
    alp[ord(si) - 97].append(i)

ans, cnt = 1 - len(s), 0
for i in t:
    ans += (cnt == 0) * len(s)
    ans, cnt = index(i, cnt, ans)
print(ans + (cnt - 1) % len(s))

Submission status: Time Limit Exceeded

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

if not all(i in s for i in t):
    print(-1)
    exit()

lens = len(s)
ans, cnt = 1 - lens, 0
for i in t:
    ans += (cnt == 0) * lens
    if i in s[cnt:]:
        cnt = (cnt + s[cnt:].index(i) + 1) % lens
    else:
        ans += lens
        cnt = (s.index(i) + 1) % lens
print(ans + (cnt - 1) % lens)

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
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)

Submission status: Time Limit Exceeded

Solution code:
import copy

s = input()
t = input()

a = [[] for i in range(26)]
for i in range(len(s)):
    num = ord(s[i]) - 97
    a[num].append(i)

flg = True
cnt = 0
x = -1
for i in range(len(t)):
    num = ord(t[i]) - 97
    if not a[num]:
        flg = False
        break
    for j in range(len(a[num])):
        if x < a[num][j]:
            x = copy.deepcopy(a[num][j])
            break
        if j == len(a[num])-1:
            cnt += 1
            x = copy.deepcopy(a[num][0])
            break
print(cnt*len(s) + x+1) if flg else print(-1)

Submission status: Time Limit Exceeded

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

n = len(s)

if set(list(t)) != (set(list(s)) & set(list(t))): ans = -1
else:
    dct = {}

    for i, ss in enumerate(s):
        if ss in dct:
            dct[ss].append(i)
        else: dct[ss] = [i]

    idx = -1
    ans = 0
    for i, tt in enumerate(t):
        jdg = True
        for j in dct[tt]:
            if idx < j:
                idx = j
                jdg = False
                break
        if jdg:
            idx = dct[tt][0]
            ans += n
    ans += idx + 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
from bisect import bisect_right

s = input()
t = input()

for ch in t:
    if ch not in s:
        print("-1")
        exit()

indices = defaultdict(list)
for i, v in enumerate(s):
    indices[v].append(i)

cnt = 0
cur = -1
mod = len(s)
for v in t:

    i = bisect_right(indices[v], cur)
    if i == len(indices[v]):
        j = indices[v][0]
    else:
        j = indices[v][i]

    if cur >= j:
        cnt += 1
    cur = j
print(cnt * mod + cur + 1)

Submission status: Time Limit Exceeded

Solution code:
s = input()
t = input()
l_s = len(s)
l_t = len(t)

s2 = s + s
l_s2 = len(s2)

next_idx_1 = [[l_s2] * 26 for i in range(l_s2+1)]

for i in reversed(range(l_s2)):
    for j in range(26):
        next_idx_1[i][j] = next_idx_1[i+1][j]
    next_idx_1[i][ord(s2[i]) - ord("a")] = i

next_idx_2 = [[l_s2] * 26 for i in range(l_s2)]

for i in reversed(range(1, l_s2)):
    for j in range(26):
        next_idx_2[i-1][j] = next_idx_2[i][j]
    next_idx_2[i-1][ord(s2[i]) - ord("a")] = i

cur_idx = 0
for i in range(l_t):
    if i == 0:
        idx = next_idx_1[cur_idx%l_s][ord(t[i]) - ord("a")]

    else:
        idx = next_idx_2[cur_idx%l_s][ord(t[i]) - ord("a")]

    if idx == l_s2:
        print("-1")
        exit()

    cur_idx += idx - (cur_idx % l_s)
    
print(cur_idx+1)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

S = input()
S2 = S + S
T = input()
SC = Counter(S)
TC = Counter(T)

N = len(S)
ans = 0
now = 0

for t in TC.keys():
    if t not in SC:
        print(-1)
        exit()

for t in T:
    i = S2[now: now + N].find(t)
    ans += i + 1
    now += i + 1
    now %= N

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def main():
    S = SI()
    T = SI()
    if set(S) | set(T) != set(S):
        print(-1)
        exit()
    s2i = {}

    for i, s in enumerate(S):
        if s in s2i:
            s2i[s].append(i)
        else:
            s2i[s] = [i]

    now = 0
    digit = -1
    for t in T:
        ng = True
        for idx in s2i[t]:
            if digit < idx:
                digit = idx
                ng = False
                break
        if ng:
            digit = s2i[t][0]
            now += 1
    print(now * len(S) + digit + 1)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def solve():
  s = input()
  t = input()
  ans = 0
  alp = [[] for _ in range(27)]
  length_s = len(s)
  for i in range(length_s):
    char = ord(s[i]) - ord('a')
    alp[char].append(i + 1)
  length_t = len(t)
  import bisect
  for i in range(length_t):
    char = ord(t[i]) - ord('a')
    if alp[char]:
      if ans % length_s >= max(alp[char]):
        ans = (ans // length_s + 1) * length_s
      index = bisect.bisect_right(alp[char], ans % length_s)
      ans += alp[char][index] - ans % length_s
    else:
      return -1
  return ans
print(solve())

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_right
s, t = open(0).read().split()
S, T = set(s), set(t)
if T & S != T:
    print(-1)
    quit()
d, l = dict(), dict()
for idx, c in enumerate(s):
    d[c] = d.get(c, []) + [idx]
    l[c] = l.get(c, 0) + 1
prev = -1
loop = 0
for c in t:
    idx = bisect_right(d[c], prev)
    if idx >= l[c]:
        loop += 1
        prev = d[c][0]
    else:
        prev = d[c][idx]
print((loop) * len(s) + prev + 1)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
s, t = [x.strip() for x in open(0).readlines()]
if set(t) - set(s) != set():
    print(-1)
else:
    d = defaultdict(list)
    n = len(s)
    for i, c in enumerate(s, 1):
        d[c].append(i)
    for k in d:
        d[k].append(d[k][0] + n)
    ans = 0
    d
    for c in t:
        q, r = divmod(ans, n)
        for x in d[c]:
            if r < x:
                ans = n*q + x
                break
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict, deque
s, t = [x.strip() for x in open(0).readlines()]
if set(t) - set(s) != set():
    print(-1)
else:
    d = defaultdict(deque)
    n = len(s)
    for i, c in enumerate(s, 1):
        d[c].append(i)
    ans = 0
    for c in t:
        while True:
            i = d[c].popleft()
            d[c].append(i + n)
            if ans < i:
                ans = i
                break
    print(ans)

Submission status: Time Limit Exceeded

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

t.reverse()

S = set(list(s))
for c in t:
    if c not in S:
        print(-1)
        exit()

cnt = 0
#print(s)
#print(t)
while True:
    for i in range(len(s)):
        if t:
            if s[i] == t[-1]:
                t.pop()
        else:
            break
    else:
        cnt += 1
        continue
    break
print(cnt*len(s)+i)

Submission status: Time Limit Exceeded

Solution code:
import collections
s = raw_input()
t = raw_input()
i = 0
c = 0
while(True):
	progress = False
	j = 0
	for j in range(len(s)):
		if s[j] == t[i]:
			i+=1
			progress = True
			if i == len(t): break
	if progress is False: break
	if i == len(t): break
	c += 1
print c * len(s) + j + 1 if progress else -1

Submission status: Time Limit Exceeded

Solution code:
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
    slist=copy.deepcopy(sbasic)
    index=-1
  pos=bisect.bisect_right(sbasic[ord(t)-ord("a")],index)
  if pos==len(sbasic[ord(t)-ord("a")]):
    roop+=1
    slist=copy.deepcopy(sbasic)
    index=-1
    pos=bisect.bisect_right(sbasic[ord(t)-ord("a")],index)
  index=sbasic[ord(t)-ord("a")][pos]
  
print(len(S)*roop+index+1)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
 
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)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
 
import sys
# 以下を記述しないとTLEになる, 何故？
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)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
 
import sys
# 以下を記述しないとTLEになる, 何故？
from itertools import count, cycle, repeat
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)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
 
import sys
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)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
 
import sys
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)

Submission status: Time Limit Exceeded

Solution code:
A = tuple(range(12700))

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)

Submission status: Time Limit Exceeded

Solution code:
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)

Submission status: Time Limit Exceeded

Solution code:
A = 0






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)

Submission status: Time Limit Exceeded

Solution code:
A = [0] * (2 * 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)

Submission status: Time Limit Exceeded

Solution code:
A = [0] * (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)

Submission status: Time Limit Exceeded

Solution code:
A = [0] * (5 * 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)

Submission status: Time Limit Exceeded

Solution code:
A = [0] * (10**6)

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)

Submission status: Time Limit Exceeded

Solution code:
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)

Submission status: Time Limit Exceeded

Solution code:
import heapq
import bisect

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)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
import bisect
import subprocess
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)

Submission status: Time Limit Exceeded

Solution code:
import sys
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)
        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)

Submission status: Time Limit Exceeded

Solution code:
import sys
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)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
import bisect
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)
        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)

Submission status: Time Limit Exceeded

Solution code:
import sys
import bisect
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)
        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)

Submission status: Time Limit Exceeded

Solution code:
import bisect
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)

Submission status: Time Limit Exceeded

Solution code:
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)

Submission status: Time Limit Exceeded

Solution code:
import sys
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)
        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)

Submission status: Time Limit Exceeded

Solution code:
import sys

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)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
import bisect
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)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
 
# 以下を記述しないとTLEになる, 何故？
from collections import Counter
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)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
 
# 以下を記述しないとTLEになる, 何故？
import collections
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)