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 = 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_left(dsl[t[i]],now)
        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

s = input()
t = input()

N = len(s)
tN = len(t)

str_array = np.full((N, 26), -1)

o_a = ord("a")
o_z = ord("z")

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

for i in range(N * 2 + 2, -1, -1):
    for byte_c in range(26):
        if s[(i + 1) % N] == chr(o_a + byte_c):
            str_array[i % N, byte_c] = 1
        elif str_array[i % N, byte_c] == -1 and str_array[(i + 1) % N, byte_c] > 0:
            str_array[i % N, byte_c] = str_array[(i + 1) % N, byte_c] + 1

ans = str_array[N - 1, alpha_index(t[0])]

if ans == -1:
    print(-1)
    exit()

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

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
s = input()
t = input()
i = 0
r = 1
n = len(s)

d = dict()
for x in t:
    if x not in d:
        d[x] = np.where(np.array(list(s)) == x)[0]
    if len(d[x]) == 0:
        r = -1
        break
    
    y = np.where(d[x]>i)[0]
    if len(y) == 0:
        r += n - i
        i = d[x][0]
        r += i
    else:
        j = d[x][y[0]]
        r += j - i
        i = j

print(r)

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
                else:
                    i += check + 1
                    index += 1
                    tmps = tmps[check + 1:]
            else:
                i += tmpsl
                find = False
            if index > tl - 1:
                break

        if index  > tl -1:
            break
    print(i)

Submission status: Wrong Answer

Solution code:
import collections, bisect

s = input()
t = input()

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

m = len(t)
x = [0] * (m + 1) # 長さを記録
y = [0] * (m + 1) # 添え字を記録

for i in range(1, m + 1):
    c = t[i - 1]
    if not c in d:
        print(-1)
        exit()
    
    a = y[i - 1]
    ptr = bisect.bisect_left(d[c], y[i - 1])
    if ptr == len(d[c]):
        b = d[c][-1]
    else:
        b = d[c][ptr]
    
    y[i] = b
    if a < b:
        x[i] = x[i - 1] + b - a
    else:
        x[i] = x[i - 1] + n - a + b
    
print(x[-1])

Submission status: Wrong Answer

Solution code:
import collections
al = {"a": 0, "b": 1, "c": 2, "d": 3, "e": 4, "f": 5,
      "g": 6, "h": 7, "i": 8, "j": 9, "k": 10, "l": 11,
      "m": 12, "n": 13, "o": 14, "p": 15, "q": 16, "r": 17,
      "s": 18, "t": 19, "u": 20, "v": 21, "w": 22, "x": 23,
      "y": 24, "z": 25}
als = [[]for _ in range(26)]
s = list(input())
for i, v in enumerate(s):
    als[al[v]].append(i)
t = list(input())
last_no = 0
aln = [0] * 26
for i, v in enumerate(t):
    if len(als[al[v]]) != 0:
        while True:
            p = als[al[v]][aln[al[v]]]
            aln[al[v]] += 1
            if p > last_no:
                als[al[v]].append(p + len(s))
                last_no = p
                break
            else:
                als[al[v]].append(p + len(s))
    else:
        print(-1)
        exit()
print(last_no + 1)

Submission status: Wrong Answer

Solution code:
import sys

s = input()
t = input()

i = 0
p_past = 0

for c in t:

    p = s.find(c)
    if p < 0:
        print(-1)
        sys.exit()

    if p > p_past:
        pass
    else:
        i += 1
    p_past = p

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

Submission status: Wrong Answer

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


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

  Tidx = [0 for _ in range(len(T)+1)]
  for i,t in enumerate(T):
    if len(sdict[t]) == 0:
      return -1
    elif tdict[t] >= len(sdict[t]):
      tdict[t] = 0
    Tidx[i+1] = sdict[t][tdict[t]]
    tdict[t] += 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:
s = list(str(input()))
t = list(str(input()))

ans = []
for a in t:
    ans.append([i for i, x in enumerate(s) if x == a])

ans2 = 0

if [] in ans:
    print(-1)
else:
    _v = 0
    for y in ans:
        for _y in y:
            if _v < _y:
                pass
            else:
                ans2 += 1
            _v = _y
            break
    print(ans2*len(s) + _v + 1)

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:
def main():
    s = input()
    t = input()

    cnt_t = [ 0 ] * 26
    beg_s = [ 10000000 ] * 26
    cnt_s = [ 0 ] * 26

    for ss in s:
        a = ord(ss) - ord('a')
        cnt_s[ a ] += 1


    for tt in t:
        a = ord(tt) - ord('a')
        cnt_t[ a ] += 1

    for i in range(26):
        if cnt_t[i] != 0 and cnt_s[i] == 0:
            print(-1)
            return

    for i in range(26):
        if cnt_t[i] == 0:
            continue

    for i in range(len(s)):
        a = ord(s[i]) - ord('a')
        beg_s[a] = min(i, beg_s[a])

    prev = 100000000
    loop = 0

    for i in range(len(t)):
        a = ord(t[i]) - ord('a')
        cur = beg_s[ a ]

        if cur < prev:
            loop += 1

        prev = cur

    print( (loop-1) * len(s) + beg_s[ ord(t[-1]) - ord('a') ] + 1 )


main()

Submission status: Wrong Answer

Solution code:
import decimal,math
from collections import Counter,defaultdict
from fractions import gcd
from bisect import bisect_right,bisect_left
decimal.getcontext().prec = 10
s=raw_input()
t=raw_input()
index={}
for i in range(len(s)):
	if(index.get(s[i])):
		index[s[i]].append(i)
	else:
		index[s[i]]=[i]	
flag=0
for i in t:
	if(index.get(i)):
		continue
	else:
		flag=1
		break
if(flag==1):
	print -1
	exit()		
ans=len(s)
co=0
bad=0
arrr = [index[t[0]][0]]
# print index
# print index['t']
# print bisect_right(index['t'],3)
for i in range(1,len(t)):
	o = index[t[i]]
	l = bisect_right(o,arrr[-1])
	# print l,o
	if(l>=len(o)):
		arrr.append(o[0])
		# print 'ok'
	else:
		arrr.append(o[l])	
# print arrr		
for i in range(1,len(arrr)):
	if(arrr[i]<arrr[i-1]):
		ans=ans+len(s)
print ans-(len(s)-arrr[-1]-1)

Submission status: Wrong Answer

Solution code:
import bisect
from collections import defaultdict

S=input()
T=input()

def solve():
    obj=defaultdict(list)
    for i,s in enumerate(S):
        obj[s].append(i)

    result=1
    mx=-1
    for t in T:
        flag=True

        if not obj[t]:
            return -1

        
        bi_pos=bisect.bisect_right(obj[t],mx)
        if bi_pos==len(obj[t]):
            mx=obj[t][0]
            result+=1
        else:
            pos=obj[t][bi_pos]
            mx=pos

    if pos==-1:
        pos=obj[t][0]
    return len(S)*(result-1)+pos+1
print(solve())

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline #文字列入力のときは注意


ss = input()
t = input()

s = ss+ss


l = len(s)
d = [[-1]*26 for _ in range(l)]
#now = [-1]*27
prev = [-1]*27

ans = -1
for i,c in enumerate(s):
#        if now[j] == -1: continue
#        if d[now[j]][ord(c)-97] == -1:
#            d[now[j]][ord(c)-97] = i
    for k in range(prev[ord(c)-97]+1,i):
        d[k][ord(c)-97] = i
#    now[ord(c)-97] = i
    prev[ord(c)-97] = i

#print(now)
#print(d)

ans = ss.find(t[0])
if ans == -1:
    print(-1)
    exit()

ls = len(ss)
#print(ans)
lt = len(t)-1

res = ans
for i in range(lt):
#    print(res, d[res])
    if d[res][ord(t[i+1])-97] == -1:
        print(-1)
        exit()
    diff = d[res][ord(t[i+1])-97] - res 
    ans += diff
    res = (res+diff)%ls


print(ans+1)

Submission status: Wrong Answer

Solution code:
s=input()
t=input()
ans=1
order=0
judge=True
d={}
preorder=0
length=len(s)

for i in t:
        if i in s:
            if i not in d:
                    indexs=[j for j, x in enumerate(s) if x==i]
                    d[i]=list(indexs)
            if min(d[i])>order:
                    order=min([j for j in d[i] if j>order])
                    ans+=order-preorder
                    preorder=0
                    preorder+=order
            else:
                    order=min(d[i])
                    ans+=order-preorder+length
                    preorder=0
                    preorder+=order
        else:
            judge=False

if judge:
    print(ans)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import sys

s = input()
t = input()
if not set(t).issubset(set(s)):
    print(-1)
    sys.exit(0)
count = 0
now_idx = 0
for t_ in t:
    if t_ in s[now_idx+1:]:
        next_idx = (now_idx+1)+ s[now_idx+1:].index(t_)
    else:
        next_idx = s.index(t_)
        count += 1
    now_idx = next_idx
print(count*len(s) + now_idx+1)

Submission status: Wrong Answer

Solution code:
alpha2num = lambda c: ord(c) - ord('a') 

s = list(input())
alpha = [[] for i in range(26)]
a = 'abcdefghijklmnopqrstuvwxyz'

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

#print(alpha)

t = list(input())
l = [0] * 26
ans = 0


s_unique = list(set(s))
t_unique = list(set(t))
check = 0
tmp = 0
#print(l)

for i in t_unique:
    if i in s_unique:
        pass
    else:
        check = 1

if check == 0:

    for i in range(len(t)):
        if i == 0:
            last = alpha[alpha2num(t[i])][l[alpha2num(t[i])]]
            l[alpha2num(t[i])] += 1
        else:
            if l[alpha2num(t[i])] >= len(alpha[alpha2num(t[i])]):
                l = [0] * 26
                tmp = alpha[alpha2num(t[i])][l[alpha2num(t[i])]]
                l[alpha2num(t[i])] += 1
                
#                print(tmp)
                last = tmp
                ans += 1
            else:
#                print(tmp)
                tmp = alpha[alpha2num(t[i])][l[alpha2num(t[i])]]
                l[alpha2num(t[i])] += 1
#                print(tmp)
                if tmp < last:
                    l = [0] * 26
                    last = tmp
                    ans += 1
                else:
                    last = tmp

#    print(ans)
    print(max(ans * len(s) + tmp + 1, len(s)))

else:
    print(-1)

Submission status: Wrong Answer

Solution code:
s = [a for a in input()]
t = input()
td = t
t = list(set(t))
d = {v:float('inf') for v in t}

l = len(s)
for i in range(l//2+1):
    if s[i] in t:
        d[s[i]] = min(d[s[i]],i+1)
    if s[l-i-1] in t:
        d[s[l-i-1]] = min(d[s[l-i-1]],l-i)

for v in d.values():
    if v == float('inf'):
        print(-1)
        exit()

cnt = 0
for i in range(1,len(td)):
    if d[td[i-1]]>d[td[i]]: cnt+=1
print(len(s)*cnt+s.index(td[-1])+1)

Submission status: Wrong Answer

Solution code:
# #ABC138 E
s = input()
t = list(input())
# from collections import defaultdict
have = {}
for x in (s):
    have[x] = 1

used  = {}
# defaultdict(None)
for x in t:
    used[x] = 1
# print(used)
# print(used.get("a"))
ans = 0
can = have.keys()
for k in used.keys():
    if not k in can:
        ans = -1
        break

if not ans == -1:  
    pos = 0
    slen = len(s) 
    rem = 0
    sub = s+s
    while len(t)>0:
        fpos = pos
        pos = sub.index(t.pop(0)) - rem
        if pos < 0:
            pos = pos + rem + fpos +1
        rem = slen- pos -1
        if fpos >= pos:
            ans += slen
        # print(sub,pos,rem)
        
        sub = s[pos+1:] + s

    ans += pos+1
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
i = 0
k = 0
cnt = 0
idx = 0
b = 0
s_s=set(s)
t_s =set(t)
if s_s  >= t_s:
  while i < len(t):
    if b == 1:
      break
    for k in range(len(s)):
      if i == len(t) - 1:
        idx = s.find(t[i])
        b = 1
        break
      if t[i] == s[k]:
        i += 1
      if k == len(s) -1:
        cnt += 1
  ans = len(s)*cnt + idx+1
else:
  ans = -1
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:
                    i += tmpsl
                    index += 1
                    find = False
                else:
                    i += check + 1
                    index += 1
                    tmps = tmps[check + 1:]
            else:
                i += tmpsl
                find = False
            if index > tl - 1:
                break

        if index  > tl -1:
            break
    print(i)

Submission status: Wrong Answer

Solution code:
import bisect

s = input()
t = input()

mp = {}

for i, c in enumerate(s):
  if c in mp:
    mp[c].append(i)
  else:
    mp[c] = [i]
    
r = 0
pos = 0

ok = 1

for i,c in enumerate(t):
  if not (c in mp):
    print(-1)
    ok = 0
  
  else:
    if mp[c][-1] < pos:
      pos = mp[c][0]
      r += 1
    else:
      j = 0
      while mp[c][j] < pos:
        j += 1
      pos = mp[c][j]

if ok == 1:
  print( r * len(s) + pos + 1 )

Submission status: Wrong Answer

Solution code:
import sys
s = input()
t = input()
l = {}
for c in set(t):
    cl = [s.find(c)]
    if cl[0] == -1:
        print("-1")
        sys.exit()
    while True:
        i = s.find(c, cl[-1] + 1)
        if (i == -1):
            break
        cl.append(i)
    l[c] = cl
result = 0
for c in t:
    lens = len(s)
    ofs = result % lens
    for i in l[c]:
        if i > ofs:
            result += i - ofs
            break
    else:
        result += lens - ofs + l[c][0]
print(result+1)

Submission status: Wrong Answer

Solution code:
from collections import deque
s=input()
t=input()
l=len(s)
lt=len(t)
ctr=0
a=[0]*26
for i in t:
  a[ord(i)-ord('a')]=1
for i in s:
  if a[ord(i)-ord('a')]==1:a[ord(i)-ord('a')]=0
if sum(a)>0:print(-1)
else:
  ta=[deque([]) for i in range(26)]
  sa=[deque([]) for i in range(26)]
  for i in range(l):
    sa[ord(s[i])-ord('a')].append(i) 
  last=0
  for i in t:
    cc=0
    ll=last+1
    for j in sa[ord(i)-ord('a')]:
      if j > last:
        ctr+=j-last
        last=j
        break
    else:
      ctr+=l-last+sa[ord(i)-ord('a')][0]
      last=sa[ord(i)-ord('a')][0]
  print(ctr+1)

Submission status: Wrong Answer

Solution code:
import collections
al = {"a": 0, "b": 1, "c": 2, "d": 3, "e": 4, "f": 5,
      "g": 6, "h": 7, "i": 8, "j": 9, "k": 10, "l": 11,
      "m": 12, "n": 13, "o": 14, "p": 15, "q": 16, "r": 17,
      "s": 18, "t": 19, "u": 20, "v": 21, "w": 22, "x": 23,
      "y": 24, "z": 25}
als = [[]for _ in range(26)]
s = list(input())
for i, v in enumerate(s):
    als[al[v]].append(i)
t = list(input())
last_no = 0
for i, v in enumerate(t):
    if len(als[al[v]]) != 0:
        while True:
            p = als[al[v]].pop(0)
            if p > last_no:
                als[al[v]].append(p + len(s))
                last_no = p
                break
            else:
                als[al[v]].append(p + len(s))
    else:
        print(-1)
        exit()
print(last_no + 1)

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

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 I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

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

  ln=len(s)

  a=set(s)
  b=set(t)

  for x in b:
    if x not in a:
      return -1

  q=['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']
  r=[[] for _ in range(len(q))]

  for i,x in enumerate(s):
    r[q.index(x)].append(i)

  ans=1
  if s[0]==t[0]:
    ans=0
  i=0
  for x in t:
    while True:
      if i>=len(s):
        i=0
      _l=r[q.index(x)]
      a=bisect.bisect_right(_l,i)
      __i=_l[a-1]
      if a==0:
        _i=_l[0]
      else:
        _i=__i

      if i==_i:
        ans+=1
        break

      if _i<i:
        ans+=ln-i-1
        ans+=_i+1
      else:
        ans+=_i-i
      i=_i
      break

  return ans

# main()
print(main())

Submission status: Wrong Answer

Solution code:
import sys
s = input()
s_li = [_ for _ in s]
t = input()
for ti in t:
    if ti not in s:
        print(-1)
        sys.exit()
new_li = []
for ti in t:
    tmp_li = []
    new_li.append([i for i, s in enumerate(s_li) if s == ti])
# print(new_li)
# nnsgton son
# [[2], [5], [0, 1, 6]]

real_li = []
for i,ni in enumerate(new_li):
    if i == 0:
        real_li.append(min(ni))
    else:
        filtered = list(filter(lambda x: x > real_li[i-1], ni))
        if len(filtered)>0:
            real_li.append(min(filtered))
        else:
            real_li.append(min(ni))

count = 0
for ni,n in enumerate(real_li[:len(real_li)-1]):
    if real_li[ni]>real_li[ni+1]:
        count += 1
score = count*len(s)+real_li[-1]+1
print(score)

Submission status: Wrong Answer

Solution code:
s=str(input())
t=str(input())
che=True
for i in t:
    if i not in s:
        che=False
if che==False:
    print(-1)
else:

    cnt=len(s)
    sbuf=s[s.index(t[0])+1:]
    sbuf1=s[:]
    t=t[1:]
    buf=0
    for i in range(len(t)):
        if t[i] in sbuf:
            sbuf=sbuf[sbuf.index(t[i])+1:]
            continue
        else:
            while(t[i] not in sbuf):
                sbuf=sbuf + sbuf1[buf]
                if buf<len(s)-1:
                    buf+=1
                else:
                    buf=0
                cnt += 1
            sbuf=sbuf[sbuf.index(t[i])+1:]

    print(cnt)

Submission status: Wrong Answer

Solution code:
import sys

s = input()
t = input()

a = []
ls = len(s)
lt = len(t)

for i in t:
    try:
        a.append(s.index(i))
    except:
        print("-1")
        sys.exit()

p = 0
q = 0

for i in range(lt):
    if i > 0:
        if i == lt - 1:
            q = a[i]
            if a[i - 1] >= a[i]:
                p += 1
        else:
            if a[i - 1] >= a[i]:
                p += 1
            else:
                q = a[i]
    else:
        q = a[i]

ans = p * ls + q + 1

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)
        else:
            ans += dsl[t[i]][0]+1 +(len(s) - now - 1)
        now = dsl[t[i]][0] 
    else:
        index = bisect.bisect_right(dsl[t[i]],now)
        if index == len(dsl[t[i]]):
            index = 0
        #print(index,now,dsl[t[i]])
        indexv = dsl[t[i]][index]
        if indexv > now:
            ans+= indexv - now
            now = indexv
        else:
            index = 0
            ans+= dsl[t[i]][index]+1 + (len(s) - now - 1)
            now = dsl[t[i]][index] 

print(ans)

Submission status: Wrong Answer

Solution code:
words = lambda t : list(map(t, input().split()))
s = input()
t = input()

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

for k,v in dict.items():
    dict[k] = sorted(v)

#print(dict)

from bisect import bisect_right
def find_min_t(cur, c):
    k = bisect_right(dict[c], cur)
    if k >= len(dict[c]):
        return dict[c][0]
    else:
        return dict[c][k]

cur = 0
ans = 0
succeed = True
for c in t:
    if c not in dict:
        succeed = False
        break
    i = find_min_t(cur, c)
    #print(c, i, cur)
    if i <= cur:
        ans += len(s)
    cur = i

if succeed:
    print(ans + cur + 1)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import sys
import queue

sys.setrecursionlimit(200000)

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 = 0
    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]
            print('aaa', t[i], i)
            continue

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

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


if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:
import sys

s = input()
t = input()
if not set(t).issubset(set(s)):
    print(-1)
    sys.exit(0)
count = 0
now_idx = 0
for t_ in t:
    if t_ in s[now_idx:]:
        next_idx = now_idx + s[now_idx:].index(t_)
    else:
        next_idx = s.index(t_)
        count += 1
    now_idx = next_idx
print(count*len(s) + now_idx+1)

Submission status: Wrong Answer

Solution code:
s=input() #数値入力 「N」だけの入力のとき
t=input()  #数値入力 「N」だけの入力のとき
sa=list(s)
ta=list(t)
#print(set(sa))
#print(set(ta))

if len(set(ta)-set(sa))>0:
    print(-1)

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:
    for n in range(0, int((10^100) / sl)):
        tmps = s
        find = True
        while find:
            tmpsl = len(tmps)
            check = tmps.find(t[index])

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

        if index  > tl -1:
            break
    print(i)

Submission status: Wrong Answer

Solution code:
#
# Yet I'm feeling like
# 	There is no better place than right by your side
# 		I had a little taste
# 			And I'll only spoil the party anyway
# 				'Cause all the girls are looking fine
# 					But you're the only one on my mind


import sys
import re
# inf = float("inf")
#sys.setrecursionlimit(1000000)

# abc='abcdefghijklmnopqrstuvwxyz'
# abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
# mod,MOD=1000000007,998244353
# vow=['a','e','i','o','u']
# dx,dy=[-1,1,0,0],[0,0,1,-1]

# from cmath import sqrt
from collections import deque, Counter, OrderedDict,defaultdict
from heapq import nsmallest, nlargest, heapify,heappop ,heappush, heapreplace
# from math import ceil,floor,log,sqrt,factorial,pow,pi,gcd,log10,atan,tan
from bisect import bisect_left,bisect_right
# import numpy as np

def dfs(curr,parent):
    visited[curr]=True
    value[curr]+=value[parent]
    for i in mydict[curr]:
        if not visited[i]:
            dfs(i,curr)


def get_array(): return list(map(int , sys.stdin.readline().strip().split()))
def get_ints(): return map(int, sys.stdin.readline().strip().split())
def input(): return sys.stdin.readline().strip()

string=input()
length=len(string)
T=input()
mydict=defaultdict(list)
for i in range(length):
    mydict[string[i]].append(i+1)
flag=0;last=0;total=0
for i in range(len(T)):
    if T[i] not in mydict:
        flag=1
        break
    else:
        new=bisect_right(mydict[T[i]],last)
        if new==0:
            value=mydict[T[i]][new]
            total+=(value-last)
            last=value
        elif new==len(mydict[T[i]]):
            value=mydict[T[i]][-1]
            total+=(length-last+value)
            last=value
        else:
            value=mydict[T[i]][new]
            total+=(value-last)
            last=value
if flag==0:
    print(total)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
# ABC138E - Strings of Impurity
import sys
input = sys.stdin.readline

def main():
    S = input()
    T = input()
    if set(T) <= set(S):
        lim_s, lim_t = len(S) - 1, len(T)
        ans, cur, idx = 1, 0, -1
        while cur < lim_t:
            x = S[idx + 1 :].find(T[cur])
            if x == -1:
                ans += 1
                idx = -1
            else:
                idx += x + 1
                cur += 1
        ans = (ans - 1) * (lim_s + 1) + idx + 1
        print(ans)
    else:  # corner case
        print(-1)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

counter=0
pos=0

for i in t:
        # print(pos)
        if i not in s:
                print(-1)
                break
        if pos<len(s)-1 and i in s[pos+1:]:
                pos+=s[pos+1:].index(i)+1
        else:
                counter+=1
                pos=s.index(i)

print((counter*len(s)+pos+1)%((10**100)*len(s)))

Submission status: Wrong Answer

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

check = False
for target in t:
    if target not in list(s):
        check = True

if check:
    print(-1)
else:
    i = 0
    flag = True
    if len(t) == 1:
        final = True
    else:
        final = False
    target = t.pop(0)
    cnt = 0
    while flag:
        for i in range(len(s)):
            if s[i] == target:
                if final:
                    cnt += i + 1
                    flag = False
                    break
                target = t.pop(0)
                if len(t) == 0:
                    final = True
        if not final:
            cnt += len(s)
    print(cnt)

Submission status: Wrong Answer

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

x = s.find(t[0])
s.replace(t[0], "0", 1)

if x == -1:
    print(-1)
else:
    if l_t == 1:
        print(x + 1)
    else:
        cnt_1 = 0
        cnt_2 = 0
        for i in range(1, l_t):
            y = s.find(t[i])
            if y == -1:
                cnt_2 = 1
                break
            else:
                if x > y:
                    cnt_1 += 1
                
                x = s.find(t[i])
                s.replace(t[i], "0", 1)
                
        if cnt_2 == 0:
            print(l_s * cnt_1 + x + 1)
        else:
            print(-1)