Problem:


Score : 500 points

Problem Statement

Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists.

Let s' be the concatenation of 10^{100} copies of s. t is a subsequence of the string {s'}_1{s'}_2\ldots{s'}_i (the first i characters in s').

Notes

A subsequence of a string a is a string obtained by deleting zero or more characters from a and concatenating the remaining characters without changing the relative order. For example, the subsequences of contest include net, c, and contest.

Constraints

1 \leq |s| \leq 10^5

1 \leq |t| \leq 10^5

s and t consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s
t

Output

If there exists an integer i satisfying the following condition, print the minimum such i; otherwise, print -1.

Sample Input 1

contest
son

Sample Output 1

10

t = son is a subsequence of the string contestcon (the first 10 characters in s' = contestcontestcontest...), so i = 10 satisfies the condition.

On the other hand, t is not a subsequence of the string contestco (the first 9 characters in s'), so i = 9 does not satisfy the condition.

Similarly, any integer less than 9 does not satisfy the condition, either. Thus, the minimum integer i satisfying the condition is 10.

Sample Input 2

contest
programming

Sample Output 2

-1

t = programming is not a substring of s' = contestcontestcontest.... Thus, there is no integer i satisfying the condition.

Sample Input 3

contest
sentence

Sample Output 3

33

Note that the answer may not fit into a 32-bit integer type, though we cannot put such a case here.



Sample input:
contest
son


Sample output:
10

Submission status: Wrong Answer

Solution code:
import bisect
s = input()
len_s = len(s)
t = input()

ind_dict = {i : [] for i in t}
for i, _s in enumerate(s):
    if _s in t:
        ind_dict[_s] += [i]

def get_least(last, s, dic):
    ls = dic[s]
    ind = bisect.bisect_right(ls, last)
    if ind >= len(ls):
        return ls[0]
    else:
        return ls[ind]

if -1 in [s.find(_t) for _t in t]:
    print(-1)
else:
    cnt = 0
    last = get_least(0, t[0], ind_dict)
    _next = last
    for _t in t[1:]:
        _next = get_least(last, _t, ind_dict)
        if _next < last:
            cnt += 1
        last = _next
    print(cnt * len(s) + _next + 1)

Submission status: Wrong Answer

Solution code:
import re
from collections import deque
#s="contestcontest"
#t="son"
s=input()
t=input()
T=deque(t)
s_ = ""
ans_list=[]
noans=0
end=False
for i in range(10**100):
    s_+=s
    for t_ in t:
        result = re.search(t_,s_)
        #print(t_,s_)
        if result != None:
            index = result.start()
            ans_list.append(index+1)
            s_ = s_[index+1:]
            T.popleft()
            if len(T) == 0:
                end=True
                break
        else:
            noans+=1
            if noans == 2:
                end=True
            break
    if end:
        break
    else:
        t=deque(T)
if noans == 2:
    print(-1)
else:
    print(sum(ans_list))

Submission status: Wrong Answer

Solution code:
import re
from collections import deque
#s="contestcontest"
#t="son"
s=input()
t=input()
T=deque(t)
s_ = ""
ans_list=[]
ans=0
end=False
for i in range(10**100):
    s_+=s
    for t_ in t:
        result = re.search(t_,s_)
        #print(t_,s_)
        if result != None:
            index = result.start()
            ans_list.append(index+1)
            s_ = s_[index+1:]
            T.popleft()
            if len(T) == 0:
                end=True
                break
        else:
            ans=-1
            end=True
            break
    if end:
        break
    else:
        t=deque(T)
if ans != 0:
    print(ans)
else:
    print(sum(ans_list))

Submission status: Wrong Answer

Solution code:
slist = input()
tlist = input()
scounter = 0
slength=len(slist)
slist=slist+slist
preposition=-1
for tchar in tlist:
    preposition=slist.find(tchar, preposition+1)
    if preposition==-1:
        scounter=-1
        break
    elif preposition>slength:
        scounter+=slength
        preposition-=slength
    # print(tchar, scounter, preposition)
if (scounter==-1):
    print(scounter)
else:
    print(scounter+preposition+1)

Submission status: Wrong Answer

Solution code:
slist = input()
tlist = input()
scounter = 0
slength=len(slist)
slist=slist+slist
preposition=-1
for tchar in tlist:
    preposition=slist.find(tchar, preposition+1)
    if preposition==-1:
        scounter=-1
        break
    elif preposition>slength:
        scounter+=slength
        preposition-=slength
    print(tchar, scounter, preposition)
if (scounter==-1):
    print(scounter)
else:
    print(scounter+preposition+1)

Submission status: Wrong Answer

Solution code:
import sys

s = input()
t = input()
s_len = len(s)
t_len = len(t)
index_list = [0]*t_len
rt1 = 0
rt2 = 0
s = s + s
for m in range(t_len):
	rt1 = s.find(t[m],rt1)
	if rt1 == -1:
		print(rt1)
		sys.exit()
	else:
		#rt1 = rt1 % s_len + 1
		#index_list[m] = rt1
		if rt1 < s_len:
			pass
		else:
			rt1 = rt1 - s_len
			rt2 += 1
rt1 += 1
rt = rt1 + rt2 * s_len
print(rt)

Submission status: Wrong Answer

Solution code:
import sys

s = input()
t = input()
s_len = len(s)
t_len = len(t)
index_list = [0]*t_len
rt1 = 0
rt2 = 0
s = s + s
for m in range(t_len):
	rt1 = s.find(t[m],rt1)
	if rt1 == -1:
		print(rt1)
		sys.exit()
	else:
		index_list[m] = rt1
		rt1+=1
		if m==0 or index_list[m-1]<index_list[m]:
			pass
		else:
			rt2 += 1

rt = rt1 + rt2 * s_len
print(rt)

Submission status: Wrong Answer

Solution code:
import sys

s = input()
t = input()
s_len = len(s)
t_len = len(t)
index_list = [0]*t_len
rt1 = 0
rt2 = 0
s = s + s
for m in range(t_len):
	rt1 = s.find(t[m],rt1)
	if rt1 == -1:
		print(rt1)
		sys.exit()
	else:
		#index_list[m] = rt1
		rt1 += 1
		if rt1 < s_len:
			pass
		else:
			rt2 += 1

rt = rt1 + rt2 * s_len
print(rt)

Submission status: Wrong Answer

Solution code:
import sys

s = input()
t = input()
s_len = len(s)
t_len = len(t)
index_list = [0]*t_len
rt1 = 0
rt2 = 0

for m in range(t_len):
	rt1 = s.find(t[m])
	if rt1 == -1:
		print(rt1)
		sys.exit()
	else:
		index_list[m] = rt1
		if m == 0 or index_list[m-1] < index_list[m]:
			pass
		else:
			rt2 += 1

rt = rt1 + rt2 * s_len + 1
print(rt)

Submission status: Wrong Answer

Solution code:
import sys

s = input()
t = input()
s_list = list(s)
s_len = len(s_list)
t_list = list(t)
t_len = len(t_list)
index_list = [0]*t_len
rt1 = 0
rt2 = 0

for m in range(t_len):
	if t_list[m] in s_list:
		index_list[m] = s_list.index(t_list[m])
	else:
		print(-1)
		sys.exit()

for n in range(t_len):
	if n == 0 or index_list[n-1] < index_list[n]:
		rt1 = index_list[n]
	else:
		rt1 = index_list[n]
		rt2 += 1

rt = rt1 + rt2 * s_len + 1
print(rt)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
s_list = list(s)
s_len = len(s_list)
t_list = list(t)
t_len = len(t_list)
index_list = [0]*t_len
rt1 = 0
rt2 = 0

for m in range(t_len):
	if t_list[m] in s_list:
		index_list[m] = s_list.index(t_list[m])

for n in range(t_len):
	if n == 0 or index_list[n-1] < index_list[n]:
		rt1 = index_list[n]
	else:
		rt1 = index_list[n]
		rt2 += 1

rt = rt1 + rt2 * s_len + 1
print(rt)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
s_list = list(s)
s_len = len(s_list)
t_list = list(t)
t_len = len(t_list)
S_list = [0]*s_len*(10^100)

for m in range(10^100):
	for n in range(s_len):
		S_list[m*s_len+n] = s_list[n]

for q in range(t_len):
	if t_list[q] in s_list:
		for p in range(len(S_list)):			
			if t_list[q] == S_list[p]:
				rt = p+1
				break
			else:
				S_list[p] = 0
	else:
		rt = -1
		break

print(rt)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
s_list = list(s)
s_len = len(s_list)
t_list = list(t)
t_len = len(t_list)
S_list = [0]*s_len*(10)

for m in range(10):
	for n in range(s_len):
		S_list[m*s_len+n] = s_list[n]
#print(S_list)
#for p in range(len(S_list)):
for q in range(t_len):
	if t_list[q] in s_list:
		for p in range(len(S_list)):			
			if t_list[q] == S_list[p]:
				rt = p+1
				break
			else:
				S_list[p] = 0
	else:
		rt = -1
		break

print(rt)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import bisect
s = input()
t = input()
ls = len(s)
dbls = s*2
idx = 0
ans = 0
dic = defaultdict(list)

for i, c in enumerate(dbls):
    dic[c] += [i+1]

print(dbls)

for c in t:
    if not c in dic.keys():
        print(-1)
        exit()
    lst = dic[c]
    i = bisect.bisect_right(lst, idx)
    idx = lst[i]
    if idx > ls:
        ans += ls
        idx -= ls
    else:
        idx = lst[i]

if not idx == 0:
    ans += idx
print(ans)

Submission status: Wrong Answer

Solution code:
from array import array
from collections import deque

#sys.setrecursionlimit(200000)

import sys

def input():
    return sys.stdin.readline().rstrip('\n')

s = array('u', list(input()))
t = array('u', list(input()))

c_in_s = {}
for c in 'abcdefghijklmnopqrstuvwxyz':
    c_in_s[c] = []
for i,c in enumerate(s):
    c_in_s[c].append(i)

# find c in s[i:]
def find_char_in_s(c, i):
    idlist = c_in_s[c]
    for id in idlist:
        if i<=id:
            return id
    return -1

# find Longest common subsequence
# returns indexes of last common char in s1 and s2
def find_lcs(s, t, offset_t):
    sid = -1
    tid = offset_t
    for i in range(tid, len(t)):
        c = t[i]
        prev_sid = sid
        sid = find_char_in_s(c, sid+1)
        if sid == -1:
            return prev_sid, i-1
        if i == len(t)-1:
            return sid,i
    return -1,-1

count = 0

i_sdash = -1
offset_t = 0
i_t = 0

while True:
    sid,tid = find_lcs(s,t, offset_t)
    if sid == -1 and i_sdash == -1:
        print(-1)
        sys.exit()
    elif sid == -1 and i_sdash >= 0:
        break
    else:
        i_sdash = len(s)*count + sid
        offset_t = tid+1
        count += 1

print(i_sdash+1)

Submission status: Wrong Answer

Solution code:
from array import array
from collections import deque

#sys.setrecursionlimit(200000)

import sys

def input():
    return sys.stdin.readline().rstrip('\n')

s = array('u', list(input()))
t = array('u', list(input()))
print(s,t)

c_in_s = {}
for c in 'abcdefghijklmnopqrstuvwxyz':
    c_in_s[c] = []
for i,c in enumerate(s):
    c_in_s[c].append(i)

# find c in s[i:]
def find_char_in_s(c, i):
    idlist = c_in_s[c]
    for id in idlist:
        if i<=id:
            return id
    return -1

# find Longest common subsequence
# returns indexes of last common char in s1 and s2
def find_lcs(s, t, offset_t):
    sid = -1
    tid = offset_t
    for i in range(tid, len(t)):
        c = t[i]
        prev_sid = sid
        sid = find_char_in_s(c, sid+1)
        if sid == -1:
            return prev_sid, i-1
        if i == len(t)-1:
            return sid,i
    return -1,-1

count = 0

i_sdash = -1
offset_t = 0
i_t = 0

while True:
    sid,tid = find_lcs(s,t, offset_t)
    if sid == -1 and i_sdash == -1:
        print(-1)
        sys.exit()
    elif sid == -1 and i_sdash >= 0:
        break
    else:
        i_sdash = len(s)*count + sid
        offset_t = tid+1
        count += 1

print(i_sdash+1)

Submission status: Wrong Answer

Solution code:
from array import array
from collections import deque

#sys.setrecursionlimit(200000)

import sys

def input():
    return sys.stdin.readline().rstrip('\n')

s = array('u', list(input()))
t = array('u', list(input()))


# find Longest common subsequence
# returns indexes of last common char in s1 and s2
def find_lcs(s, t, offset_t):
    sid = -1
    tid = offset_t
    for i,c in enumerate(s):
        if tid > len(t)-1:
            break
        if c == t[tid]:
            sid = i # current index in s
            tid += 1 # next index in t
    return sid, tid-1

count = 0

i_sdash = -1
offset_t = 0
i_t = 0

while True:
    sid,tid = find_lcs(s,t, offset_t)
    if sid == -1 and i_sdash == -1:
        print(-1)
        sys.exit()
    elif sid == -1 and i_sdash >= 0:
        break
    else:
        i_sdash = len(s)*count + sid
        offset_t = tid+1
        count += 1

print(i_sdash+1)

Submission status: Wrong Answer

Solution code:
from array import array
from collections import deque

#sys.setrecursionlimit(200000)

import sys

def input():
    return sys.stdin.readline().rstrip('\n')

s = input()
t = input()


# find Longest common subsequence
# returns indexes of last common char in s1 and s2
def find_lcs(s1,s2):
    s2array = array('u', s2)
    i_s1 = -1
    i_s2 = 0
    for i,c in enumerate(s1):
        if i_s2 > len(s2)-1:
            break
        if c == s2array[i_s2]:
            i_s1 = i # current index in s1
            i_s2 += 1 # next index in s2
    return i_s1, i_s2-1

count = 0

i_sdash = -1
i_t = 0
ttail = t[0:]

while True:
    i,j = find_lcs(s,ttail)
    ttail = ttail[j+1:]
    if i == -1 and i_sdash == -1:
        print(-1)
        sys.exit()
    elif i == -1 and i_sdash >= 0:
        break
    else:
        i_sdash = len(s)*count + i
        count += 1

print(i_sdash+1)

Submission status: Wrong Answer

Solution code:
from array import array
from collections import deque

#sys.setrecursionlimit(200000)

import sys

def input():
    return sys.stdin.readline().rstrip('\n')

s = input()
t = input()


# find Longest common subsequence
# returns indexes of last common char in s1 and s2
def find_lcs(s1,s2):
    s2array = array('u', s2)
    i_s1 = -1
    i_s2 = 0
    for i,c in enumerate(s1):
        if i_s2 > len(s2)-1:
            break
        if c == s2array[i_s2]:
            i_s1 = i # current index in s1
            i_s2 += 1 # next index in s2
    return i_s1, i_s2-1

count = 0

i_sdash = 0
i_t = 0
ttail = t[0:]

while True:
    i,j = find_lcs(s,ttail)
    ttail = ttail[j+1:]
    if i == -1 and i_sdash == 0:
        print(-1)
        sys.exit()
    elif i == -1 and i_sdash > 0:
        break
    else:
        i_sdash = len(s)*count + i
        count += 1

print(i_sdash+1)

Submission status: Wrong Answer

Solution code:
from array import array
from collections import deque

#sys.setrecursionlimit(200000)

import sys

def input():
    return sys.stdin.readline().rstrip('\n')

s = input()
t = input()


# find Longest common subsequence
# returns indexes of last common char in s1 and s2
def find_lcs(s1,s2):
    s2array = array('u', s2)
    i_s1 = 0
    i_s2 = 0
    for i,c in enumerate(s1):
        if i_s2 > len(s2)-1:
            break
        if c == s2array[i_s2]:
            i_s1 = i # current index in s1
            i_s2 += 1 # next index in s2
    return i_s1, i_s2-1

count = 0

i_sdash = 0
i_t = 0
ttail = t[0:]

while True:
    i,j = find_lcs(s,ttail)
    ttail = ttail[j+1:]
    if i == 0 and i_sdash == 0:
        print(-1)
        sys.exit()
    elif i == 0 and i_sdash > 0:
        break
    else:
        i_sdash = len(s)*count + i
        count += 1

print(i_sdash+1)

Submission status: Wrong Answer

Solution code:
S=input();T=input();p=-1
for s in T:
  q=(p+1)%len(S)
  if not s in S:break
  if s in S[q:]:p+=S[q:].index(s)+1
  else:p+=S.index(s)+len(S[q:])+1
print(p+1)

Submission status: Wrong Answer

Solution code:
slist = input()
tlist = input()
scounter = 0
slength=len(slist)
pretchar="aa"
for tchar in tlist:
    if pretchar==tchar:
        scounter+=slength
        # print(tchar, slist[scounter % slength], scounter)
        continue
    Sstart=scounter%slength
    while True:
        # print(tchar,slist[scounter%slength],scounter)
        if tchar == slist[scounter%slength]:
            scounter += 1
            pretchar=tchar
            break
        scounter += 1
        if scounter%slength==Sstart:
            scounter=-1
            break
    if scounter==-1:
        break
print(scounter)

Submission status: Wrong Answer

Solution code:
slist = input()
tlist = input()
scounter = 0
slength=len(slist)
pretchar="aa"
for tchar in tlist:
    if pretchar==tchar:
        scounter+=slength
        continue
    Sstart=scounter%slength
    while True:
        # print(tchar,slist[scounter%slength],scounter)
        if tchar == slist[scounter%slength]:
            scounter += 1
            pretchar=tchar
            break
        scounter += 1
        if scounter%slength==Sstart:
            scounter=-1
            break
    if scounter==-1:
        break
print(scounter)

Submission status: Wrong Answer

Solution code:
slist = input()
tlist = input()
scounter = 0
slength=len(slist)
for tchar in tlist:
    Sstart=scounter%slength
    while True:
        print(tchar,slist[scounter%slength],scounter)
        if tchar == slist[scounter%slength]:
            scounter += 1
            break
        scounter += 1
        if scounter%slength==Sstart:
            scounter=-1
            break
    if scounter==-1:
        break
print(scounter)

Submission status: Wrong Answer

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

slist = list(s)
tlist = list(t)
# 番兵設定

scounter = 0
slength=len(slist)

for tchar in tlist:
    tcharcheckflag=False
    Slist=[]
    for schar in slist[scounter%slength:]:
        Slist.append(schar)
    for schar in slist[:scounter%slength]:
        Slist.append(schar)
    Slist.append("aa")
    for schar in Slist:
        print(schar,tchar,scounter)
        if schar == "aa":
            scounter = -1
            tcharcheckflag=True
            break
        if tchar == schar:
            scounter += 1
            tcharcheckflag=True
            break
        scounter+=1
    if scounter == -1:
        break

print(scounter)

Submission status: Wrong Answer

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

slist = list(s)
tlist = list(t)
# 番兵設定
slist.append("aa")
scounter = 0
slength=len(slist)

for tchar in tlist:
    tcharcheckflag=False
    for schar in slist[scounter%slength:]:
        # print(schar,tchar,scounter)
        if schar == "aa":
            scounter = -1
            tcharcheckflag=True
            break
        if tchar == schar:
            scounter += 1
            tcharcheckflag=True
            break
    if tcharcheckflag==False:
        for schar in slist[:scounter%slength]:
            print(schar, tchar, scounter)
            if schar == "aa":
                scounter = -1
                tcharcheckflag = True
                break
            if tchar == schar:
                scounter += 1
                tcharcheckflag = True
                break


    if scounter == -1:
        break

print(scounter)

Submission status: Wrong Answer

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

slist = list(s)
tlist = list(t)
# 番兵設定
slist.append("aa")
scounter = 0

for tchar in tlist:
    for schar in slist:
        # print(schar)
        # print(scounter)
        # sを探してもない文字だった場合
        if schar == "aa":
            scounter = -1
            break
        if tchar == schar:
            scounter += 1
            break
        scounter += 1

    if scounter == -1:
        break

print(scounter-1)

Submission status: Wrong Answer

Solution code:
s=input()
t=input()
 
slist=list(s)
tlist=list(t)
#番兵設定
slist.append("aa")
scounter=0
 
for tchar in tlist:
  for schar in slist:
    #sを探してもない文字だった場合
    if schar =="aa":
      scounter=-1
      break
    if tchar==schar:
      break
    scounter+=1
    
  if scounter==-1:
    break
    
print(scounter)

Submission status: Wrong Answer

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

flag=0
slice_flag=0

for i in range(len(t)):
    if s.find(t[i:i+1])!=-1:
        if s.find(t[i:i+1])<slice_flag:
            flag+=s.find(t[i:i+1])+len(s)-slice_flag
        else:
            flag+=s.find(t[i:i+1])-slice_flag
    else:
        flag=-2
        break
    slice_flag=s.find(t[i:i+1])

print(flag+1)

Submission status: Wrong Answer

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

s_ = s[:]

ti = 0
ans = 0
flag = True

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

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

Submission status: Wrong Answer

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

s_ = s[:]

ti = 0
ans = 0
flag = True

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

Submission status: Wrong Answer

Solution code:
import sys
s = input()
t = input()
i = 0
idx = 0
ccidx = -1
for tc in t:
  try:
    idx = s.index(tc)
    if idx > ccidx:
      i += (idx-ccidx)
    else:
      i += (len(s)-ccidx-1)
      i += (idx+1)
    ccidx = idx
  except ValueError:
    print(-1)
    sys.exit(0)
print(i)

Submission status: Wrong Answer

Solution code:
import sys
s = 'contest'
t = 'programmnig'
i = 0
idx = 0
ccidx = -1
for tc in t:
  try:
    idx = s.index(tc)
    if idx > ccidx:
      i += (idx-ccidx)
    else:
      i += (len(s)-ccidx-1)
      i += (idx+1)
    ccidx = idx
  except ValueError:
    print(-1)
    sys.exit(0)
print(i)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
i = 0
idx = 0
ccidx = -1
for tc in t:
  try:
    idx = s.index(tc)
    if idx > ccidx:
      i += (idx-ccidx)
    else:
      i += (len(s)-ccidx-1)
      i += (idx+1)
    ccidx = idx
  except ValueError:
    print(-1)
    break
print(i)

Submission status: Wrong Answer

Solution code:
import bisect
S = input()
T = input()
D = [[] for _ in range(26)]
for i in range(26):
  D[i] = [0] * S.count(chr(97 + i))
P = [0] * 26
for i, s in enumerate(S):
  n = ord(s) - 97
  D[n][P[n]] = i + 1
  P[n] += 1
pos = 0
loop = 0
for t in T:
  n = ord(t) - 97
  if D[n] == []:
    print(-1)
    break
  _pos = bisect.bisect(D[n], pos)
  _d = D[n][pos % len(D[n])]
  if _d <= pos:
    loop += 1
  pos = _d
else:
  print(len(S) * loop + pos)

Submission status: Wrong Answer

Solution code:
import bisect
S = input()
T = input()
D = [[] for _ in range(26)]
for i in range(26):
  D[i] = [0] * S.count(chr(97 + i))
P = [0] * 26
for i, s in enumerate(S):
  n = ord(s) - 97
  D[n][P[n]] = i + 1
  P[n] += 1
pos = 0
loop = 0
for t in T:
  n = ord(t) - 97
  if D[n] == []:
    print(-1)
    break
  _pos = bisect.bisect(D[n], pos)
  if _pos == len(D[n]):
    loop += 1
  pos = D[n][pos % len(D[n])]
else:
  print(len(S) * loop + pos)

Submission status: Wrong Answer

Solution code:
S = input()
T = input()
D = [[] for _ in range(26)]
for i in range(26):
  D[i] = [0] * S.count(chr(97 + i))
P = [0] * 26
for i, s in enumerate(S):
  n = ord(s) - 97
  D[n][P[n]] = i + 1
  P[n] += 1
def bsearch(t, pos, loop):
  d = D[ord(t) - 97]
  l = len(d)
  if l < 1:
    return -1, 0
  if pos < d[0]:
    return d[0], loop
  if d[l - 1] <= pos:
    loop += 1
    return d[0], loop
  low = 0
  high = l
  while high - low > 1:
    mid = (high - low) // 2
    if pos < d[mid]:
      high = mid
    else:
      low = mid
  return d[high], loop
pos = 0
loop = 0
for t in T:
  pos, loop = bsearch(t, pos, loop)
  if pos == -1:
    print(-1)
    break
else:
  print(len(S) * loop + pos + 1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import numpy as np


s = input()
t = input()
l = len(s)

idx_dict = defaultdict(list)

#各文字の index を取得
for i, v in enumerate(s):
    idx_dict[v].append(i)

#リストを ndarray に変換
for key, value in idx_dict.items():
    idx_dict[key] = np.array(value)

l = len(s)
rnd = 0
idx = 0
ans = 0

# index のリストを取り出し、現在の idx と比べる
# idx より大きい値がない場合は一周ふやす
for v in t:
    # idx_dict の key に　v が存在しなかったら -1
    if v not in idx_dict:
        ans = -1
        break
    arr = idx_dict[v]
    ar = arr[arr >= idx]
    if ar.size > 0:
        idx = ar[0]          
    else:
        idx = arr[0]
        rnd += 1

if not ans:       
    ans = ((rnd * l) + idx) + 1
        
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import exit, stderr, stdin, setrecursionlimit
input = stdin.readline
# setrecursionlimit(10**7)

def debug(var, name="hoge"):
    print(name +":" + str(type(var)) + " = " + repr(var), file=stderr)
    return



def main():
    # まず可能かどうか
    S = str(input().rstrip())*2
    T = str(input().rstrip())
    LS = len(S)//2
    LT = len(T)
    S *= 3
    # はじめて出る位置
    char_p = dict()
    for i in range(LS):
        if not S[i] in char_p:
            char_p[S[i]] = i
    # print(char_p)

    # char_pの文字が２つめのはじめて出る位置までに
    char_d = dict(zip(char_p.keys(),[[0 for __ in range(LS+char_p[c]+1)] for c in char_p]))
    for c in char_p:
        dis = 0
        for i in range(LS+char_p[c],-1,-1):
            if S[i] == c:
                char_d[c][i] = dis
                dis = 1
            else:
                char_d[c][i] = dis
                dis += 1
    # 答えの計算
    debug(char_d)
    cur = 0
    for t in T:
        if t not in char_p:
            print(-1)
            exit()
        else:
            debug(t,'string')
            # はじめは、はじめて出る位置まで進む
            if cur == 0:
                cur = char_p[t]

            #それ以外なら区切りで相対的な位置を出して足す
            else:
                rel_cur = cur%LS
                debug(rel_cur,"relative_cursol")
                cur += char_d[t][rel_cur]
        debug(cur,"cur")
        # print()
    print(cur+1)






if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from sys import exit, stderr, stdin, setrecursionlimit
input = stdin.readline
# setrecursionlimit(10**7)

def debug(var, name="hoge"):
    print(name +":" + str(type(var)) + " = " + repr(var), file=stderr)
    return



def main():
    # まず可能かどうか
    S = str(input().rstrip())*2
    T = str(input().rstrip())
    LS = len(S)//2
    LT = len(T)
    S *= 3
    # はじめて出る位置
    char_p = dict()
    for i in range(LS):
        if not S[i] in char_p:
            char_p[S[i]] = i
    # print(char_p)

    # char_pの文字が２つめのはじめて出る位置までに
    char_d = dict(zip(char_p.keys(),[[0 for __ in range(LS+char_p[c]+1)] for c in char_p]))
    for c in char_p:
        dis = 0
        for i in range(LS+char_p[c],-1,-1):
                char_d[c][i] = dis
                dis += 1
    # 答えの計算
    debug(char_d)
    cur = 0
    for t in T:
        if t not in char_p:
            print(-1)
            exit()
        else:
            debug(t,'string')
            # はじめは、はじめて出る位置まで進む
            if cur == 0:
                cur = char_p[t]

            #それ以外なら区切りで相対的な位置を出して足す
            else:
                rel_cur = cur%LS
                debug(rel_cur,"relative_cursol")
                cur += char_d[t][rel_cur]
        debug(cur,"cur")
        # print()
    print(cur+1)






if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from sys import exit, stderr, stdin, setrecursionlimit
input = stdin.readline
# setrecursionlimit(10**7)

def debug(var, name="hoge"):
    print(name +":" + str(type(var)) + " = " + repr(var), file=stderr)
    return



def main():
    # まず可能かどうか
    S = str(input().rstrip())*2
    T = str(input().rstrip())
    LS = len(S)//2
    LT = len(T)
    S *= 3
    # はじめて出る位置
    char_p = dict()
    for i in range(LS):
        if not S[i] in char_p:
            char_p[S[i]] = i
    # print(char_p)

    # char_pの文字が２つめのはじめて出る位置までに
    char_d = dict(zip(char_p.keys(),[[0 for __ in range(LS+char_p[c]+1)] for c in char_p]))
    for c in char_p:
        dis = 0
        for i in range(LS+char_p[c],-1,-1):
            if S[i] == c:
                dis = 0
                char_d[c][i] = dis
            else:
                dis += 1
                char_d[c][i] = dis
    # 答えの計算
    # print(char_d)
    cur = 0
    for t in T:
        if t not in char_p:
            print(-1)
            exit()
        else:
            debug(t,'string')
            # はじめは、はじめて出る位置まで進む
            if cur == 0:
                cur = char_p[t]

            #それ以外なら区切りで相対的な位置を出して足す
            else:
                rel_cur = cur%LS
                debug(rel_cur,"relative_cursol")
                cur += char_d[t][rel_cur]
        debug(cur,"cur")
        # print()
    print(cur+1)






if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def get_answer(input_list):
    so = input_list[0]
    t = input_list[1]

    s_len = len(so)

    """
    contest son
    s:5
    o:1 7+2
    n:2 7+3
    
    aabbcc abca
    a:0 0
    b:2 2
    c:4 4
    a:0 0 +6
    
    aa aaaa
    a:0
    a:1
    a:0 + 2
    a:1 + 2    
    """

    t_i = [-1] * len(t)
    s = so + so
    s_index = 0
    for i in range(len(t)):
        index = s.find(t[i], s_index)
        if index < 0:
            break
        index = index % s_len
        t_i[i] = index
        s_index += 1
        s_index = s_index % s_len

    if -1 in t_i:
        return -1


    t_len = len(t_i)
    ret = 0 #len(t_i)
    ret_i = 0
    for i in range(t_len):
        t1 = t_i[i]
        ret = t1
        if i < t_len - 1:
            t2 = t_i[i + 1]
            if t1 >= t2:
                ret_i += 1


    """
    for i in range((1000 - s_index) * s_len):
        find_flag = True
        ss = s
        for tt in t:
            ii = ss.find(tt)
            if ii < 0:
                find_flag = False
                break
            ss = ss[ii + 1:]
        if find_flag:
            ret = len(s)
            break
        s += so[s_index]
        s_index = (s_index + 1) % s_len
    """
    return ret_i * s_len + ret + 1

# b33 179
if __name__ == "__main__":
    input_list = []
    while True:
        try:
            input1 = input()
            if not input1:
                break
            if len(input1) == 0:
                break
            input_list.append(input1)
        except EOFError:
            break
    print(get_answer(input_list))

Submission status: Wrong Answer

Solution code:

s = input()
t = input()
s_letters = set(s)
letter_indices = {letter: [] for letter in s_letters}
for index, letter in enumerate(s): # map letter to its index in s
    letter_indices[letter].append(index)
for letter in t:
    if letter not in s_letters:
        print(-1)
        break
else:
    index = 0
    i = 1
    for letter in t:
        for letter_index in letter_indices[letter]:
            if letter_index > index: # can use this copy of s
                index = letter_index
                break
        else:
            index = letter_indices[letter][0]
            i += 1
    print((i-1)*len(s)+letter_index+1)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
import bisect
s = input()
t = input()
print(-1)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
import bisect
s = input()
t = input()



count = 0
s_len = len(s)
s_list = np.zeros(len(s))
t_list = []*len(t)
indexes = []

for i in range(26):
  
  indexes.append([j for j, x in enumerate(s) if x == chr(i + ord('a')) ])
  

for i,k in enumerate(t):

  index = indexes[ord(k)-ord('a')]
  
  if len(index) == 0:
    print(-1)
    sys.exit(0)
  else:
    t_list.insert(i,index)
    


pre = -1
for i in range(len(t)):
  
  #high = [j  for j in t_list[i] if j > pre]
  high = bisect.bisect_left(t_list[i], pre) 

  if high >= len(t_list[i]):
    
    count += s_len - pre  + t_list[i][0]
    pre = t_list[i][0] 
  else:
    
    count += t_list[i][high] - pre 
    pre = t_list[i][high]

print(count)