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

c=0
j=-1
k=1

#t
for i in range(0,len(t)):
  while True:
    j=j+1
    c=c+1

    if t[i:i+1]==s[j:j+1]:
      #kはもう一度一文字めからカウントし直し
      k=1
      break
    else:
      #hitしなかった場合、index　カウントアップ
      k=k+1
      if j>len(s)-1:
        #jがお尻まで行ったら先頭に戻る
        j=0
      #一周回ってもヒットしなかったらエラー
      if k>len(s)+1:
        print("-1")
        exit()
print(c)

Submission status: Wrong Answer

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

c=0
j=0
k=0

#t
for i in range(0,len(t)):
  while True:
    if t[i:i+1]==s[j:j+1]:
      j=j+1
      k=1
      c=c+1
      break
    else:
      if j==len(s):
        j=0
      else:
        if k+1==len(s):
          print("-1")
          exit()
        else:
          j=j+1
          k=k+1
          c=c+1
print(c)

Submission status: Wrong Answer

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

c=1
j=0
k=0

#t
for i in range(0,len(t)):
  while True:
    if t[i:i+1]==s[j:j+1]:
      j=j+1
      k=0
      break
    else:
      if j==len(s):
        j=0
      else:
        if k+1==len(s):
          print("-1")
          exit()
        else:
          j=j+1
          k=k+1
          c=c+1
print(c)

Submission status: Wrong Answer

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

c=1
j=0
k=0

flag = 0

#t
for i in range(0,len(t)):
  flag = 0
  while True:
    if t[i:i+1]==s[j:j+1]:
#      print(i)
#      print(j)
#      print(c)
      flag = 1
      k=0
      break
    else:
      if j==len(s):
        j=0
#        print("折り返し！")
      else:
        j=j+1
        k=k+1
        c=c+1
        if k==len(s):
          print("-1")
          exit()
#        print("countUp")
print(c)

Submission status: Wrong Answer

Solution code:
def main():
    s = input()
    t = input()
    if judge(s, t):
        makeCount(s, t)
    else:
        print("-1")

def judge(s, t):
    ans = True
    while(t != ""):
        c_t = t[0]
        t = t.replace(c_t, "")
        if c_t in s:
            s = s.replace(c_t, "")
        else:
            ans = False
            break
    return ans

def makeCount(s, t):
    count = 0
    p_t = 0
    flag = True
    s_l = len(s)
    t_l = len(t)
    dum = [None]
    """
    while(flag):
        for i in range(s_l):
            if(p_t == t_l):
                flag = False
                break
            elif(s[i] == t[p_t]):
                p_t += 1
            count += 1
    """
    for d in dum:
        for i in range(s_l):
            if(p_t == t_l):
                dum.clear()
                break
            elif(s[i] == t[p_t]):
                p_t += 1
            else:
                dum.append(None)
            count += 1
    print(count)

main()

Submission status: Wrong Answer

Solution code:
def main():
    s = input()
    t = input()
    if judge(s, t):
        makeCount(s, t)
    else:
        print("-1")

def judge(s, t):
    ans = True
    dum = [None]
    for d in dum:
        c_t = t[0]
        t = t.replace(c_t, "")
        dum.append(None)
        if c_t in s:
            s = s.replace(c_t, "")
            if(t == ""):
                dum.clear()
        else:
            ans = False
            break
    """
    while(t != ""):
        c_t = t[0]
        t = t.replace(c_t, "")
        if c_t in s:
            s = s.replace(c_t, "")
        else:
            ans = False
            break
    """
    return ans

def makeCount(s, t):
    count = 0
    p_t = 0
    flag = True
    s_l = len(s)
    t_l = len(t)
    dum = [None]
    """
    while(flag):
        for i in range(s_l):
            if(p_t == t_l):
                flag = False
                break
            elif(s[i] == t[p_t]):
                p_t += 1
            count += 1
    """
    for d in dum:
        for i in range(s_l):
            if(p_t == t_l):
                dum.clear()
                break
            elif(s[i] == t[p_t]):
                p_t += 1
            else:
                dum.append(None)
            count += 1
    print(count)

main()

Submission status: Wrong Answer

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

def main():
    s = input()
    t = input()
    if judge(s, t):
        makeCount(s, t)
    else:
        print("-1")

def judge(s, t):
    ans = True
    while(t != ""):
        c_t = t[0]
        t = t.replace(c_t, "")
        if c_t in s:
            s = s.replace(c_t, "")
        else:
            ans = False
            break
    return ans

def makeCount(s, t):
    count = 0
    p_t = 0
    flag = True
    s_l = len(s)
    t_l = len(t)
    while(flag):
        for i in range(s_l):
            if(p_t == t_l):
                flag = False
                break
            elif(s[i] == t[p_t]):
                p_t += 1
            count += 1
    print(count)

main()

Submission status: Wrong Answer

Solution code:
import numpy as np

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

s = input()
t = input()
#s = 'contest'
#t = 'programming'

for i, x in enumerate(s):
    alp[ord(x) - 97].append(i)

ind_prev = 0
loop = 0
flag = True
for x in t:
    letter = alp[ord(x) - 97]

    if len(letter) == 0:
        print(-1)
        flag = False
        break
        
    if ind_prev >= letter[-1]:
        ind_prev = 0
        ind_prev_each_letter = [0 for _ in range(26)]
        loop += 1

    ind = np.searchsorted(letter, ind_prev)
    ind_prev = letter[ind]

if flag:
    print(loop * len(s) + ind_prev + 1)

Submission status: Wrong Answer

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

def main():
    s = input().strip()
    t = input().strip()

    if not all(s.find(c) >= 0 for c in t):
        print(-1)
        exit()
    
    sa = [[] for _ in string.ascii_lowercase]
    for i,c in enumerate(s):
        sa[ord(c)-ord('a')].append(i+1)
#    print(sa)

    ls = len(s)
    ans = 0
    i = 0
    for c in t:
        a = sa[ord(c)-ord('a')]
        if len(a) == 0 or i > a[-1]:
            ans += ls-i
            i = 0
        for j in a:
            if j > i:
                ans += j-i
                i = j
                break
#        print("c,a,i,ans=", c, a, i, ans)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

s = str(input())
t = str(input())

s = [s[i] for i in range(len(s)-1) ] 
t = [t[i] for i in range(len(t)-1) ]

s_modified = []


for _ in range(10):
	s_modified += s[:]

print(s_modified)	

T = len(t)
p = 0 


for  i in range(T):
	if not t[i] in s :
		print(-1)
		break
	else:
		if len(s_modified) < len(s):
			# for _ in range(100):
			s_modified += s[:]
		
		r = s_modified.index(t[i])
		p += r + 1
		
		del s_modified[:r+1]
		# print(i)
		# print(r)
		# print(s_modified)

		
		if i == T-1:
			print(p)

Submission status: Wrong Answer

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

def main():
    s = input().strip()
    t = input().strip()

    if not all(s.find(c) >= 0 for c in t):
        print(-1)
        exit()
    
    sa = [[] for _ in string.ascii_lowercase]
    for i,c in enumerate(s):
        sa[ord(c)-ord('a')].append(i)

    ls = len(s)
    ans = 0
    i = -1
    for c in t:
        a = sa[ord(c)-ord('a')]
        if len(a) == 0 or i > a[-1]:
            ans += (ls-i-1)%ls
            i = -1
        for j in a:
            if j > i:
                ans += j-i
                i = j
                break
#        print("c,a,i,ans=", c, a, i, ans)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def main():
    s = input().strip()
    t = input().strip()

    if not all(s.find(c) >= 0 for c in t):
        print(-1)
        exit()
    
    sa = [[] for _ in string.ascii_lowercase]
    for i,c in enumerate(s):
        sa[ord(c)-ord('a')].append(i)

    ls = len(s)
    ans = 0
    i = -1
    for c in t:
        a = sa[ord(c)-ord('a')]
        if len(a) == 0 or i > a[-1]:
            ans += ls - (i+1)%ls
            i = -1
        for j in a:
            if j > i:
                ans += j-i
                i = j
                break
#        print("c,a,i,ans=", c, a, i, ans)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def main():
    s = input().strip()
    t = input().strip()

    if not all(s.find(c) >= 0 for c in t):
        print(-1)
        exit()
    
    sa = [[] for _ in string.ascii_lowercase]
    for i,c in enumerate(s):
        sa[ord(c)-ord('a')].append(i)

    ls = len(s)
    ans = 0
    i = 0
    for c in t:
        a = sa[ord(c)-ord('a')]
        if len(a) == 0 or i > a[-1]:
            ans += ls - i%ls
            i = 0
        for j in a:
            if j >= i:
                ans += j-i
                i = j
                break
#        print("c,a,i, ans=", c, a, i, ans)
    print(ans+1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
S = input()
T = input()
LS = len(S)
LT = len(T)

dp = [0] * (LT+1)
dp[0] = 0
valid = True

for i in range(1,LT+1):
    t = T[i-1]
    p = dp[i-1] % LS
    f = False  # find or not
    for j in range(p+1, LS):
        if S[j] == t:
            dp[i] = dp[i-1] + j - p
            f = True
            break
    if f:
        continue
    for j in range(p+1):
        if S[j] == t:
            dp[i] = dp[i-1] + (LS - p) + j
            f = True
            break
    if f:
        continue
    valid = False
    break
    
print(dp[-1] + 1 if valid else -1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

def main():
    s = input()
    t = input()
    if judgeMate(s, t):
        countMakeTbyS(s, t)
    else:
        print("-1")

def judgeMate(s, t):
    ans = True
    for i in range(len(t)):
        t.replace(t[i], "")
        if t[i] in s:
            s.replace(t[i], "")
        else:
            ans = False
            break
    return ans

def countMakeTbyS(s, t):
    count = 0
    p_t = 0
    flag = True
    while(flag):
        for i in range(len(s)):
            if(p_t == len(t)):
                flag = False
                break
            elif(s[i] == t[p_t]):
                p_t += 1
            count += 1
    print(count + 1)

main()

Submission status: Wrong Answer

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

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


    cnt = 0
    now = -1

    for i in range(len(t)):
        if index[ord(t[i]) - ord("a")] == []:
            print(-1)
            exit()
        for j in range(len(index[ord(t[i]) - ord("a")])):
            if now < index[ord(t[i]) - ord("a")][j]:
                now = index[ord(t[i]) - ord("a")][j]
                break
            if j == len(index[ord(t[i]) - ord("a")]) - 1:
                now = index[ord(t[i]) - ord("a")][j]
                cnt += 1
    ans = cnt * len(s) + now + 1
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

def main():
    s = input()
    t = input()
    if judgeMate(s, t):
        countMakeTbyS(s, t)
    else:
        print("-1")

def judgeMate(s, t):
    ans = True
    for i in range(len(t)):
        t.replace(t[i], "")
        if t[i] in s:
            s.replace(t[i], "")
        else:
            ans = False
            break
    return ans

def countMakeTbyS(s, t):
    def findNumber(s, c, start):
        loc = s.find(c, start)
        if loc == -1:
            loc = s.find(c)
        return loc
    count = 0
    """
    p_t = 0
    flag = True
    while(flag):
        for i in range(len(s)):
            if(p_t == len(t)):
                flag = False
                break
            elif(s[i] == t[p_t]):
                p_t += 1
            count += 1
    """
    pre = 0
    for c in t:
        loc = findNumber(s, c, pre)
        if loc > pre:
            count += (loc - pre)
        elif loc == pre:
            count += len(s)
        elif loc < pre:
            count += len(s) - (pre - loc)
        pre = loc
    print(count + 1)

main()

Submission status: Wrong Answer

Solution code:
import bisect
s = input()*2
lens = len(s)//2
t = input()
appear = {ch: [] for ch in 'abcdefghijklmnopqrstuvwxyz'}
cnt = -1
bis = bisect.bisect_right
ans = 0

for i, ch in enumerate(s):
  appear[ch].append(i)
  
for j in t:
  if appear[j]:
    idx = bis(appear[j],cnt)
    ans += appear[j][idx] - cnt
    cnt = appear[j][idx]%lens
    print(cnt)
  else:
    print(-1)
    exit()
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
s = input()*2
lens = len(s)//2
t = input()
appear = {ch: [] for ch in 'abcdefghijklmnopqrstuvwxyz'}
cnt = 0
bis = bisect.bisect_left
ans = 0

for i, ch in enumerate(s):
  appear[ch].append(i)
  
for j in t:
  if appear[j]:
    idx = bis(appear[j],cnt)
    ans += appear[j][idx] - cnt
    cnt = appear[j][idx]%lens
  else:
    print(-1)
    exit()
print(ans+1)

Submission status: Wrong Answer

Solution code:
import sys

#+++++

def sss(cc, l):
	#print(cc,l)
	if len(l)==0:
		return (-1,'no_chr')
	elif cc > l[-1]:
		return (-1,'over')
	
	#にぶたんを書く
	if cc <= l[0]:
		return (l[0],'')
	
	ue=len(l) - 1 #以下
	sita=0#より大きい
	while ue - sita > 1:
		mid = (ue + sita)//2
		if l[mid] >= cc:
			ue = mid
		else:
			sita = mid
	return (ue,'')

def main():
	s = input()
	t = input()
	i = 0
	bbb=[[] for i in range(26)]
	ctoi=lambda c : ord(c)-97
	
	for i, c in enumerate(s):
		bbb[ctoi(c)].append(i+1)
	
	for i, b in enumerate(bbb):
		pass
		#print(chr(i+ord('a')) + ':' + str(b))
	
	cc = 0
	kkk=0
	ret = 0
	for c in t:
		address=ord(c) - ord('a')
		l=bbb[address]
		#print(cc,l,'a')
		#cc以上の値をみつける。
		nc, st = sss(cc, l)
		cc = nc + 1
		#print(cc)
		if st =='no_chr':
			print(-1)
			return 
			
		if st == 'over':
			kkk += 1
			cc = l[0] + 1
			
	#print(len(s), kkk, cc)
	ret = len(s) * kkk + cc - 1
	print(ret)
			
		

	
	
#+++++

if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Wrong Answer

Solution code:
from bisect import bisect

s = input()
t = input()

if len(set(t) & set(s)) != len(set(t)):
    print(-1)
    exit()

d = dict()

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

ans = 0
res = d[t[0]][0]

for i in range(1, len(t)):
    index = bisect(d[t[i]], res)
    if index == len(d[t[i]]):
        ans += len(s)
        res = d[t[i]][0]
    else:
        res = d[t[i]][index]

ans += d[t[-1]][0] + 1

print(ans)

Submission status: Wrong Answer

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools, os, pdb
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
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 LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def _I(): return int(sys.stdin.readline())
def _F(): return float(sys.stdin.readline())
def _pf(s): return print(s, flush=True)
def perr(s): return print(s, file=sys.stderr)

S = input()
T = input()

if any(i not in sorted(list(set(S))) for i in sorted(list(set(T)))):
    print(-1)
    exit()

d = {}
for sidx, s in enumerate(S):
    if s not in d.keys():
        d[s] = []
    d[s].append(sidx)
# print('d', d)
current = None
count = 0
ans = 0
for tidx, t in enumerate(T):
    #  pdb.set_trace()
    #  print('--- t: ', t, tidx)
    if current == None:
        current = d[t][0]
        #  print('current', current)
    else:
        # 1文字ごと見ていく
        b = bisect.bisect_left(d[t], current)
        # print('b', b)
        # print('current', current)
        if b == len(d[t]):
            # 超えている場合
            current = d[t][0]
            if tidx == len(T) - 1:
                # 最後の文字なので終わり
                ans = current+1
                # print('last1', ans)
                break
            else:
                # 続く
                # print('continue')
                count += 1
                continue
        else:
            # 文字列の最後の前に探している文字があった場合
            if tidx == len(T) - 1:
                # 最後の文字なので終わり
                # print('last2', d[t][b])
                ans = d[t][b]+1
                break
            else:
                # print('continue1')
                current = d[t][b]
                continue

        #  idx = d[t][b]
        #  print('=== idx', idx)
        #  if current <= idx:
        #      # もう一周
        #      current = d[t][0]
        #      if tidx == len(T) - 1:
        #          # 終わり
        #          ans += current+1
        #          break
        #      count += 1
perr('***count')
perr(count)
# print('ans ', ans)
print(len(S)*count + ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import bisect

s = input()
t = input()

ls = len(s)-1
cnt = 0
num = -1
di = defaultdict(list)
for ind,i in enumerate(s):
    di[i].append(ind)

for i in t:
    if(i not in di):
        print(-1)
        break
    if(di[i][-1]<=num):
        cnt += ls-num+di[i][0]+1
    else:
        if(num==-1):
            num=0
        x = bisect.bisect_left(di[i],num)
        cnt += di[i][x]-num
    num = cnt%(ls+1)

else:
    print(cnt+1)

Submission status: Wrong Answer

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

ls = len(s)-1
cnt = 0
num = 0
di = defaultdict(list)
for ind,i in enumerate(s):
    di[i].append(ind)

for i in t:
    num = cnt%(ls+1)
    if(i not in di):
        print(-1)
        break
    if(max(di[i])<=num):
        cnt += ls-num+min(di[i])+1
    else:
        x = bisect.bisect_left(di[i],num)
        cnt += di[i][x]-num

else:
    print(cnt+1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import bisect
s = input()
t = input()
#%%
ls = len(s)-1
cnt = 0
num = 0
di = defaultdict(list)
for ind,i in enumerate(s):
    di[i].append(ind)

for i in t:
    num = cnt%(ls+1)
    if(i not in di):
        print(-1)
        break
    if(max(di[i])<=num):
        cnt += ls-num+min(di[i])
    else:
        x = bisect.bisect_left(di[i],num)
        cnt += di[i][x]-num+1
    print(i,cnt)

else:
    print(cnt+1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from bisect import bisect_left

s = input()
t = input()
s_len = len(s)
t_len = len(t)
if not set(t) <= set(s):
  print(-1)
else: 
  idx = defaultdict(list)
  for i in range(s_len):
    idx[s[i]].append(i)
  
  loop_num = 0
  current_position = -1
  for i in range(t_len):
    char = t[i]
    tmp_idx = idx[char]
    n = bisect_left(tmp_idx, current_position)
    if n < len(tmp_idx):
      current_position = tmp_idx[n]
    else:
      current_position = tmp_idx[0]
      loop_num += 1
  print(s_len * loop_num + current_position + 1)

Submission status: Wrong Answer

Solution code:

s = str(input())
t= str(input())

sdict = {}
for i in range(len(s)):
    if s[i] not in sdict:
        sdict[s[i]]=[]
    sdict[s[i]].append(i+1)
    
#print(sdict)

#二分木探索
def search(list_,mark_):
    if  len(list_) == 1:
            return list_[0]
    else:
            mid = len(list_)//2
            if list_[mid-1]>mark_:
                return search(list_[:mid],mark_)
            else:
                return search(list_[mid:],mark_)

ans = 0
mark = 0
for _ in range(len(t)):
    target = t[_]
    if target not in sdict:
        ans = -1
        break
    else:
        if sdict[target][-1]<mark:
            mark = sdict[target][0]
            ans += len(s)
        else:
            posi = search(sdict[target],mark)
            mark = posi
        
if ans != -1:
    ans += mark
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

s = input()
t = input()
T = set(t)
s_num = len(s)
t_num = len(T)

# make a dictonary whose keys are the alphabets in t
dict = {}
appearing_times = {}

# when each alphabet in t appears in s
for w in T:
    dict[w] = []
    appearing_times[w] = 0
    index = -1
    while True:
        index = s.find(w, index + 1)
        if index == -1 and appearing_times[w] == 0:
            print(-1)
            exit()
        elif index == -1:
            break
        else:
            dict[w].append(index)
            appearing_times[w] += 1

# how many times s is repeated
count = 0
# 0 <= length <= s_num - 1
s_index = 0

for w in t:
    idx = bisect.bisect_left(dict[w], s_index)
    if idx < appearing_times[w]:
        s_index = dict[w][idx]
    else:
        count += 1
        s_index = dict[w][0]

print(count * s_num + s_index + 1)

Submission status: Wrong Answer

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

sdict = {}
for i in range(len(s)):
    if s[i] not in sdict:
        sdict[s[i]]=[]
    sdict[s[i]].append(i+1)
    
#print(sdict)

#二分木探索
def search(list_,mark_):
    if list_[-1] < mark_:
        return -1
    else:
        if  len(list_) == 1:
            return list_[0]
        else:
            mid = len(list_)//2
            if list_[mid-1]>mark_:
                return search(list_[:mid],mark_)
            else:
                return search(list_[mid:],mark_)

ans = 0
mark = 0
for _ in range(len(t)):
    target = t[_]
    if target not in sdict:
        ans = -1
        break
    else:
        posi = search(sdict[target],mark)
        if posi == -1:
            mark = sdict[target][0]
            ans += len(s)
        else:
            mark = posi
        
if ans != -1:
    ans += mark
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from operator import itemgetter #sortedの対象を決めたい
from fractions import gcd #最大公約数求めたい
from math import ceil, floor, sqrt #小数点切り上げ、切り捨て、平方根求めたい
from copy import deepcopy #参照で影響されないコピーが作られる
from collections import Counter, deque #要素ごとの出現回数を数える。双方向アクセス可能データ型
import heapq
from functools import reduce
sys.setrecursionlimit(200000)

input = sys.stdin.readline
# template




s = str(input())
t = str(input())

for i in range(len(t)):
    if s.find(t[i])==-1:
        print(-1)
        break

else:
    a = 0
    count = 0
    for k in t:
        if k in s[a:]:
            a = s.find(k, a) + 1
        else:
            count += 1
            a = s.find(k) + 1

    print(count*len(s) + a)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
 
t = "&" + t
td = [0]*(len(t))
d = {chr(i):[] for i in range(97, 97+26)}
ds, dc = d.copy(), d.copy()
for i, key in enumerate(s):
    d[key] += [i]
for key, val in d.items():
    ds[key] = len(val)
    dc[key] = 0
    
lc = 0
impossible = False
 
for i, key in enumerate(t[1:]):
    if d[key]:
        ti = dc[key] % ds[key]
        td[i+1] = d[key][ti]+1
        dc[key] += 1
        if td[i] >= td[i+1]: lc += 1
    else:
        impossible = True
        break
        
print(-1) if impossible else print(lc*len(s) + td[-1])

Submission status: Wrong Answer

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

t = "&" + t
td = [0]*(len(t))
d = {chr(i):[] for i in range(97, 97+26)}
ds, dc = d.copy(), d.copy()
for i, key in enumerate(s):
    d[key] += [i]
for key, val in d.items():
    ds[key] = len(val)
    dc[key] = 0
    
lc = 0
impossible = False

for i, key in enumerate(t[1:]):
    if d[key]:
        ti = dc[key] % ds[key]
        td[i+1] = d[key][ti]+1
        dc[key] += 1
        if td[i] > td[i+1]: lc += 1
    else:
        impossible = True
        break
        
print(-1) if impossible else print(lc*len(s) + td[-1])

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
s_ = s[:]
cnt = 0
tlen = len(t)
ans = 0
flag = True

while cnt < tlen:
    
    num = s_.find(t[cnt])
    
    if num != -1:
        ans += (num+1)
        s_ = s_[(num+1):]
        cnt += 1
        flag = False
    else:
        if flag:
            print(-1)
            break
        ans += len(s_)
        s_ = s[:]
        flag = True
        
print(ans)

Submission status: Wrong Answer

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

s_dash = ""
for i in range(10):
    s_dash += s
s_list = list(s_dash)

i = 0
search_flag = 0
while len(s_list) > 0:
    if len(t_list) == 0:
        search_flag = 1
        break
    i = i + 1
    if s_list.pop(0) == t_list[0]:
        t_list.pop(0)
            
if search_flag != 1:
    i = -1

print(i)

Submission status: Wrong Answer

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

alpha = [[] for _ in range(26)]
c2i = lambda x: ord(x) - ord('a')

for i, sl in enumerate(s):
    alpha[c2i(sl)].append(i)

pos = 0
count = 1
length = len(s)
for tl in t:
    places = alpha[c2i(tl)]
    if len(places) == 0:
        print(-1)
        break

    index = bisect_left(places, pos+1)
    if index != len(places):
        place = places[index]
        count += place - pos
        pos = place
    else:
        count += (length-pos-1) + places[0] + 1
        pos = places[0]
else:
    print(count)

Submission status: Wrong Answer

Solution code:
s = list(input())
t = list(input())
not_exist_flag = 0
finish_flag = False
t_index = 0

for count in range(len(s)):
    for char_num in range(len(s)):
        if t[t_index] == s[char_num]:
            if t_index == len(t) - 1:
                finish_flag = True
                break
            t_index += 1
    if finish_flag:
        break

if t_index != len(t) - 1:
    print(-1)
else:
    print(count * len(s) + char_num + 1)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
n = len(s)
nxt = [list() for x in range(n + 1)]
nxt[n] = [n for x in range(26)]
print(*nxt[n])
for i in range (n - 1, -1, -1):
    for v in nxt[i + 1]:
        nxt[i].append(v)
    nxt[i][ord(s[i]) - ord('a')] = i;
ans = 0
cur = 0
for i in range (len(t)):
    c = ord(t[i]) - ord('a')
    if (nxt[0][c] == n) :
        ans = 0
        cur = -1
        break
    if (nxt[cur][c] == n) :
        ans += 1
        cur = 0
    cur = nxt[cur][c] + 1
print(ans * n + cur)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
n = len(s)
nxt = [list() for x in range(n + 1)]
nxt[n] = [n for x in range(26)]
print(*nxt[n])
for i in range (n - 1, -1, -1):
    for v in nxt[i + 1]:
        nxt[i].append(v)
    nxt[i][ord(s[i]) - ord('a')] = i;
ans = 0
cur = 0
for i in range (len(t)):
    c = ord(t[i]) - ord('a')
    if (nxt[0][c] == n) :
        ans = 0
        cur = -1
        break
    if (nxt[cur][c] == n) :
        print("wtf")
        ans += 1
        cur = 0
    cur = nxt[cur][c] + 1
print(ans * n + cur)

Submission status: Wrong Answer

Solution code:
import os
import bisect
s = input()
t = input()

s_idx_map = {}
for i in range(len(s)):
    c = s[i]
    if c not in s_idx_map:
        s_idx_map[c] = [i]
    else:
        s_idx_map[c].append(i)
t_idx_map = {}
for i in range(len(t)):
    c = t[i]
    if c not in t_idx_map:
        t_idx_map[c] = [i]
    else:
        t_idx_map[c].append(i)

ans = 0
current_idx = -1
for c in t:
    if c not in s_idx_map:
        print(-1)
        os.sys.exit()
    induces = s_idx_map[c]
    pos = bisect.bisect_left(induces, current_idx)
    if pos < len(induces):
        ans += induces[pos] - current_idx
        current_idx = induces[pos]
    else:
        ans += (len(s)-1) - current_idx
        ans += induces[0] + 1
        current_idx = induces[0]

print(ans)

Submission status: Wrong Answer

Solution code:
import os
import bisect
s = input()
t = input()

s_idx_map = {}
for i in range(len(s)):
    c = s[i]
    if c not in s_idx_map:
        s_idx_map[c] = [i]
    else:
        s_idx_map[c].append(i)
t_idx_map = {}
for i in range(len(t)):
    c = t[i]
    if c not in t_idx_map:
        t_idx_map[c] = [i]
    else:
        t_idx_map[c].append(i)

ans = 0
current_idx = -1
for c in t:
    if c not in s_idx_map:
        print(-1)
        os.sys.exit()
    induces = s_idx_map[c]
    pos = bisect.bisect_left(induces, current_idx)
    if pos < len(induces):
        ans += induces[pos] - current_idx
    else:
        ans += (len(s)-1) - current_idx
        ans += induces[0] + 1
    current_idx = induces[0]
    
print(ans)

Submission status: Wrong Answer

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

cflg = 1
if len(set(t))>=len(set(s)):
    cflg = 0
cnt = 0
while cflg == 1:
    cflg = 0
    for i in range(len(s)):
        cnt+=1
        if s[i] == t[0]:
            cflg = 1
            t = t[1:]
            if len(t) == 0:
                cflg = 0
                break

if len(t) !=0:
    print(-1)
else:
    print(cnt)

Submission status: Wrong Answer

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

def solve():
  s = input().strip()
  t = input().strip()
  query = [[] for _ in range(26)]
  n = len(s)
  for i in range(n):
    alphabet = ord(s[i])-97
    query[alphabet].append(i+1)

  
  place = -1
  loops = 0
  for j in t:
    tmp = ord(j)-97
    if len(query[tmp]) == 0:
      print(-1)
      exit()
    else:
      nextplace = bisect.bisect_left(query[tmp],place)
      if nextplace == len(query[tmp]):
        loops += 1
        place = query[tmp][0]
      else:
        place = query[tmp][nextplace]

  print(loops*n +place)
solve()

Submission status: Wrong Answer

Solution code:
import string
import bisect
S = input()
T = input()
n = len(S)
sa = {_: [] for _ in string.ascii_lowercase}
for i, s in enumerate(S):
    sa[s] += [i]
ans = 0
i = -1
for t in T:
    if sa[t]:
        j = bisect.bisect_left(sa[t], i)
        if j == len(sa[t]):
            ni = sa[t][0] + n
        else:
            ni = sa[t][j]
        ans += ni - i
        i = ni % n
    else:
        print(-1)
        exit()
print(ans)