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 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)-97]
  
  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)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

s = input()
t = input()



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

for i,k in enumerate(t):

  indexes = [j for j, x in enumerate(s) if x == k]
  if len(indexes) == 0:
    print(-1)
    sys.exit(0)
  else:
    t_list.insert(i,indexes)
    

import bisect
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) 
  print(high , len(t_list[i]))
  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)

Submission status: Wrong Answer

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

    s = so[:len(t)]
    s_index = len(s) % len(so)
    s_len = len(so)
    ret = -1

    """
    abcde
    ba abcde a
    
    """
    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

# 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:
def get_answer(input_list):
    so = input_list[0]
    s = so[0]
    s_index = 1 % len(so)
    s_len = len(so)
    t = input_list[1]
    ret = -1

    """
    """
    for i in range(1000 * 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


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:
def get_answer(input_list):
    so = input_list[0]
    s = so[0]
    s_index = 1 % len(so)
    s_len = len(so)
    t = input_list[1]
    ret = -1

    """
    """
    for i in range(1000):
        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


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:
def get_answer(input_list):
    s = input_list[0]
    so = s
    s_index = 0
    s_len = len(so)
    t = input_list[1]
    ret = -1

    """
    """
    for i in range(1000):
        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


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()
ci = []
maxs = len(s)
for c in t:
    ci.append(s.find(c) + 1)
tmp = l = ci[0]
if 0 in ci:
    l = -1
else:    
    for i in ci[1:]:
        if i <= tmp:
            l += (maxs - tmp) + i
        else:
            l += i - tmp
        tmp = i
print(l)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
cnt = 0
last = 0
b = [{} for i in range(len(s))]
b[-1][s[-1]] = len(s) - 1
for i in range(len(s)-1)[::-1]:
    b[i] = b[i + 1].copy()
    b[i][s[i]] = i

for i in t:
    if i in b[last]:
        last = b[last][i]
    else:
        if i not in s:
            print(-1)
            exit()
        last = b[0][i]
        cnt +=1
print(cnt*len(s) + last + 1)

Submission status: Wrong Answer

Solution code:
import sys
s = input()
t = input()
table = [[float("inf") for i in range(len(s))] for j in range(26)]

for i in range(len(s)):
  table[ord(s[i])-97][i] = 0
for i in range(26):
  for j in range(len(s)):
    table[i][len(s)-j-2] = min(table[i][len(s)-j-1] +1, table[i][len(s)-j-2])
  for j in range(len(s)):
    table[i][len(s)-j-2] = min(table[i][len(s)-j-1] +1, table[i][len(s)-j-2])

for letter in t:
  if s.find(letter) == -1:
    print(-1)
    sys.exit()
    
place = 0
ans = 1
for i in range(len(t)):
  letter = t[i]
  move = table[ord(t[i])-97][place]
  ans += move
  place = (place + move+1)%len(s)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
s = input()
t = input()
table = [[float("inf") for i in range(len(s))] for j in range(26)]

for i in range(len(s)):
  table[ord(s[i])-97][i] = 0
for i in range(26):
  for j in range(len(s)):
    table[i][len(s)-j-2] = min(table[i][len(s)-j-1] +1, table[i][len(s)-j-2])
  for j in range(len(s)):
    table[i][len(s)-j-2] = min(table[i][len(s)-j-1] +1, table[i][len(s)-j-2])

for letter in t:
  if s.find(letter) == -1:
    print(-1)
    sys.exit()
    
place = 0
ans = 1
for i in range(len(t)):
  letter = t[i]
  move = table[ord(t[i])-97][place]
  ans += move
  place = (place + move)%len(s)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(int(1e6))

s = input()
t = input()

len_s = len(s)
set_s = set(s)
set_t = set(t)

if len(set_t - set_s) != 0:
    print(-1)
    sys.exit()

n = {}
ss = s + s
ans = 0
for c in t:
    i = ans % len_s
    if not c in n:
        n[c] = [0] * len_s
    if n[c][i] == 0:
        n[c][i] = ss[i+1:].find(c) + 1
    ans += n[c][i]

print(ans+1)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(int(1e6))

s = input()
t = input()

len_s = len(s)
set_s = set(s)
set_t = set(t)

if len(set_t - set_s) != 0:
    print(-1)
    sys.exit()

n = {}
ss = s + s
for c in set_t:
    n_c = [0] * len_s
    for i in range(len_s):
        n_c[i] = ss[i+1:].find(c) + 1
    n[c] = n_c

ans = 0
for c in t:
    i = ans % len_s
    ans += n[c][i]

print(ans+1)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(int(1e6))

s = input()
t = input()

len_s = len(s)
set_s = set(s)
set_t = set(t)

if len(set_t - set_s) != 0:
    print(-1)
    sys.exit()

n = {}
ss = s + s
for c in set_t:
    n[c] = [0] * len_s
    for i in range(len_s):
        n[c][i] = ss[i+1:].find(c) + 1

ans = 0
for c in t:
    i = ans % len_s
    ans += n[c][i]

print(ans+1)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(int(1e6))

s = input()
t = input()

len_s = len(s)
set_s = set(s)

if len(set(t) - set_s) != 0:
    print(-1)
    sys.exit()

n = {}
ss = s + s
for c in set_s:
    n[c] = [0] * len_s
    for i in range(len_s):
        n[c][i] = ss[i+1:].find(c) + 1

ans = 0
for c in t:
    i = ans % len_s
    ans += n[c][i]

print(ans+1)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(int(1e6))

s = input()
t = input()

len_s = len(s)
n = [dict() for i in range(len_s)]
set_s = set(s)
ss = s + s
for i in range(len_s):
    for c in set_s:
        n[i][c] = ss[i+1:].find(c) + 1

ans = 0
for c in t:
    i = ans % len_s
    if c in n[i]:
        ans += n[i][c]
    else:
        ans = -2
        break

print(ans+1)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(int(1e6))

s = input()
t = input()

len_s = len(s)
n = [dict() for i in range(len_s)]
set_s = set(s)
ss = s + s
for i in range(len_s):
    n[i] = {}
    for c in set_s:
        n[i][c] = ss[i+1:].find(c) + 1

ans = 0
for c in t:
    i = ans % len_s
    if c in n[i]:
        ans += n[i][c]
    else:
        ans = -2
        break

print(ans+1)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(int(1e6))

s = input()
t = input()

len_s = len(s)
n = [0] * len_s
set_s = set(s)
ss = s + s
for i in range(len_s):
    n[i] = {}
    for c in set_s:
        n[i][c] = ss[i+1:].find(c) + 1

ans = 0
for c in t:
    i = ans % len_s
    if c in n[i]:
        ans += n[i][c]
    else:
        ans = -2
        break

print(ans+1)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(int(1e6))

s = input()
t = input()

len_s = len(s)
n = [0] * len_s
set_s = set(s)
ss = s + s
for i in range(len_s):
    n[i] = {}
    for c in set_s:
        n[i][c] = 0
        for j in range(i+1, len(ss)):
            n[i][c] += 1
            if c == ss[j]:
                break

ans = 0
for c in t:
    i = ans % len_s
    if c in n[i]:
        ans += n[i][c]
    else:
        ans = -2
        break

print(ans+1)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
i, c, d = 0, 0, 0

for l in range(len(t)):
  p, k = 0, 0
  while t[l] != s[k]:
    k += 1
    if k == len(s):
      i = -1
      break

if i != -1:
  for j in range(len(t)):
    g = 0
    while t[j] != s[d] and c < 10 ** 100 * len(s):
      d += 1
      g += 1
      if d == len(s):
        d = 0
        c += 1
    i += g
    
print(i+1)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(int(1e6))

s = input()
t = input()
n = {}
len_s = len(s)

for i in range(len_s):
    if s[i] in n:
        n[s[i]].append(i)
    else:
        n[s[i]] = [i]

ans = 0
for c in t:
    i = ans % len_s
    if c in s:
        for j in n[c]:
            if j > i:
                k = j
                break
        else:
            k = len_s + n[c][0]

        ans += k - i
    else:
        ans = -2
        break

print(ans+1)

Submission status: Wrong Answer

Solution code:
#import sys
#sys.setrecursionlimit(int(1e6))

s = input()
t = input()
n = {}
len_s = len(s)

for i in range(len_s):
    if s[i] in n:
        n[s[i]].append(i)
    else:
        n[s[i]] = [i]

ans = 0
for c in t:
    i = ans % len_s
    if c in s:
        for j in n[c]:
            if j > i:
                k = j
                break
        else:
            k = n[c][0]
        if k > i:
            ans += k - i
        else:
            ans += len_s - i + k
    else:
        ans = -1
        break

print(ans+1)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(int(1e6))

s = input()
t = input()
n = {}
len_s = len(s)

for i in range(len_s):
    if s[i] in n:
        n[s[i]].append(i)
    else:
        n[s[i]] = [i]

ans = 0
for c in t:
    i = ans % len_s
    if c in s:
        for j in n[c]:
            if j > i:
                k = j
                break
        else:
            k = n[c][0]
        if k > i:
            ans += k - i
        else:
            ans += len_s - i + k
    else:
        ans = -1
        break

print(ans+1)

Submission status: Wrong Answer

Solution code:
import sys

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

st = tuple(s)
tt = tuple(t)
sg = set(st)
tg = set(tt)

if not tg <= sg:
  print('-1')
  sys.exit()

cnt = 0
f = int(len(st)/2)

for i in tt:
  try:
    f = st.index(i,f+1,len(st))
  except ValueError:
    f = st.index(i,0,f+1)
    cnt += 1

print(len(st) * (cnt - 1) + f + 1)

Submission status: Wrong Answer

Solution code:
import sys

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

st = tuple(s)
tt = tuple(t)
sg = set(st)
tg = set(tt)

if not tg <= sg:
  print('-1')
  sys.exit()

cnt = 0
f = int(len(st)/2)
sta = st

for i in tt:
  try:
    f = sta.index(i,f+1,len(sta))
    a = True
  except ValueError:
    f = sta.index(i,0,f+1)
    cnt += 1
    sta += sta
    a = False
      
print(len(st) * (cnt - a) + f + 1)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
n = len(t)
x = []
for i in range(n):
  a = t[i]
  if a in s:
    num = s.find(a)+1
    x.append(num)
  else:
    print(-1)
    exit()

cnt = 0  
for j in range(1,n):
  if x[j-1] > x[j]:
    cnt+=1
ans = cnt * len(s) + x[-1]  
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
n = len(t)
x = []
for i in range(n):
  a = t[i]
  num = s.find(a)+1
  x.append(num)

cnt = 0  
for j in range(1,n):
  if x[j-1] > x[j]:
    cnt+=1
ans = cnt * len(s) + x[-1]  
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
c = 0
pre = 0
rep = 0
dict = {word:[] for word in 'abcdefghijklmnopqrstuvwxyz'}
for i, w in enumerate(s):
    dict[w].append(i+1)
for n in t:
    if len(dict[n]) < 1:
        c = -1
        break
    if pre > dict[n][0]:
        rep += 1
    pre = dict[n][0]
    if len(dict[n]) > 1:
        num = dict[n].pop(0)
        dict[n].append(num)
if c == -1:
    print(-1)
else:
    print(len(s) * rep + pre)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
c = 0
pre = 0
rep = 0
dict = {word:[] for word in 'abcdefghijklmnopqrstuvwxyz'}
for i, w in enumerate(s):
    dict[w].append(i+1)
for n in t:
    if len(dict[n]) < 1:
        c = -1
        break
    if pre > dict[n][0]:
        rep += 1
    pre = dict[n][0]
    if len(dict[n]) > 1:
        num = dict[n].pop(0)
        dict[n].append(num)
if c == -1:
    print(-1)
else:
    print(len(s) * rep + dict[t[-1]][-1])

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
alphabet = 'abcdefghijklmnopqrstuvwxyz'
c = 0
pre = 0
rep = 0
dict = {word:[] for word in alphabet}
for i, w in enumerate(s):
    dict[w].append(i+1)
for n in t:
    if len(dict[n]) < 1:
        c = -1
        break
    if pre > dict[n][0]:
        rep += 1
    pre = dict[n][0]
    if len(dict[n]) > 1:
        num = dict[n].pop(0)
        dict[n].append(num)
if c == -1:
    print(-1)
else:
    print(len(s) * rep + max(dict[t[-1]]))

Submission status: Wrong Answer

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

def func(S, T):

    merge_S = list(set(S))
    merge_T = list(set(T))
    for t in merge_T:
        if t not in merge_S:
            return -1

    S_2 = S+S
    len_s = len(S)

    ans = 0

    for t in T:
        ans += S_2[(ans+1)%len_s:].index(t) + 1

    return ans + 1

print(func(S,T))

Submission status: Wrong Answer

Solution code:

from collections import deque
import sys
 
def umeru(r, ima):
	# ima: 今使ったカウンタ（※sをnokori込みで何回繰り返したか。端数なし）
	# r: あと埋めなきゃいけない文字
	# nokori: 余分にある文字。nokoriとrはdisjoint←になるなら不要？

	ima = ima + 1
	count = 0
	nowmax = -1
	nowdic = {}
	nowindex = 0
	
	for x in r:
		if (x in nowdic):
			depth = nowdic[x] + 1
		else:
			depth = 0
		# print(x + " d" + str(depth))
		nowdic[x] = depth
		
		try:
			nowindex = dic[x][depth]
			# print(nowindex)
			if (nowmax < nowindex):
				nowmax = nowindex
				count += 1
			else:
				# 戻ってしまう→次が必要
				# print("back" + nowmax + " " + nowindex)
				break
		except:
			# indexがない→使えない
			# print("error " + x)
			break
	
	for i in range(count):
		r.popleft()
	
	return [r, ima, nowmax]

l = sys.stdin.readlines()
s = list(l[0].strip())
t = list(l[1].strip()) 
 
if (not (set(t) <= set(s))) :
	print(-1)
else:
	r = deque(t)
	ima = 0
	ans = 0;
	
	dic = {}
	for i in range(len(s)):
		if (s[i] in dic):
			dic[s[i]].append(i)
		else:
			dic[s[i]] = [i]
	# print(dic)
    
	while True:
		r, ima, nowmax = umeru(r, ima)
		# print(r)
		if len(r) == 0:
			break
	ans = len(s) * (ima - 1) + nowmax + 1
	print(ans)

Submission status: Wrong Answer

Solution code:
#E問題
import math
import bisect
from collections import defaultdict

S = list(str(input()))
T = list(str(input()))

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

flag = 0
x = -1
loop = 0
for t in T:
    tindl = index[t]
    if len(tindl) == 0:
        flag = 1
        break
    j = bisect.bisect_left(tindl,x)
    if j == len(tindl):
        loop+=1
        x = tindl[0]
    else:
        x = tindl[j]

if flag == 1:
    print(-1)
else:
    print(loop*len(S)+x+1)

Submission status: Wrong Answer

Solution code:
#E問題
import math
import bisect
from collections import defaultdict

S = list(str(input()))
T = list(str(input()))

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

flag = 0
x = -1
loop = 0
for t in T:
    tindl = index[t]
    if len(tindl) == 0:
        print(-1)
        flag = 1
        break
    j = bisect.bisect_left(tindl,x)
    if j == len(tindl):
        loop+=1
        x = tindl[0]
    else:
        x = tindl[j]

if flag == 1:
    print(-1)
else:
    print(loop*len(S)+x+1)

Submission status: Wrong Answer

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

def func(S, T):
    # Tの文字列のうち、一文字でもSにないなら条件を満たさない
    for t in T:
        if t not in S:
            return -1

    S_2 = S+S
    len_s = len(S)
    ans = 1
    merge_T = list(set(T))
    alp2num = {t: merge_T.index(t) for t in merge_T}
    dis_S = [[-1]*len(merge_T) for _ in range(len_s)]
    pt = 0

    for t in T:
        it = alp2num[t]
        if dis_S[pt][it] == -1:
            dis_S[pt][it] = S_2[pt+1:].index(t) + 1
        ans += dis_S[pt][it]
        pt = (pt + dis_S[pt][it]) % len_s

    return ans

print(func(S,T))

Submission status: Wrong Answer

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

def func(S, T):
    # Tの文字列のうち、一文字でもSにないなら条件を満たさない
    for t in T:
        if t not in S:
            return -1

    S_2 = S+S
    len_s = len(S)
    ans = 1
    merge_T = list(set(T))
    alp2num = {t: merge_T.index(t) for t in merge_T}
    dis_S = [[-1]*len(merge_T) for _ in range(len_s)]
    pt = 0

    for t in T:
        it = alp2num[t]
        if dis_S[pt][it] == -1:
            dis_S[pt][it] = S_2[pt:].index(t)
        ans += dis_S[pt][it]
        pt = (pt + dis_S[pt][it]) % len_s

    return ans

print(func(S,T))

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
s_1 = 0
count = 0
flag = False
for v in range(len(t)):
    for w in range(s_1 + 1, len(s)):
        if t[v] == s[w]:
            s_1 = w
            break
    else:
        for w in range(0, s_1 + 1):
            if t[v] == s[w]:
                s_1 = w
                count = count + 1
                break
        else:
            flag = True
    if flag:
        break
if flag:
    print(-1)
else:
    print(len(s)*count + s_1 + 1)

Submission status: Wrong Answer

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

def func(S, T):
    # Tの文字列のうち、一文字でもSにないなら条件を満たさない
    for t in T:
        if t not in S:
            return -1

    S_2 = S+S
    len_s = len(S)
    ans = 1
    pt = 0
    set_T = set(T)
    alp2num = {t: list(set_T).index(t) for t in list(set_T)}

    # 各文字の順方向の距離を計測する i.e. "abcde" -> aからeは4、eからbは2
    dis_T = [[-1]*len(set_T) for _ in range(len_s)]

    for t in T:
        it = alp2num[t]
        if dis_T[pt][it] == -1:
            dis_T[pt][it] = S_2[pt:].index(t)
        ans += dis_T[pt][it]
        pt = (pt + dis_T[pt][it]) % len_s

    return ans

print(func(S,T))

Submission status: Wrong Answer

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

def func(S, T):
    # Tの文字列のうち、一文字でもSにないなら条件を満たさない
    for t in T:
        if t not in S:
            return -1

    len_s = len(S)
    ans = 1
    len_s = len(S)
    index = 0

    # 各文字の順方向の距離を計測する i.e. "abcde" -> aからeは4、eからbは2
    dis_S = {i: {0:0} for i in range(len_s)}

    for t in T:
        if t not in dis_S[index].keys():
            dis_S[index][t] = (S+S)[index:].index(t)
        ans += dis_S[index][t]
        index = (index + dis_S[index][t]) % len_s

    return ans

print(func(S,T))

Submission status: Wrong Answer

Solution code:
import bisect
import collections

s = list(input())
t = list(input())

n = len(s)
m = len(t)

f = [False]*26
f2 = [False]*26

alpha = "abcdefghijklmnopqrstuvwxyz"
for i in range(n):
    f[alpha.index(s[i])] = True
for j in range(m):
    f2[alpha.index(t[j])] = True
for k in range(26):
    if not f[k] & f2[k]:
        print(-1)
        quit()
ans = 0
ind = s.index(t[0])
ans+= (ind+1)
for i in range(1,m):
    s = s[ind+1:] + s[:ind+1]
    ind = s.index(t[i])
    ans += (ind + 1)
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
len_s = len(s)
c = 0
pre = 0
over = 0
order = []
for i in t:
    if not i in s:
        c = -1
        break
    else:
        order.append(s.find(i)+1)
        if order[len(order)-1] < order[len(order)-2]:
            over += 1  
if c != -1:
    c = over * len_s + order.pop()
print(c)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from bisect import bisect_left
 
S = input().rstrip()
T = input().rstrip()
 
LS = len(S)
index = defaultdict(list)
 
for i, s in enumerate(S):
  index[s].append(i)

x = -1
loop = 0
for t in T:
  arr = index[t]  
  if len(arr) == 0:
    print(-1)
    exit()  
  i = bisect_left(arr, x)  
  if i == len(arr):
    loop += 1
    x = arr[0]
  else:
    x = arr[i]

print(loop*LS+x+1)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

s = input()
t = input()

oa = ord('a')

l = [[] for _ in range(26)]
for i,c in enumerate(s):
    l[ord(c) - oa].append(i)

x = -1
p = 0
for c in t:
    ll = l[ord(c)-oa]
    if len(ll)==0:
        print(-1)
        exit()

    y = bisect_left(ll, x)
    if y==len(ll):
        p += 1
        x = ll[0]
    else:
        x = ll[y]


print(p*len(s)+x+1)

Submission status: Wrong Answer

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

ds = {}  # ｓに含まれる文字それぞれの数
dsl = {}  # ｓの文字の位置
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 ds.keys():
        ds[t[i]] = 0

#print(ds, dsl)

ans = 1
now = 0
for i in range(len(t)):
    if ds[t[i]] == 0:  # ｓにｔを構成する文字がない場合
        print("-1")
        exit()
    elif ds[t[i]] == 1:
        if dsl[t[i]][0] > now:  # ｔの次の文字が現在地より右にある場合
            ans += (dsl[t[i]][0] - now)
        # elif dsl[t[i]][0] == now:  # ｔの次の文字が現在地にしかない場合（いる？）
        #     ans += len(s)
        else:  # ｔの次の文字が現在地より左にある場合
            ans += dsl[t[i]][0] + (len(s) - now)
        now = dsl[t[i]][0]
    else:
        index = bisect.bisect_right(dsl[t[i]], now)
        if index == len(dsl[t[i]]):
            index = -1
        if dsl[t[i]][index] > now:  # ｔの次の文字が現在地より右にある場合
            ans += (dsl[t[i]][index] - now)
            now = dsl[t[i]][index]
        else:  # ｔの次の文字が現在地より左にある場合
            ans += dsl[t[i]][0] + (len(s) - now)
            now = dsl[t[i]][0]

print(ans)

Submission status: Wrong Answer

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

ds = {}  # ｓに含まれる文字それぞれの数
dsl = {}  # ｓの文字の位置
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 ds.keys():
        ds[t[i]] = 0

#print(ds, dsl)

ans = 1
now = 0
for i in range(len(t)):
    if ds[t[i]] == 0:  # ｓにｔを構成する文字がない場合
        print("-1")
        exit()
    elif ds[t[i]] == 1:
        if dsl[t[i]][0] > now:  # ｔの次の文字が現在地より右にある場合
            ans += (dsl[t[i]][0] - now)
        elif dsl[t[i]][0] == now:  # ｔの次の文字が現在地にしかない場合（いる？）
            ans += len(s)
        else:  # ｔの次の文字が現在地より左にある場合
            ans += dsl[t[i]][0] + (len(s) - now)
        now = dsl[t[i]][0]
    else:
        index = bisect.bisect_right(dsl[t[i]], now)
        if index == len(dsl[t[i]]):
            index = -1
        if dsl[t[i]][index] > now:  # ｔの次の文字が現在地より右にある場合
            ans += (dsl[t[i]][index] - now)
            now = dsl[t[i]][index]
        elif dsl[t[i]][index] == now:  # ｔの次の文字が現在地にしかない場合（いる？）
            ans += len(s)
        else:  # ｔの次の文字が現在地より左にある場合
            ans += dsl[t[i]][0] + (len(s) - now)
            now = dsl[t[i]][0]

print(ans)

Submission status: Wrong Answer

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

ds = {}  # ｓに含まれる文字それぞれの数
dsl = {}  # ｓの文字の位置
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 ds.keys():
        ds[t[i]] = 0

#print(ds, dsl)

ans = 1
now = 0
for i in range(len(t)):
    if ds[t[i]] == 0:  # ｓにｔを構成する文字がない場合
        print("-1")
        exit()
    elif ds[t[i]] == 1:
        if dsl[t[i]][0] > now:  # ｔの次の文字が現在地より右にある場合
            ans += (dsl[t[i]][0] - now)
        elif dsl[t[i]][0] == now:  # ｔの次の文字が現在地にしかない場合（いる？）
            ans += 0#len(s)
        else:  # ｔの次の文字が現在地より左にある場合
            ans += dsl[t[i]][0] + (len(s) - now)
        now = dsl[t[i]][0]
    else:
        index = bisect.bisect_right(dsl[t[i]], now)
        if index == len(dsl[t[i]]):
            index = -1
        if dsl[t[i]][index] > now:  # ｔの次の文字が現在地より右にある場合
            ans += (dsl[t[i]][index] - now)
            now = dsl[t[i]][index]
        else:  # ｔの次の文字が現在地より左にある場合
            ans += dsl[t[i]][0] + (len(s) - now)
            now = dsl[t[i]][0]

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
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

#print(ds, dsl)

ans = 1
now = 0
for i in range(len(t)):
    if ds[t[i]] == 0:  # ｓにｔを構成する文字がない場合
        print("-1")
        exit()
    elif ds[t[i]] == 1:
        if dsl[t[i]][0] > now:  # ｔの次の文字が現在地より右にある場合
            ans += (dsl[t[i]][0] - now)
        elif dsl[t[i]][0] == now:  # ｔの次の文字が現在地にしかない場合（いる？）
            ans += len(s)
        else:  # ｔの次の文字が現在地より左にある場合
            ans += dsl[t[i]][0] + (len(s) - now)
        now = dsl[t[i]][0]
    else:
        index = bisect.bisect_right(dsl[t[i]], now)
        if index == len(dsl[t[i]]):
            index = -1
        if dsl[t[i]][index] > now:  # ｔの次の文字が現在地より右にある場合
            ans += (dsl[t[i]][index] - now)
            now = dsl[t[i]][index]
        else:  # ｔの次の文字が現在地より左にある場合
            index = 0
            ans += dsl[t[i]][0] + (len(s) - now)
            now = dsl[t[i]][0]

print(ans)

Submission status: Wrong Answer

Solution code:
#解説解答
S = input()
T = input()
ls = len(S)
lt = len(T)
s1 = [[] for _ in range(26)]
s2 = [[] for _ in range(26)]
for i in range(ls):
  s1[ord(S[i])-97].append(i)
  s2[ord(S[i])-97].append(i+ls)
s = [[] for _ in range(26)]
for k in range(26):
  s[k] = s1[k] + s2[k]
#print(s)
ans = 0
mans = 0
import bisect
for j in range(lt):
  now = ord(T[j])-97
  if s[now] == []:
    print(-1)
    break
  here = bisect.bisect_left(s[now],mans)
  ans = ans - mans + s[now][here]
  mans = s[now][here] % ls
  #print(now,here,s[now],s[now][here],ans,mans)
else:
  print(ans+1)

Submission status: Wrong Answer

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

def lower_bound(l,k):
    def isOk(i):
        return l[i]>=k

    ok = len(l)
    ng = -1
    while abs(ok-ng) > 1:
        mid = (ok+ng)//2

        if isOk(mid):
            ok = mid
        else:
            ng = mid
    return ok

s = input()
n = len(s)
t = input()
m = len(t)

d = {}
for c in t:
    if c in d:
        continue
    else:
        d[c] = []
    for i in range(2*n):
        if s[i%n]==c:
            d[c].append(i)
res = -1
for i in range(m):
    c = t[i]
    if len(d[c])==0:
        print(-1)
        exit()

    j = lower_bound(d[c],res%n)
    res += d[c][j] - res%n

print(res+1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
s = input()
n = len(s)
t = input()
m = len(t)

d = {}
for c in t:
    if c in d:
        continue
    else:
        d[c] = []
    for i in range(2*n):
        if s[i%n]==c:
            d[c].append(i)

res = 0
for i in range(m):
    c = t[i]
    if len(d[c])==0:
        print(-1)
        exit()

    j = 0
    while res%n>d[c][j]:
        j += 1
    res += d[c][j] - res%n

print(res+1)