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:
s = input()
t = input()
ls,lt = len(s),len(t)
i = j = 0
si = lp = 0
while True:
    tc = t[i]
    si = s.find(tc)
    if si == -1:
        print(-1)
        exit(0)
    else:
        if i == lt-1:
            print(lp*ls + si+1)
            exit(0)
        else:
            while True:
                if t[i+1] in s[si+1:]:
                    si = s.find(t[i+1])
                    if i+1 == lt-1:
                        #print(lp*ls + si+1)
                        exit(0)
                    else:
                        i += 1
                else:
                    i+=1
                    break
    lp += 1
    if i >= lt:
        print(-1)
        break

Submission status: Wrong Answer

Solution code:
import bisect
from collections import defaultdict

S = input()
T = input()

if set(T) - set(S) != set():
    print(-1)
    exit()

d = defaultdict(list)
l = [0 for _ in range(26)]

for i, v in enumerate(S, 1):
    l[ord(v)-ord('a')] += 1
    d[v].append(i)

len_S = len(S)

# print(l)

ans = 0
c = 0
for t in T:
    i = bisect.bisect_left(d[t], c)
    # print(l[ord(t)-ord('a')])
    if i < l[ord(t)-ord('a')]:
        ans += d[t][i]-c
        c = d[t][i]
        # print(d[t][i])
    else:
        # print(ans, len_S, c, d[t][0])
        ans += len_S-c+d[t][0]
        c = d[t][0]
    # print(ans)

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

Submission status: Wrong Answer

Solution code:

import bisect

s = input()
t = input()

if set(list(t)).issubset(set(list(s))):
    index_list = [ [] for i in range(26) ]
    for i, v in enumerate(s,1):
        index_list[ord(v)-97].append(i)
    ls = len(s)
    for i in range(26):
        index_list[i].append(ls+1)

    index = 0
    turn = 0 
    for v in t:
        new_index = bisect.bisect_left(index_list[ord(v)-97],index)
        index = index_list[ord(v)-97][new_index]
        if index == ls+1:
            new_index = bisect.bisect_left(index_list[ord(v)-97],0)
            index = index_list[ord(v)-97][new_index]
            turn += 1

    print(index+turn*ls)

else:
    print(-1)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

from string import ascii_lowercase

s = input()
t = input()

dp = {c: [] for c in ascii_lowercase}
for i, c in enumerate(s):
    dp[c].append(i)

N = len(s)
ans = 0
prev = -1
# print(dp)
for c in t:
    if not dp[c]:
        ans = -1
        break
    i = bisect_left(dp[c], prev)
    if i == len(dp[c]):
        i = 0
    k = dp[c][i]
    # print(k)
    if k > prev:
        d = k - prev
    elif k == prev:
        d = N
    else:
        d = N + k - prev
    # print('d:', d)
    ans += d
    prev = dp[c][i]
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

debug = True
debug = False

def dprint(*objects):
    if debug == True:
        print(*objects)

def main():
    s = SI()
    t = SI()

    d = {}
    for i, c in enumerate(s):
        if c not in d.keys():
            d[c] = [i]
        else:
            d[c].append(i)

    def init_didx():
        didx = {}
        for k in d.keys():
            didx[k] = 0
        return didx

    didx = init_didx()
    for c in set(t):
        if c not in d.keys():
            print(-1)
            return

    dprint(d)
    dprint(didx)

    i_loop = 0
    currentidx = -1
    for c in t:
        # 次の文字を読む
        poslist = d[c]
        if len(poslist) <= didx[c]:
            # 文字を取り切っている場合は次のループに
            i_loop += 1
            didx = init_didx()
            currentidx = poslist[0]
            didx[c] += 1
        else:
            pos = poslist[didx[c]]
            if pos > currentidx:
                # 同じループでいけるならいく
                currentidx = pos
                didx[c] += 1
            else:
                # いけないなら次のループに
                i_loop += 1
                didx = init_didx()
                currentidx = pos

        dprint(c, i_loop, pos)

    print(len(s)*i_loop + currentidx + 1)

main()

Submission status: Wrong Answer

Solution code:
import string
import bisect

s=input()
t=input()
tng=len(t)
sng=len(s)

komoji=string.ascii_lowercase
kjlist=[]
kjlist2=[]
mada=[]
for kj in komoji:
    kjlist.append([kj, []])
    kjlist2.append(kj)
    mada.append(kj)

for tt in range(sng):
    ban=kjlist2.index(s[tt])
    kjlist[ban][1].append(tt)

#print(kjlist)
kosu=-1
now=-1
nasi=0
zerosw=0
for ss in range(tng):
    ban=kjlist2.index(t[ss])
    if len(kjlist[ban][1])>0:
        basho=bisect.bisect_right(kjlist[ban][1], now)
        #print(kjlist[ban][1], now, basho)
        if basho==len(kjlist[ban][1]):
            now=kjlist[ban][1][0]
            maekos=kosu
            kosu=(int(kosu/sng)+1)*sng+now
        else:
            now=kjlist[ban][1][basho]
            maekos=kosu
            kosu=int(kosu/sng)*sng+now
    else:
        nasi=1
    #print(kosu)
if nasi==0:
    print(kosu+1)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
dic = {"a":[],"b":[],"c":[],"d":[],"e":[],"f":[],"g":[],"h":[],"i":[],"j":[],"k":[],"l":[],"m":[],"n":[],"o":[],"p":[],"q":[],"r":[],"s":[],"t":[],"u":[],"v":[],"w":[],"x":[],"y":[],"z":[]}
for i,c in enumerate(s):
    dic[c] += [i]
#print(dic)
cnt = 0
ind = 0
ans = 0
for c in t:
    d = dic[c]
    #print("c=",c,"d=",d)
    for i in d: #iは各文字のindex 昇順
        #print("i=",i)
        if ind < i: #もし現在位置<iなら
            ind = i
            #print("found, ind=",i)
            break
    else:
        #print("not found")
        cnt += 1
        ind = -1
        # もう一度
        for j in d :  # jは各文字のindex 昇順
            #print("2周目i=", i)
            if ind < j :  # もし現在位置<iなら
                ind = j
                #print("2周目found, ind=", j)
                break
        else:
            #print("!!!!!!!!!!!!!")
            ans = -1
            break
print(cnt*len(s)+ind+1 if ans > -1 else -1)

Submission status: Wrong Answer

Solution code:
import bisect
from collections import defaultdict

S = input()
T = input()

if set(T) - set(S) != set():
    print(-1)
    exit()

d = defaultdict(list)
l = [0 for _ in range(26)]

for i, v in enumerate(S, 1):
    l[ord(v)-ord('a')] += 1
    d[v].append(i)

len_S = len(S)

# print(l)

ans = 0
c = 0
for t in T:
    i = bisect.bisect_left(d[t], c)
    # print(l[ord(t)-ord('a')])
    if i < l[ord(t)-ord('a')]:
        ans += d[t][i]-c
        c = d[t][i]
        # print(d[t][i])
    else:
        # print(ans, len_S, c, d[t][0])
        ans += len_S-c+d[t][0]
        c = d[t][0]
    # print(ans)

print(ans)

Submission status: Wrong Answer

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

ds = {}
for i,s in enumerate(S) :
    if s not in ds :
        ds[s] = [i]
    else :
        ds[s].append(i)
L = []
ind = 0
for t in T :
    if t not in ds :
        ans = -1
        break
    for v in ds[t] :
        if v > ind :
            L.append(v)
            break
    else :
        L.append(ds[t][0])

    cnt = 0
    val = L[0]
    for i in range(1,len(L)) :
        if val >= L[i] :
            val = L[i]
            cnt += 1

    ans = cnt * len(S) + L[-1] + 1
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

s = input()
t = input()

if set(list(t)).issubset(set(list(s))):
    index_list = [ [] for i in range(26) ]
    for i, v in enumerate(s,1):
        index_list[ord(v)-97].append(i)
    ls = len(s)
    for i in range(26):
        index_list[i].append(ls+1)

    index = 0
    turn = 0 
    for v in t:
        new_index = bisect.bisect_left(index_list[ord(v)-97],index)
        index = index_list[ord(v)-97][new_index]
        if index == ls+1:
            new_index = bisect.bisect_left(index_list[ord(v)-97],0)
            index = index_list[ord(v)-97][new_index]
            turn += 1

    print(index+turn*ls)

else:
    ans = -1

Submission status: Wrong Answer

Solution code:
def main():
	s = input()
	t = input()
	indx = [[] for _ in range(26)]
	loop = 0
	pre = -1
	lens = len(s)
	for i in range(lens):
		indx[ord(s[i])-97] += [i]
	for i in range(len(t)):
		c = ord(t[i])-97
		if indx[c] == []:
			print(-1)
			return
		temp = lens+2
		for j in indx[c]:
			if j >= pre:
				pre = j
				break
			elif j < pre and j < temp:
				temp = j
			if j == indx[c][-1]:
				pre = temp
				loop += 1
	print(loop*lens + pre+1)

main()

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
ls,lt = len(s),len(t)
i = j = 0
si = lp = 0
while True:
    tc = t[i]
    si = s.find(tc)
    if si == -1:
        print(-1)
        exit(0)
    else:
        if i == lt-1:
            print(lp*ls + si+1)
            exit(0)
        else:
            while True:
                if t[i+1] in s[si+1:]:
                    si = s.find(t[i+1])
                    if i+1 == lt-1:
                        print(lp*ls + si+1)
                        exit(0)
                    else:
                        i += 1
                else:
                    i+=1
                    break
    lp += 1
    if i >= lt:
        print(-1)
        break

Submission status: Wrong Answer

Solution code:
s=input().rstrip()
x=list(s)
t=input().rstrip()
y=list(t)
for i in range(0,len(y)):
    if y[i] not in x:
        print(-1)
        break;
else:
    C=0;
    D=0;
    for i in range(0,len(y)):
        T=y[i]
        CC=x[C:len(x)]
        if T in CC:
            V=CC.index(T)
            C=V+C;
        else:
            D+=1;
            V=x.index(T)
            C=V;
    print(D*len(x) + C+1)

Submission status: Wrong Answer

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

if not set(S) >= set(T):
    print('-1')
    exit()

str_repeat_num = 0
now_idx = 0
for t in T:
    idx = set()
    for i, s in enumerate(S[now_idx:]):
        if t == s:
            now_idx = i + 1 + now_idx
            break
    else:
        str_repeat_num += 1
        for i, s in enumerate(S[:now_idx]):
            if t == s:
                now_idx = i
print(len(S) * str_repeat_num + now_idx)

Submission status: Wrong Answer

Solution code:
import bisect
s=list(input())
t=list(input())
N=len(s)
sd={x:[] for x in sorted(set(s))}
if not set(t) <= set(sd.keys()):
    print(-1)
    exit()

for i,c in enumerate(s):
    sd[c].append(i)
index=0
ans=0
for c in t:
    i=bisect.bisect_right(sd[c],index)
    if i==len(sd[c]):
        ans+=1
        i=bisect.bisect_left(sd[c],0)
    index=sd[c][i]
print(ans*N+index+1)

Submission status: Wrong Answer

Solution code:
s=input()
t=input()
s1=set(s)
t1=set(t)
union=s1.union(t1)
if(len(union)!=len(s1)):
  print(-1)
else:
  ans=0
  for i in range(len(t)):
    c=t[i]
    ind=s.index(c)
    nl=s[:ind]
    nr=s[ind+1:]
    if(i==len(t)-1):
      ans+=ind+1
    else:
      if(t[i+1] in nr):
        ans+=0
      elif(t[i+1] in nl):
        ans+=len(s)
  print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import *
s=input()
t=input()
a=[[]for i in range(26)]
for i in range(len(s)):
    a[ord(s[i])-97].append(i)
p=-1
b=[len(i)for i in a]
r=0
for i in t:
    if b[ord(i)-97]==0:
        print(-1)
        break
    c=bisect_left(a[ord(i)-97],p)
    if c==b[ord(i)-97]:
        p=-1
        c=bisect_left(a[ord(i)-97],p)
        r+=len(s)
        p=a[ord(i)-97][c]
    else:
        p=a[ord(i)-97][c]
else:
    print(r+p+1)

Submission status: Wrong Answer

Solution code:
def solve(s, t):
    prev = {}
    follow = [{} for _ in range(len(s) + 1)]
    for i, c in enumerate(s, start=1):
        for d, j in prev.items():
            if c not in follow[j]:
                follow[j][c] = i
        if c not in prev:
            follow[0][c] = i
        prev[c] = i
    ans = 0
    i = 0
    for c in t:
        if c not in follow[i]:
            if c not in follow[0]:
                return -1
            ans += len(s)
            i = follow[0][c]
            continue
        i = follow[i][c]
    return ans + i


s = input()
t = input()
print(solve(s, t))

Submission status: Wrong Answer

Solution code:

import bisect

s = input()
t = input()

dic = {}

for i in range(len(s)):

    if s[i] not in dic:

        dic[s[i]] = [i]

    else:

        dic[s[i]].append(i)

roop = 0
point = -1
flag = True

for i in range(len(t)):


    if t[i] not in dic:
        flag = False
        break

    ind = bisect.bisect_left(dic[t[i]],point)

    if ind >= len(dic[t[i]]):
        roop += 1
        point = dic[t[i]][0]

    else:
        point = dic[t[i]][ind]


if flag:
    print (len(s) * roop + point + 1)

else:
    print (-1)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
temp = 0
cycle = 0
ans = True
for tt in t:
    if tt in s[temp:]:
        k = s[temp:].find(tt)
        temp += k
    elif tt in s:
        temp = s.find(tt)
        cycle += 1
    else:
        ans = False
        break
if ans:    
    print((cycle*len(s)) + temp + 1)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import bisect
from collections import defaultdict
S = input()
T = input()
len_S = len(S)
s2i = defaultdict(list)
for i, si in enumerate(S):
    s2i[si].append(i)
ans = 1
pi = -1
for ti in T:
    si = s2i.get(ti)
    if not si:
        ans = -1
        break
    il = bisect.bisect_right(si,pi)
    if len(si) <= il:
        ans += len_S
        if si[il-1] == len_S - 1:
            pi = si[0]
        else:
            pi = si[il-1]
    else:
        pi = si[il]

if ans != -1:
    ans += pi

print(ans)

Submission status: Wrong Answer

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

x=list(s)
y=list(t)
import sys
if len(set(y)-set(x))!=0:
	print(-1)
	sys.exit()

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



import bisect
#t=bisect.bisect_right(res[p-1],y)

repeat=0
last=0
for i in range(len(t)):
	temp=a[ord(y[i])-97]
	if last>=temp[-1]:
		repeat+=1
		last=temp[0]
	else:
		last=temp[bisect.bisect_right(temp,last)]
	#print(temp,repeat,last)
print(repeat*len(s)+last+1)

Submission status: Wrong Answer

Solution code:
import collections
s = str(input())
t = str(input())
list1 = []
count = 0

for i in range(len(t)):
  if count != 0:
    break
  if t[i] not in s:
  	count += 1
  else:
    index = s.index(t[i])
    list1.append(index)
  
if count == 0:
  ans = 0
  roop = 0
  for i in range(1, len(list1)):
    if list1[i] <= list1[i-1]:
      roop += 1
  ans = len(s)*roop + list1[-1] + 1
  print(ans)
else:
  print("-1")

Submission status: Wrong Answer

Solution code:
import string
import bisect

s=input()
t=input()
tng=len(t)
sng=len(s)

komoji=string.ascii_lowercase
kjlist=[]
kjlist2=[]
mada=[]
for kj in komoji:
    kjlist.append([kj, []])
    kjlist2.append(kj)
    mada.append(kj)

for tt in range(sng):
    ban=kjlist2.index(s[tt])
    kjlist[ban][1].append(tt)

#print(kjlist)
kosu=0
now=0
nasi=0
zerosw=0
for ss in range(tng):
    ban=kjlist2.index(t[ss])
    if len(kjlist[ban][1])>0:
        basho=bisect.bisect_left(kjlist[ban][1], now)
        if basho==len(kjlist[ban][1]):
            now=kjlist[ban][1][0]
            maekos=kosu
            kosu=(int(kosu/sng)+1)*sng+now
            if kosu==maekos:
                if kosu!=0:
                    kosu+=sng
                elif zerosw==0:
                    zerosw=1
                else:
                    kosu+=sng
        else:
            now=kjlist[ban][1][basho]
            maekos=kosu
            kosu=int(kosu/sng)*sng+now
            if kosu==maekos:
                if kosu!=0:
                    kosu+=sng
                elif zerosw==0:
                    zerosw=1
                else:
                    kosu+=sng
    else:
        nasi=1
    #print(kosu)
if nasi==0:
    print(kosu+1)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
r = 0
p = 0
x = 0
for i in t:
    if s.find(i) == -1:
        print(-1)
        exit()
    if p > s.find(i):
        if s[p:].find(i) == -1:
            x += 1
            p = s.find(i)
        else:
            p = p + s[p:].find(i)
    else:
        p = s.find(i)
print(x * len(s) + p + 1)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from collections import defaultdict
from bisect import bisect_right

s = ns()
t = ns()

ok = True

sset = set(list(s))
for ti in t:
    if ti not in sset:
        ok = False
        break

if not ok:
    print(-1)

else:
    n = len(s)

    sdouble = s + s

    charidx = defaultdict(list)

    for i, si in enumerate(s):
        charidx[si].append(i)
        charidx[si].append(n+i)

    move = [{ki: -1 for ki in charidx.keys()} for _ in range(n)]

    for i, si in enumerate(s):
        for ki in charidx.keys():
            nexidx = bisect_right(charidx[ki], i)
            move[i][ki] = (charidx[ki][nexidx] - i) % (n+1)

    ans = s.index(t[:1])
    cur = ans

    for i, ti in enumerate(t[1:]):
        nex = move[cur][ti]
        ans += nex
        cur = (cur + nex) % n

    print(ans+1)

Submission status: Wrong Answer

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


def find_t(idx, S, t, pre):
    while True:
        idx = S.find(t, idx + 1)

        if idx == -1:
            return -1
        elif idx > pre:
            return idx


pre = -1
flag = True
ans = 0
for t in T:
    idx = S.find(t)

    if idx == -1:
        print(-1)
        flag = False

    if idx <= pre:
        idx_next = find_t(idx, S, t, pre)
        if idx_next == -1:
            ans += idx - pre + len(S)
            pre = idx
        else:
            ans += idx_next - pre
            pre = idx_next
    else:
        ans += idx - pre
        pre = idx

if flag:
    print(ans)

Submission status: Wrong Answer

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

#print(t[0])

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

if condition==1:
  print(-1)
else:
  j=0
  count=-1
  while j <len(t)-1:
    count=count+1
    for k in range(len(s)):
      if t[j]==s[k]:
        j=j+1
        #print(j,k)
        if j==len(t):
          print(count*len(s)+k+1)
          break

Submission status: Wrong Answer

Solution code:
import bisect

S = input()
T = input()

idx = {}
for i, s in enumerate(S):
    if s in idx.keys():
        idx[s].append(i + 1)
    else:
        idx[s] = [i + 1]

x = 0
current = 0
impossible = 0
for i, t in enumerate(T):
    if t in idx.keys():
        idx_list = idx[t]
        j = bisect.bisect_left(idx_list, current)
        if j < len(idx_list):
            current = idx_list[j]
        else:
            x += 1
            current = idx_list[0]
    else:
        impossible = 1
        break

if impossible:
    print(-1)
else:
    print(x * len(S) + current)

Submission status: Wrong Answer

Solution code:
s=input()
t=input()
leng=len(s)
lent=len(t)
count=0
tmp=0
i=0
while i<lent:
   ans=s[tmp:].find(t[i])
   if ans!=-1:
       i += 1
       tmp += ans+1
       if i ==lent:
           count += tmp
           break
       continue
   if tmp==0:
       conut=-1
       break
   tmp=0
   count+=leng
if count>leng*10**100:
    count=-1
print(count)

Submission status: Wrong Answer

Solution code:
import sys
from bisect import bisect_left
s = input()
t = input()
n = len(t)
index = [[] for i in range(26)]
for i in range(len(s)):
    j = ord(s[i])-97
    index[j].append(i)
tmp = -1
ans = 0
for i in range(n):
    j = ord(t[i])-97
    if len(index[j])==0:
        print(-1)
        sys.exit()
    k = bisect_left(index[j], tmp)
    if k<len(index[j]):
        tmp = index[j][k]
    else:
        ans+=1
        tmp = index[j][0]
print(ans*len(s)+tmp+1)

Submission status: Wrong Answer

Solution code:

def main():
    S = list(input())
    T = list(input())
    sl = len(S)

    Sdic = {}
    for i, s in enumerate(S):
        if not s in Sdic.keys():
            Sdic[s] = [i]
        else:
            Sdic[s].append(i)

    pret = 0
    ans = 0
    for t in T:
        try:
            update = False
            for it in Sdic[t]:
                if it > pret:
                    update = True
                    pret = it
                    break
            if not update:
                ans += sl
                pret = Sdic[t][0]
            #print(t, update, pret)
        except KeyError:
            ans = -1
            break
    if ans != -1:
        ans += pret+1
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

if not len(set(t) - set(s)) == 0:
    print(-1)
else:
    idxes = [s.index(ti) for ti in t]
    cnt = 0
    n = 0
    
    for i in idxes[:-1]:
        if n >= i:
            cnt += 1
        n = i

    print(cnt * len(s) + idxes[-1] + 1)

Submission status: Wrong Answer

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

count = 0
ans = 0
j = 0
i = 0
error=0
while True:
    if error > 2:
        print(-1)
        break
    if count == len(T):
        print(ans+1)
        break
    if i == len(S):
        i = 0
        error += 1
    if S[i]==T[j]:
        count += 1
        j += 1
        error = 0
    else:
        i += 1
        ans += 1

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return list(map(int, sys.stdin.readline().split()))
def II(): return int(sys.stdin.readline())
def LS(): return list(map(list, sys.stdin.readline().split()))
def S(): return sys.stdin.readline()[:-1]

def main():
    print(-1)
    return
    s = list(S())
    t = list(S())
    alfa = defaultdict(lambda: INF)
    for i, c in enumerate(s):
        alfa[c] = min(i, alfa[c])
    pre = 0
    ans = 0
    for i, c in enumerate(t):
        index = alfa[c]
        if index == INF:
            print(str(-1))
            return
        if index > pre:
            pre = index
        else:
            pre = index
            ans += len(s)

    print(ans + pre + 1)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
alphabet_small = [chr(i) for i in range(97, 97+26)]
s = input()
t = input()
m = len(s)
n = len(t)

alpha = [[] for i in range(26)]
for i,let in enumerate(s):
    for j,al in enumerate(alphabet_small):
        if let == al:
            alpha[j].append(i)

#alpha　はsのどこに文字があるか

dp = [0 for i in range(n+1)] #i文字目までの最小
#求めるのはdp[n]
t_num = []
append = t_num.append
for let in t:
    for j,al in enumerate(alphabet_small):
        if let == al:
            t_num.append(j)

#t_num　はtの文字を数字に変換
now = -1
for i,let_num in enumerate(t_num):
    #print(i,let_num,alpha[let_num])
    if len(alpha[let_num]) == 0:
        print(-1)
        exit()
    else:
        if alpha[let_num][-1] >= now:
            next = bisect_left(alpha[let_num],now)
            next = alpha[let_num][next]
            #print(next)
            dp[i+1] = dp[i] + next - now
        else:
            next = alpha[let_num][0]
            #print(next)
            dp[i+1] = dp[i] + next - now + m
        now = next


print(dp[n])

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
temp = 0
cycle = 0
ans = True
for tt in t:
    if tt in s:
        k = s.find(tt)
        if temp < k:
            temp = s.find(tt)
        else:
            temp = s.find(tt)
            cycle += 1
    else:
        ans = False
        break
if ans:    
    print((cycle*len(s)) + temp + 1)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left, bisect_right
from collections import defaultdict

def find_gt(a, x):
    'Find leftmost value greater than x'
    i = bisect_right(a, x)
    if i != len(a):
        return a[i]
    return -1

s = list(input())
t = list(input())
if len(set(t)-set(s)) != 0:
    print(-1)
    exit(0)

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

index = 0
round = 0
for tt in t:
    index = find_gt(sdict[tt], index)
    if index == -1:
        index = sdict[tt][0]
        round += 1
ans = round * len(s) + index + 1
print(ans)

Submission status: Wrong Answer

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

count = 0
ans = 0
j = 0
i = 0
error=0
while True:
    if error > 2:
        print(-1)
        break
    if count == len(T):
        print(ans+1)
        break
    if i == len(S):
        i = 0
        error += 1
    if S[i]==T[j]:
        count += 1
        j += 1
        error = 0
    else:
        i += 1
        ans += 1
    print('i,j='+str(i)+','+str(j))

Submission status: Wrong Answer

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

x=list(s)
y=list(t)
import sys
if len(set(y)-set(x))!=0:
	print(-1)
	sys.exit()

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



import bisect
#t=bisect.bisect_right(res[p-1],y)

repeat=0
last=0
for i in range(len(t)):
	temp=a[ord(y[i])-97]
	if last>=temp[-1]:
		repeat+=1
		last=temp[0]
	else:
		last=temp[bisect.bisect_left(temp,last)]
	#print(temp,repeat,last)
print(repeat*len(s)+last+1)

Submission status: Wrong Answer

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

d = dict()
for i in range(26):
    d[chr(97+i)] = i

s_index = [[] for i in range(26)]
for i in range(len(s)):
    s_index[d[s[i]]].append(i)


flag = 1
ans = 0
index = -1
for ti in t:
    s_char = s_index[d[ti]]
    if len(s_char) == 0:
        flag = 0
        ans = -1
        break
    next = s_char[bisect_left(s_char, index) % len(s_char)]
    if next == index:
        next = s_char[(bisect_left(s_char, index)+1) % len(s_char)]

    if next == index:
        ans += len(s)
    else:
        if index == -1:
            ans += next - index
        else:
            ans += (next - index) % len(s)

    index = next


if flag == 1 and len(s) == 1:
    ans += 1


print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
from collections import defaultdict
S = input()
T = input()
len_S = len(S)
s2i = defaultdict(list)
for i, si in enumerate(S):
    s2i[si].append(i)
ans = 1
pi = -1
for ti in T:
    si = s2i.get(ti)
    if not si:
        ans = -1
        break
    il = bisect.bisect_right(si,pi)
    if len(si) <= il:
        ans += len_S
        pi = si[il-1]
    else:
        pi = si[il]
if ans != -1:
    ans += pi

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
s=list(input())
t=list(input())
N=len(s)
sd={x:[] for x in sorted(set(s))}
td={x:[] for x in sorted(set(t))}
if not set(td.keys()) <= set(sd.keys()):
    print(-1)
    exit()

for i,c in enumerate(s):
    sd[c].append(i)
index=0
ans=0
for c in t:
    i=bisect.bisect_left(sd[c],index)
    if i==len(sd[c]):
        ans+=1
        i=bisect.bisect_left(sd[c],0)
    index=sd[c][i]
print(ans*N+index+1)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
la = []
for l in t:
    la.append(s.find(l))
if min(la)<0:
    print("-1")
else:
    counter = 0
    for i in range(len(la)-1):
        if la[i]>la[i+1]:
            counter+=1
        elif la[i]==la[i+1]:
            if  not t[i] in s[la[i]+1:]:
                counter +=1
    print(counter*len(s)+la[-1]+1)

Submission status: Wrong Answer

Solution code:
from collections import deque
s = str(input())
t = str(input())

sc = [0] * 26
tc = [0] * 26

for i in range(len(s)):
    sc[ord(s[i]) - ord("a")] += 1
for i in range(len(t)):
    tc[ord(t[i]) - ord("a")] += 1

# 不可能な場合
for i in range(26):
    if tc[i] > 0 and sc[i] == 0:
        print(-1)
        exit()


alph = [[] for _ in range(26)]

for i in range(len(s)):
    alph[ord(s[i]) - ord("a")].append(i)

alph_q = [deque(alph[i]) for i in range(26)]

ans = [0] * len(t)
for i in range(len(t)):
    num = ord(t[i]) - ord("a")

    if len(alph_q[num]) <= 0:
        alph_q[num] = deque(alph[num])
    ans[i] = alph_q[num].popleft()

count = 0
pre = -1
for v in ans:
    if pre >= v:
        count += 1
    pre = v

print(count * len(s) + ans[-1] + 1)

Submission status: Wrong Answer

Solution code:
import string
import bisect

s=input()
t=input()
tng=len(t)
sng=len(s)

komoji=string.ascii_lowercase
kjlist=[]
kjlist2=[]
mada=[]
for kj in komoji:
    kjlist.append([kj, []])
    kjlist2.append(kj)
    mada.append(kj)

for tt in range(sng):
    ban=kjlist2.index(s[tt])
    kjlist[ban][1].append(tt)

#print(kjlist)
kosu=0
now=0
nasi=0
for ss in range(tng):
    ban=kjlist2.index(t[ss])
    if len(kjlist[ban][1])>0:
        basho=bisect.bisect_left(kjlist[ban][1], now)
        #print(basho, kjlist[ban][1], now)
        if basho==len(kjlist[ban][1]):
            now=kjlist[ban][1][0]
            kosu=(int(kosu/sng)+1)*sng+now
        else:
            now=kjlist[ban][1][basho]
            kosu=int(kosu/sng)*sng+now
    else:
        nasi=1
    #print(kosu)
if nasi==0:
    print(kosu+1)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import sys
s = input()
t = input()
for ti in t:
    if ti not in s:
        print(-1)
        sys.exit()
new_li = []
for ti in t:
    new_li.append(s.find(ti))
# print(new_li) [5, 4, 2, 3, 4, 2, 0, 4]
count = 0
for ni,n in enumerate(new_li[:len(new_li)-1]):
    if new_li[ni]>new_li[ni+1]:
        count += 1
score = count*len(s)+new_li[-1]+1
print(score)

Submission status: Wrong Answer

Solution code:
s=input()
t=input()
leng=len(s)
lent=len(t)
count=0
tmp=0
i=0
while i<lent:
   ans=s[tmp:].find(t[i])
   if ans!=-1:
       i += 1
       tmp += ans+1
       if i ==lent:
           count += tmp
           break
       continue
   if tmp==0:
       conut=-1
       break
   tmp=0
   count+=leng
print(count)