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: Wrong Answer

Solution code:
import numpy as np

s = input()
t = input()

N = len(s)

tN = len(t)


def alpha_index(c):
    return ord(c) - ord("a")


# 次の特定の文字が何文字先にあるか
next_string = np.full((N, 26), 0)


def sarch_char(nowindex, c):
    if next_string[nowindex, alpha_index(c)] != 0:
        return next_string[nowindex, alpha_index(c)]
    else:
        for i in range(1, N):
            if s[(i + nowindex) % N] == c:
                next_string[nowindex, alpha_index(c)] = i
                return i
        next_string[nowindex, alpha_index(c)] = -1
        return -1


if not t[0] in s:
    print(-1)
    exit()

ans = s.find(t[0])
index = ans

for i in range(1, tN):
    nextindex = sarch_char(index, t[i])
    print(nextindex)
    if nextindex == -1:
        print(-1)
        exit()
    ans += nextindex
    index = int(ans % N)

print(ans + 1)

Submission status: Wrong Answer

Solution code:
import bisect
s = input()
t = input()
lens=len(s)
lent=len(t)
d = {}

for i in range(lens):
    if s[i] in d:
        d[s[i]].append(i)
    else:
        d[s[i]] = [i]

tmp=0
r=0
snum=0
for i in range(lent):
    if d.get(t[i],None)==None:
        print("-1")
        exit()
    else:
        lis=d[t[i]]
        lis.sort()
        r = bisect.bisect_left(lis,tmp)
        if(len(lis) <= r):
            snum+=lens
            tmp=lis[0]
        else:
            tmp=lis[r]+1
print(snum+tmp)

Submission status: Wrong Answer

Solution code:
s = str(input())
t = str(input())
count = 0
result = True

for i in range(len(t)):
  if not (t[i] in s):
    print(-1)
    result = False
  else:
    new_s = s*2
    count += new_s.find(t[i],count%len(s))
    
if result:
  print(count)
else:
  print(-1)

Submission status: Wrong Answer

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

ans = -1
i = 0

for t in T:
    try:
        S[i:].index(t)
    except Exception:
        print(-1)
        exit()

Submission status: Wrong Answer

Solution code:
s,t = [map(str,input()) for i in range(2)]
s_list = list(s)
t_list = list(t)
res = 0
count = 0
check_len = 0

for i in t_list:
    for j in s_list:
        count += 1
        if i == j:
            break
        elif count == len(s_list):
            res = -1
    if res ==  -1:
        break
    count = 0

if res != -1:
    for i in t_list:
        for j in s_list:
            res += 1
            if i == j:
                check_len += 1
                if check_len == len(t_list):
                    break

print(res)

Submission status: Wrong Answer

Solution code:
#coding: utf-8
s = str(input())
t = str(input())
ans = 0
ans1 = 0
# s.sort(reversed=True)
# t.sort(reversed=True)
flg = 0

for i in range(len(t)):
	for j in range(len(s)):
		if t[i] == s[j]:
			print(j)
			flg = 1
			if j >= ans1:
				ans = ans + j +1
				ans1 = j
			else:
				ans = ans + len(t) + j+1 - ans1+1
				ans1 = j
			break
		# elif t[i] < s[j]:
		# 	flg = 0
		# 	break
	if flg == 0:
		ans = -1
		break
	else:
		flg = 0

print(ans)

Submission status: Wrong Answer

Solution code:
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; cnt = 0; ansli = []; tmpli = []; candili = []; stillset = set()
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]#WideIntPoint
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def s_list(): return list(input())
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]#HeightString
def mi(n): return [wi() for _ in range(n)]#MatrixInt
def num__grid(n): return [[int(i) for i in sys.stdin.readline().split()[0]] for _ in range(n)]#NumberGrid
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]
def grid(n): return [s_list() for _ in range(n)]


s = input()
t = input()
ns = len(s)
nt = len(t)
alpha = "abcdefghijklmnopqrstuvwxyz"
aldi = {}
for i in alpha:
    aldi[i] = []

for i in range(ns):
    aldi[s[i]].append(i + 1)


num = []
if aldi[t[0]] == []:
    print("-1")
    exit()
mae = aldi[t[0]][0]
num.append(mae)
for i in range(1, nt):
    if aldi[t[i]] == []:
        print("-1")
        exit()
    else:
        kk = bisect_right(aldi[t[i]], mae)
        if kk == len(aldi[t[i]]):
            kk = aldi[t[i]][0]
        else:
            kk = aldi[t[i]][kk]
        mae = kk
        num.append(kk)

ans += num[0]
for i in range(1, len(num)):
    if num[i] < num[i - 1]:
        ans += ns - num[i - 1] + num[i]
    else:
        ans += num[i] - num[i - 1]

if ans > 10 ** 100:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

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

ds = {}
dsl ={}
dt = {}
for i in range(len(s)):
    if s[i] not in ds.keys():    
        ds[s[i]] = 0
        dsl[s[i]] = []
    ds[s[i]]+=1
    dsl[s[i]].append(i)

for i in range(len(t)):
    if t[i] not in dt.keys():    
        dt[t[i]] = 0
    if t[i] not in ds.keys():
        ds[t[i]] = 0
    dt[t[i]]+=1

import bisect
ans = 1
now = 0
for i in range(len(t)):
    if ds[t[i]] == 0:
        print("-1")
        exit()
    elif ds[t[i]] == 1:
        if dsl[t[i]][0] > now:
            ans += (dsl[t[i]][0] - now)
        elif dsl[t[i]][0] == now:
            ans+=len(s)
        else:
            ans += dsl[t[i]][0] +(len(s) - now)
        now = dsl[t[i]][0] 
    else:
        index = bisect.bisect_right(dsl[t[i]],now)
        if index == len(dsl[t[i]]):
            index = 0
        if dsl[t[i]][index] == now:
            index = bisect.bisect_right(dsl[t[i]],now+1)
            if index == len(dsl[t[i]]):
                index = 0            
        indexv = dsl[t[i]][index]
        if indexv > now:
            ans+= indexv - now
            now = indexv
        else:
            index = 0
            ans+= dsl[t[i]][0] + (len(s) - now)
            now = dsl[t[i]][0] 

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
import math
import sys
import bisect
sys.setrecursionlimit(10000000)

def main():
    s = input()
    t = input()
    alphabets = dict.fromkeys(list("abcdefghijklmnopqrstuvwxyz"), None)
    # alphabets = alphabet.copy()
    for key_ in alphabets:
        lists = []
        for i in range(len(s)):
            if key_ == s[i]:
                lists.append(i)
        alphabets[key_] = lists
    # print(alphabets)
    now = -1
    count = 0
    for alpha in t:
        if len(alphabets[alpha]) == 0:
            print(-1)
            sys.exit()
        else:
            lists = alphabets[alpha]
            index = bisect.bisect_left(lists, now)
            if index >= len(lists):
                now = -1
                count += len(s)
                index = bisect.bisect_left(lists, now)
                now = lists[index]
            else:
                now = lists[index]
            # print(now, count)
    print(count + now + 1)
            







if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline

s = input().strip()
t = input().strip()

sd = {}
for c in s:
    if c not in sd:
        sd[c] = 1
    else:
        sd[c] += 1
td = {}
for c in t:
    if c not in td:
        td[c] = 1
    else:
        td[c] += 1

if not all(k in sd for k in td.keys()):
    print(-1)
    exit()

ls = len(s)
x = 0
k = 0
for i,c in enumerate(t):
    while True:
        tmp = s[k:].find(c)
        if tmp >= 0:
            k += tmp
            break
        else:
            x += ls
            k = 0
first = s.find(t[0])
print(x + k + 1)

Submission status: Wrong Answer

Solution code:
import bisect

S = input()
T = input()

d = [[] for _ in range(27)]
alpha2num = lambda c: ord(c) - ord("a") + 1
ans = 0

for i, s in enumerate(S):
    d[alpha2num(s)].append(i)

idx = -1
for t in T:
    dl = d[alpha2num(t)]
    if not(dl):
        ans = -1
        break
    insert_idx = bisect.bisect_left(dl, idx)
    if insert_idx == len(dl):
        ans += (len(S) - idx + dl[-1])
        idx = dl[-1]
    else:
        ans += dl[insert_idx] - idx
        idx = dl[insert_idx]

print(ans)

Submission status: Wrong Answer

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

dic = {}
for i in range(len(s)):
    c = s[i]
    if c not in dic:
        dic[c] = []
    dic[c].append(i)

repeat = 0
focus = 0
ans = 0
for i in range(len(t)):
    c = t[i]
    if c not in dic:
        ans = -1
        break
    if dic[c][-1] <= focus:
        repeat+=1
        focus = dic[c][0]
    else:
        l = 0
        r = len(dic[c])
        while r == l:
            mid = (r+l)//2
            if(dic[c][mid] < focus):
                l = mid + 1
            else:
                r = mid
        focus = dic[c][l]

print(-1 if ans == -1 else repeat*len(s)+focus+1)

Submission status: Wrong Answer

Solution code:
t = input()
s = input()
# t = "contest"
# s = "son"
list = []
re_i = 0
count = 0

for i in s:
    re_i = t[re_i:].find(i)
    if re_i  >=0:
        ab_i = t.find(i)
        list.append(ab_i)
    else:
        ab_i = t.find(i)
        count += 1
        if ab_i >=0:
            list.append(ab_i)

if len(list)!= len(s):
    print(-1)
else:
    print((count-1)*len(t)+list[-1]+1)

Submission status: Wrong Answer

Solution code:
import bisect

s = input()
t = input()
dic = {}

for i in "abcdefghijklmnopqrstuvwxyz":
    dic[i] = []

for i in range(len(s)):
    dic[s[i]].append(i)

f = 0
ans = 0
for moji in t:
    if len(dic[moji]) == 0:
        print(-1)
        exit()

    index = bisect.bisect_right(dic[moji], f)
    if index == len(dic[moji]):
        ans += len(s)
        f = dic[moji][0]
    else:
        f = dic[moji][index]



ans += f + 1
print(ans)

Submission status: Wrong Answer

Solution code:
# s = input()
# t = input()
s="contest"
t="sentence"
#解の存在確認
s_set = set(s)
t_set = set(t)
i = 0
if not t_set <= s_set:
    print(-1)
else:
    search_str = s
    num_s = len(search_str)
    num = len(t)
    j = 0
    idx = 0
    c = t[j]
    while j < num:
        prev = -1
        while True:
            idx = search_str.find(c, prev + 1)
            if idx == -1:#見つからない
                i += num_s - prev - 1
                break
            else:
                i += idx - prev
                j += 1
                if j >= num:
                    break
                c = t[j]
            prev = idx
    # num = len(t)
    # j = 0
    # while j < num:
    #     c = t[j]
    #     for d in s:
    #         i += 1
    #         if c == d:
    #             j += 1
    #             if j >= num:
    #                 break
    #             c = t[j]
    print(i)

Submission status: Wrong Answer

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

i = 0
j = 0
flag = 0
while(i < len(t)-1):
    pt = i
 #   print(i)
    for j in range(len(s)):
        if(t[i] == s[j]):
            i += 1
            if(i ==  len(t)-1):
                break
    if(pt == i):
        flag = 1
        break
    
#print("\nans")
if(flag == 1):
    print(-1)
else:
    print((i-1)*len(s)+(j+2))

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations
import sys
import bisect
import string
sys.setrecursionlimit(10**6)
def SI():
    return input().split()
def MI():
    return map(int,input().split())
def I():
    return int(input())
def LI():
    return [int(i) for i in input().split()]
YN=['Yes','No']
mo=10**9+7

s=input()
t=input()
s=list(s)
t=list(t)
Ss=set(s)
St=set(t)
n=len(s)

alp=string.ascii_lowercase
di=dict([(i,j) for j,i in enumerate(alp)])
#print(di)
g=[{} for _ in range(n)]
fp=[-1 for i in range(26)]
lp=[-1 for i in range(26)]

for i,c in enumerate(s):
    fp[di[c]]=i
    for next_c in [di[l] for l in Ss]:
        if fp[next_c]!=-1:
            g[i][next_c]=(fp[next_c],1)

for i in range(n)[::-1]:
    lp[di[s[i]]]=i
    for next_c in [di[l] for l in Ss]:
        if lp[next_c]!=-1:
            g[i][next_c]=(lp[next_c],0)

m=[[] for i in range(n)]


#print(Ss,St,St-Ss)
if len(St-Ss)!=0:
    ans=-1
else:
    loop=0
    has=0
    st=di[t[0]]
    loop+=g[0][st][1]
    i_n=g[0][st][0]
    has=i_n
    for i in range(1,len(t)):
        ch=di[t[i]]
        loop+=g[i_n][ch][1]
        i_n=g[i_n][ch][0]
        has=g[i_n][ch][0]
        #print(loop,has,i_n,g[i_n][ch])
    ans=loop*n+has+1
print(ans)#,loop,has)
#print([[i,di[i]] for i in s])
'''
for i in g:
    print(sorted(list(i.items()),key=lambda x:x[0]))
'''

Submission status: Wrong Answer

Solution code:
s=input()
t=input()
ans=0
ti=0
si=0
f=0
try:
  for ti in range(len(t)):
    while t[ti]!=s[si]:
      ans+=1
      si+=1
      si=si%len(s)
      f+=1
      if f>len(s)+1000000:
        exit(0)
    si+=1
    ti+=1
    ans+=1
    f=0
except: 
  print("-1")
  exit(0)
  
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
s = input()
t = input()
lens=len(s)
lent=len(t)
d = {}

for i in range(lens):
    if s[i] in d:
        d[s[i]].append(i)
    else:
        d[s[i]] = [i]

tmp=0
r=0
snum=0
for i in range(lent):
    if d.get(t[i],None)==None:
        print("-1")
        exit()
    else:
        lis=d[t[i]]
        lis.sort()
        r = bisect.bisect_left(lis,tmp)
        if(len(lis) <= r):
            snum+=lens
            tmp=lis[0]
        else:
            tmp=lis[r]+1
print(snum+tmp+1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

def find(array, n):
    for i, number in enumerate(array):
        if number > n:
            return number
    return array[0]

s = input().strip()
t = input().strip()
n, m = len(s), len(t)
dic = {}
ans = 0
for i, letter in enumerate(s):
    if letter in dic:
        dic[letter].append(i)
    else:
        dic[letter] = [i]
try:
    cur = dic[t[0]][0]
except:
    print(-1)
    exit()
for i in range(1,m):
    try:
        pos = find(dic[t[i]], cur)
    except:
        print(-1)
        exit()
    if cur < pos:
        cur = pos
    else:
        ans += n
        cur = pos
ans += find(dic[t[m-1]], cur) + 1
print(ans)

Submission status: Wrong Answer

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

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

if set_s != set_t:
    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: Wrong Answer

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+1]+s).index(i)
        b = s.index(i)
print(j)

Submission status: Wrong Answer

Solution code:
import sys

s = input()
t = input()

i = 0
p_past = 0

for c in t:
    if s.find(c) < 0:
        print(-1)
        sys.exit()

    p = s.find(c, p_past + 1)

    if p > p_past:
        p_past = p
    else:
        i += 1
        p_past = s.find(c)

print(i * len(s) + s.find(c) + 1)

Submission status: Wrong Answer

Solution code:
S=input()
T=input()
dp=[[10**6]*len(S) for i in range(26)]
for i,s in enumerate(S):
    dp[ord(s)-ord('a')][i]=0
for i in range(len(S)-1,-1,-1):
    for j in range(26):
        dp[j][i-1]=min(dp[j][i-1],dp[j][i]+1)
for i in range(len(S)-1,-1,-1):
    for j in range(26):
        dp[j][i-1]=min(dp[j][i-1],dp[j][i]+1)
x=1
i=0
for t in T:
    ti=ord(t)-ord('a')
    if dp[ti][i]<1e6:
        x=x+dp[ti][i] if dp[ti][i]>0 else x+len(S)
        i=(i+dp[ti][i])%len(S)
    else:
        x=-1
        break
print(x)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from collections import defaultdict
from copy import deepcopy
from bisect import bisect_right


## input
def main():
  S = str(input())
  T = str(input())
  sdict = defaultdict(lambda:set([]))
  for i,s in enumerate(S):
    sdict[s].add(i)
  for s in sdict:
    sdict[s] = list(sorted(sdict[s]))

  idx = 0
  Tidx = [0 for _ in range(len(T)+1)]
  for i,t in enumerate(T):
    if len(sdict[t]) == 0:
      return -1
    ti = bisect_right(sdict[t],idx)
    if ti < len(sdict[t]):
      Tidx[i+1] = sdict[t][ti]
    else:
      Tidx[i+1] = sdict[t][0]
    idx = Tidx[i+1]

  ans = 0
  slen = len(S)
  for i in range(1,len(Tidx)):
    if Tidx[i-1] < Tidx[i]:
      ans += Tidx[i]-Tidx[i-1]
    else:
      ans += slen-Tidx[i-1]+Tidx[i]
  return ans

if __name__ == "__main__":
  ans = main()
  print(ans)

Submission status: Wrong Answer

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
import fractions
from functools import reduce
def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)


s = list(input())

t = list(input())
flag = -1
on = [[-1 for j in range(len(s))] for i in range(26)]

al = [-1 for i in range(26)]
alll = [0 for i in range(26)]
for i in range(len(s)):
    k = ord(s[i]) - 97
    on[k][i] = i
    if al[k] == -1:
        al[k] = i

for i in range(26):
    for j in range(len(s)-2,-1,-1):
        if on[i][j+1] != -1 and on[i][j] == -1:
            on[i][j] = on[i][j+1]

co = 0
su = 0
ppp = 0
t = cl.deque(t)
while t:
    k = t.popleft()
    kkk = al[ord(k)-97]
    ppp = kkk+1
    if kkk == -1:
        print(kkk)
        exit()
    while True:
        if len(t) == 0:
            break
        k = t.popleft()
        kk = on[ord(k)-97][kkk]
        if kkk >= kk:
            break
        else:
            ppp = kk+1
            kkk = kk
    if len(t) != 0:
        co += 1

print(len(s)*co + ppp)

Submission status: Wrong Answer

Solution code:
t = input()
s = input()
# t = "contest"
# s = "son"
list = []
re_i = 0
count = 0

for i in s:
    re_i = t[re_i:].find(i)
    if re_i  >=0:
        ab_i = t.find(i)
        list.append(ab_i)
    else:
        ab_i = t.find(i)
        if ab_i >=0:
            list.append(ab_i)
            count += 1
if len(list)!= len(s):
    print(-1)
else:
    print((count-1)*len(t)+list[-1]+1)

Submission status: Wrong Answer

Solution code:
import bisect

S = input()
T = input()

d = [[] for _ in range(27)]
alpha2num = lambda c: ord(c) - ord("a") + 1
ans = 0

for i, s in enumerate(S):
    d[alpha2num(s)].append(i)

idx = -1
for t in T:
    dl = d[alpha2num(t)]
    if not(dl):
        ans = -1
        break
    insert_idx = bisect.bisect_left(dl, idx)
    if insert_idx == len(dl):
        ans += (len(S) - idx + dl[-1])
        idx = dl[0]
    else:
        ans += dl[insert_idx] - idx
        idx = dl[insert_idx]

print(ans)

Submission status: Wrong Answer

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


sl = len(s)
tl = len(t)
miss = False
for ttt in range(0, tl):
    if s.find(t[ttt]) == -1:
        miss = True

i = 0
index = 0
if miss:
    print("-1")
else:
    while i <= 10^100 * sl:
        tmps = s
        find = True
        while find:
            tmpsl = len(tmps)
            check = tmps.find(t[index])

            if check != -1:
                if tmpsl == check + 1:
                    i += tmpsl
                    index += 1
                    find = False
                    tmps = ""
                else:
                    i += check + 1
                    index += 1
                    tmpss = tmps[check + 1:]
                    tmps = tmpss
            else:
                i += tmpsl
                find = False
                tmps = ""
            if index > tl - 1:
                break
        if index  > tl -1:
            break
    print(i)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations
import sys
import bisect
import string
sys.setrecursionlimit(10**6)
def SI():
    return input().split()
def MI():
    return map(int,input().split())
def I():
    return int(input())
def LI():
    return [int(i) for i in input().split()]
YN=['Yes','No']
mo=10**9+7

s=input()
t=input()
s=list(s)
t=list(t)
Ss=set(s)
St=set(t)
n=len(s)

alp=string.ascii_lowercase
di=dict([(i,j) for j,i in enumerate(alp)])
#print(di)
g=[{} for _ in range(n)]
fp=[-1 for i in range(26)]
lp=[-1 for i in range(26)]

for i,c in enumerate(s):
    fp[di[c]]=i
    for next_c in [di[l] for l in Ss]:
        if fp[next_c]!=-1:
            g[i][next_c]=(fp[next_c],1)

for i in range(n)[::-1]:
    lp[di[s[i]]]=i
    for next_c in [di[l] for l in Ss]:
        if lp[next_c]!=-1:
            g[i][next_c]=(lp[next_c],0)

m=[[] for i in range(n)]


#print(Ss,St,St-Ss)
if len(St-Ss)!=0:
    ans=-1
else:
    loop=0
    has=0
    st=di[t[0]]
    i_n=g[0][st][0]
    has=i_n
    for i in range(1,len(t)):
        ch=di[t[i]]
        loop+=g[i_n][ch][1]
        i_n=g[i_n][ch][0]
        has=g[i_n][ch][0]
        #print(loop,has,i_n,g[i_n][ch])
    ans=loop*n+has+1
print(ans)#,loop,has)
#print([[i,di[i]] for i in s])
'''
for i in g:
    print(sorted(list(i.items()),key=lambda x:x[0]))
'''

Submission status: Wrong Answer

Solution code:
import bisect

s = input()
t = input()

s_dict = {}
for i, si in enumerate(s):
    s_dict[si] = [i] if si not in s_dict else s_dict[si] + [i]

# print(s_dict)

s_len = len(s)
counter = 0
fail = False
memo = {}
for ti in t:
    if ti not in s_dict:
        fail = True
        break
    # counter in s
    c_in_s = counter % s_len
    c = 0
    if (ti, c_in_s) not in memo:
        c = bisect.bisect(s_dict[ti], c_in_s)
        memo[(ti, c_in_s)] = c
    else:
        c = memo[(ti, c_in_s)]
    if c == len(s_dict[ti]):
        counter += s_len - c_in_s
        counter += s_dict[ti][0]
    else:
        counter += s_dict[ti][c] - c_in_s

print(counter + 1 if not fail else '-1')

Submission status: Wrong Answer

Solution code:
s=input()
t=input()
ans=0
ti=0
si=0
f=0
try:
  for ti in range(len(t)):
    while t[ti]!=s[si]:
      ans+=1
      si+=1
      si=si%len(s)
      f+=1
      if f>len(s)+1:
        exit(0)
    si+=1
    ti+=1
    ans+=1
    f=0
except: 
  print("-1")
  exit(0)
  
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

def resolve():
    s = input()
    t = input()

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

    tmp = -1
    count = 0
    used = {s: 0 for s in dic}

    for i in range(len(t)):

        if t[i] not in dic:
            print('-1')
            sys.exit(0)

        idx = used[t[i]]
        used[t[i]] += 1

        if idx >= len(dic[t[i]]):
            if i < len(t)-1:
                count +=1
            used = {s: 0 for s in dic}
            used[t[i]] += 1
            tmp = dic[t[i]][0]
            continue

        a = dic[t[i]][idx]
        if a <= tmp:
            used = {s: 0 for s in dic}
            count += 1
            tmp = dic[t[i]][0]
            used[t[i]] += 1
        else:
            tmp = a

    print(count * len(s) + (tmp+1))


if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:
s, t = map(str, [input() for i in range(2)])

index=0
count=0
ans=0
result=True
for moji in t:
    index = s[index:].find(moji)
    if index>-1:
        ans=ans+index+1
        index+=1
    else:
        index = s.find(moji)
        if index>-1:
            count+=1
            ans=len(t)*count+index+1
            index+=1
        else:
            result=False
            print("-1")
            break
if result:
    print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations
import sys
import bisect
import string
sys.setrecursionlimit(10**6)
def SI():
    return input().split()
def MI():
    return map(int,input().split())
def I():
    return int(input())
def LI():
    return [int(i) for i in input().split()]
YN=['Yes','No']
mo=10**9+7

s=input()
t=input()
s=list(s)
t=list(t)
Ss=set(s)
St=set(t)
n=len(s)

alp=string.ascii_lowercase
di=dict([(i,j) for j,i in enumerate(alp)])
#print(di)
g=[{} for _ in range(n)]
fp=[-1 for i in range(26)]
lp=[-1 for i in range(26)]

for i,c in enumerate(s):
    fp[di[c]]=i
    for next_c in [di[l] for l in Ss]:
        if fp[next_c]!=-1:
            g[i][next_c]=(fp[next_c],1)

for i in range(n)[::-1]:
    lp[di[s[i]]]=i
    for next_c in [di[l] for l in Ss]:
        if lp[next_c]!=-1:
            g[i][next_c]=(lp[next_c],0)

m=[[] for i in range(n)]


#print(Ss,St,St-Ss)
if len(St-Ss)!=0:
    ans=-1
else:
    loop=0
    has=0
    st=di[t[0]]
    i_n=g[0][st][0]
    for i in range(1,len(t)):
        ch=di[t[i]]
        loop+=g[i_n][ch][1]
        i_n=g[i_n][ch][0]
        has=g[i_n][ch][0]
        #print(loop,has,i_n,g[i_n][ch])
    ans=loop*n+has+1
print(ans)#,loop,has)
#print([[i,di[i]] for i in s])
'''
for i in g:
    print(sorted(list(i.items()),key=lambda x:x[0]))
'''

Submission status: Wrong Answer

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 = -2
            break
        else:
            ind = 0
            while charDict[hashCharDict][ind] <= pk_1:
                ind += 1
                if ind >= len(charDict[hashCharDict]):
                    break
            if ind >= len(charDict[hashCharDict]):
                pk_1 = charDict[hashCharDict][0]
                ret = ((ret + 1) // Ns + 1) * Ns + pk_1
            else:
                pk_1 = charDict[hashCharDict][ind]
                ret = ((ret + 1) // Ns) * Ns + pk_1
        # print "ret: " + str(ret)
    print ret + 1

if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

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

ds = {}
dsl ={}
dt = {}
for i in range(len(s)):
    if s[i] not in ds.keys():    
        ds[s[i]] = 0
        dsl[s[i]] = []
    ds[s[i]]+=1
    dsl[s[i]].append(i)

for i in range(len(t)):
    if t[i] not in dt.keys():    
        dt[t[i]] = 0
    if t[i] not in ds.keys():
        ds[t[i]] = 0
    dt[t[i]]+=1

import bisect
ans = 1
now = 0
for i in range(len(t)):
    if ds[t[i]] == 0:
        print("-1")
        exit()
    elif ds[t[i]] == 1:
        if dsl[t[i]][0] > now:
            ans += (dsl[t[i]][0] - now)
        else:
            ans += dsl[t[i]][0] +(len(s) - now)
        now = dsl[t[i]][0] 
    else:
        index = bisect.bisect_right(dsl[t[i]],now)
        if index == len(dsl[t[i]]):
            index = 0
        if dsl[t[i]][index] == now:
            index = bisect.bisect_right(dsl[t[i]],now+1)
            if index == len(dsl[t[i]]):
                index = 0            
        indexv = dsl[t[i]][index]
        if indexv > now:
            ans+= indexv - now
            now = indexv
        else:
            index = 0
            ans+= dsl[t[i]][0] + (len(s) - now )
            now = dsl[t[i]][0] 

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict as dd
from bisect import bisect_left as bl
s = input()
t = input()
ind = dd(list)
res = 0
for i in range(len(s)):
  ind[s[i]].append(i)
#print(ind)
for i in range(len(t)):
  if len(ind[t[i]]) == 0:
    print(-1)
    exit(0)
  temp = bl(ind[t[i]], res % len(s))
  #print(temp, res, t[i])
  if temp == len(ind[t[i]]):
    res += (len(s) - res % len(s)) + ind[t[i]][0]
  else:
    res += ind[t[i]][temp] - res % len(s)
print(res + 1)