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

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

idxs = []
for t in T:
    idx = []
    for i, s in enumerate(S):
        if t == s:
            idx.append(i)
    idxs.append(idx)

str_repeat_num = 0
now_idx = 0
for i, t in enumerate(T):
    l = list(filter(lambda x: x > now_idx, idxs[i]))
    if len(l) != 0:
        now_idx = l[0]
    else:
        str_repeat_num += 1
        now_idx = idxs[i][0]
print(len(S) * str_repeat_num + now_idx + 1)

Submission status: Wrong Answer

Solution code:
import bisect
S,T = input(),input()
lenS = len(S)
alp = [[]for i in range(26)]
for i in range(len(S)):
    alp[ord(S[i])-97].append(i)

if alp[ord(T[0])-97] == []:
    print(-1)
    exit()

l = [alp[ord(T[0])-97][0]]
for c in T[1:]:
    A = alp[ord(c)-97]
    if A == []:
        print(-1)
        exit()
    l.append(A[bisect.bisect_left(A,l[-1]+1)%len(A)])

ans = 0
for i in range(len(l)-1):
    if l[i] > l[i+1]:
        ans += 1
print(ans*lenS+l[-1]+1)

Submission status: Wrong Answer

Solution code:
from bisect import bisect 
s=input()
t=input()
n=len(s)
m=len(t)
ord_a=ord("a")
s_=[[] for i in range(26)]
for i in range(n):
  s_[ord(s[i])-ord_a].append(i)
ans=0
rep=0
for i in range(m):
  k=ord(t[i])-ord_a
  #print(s_[k])
  if (not s_[k]):
    print(-1)
    exit()
  if s_[k][-1]<=ans%n:
    rep+=1
    ans=rep*n+s_[k][0]
  else:
    pos=bisect(s_[k],ans%n)
    #print(pos)
    ans+=s_[k][pos]-ans%n
  #print(ans)
print(ans+1)

Submission status: Wrong Answer

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

next_map = sorted(zip(s*2,range(N*2)))
br = bisect.bisect_right
ans = 0
i = 0
for c in t:
    map_idx = br(next_map,(c,i))
    if map_idx == N*2:
        ans = -2
        break
    a,j = next_map[map_idx]
    if a != c :
        ans = -2
        break
    ans += j - i
    i = j%N
ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
s = input()
t = list(input())
sx = s * 10
for i, v in enumerate(sx):
    if t[0] == v:
        if len(t) == 1:
            print(i+1)
            exit()
        else:
            t = t[1:]
print(-1)

Submission status: Wrong Answer

Solution code:
def hersCode(L, n):
  if n >= L[-1]:
    return -1
  ok = len(L) - 1
  ng = -1
  while abs(ok - ng) > 1:
    mid = abs(ok + ng) // 2
    if L[mid] > n:
      ok = mid
    else:
      ng = mid
  return ok


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

R = {}

for i in t:
  if i not in R:
    R[i] = []
    flag = True
    for j in range(len(s)):
      if i == s[j]:
        R[i].append(j)
        flag = False
    if flag:
      print(-1)
      exit()

num = -1
cnt = 0

for i in t:
  t = hersCode(R[i], num)
  if t == -1:
    num = -1
    cnt += 1
    t = hersCode(R[i], num)
  num = R[i][0]
print(cnt * len(s) + num + 1)

Submission status: Wrong Answer

Solution code:
s=input()
t=input()
u=[0]*len(t)
for i in range(len(t)):
    u[i]=s.find(t[i])+1
ans=u[0]
for i in range(1,len(t)):
    if u[i]>u[i-1]:
        ans+=u[i]-u[i-1]
    else:
        ans+=len(s)-u[i-1]+u[i]
print(-1 if 0 in u else ans)

Submission status: Wrong Answer

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

import sys
from collections import deque, defaultdict
from copy import deepcopy

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

S = input()
T = input()
N = len(S)

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

d2 = deepcopy(d)
cur = -1
ans = 0
for i, t in enumerate(T):
    if len(d2[t]) == 0:
        print(-1)
        exit()
    idx = d2[t].popleft()
    if idx < cur:
        ans += N
        d2 = deepcopy(d)
    cur = idx
ans += cur+1
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
S,T = input(),input()
lenS = len(S)
alp = [[]for i in range(26)]
for i in range(len(S)):
    alp[ord(S[i])-97].append(i)
    
if alp[ord(T[0])-97] == []:
    print(-1)
    exit()

l = [alp[ord(T[0])-97][0]]
for c in T[1:]:
    A = alp[ord(c)-97]
    if A == []:
        print(-1)
        exit()
    l.append(A[bisect.bisect_left(A,l[-1])%len(A)])

ans = 0
for i in range(len(l)-1):
    if l[i] > l[i+1]:
        ans += 1
print(ans*lenS+l[-1]+1)

Submission status: Wrong Answer

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

print(ans)

Submission status: Wrong Answer

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

n = 0
ss = s
for c in t:
    if c in s:
        i = ss.find(c) + 1
        n += i
        ss = ss[i:] + s
    else:
        n = -1
print(n)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

alphabet = 'abcdefghijklmnopqrstuvwxyz'
cache = {c1: {c2: 0 for c2 in alphabet} for c1 in alphabet}

def search(origin, c, offset):
    offset = offset % len(origin)
    o = origin[offset:] + origin
    try:
        return o.index(c)
    except ValueError:
        return None

for c1 in alphabet:
    offset = search(s, c1, 0)
    if offset is None:
        for c2 in alphabet:
            cache[c1][c2] = None
        continue
    for c2 in alphabet:
        offset2 = search(s, c2, offset+1)
        if offset2 is None:
            cache[c1][c2] = None
            continue
        cache[c1][c2] = offset2 + 1

a = search(s, t[0], 0)
if a is None:
    print(-1)
    exit()

result = a + 1
for i in range(1, len(t)):
    if cache[t[i - 1]][t[i]] is None:
        print(-1)
        exit()
    result += cache[t[i-1]][t[i]]

print(result)

Submission status: Wrong Answer

Solution code:
import string
import bisect

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

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

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

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

Submission status: Wrong Answer

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


s = input().strip()
t = input().strip()
n = len(s)
m = len(t)
p = 0
q = 0
fold = 0
l = [[] for _ in range(26)]
lim = [0 for _ in range(26)] 
for i, x in enumerate(s):
	l[ord(x) - 97].append(i)
	lim[ord(x) - 97] += 1
r = [0 for _ in range(26)]
cur = -1
while (q < m):
	#print(r)
	x = t[q]
	ind = ord(x) - 97
	if l[ind] == []:
		print(-1)
		exit(0)
	else:
		if r[ind] >= lim[ind]:
			fold += 1
			r = [0 for _ in range(26)]
			cur = -1
		else:
			c = l[ind][r[ind]]
			if c > cur:
				cur = c
				q += 1
				r[ind] += 1
			else:
				fold += 1
				r = [0 for _ in range(26)]
				cur = -1
print(fold * n + cur + 1)

Submission status: Wrong Answer

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

def f(s,t):
    n = len(s)
    m = len(t)
    cnt = 0
    for i in range(m):
        c = 0
        while c<n and s[cnt%n]!=t[i]:
            cnt += 1
            c += 1
        if c==n:
            return -1

    return cnt+1

if __name__ == '__main__':
    s = input()
    t = input()
    print(f(s,t))

Submission status: Wrong Answer

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

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

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



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

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

Submission status: Wrong Answer

Solution code:
from collections import*
from bisect import*
s=input()
n=len(s)
t=input()
d=defaultdict(list)
for i,c in enumerate(s):
    d[c].append(i)
m=o=0
for c in t:
    if not d[c]:
        print(-1)
        exit()
    i=bisect(d[c],m)
    if len(d[c])==i:
        o+=1
        m=d[c][0]
    else:
        m=d[c][i]
print(o*n+m+1)

Submission status: Wrong Answer

Solution code:
import bisect


def solve(s, t):
    indexes = {}
    for i, c in enumerate(s):
        indexes.setdefault(c, []).append(i)
    index = 0
    loop_count = 0
    for c in t:
        if c not in indexes:
            return -1
        else:
            found_i = bisect.bisect_left(indexes[c], index)
            if found_i == len(indexes[c]):
                loop_count += 1
                index = indexes[c][0]
            else:
                index = indexes[c][found_i]
    return index + 1 + loop_count * len(s)


# assert (solve("contest", "son") == 10)
# assert (solve("contest", "programming") == -1)
# assert (solve("contest", "sentence") == 33)

if __name__ == "__main__":
    s = input()
    t = input()
    print(solve(s, t))

Submission status: Wrong Answer

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

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


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

Submission status: Wrong Answer

Solution code:
s = input() # しらべるやつ
t = input() # これがあるか
len_s = len(s)
len_t = len(t)
pre = 0
now = 0

tidx = 0
while now - pre <= len_s:
    if t[tidx] == s[now%len_s]:
        tidx += 1
        if tidx >= len_t:
            print(now+1)
            exit()
        pre = now
    else:
        now += 1
print(-1)

Submission status: Wrong Answer

Solution code:
from collections import Counter
S = input()
T = input()
cts = Counter(S)
ctt = Counter(T)
if set(ctt.keys()) - set(cts.keys()):
  print(-1)
  exit()

SS = S + S
previdx = {}
L = len(S)
for i, c in enumerate(S):
  if c not in previdx:
    previdx[c] = i

keys = list(previdx.keys())
nxts = [{} for _ in range(L)]
for i in range(L-1, -1, -1):
  c = S[i]
  nxt = nxts[i]
  for k in keys:
    nxt[k] = (previdx[k] - i) % L
  previdx[c] = i

ans = previdx[T[0]]
pos = ans
for c in T[1:]:
  d = nxts[pos][c]
  ans += d
  pos = (pos + d) % L
print(ans+1)

Submission status: Wrong Answer

Solution code:
s=list(input())
s_len=len(s)
t=list(input())
s_alphabets=[]
s_reference={}
ref_length=0
for i,alphabet in enumerate(s):
    if alphabet not in s_alphabets:
        s_alphabets.append(alphabet)
        s_reference[alphabet]=i
s_alphabets_set=set(s_alphabets)

prev_checked=0
ans=0
flag=True
for i in t:
    if i not in s_alphabets_set:
        flag=False
        print(-1)
        break
    else:
        checked=s_reference[i]
        if checked<prev_checked:
            ans+=s_len
        prev_checked=checked
if flag:
    print(ans+checked+1)

Submission status: Wrong Answer

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

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

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


flag = 1
ans = 0
index = -1
for ti in t:
    s_char = s_index[d[ti]]

    if len(s_char) == 0:
        flag = 0
        ans = -1
        break
    next = s_char[bisect_left(s_char, index) % len(s_char)]
    if next == index:
        next = s_char[(bisect_left(s_char, index)+1) % len(s_char)]

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

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from bisect import bisect_left,bisect_right
S = input()
T = input()

s_map = defaultdict(list)

for i, s in enumerate(S + S):
    s_map[s].append(i + 1)

ans = 0
current_index = 0
for t in T:
    if not s_map[t]:
        print(-1)
        exit()

    index = bisect_right(s_map[t],current_index)
    current_index = s_map[t][index]

    if current_index > len(S):
        ans += len(S)
        current_index = current_index % len(S)


print(ans + current_index)

Submission status: Wrong Answer

Solution code:
def hersCode(L, n):
  if n > L[-1]:
    return -1
  ok = len(L) - 1
  ng = -1
  while abs(ok - ng) > 1:
    mid = abs(ok + ng) // 2
    if L[mid] > n:
      ok = mid
    else:
      ng = mid
  return ok


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

R = {}

for i in t:
  if i not in R:
    R[i] = []
    flag = True
    for j in range(len(s)):
      if i == s[j]:
        R[i].append(j)
        flag = False
    if flag:
      print(-1)
      exit()

num = -1
cnt = 0

for i in t:
  t = hersCode(R[i], num)
  if t == -1:
    num = -1
    cnt += 1
    t = hersCode(R[i], num)
  num = R[i][0]
print(cnt * len(s) + num + 1)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

s = input()
t = input()

table = {}
for i in range(len(s)):
    if s[i] in table:
        table[s[i]].append(i)
    else:
        table[s[i]] = [i]

dup = 0
prev = -1
for i in range(len(t)):
    if t[i] not in table:
        print(-1)
        exit()

    ind_list = table[t[i]]
    if prev < ind_list[-1]:
        temp = bisect_left(ind_list, prev)
        prev = ind_list[temp]
        continue
    else:
        dup += 1
        prev = ind_list[0]

print(dup * len(s) + prev + 1)

Submission status: Wrong Answer

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

ans = []
for x in t:
  tmp = s.find(x)
  if tmp < 0:
    print('-1')
    sys.exit()
  else:
    ans.append(tmp)

cnt = 0
for i in range(len(ans)-1):
  if ans[i] >= ans[i+1]:
    cnt += len(s)

cnt += ans[len(ans)-1]+1

print(cnt)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

s = input()
t = input()
ls, lt = len(s), len(t)

exsist = [[] for _ in range(26)]
for i in range(ls) :
    exsist[ord(s[i])-97].append(i)

ret = 0
now = 0
for i in range(lt) :
    ot = ord(t[i])-97
    if len(exsist[ot]) == 0 :
        print('-1')
        break
    
    a = bisect_left(exsist[ot], now)
    if a == len(exsist[ot]) :
        ret += ls
        now = exsist[ot][bisect_left(exsist[ot], 0)]
    else :
        now = exsist[ot][a]
        
else :
    print(ret+now+1)

Submission status: Wrong Answer

Solution code:
s=list(input())
t=input()
ans=0
T=[0]
for i in range(len(t)):
    if t[i] not in s:
        print(-1)
        exit()
    elif t[i] in s[T[-1]+1:]:
        ind=s[T[-1]+1:].index(t[i])
        T.append(ind+T[-1]+1)
    else:
        ans+=1
        T.append(s.index(t[i]))

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

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

s = input()
t = input()
ls, lt = len(s), len(t)

exsist = [[] for _ in range(26)]
for i in range(ls) :
    exsist[ord(s[i])-97].append(i)

print(exsist)
ret = 0
now = 0
for i in range(lt) :
    ot = ord(t[i])-97
    if len(exsist[ot]) == 0 :
        print('-1')
        break
    
    a = bisect_left(exsist[ot], now)
    if a == len(exsist[ot]) :
        ret += ls
        now = exsist[ot][bisect_left(exsist[ot], 0)]
    else :
        now = exsist[ot][a]
        
else :
    print(ret+now+1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
s=input()
n=len(s)
t=input()
d=defaultdict(list)
for i,c in enumerate(s):
    d[c].append(i)
m=o=0
for c in t:
    if not d[c]:
        print(-1)
        exit()
    for i in d[c]:
        if i>m:
            m=i
            break
    else:
        o+=1
        m=d[c][0]
print(o*n+m+1)

Submission status: Wrong Answer

Solution code:
S = input()
T = input()
N = len(S)
def calc(s):
    ret = [0] * 26
    for ss in s:
        ret[ord(ss)-97] += 1
    return ret

X = calc(S)
Y = calc(T)
ma = 0
for i in range(26):
    ma = max(ma, -(-Y[i]//X[i])-1 if X[i] else (1<<100 if Y[i] else 0))
A = [i for i in range(26) if Y[i]]
if ma == 1<<100:
    print(-1)
else:
    NE = [[-1] * N for _ in range(26)]
    for a in A:
        b = -1
        for i in list(range(N)[::-1])*2:
            if NE[a][i] >= 0:
                break
            if b >= 0:
                NE[a][i] = b
            if ord(S[i]) - 97 == a:
                b = i
    ans = 1
    i = 0
    for t in T:
        ni = NE[ord(t)-97][i]
        ans += ni - i + (N if ni <= i else 0)
        i = ni
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from bisect import bisect_left

S = input()
T = input()

s_set = set(list(S))
t_set = set(list(T))

if not t_set <= s_set:
    print(-1)
    exit()


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


t_position = [-1] * len(T)
for i, t in enumerate(T):
    i_mod = i % len(s_position[t])
    ti = s_position[t][bisect_left(s_position[t], i_mod)]
    t_position[i] = ti


ans = 0
for i in range(len(T) - 1):
    if t_position[i] >= t_position[i + 1]:
        ans += len(S)

print(ans + t_position[-1] + 1)

Submission status: Wrong Answer

Solution code:
s=input()
t=input()
l=len(s)
lt=len(t)
ctr=0
a=[0]*26
for i in t:
  a[ord(i)-ord('a')]=1
for i in s:
  if a[ord(i)-ord('a')]==1:a[ord(i)-ord('a')]=0
if sum(a)>0:print(-1)
else:
  j=0
  for i in range(10*100):
    if t[j]==s[i%l]:
      j+=1
      if j==lt:break
    ctr+=1
  print(ctr+1)

Submission status: Wrong Answer

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


s = input().strip()
t = input().strip()
n = len(s)
m = len(t)
p = 0
q = 0
fold = 0
l = [[] for _ in range(26)]
lim = [0 for _ in range(26)] 
for i, x in enumerate(s):
	l[ord(x) - 97].append(i)
	lim[ord(x) - 97] += 1
r = [0 for _ in range(26)]
cur = -1
while (q < m):
	#print(r)
	x = t[q]
	ind = ord(x) - 97
	if l[ind] == []:
		print(-1)
		exit(0)
	else:
		if r[ind] >= lim[ind]:
			fold += 1
			r = [0 for _ in range(26)]
			cur = -1
		else:
			c = l[ind][r[ind]]
			if c > cur:
				cur = c
				q += 1
				r[ind] += 1
			else:
				fold += 1
				r = [0 for _ in range(26)]
				cur = -1
rem = 0
for i in range(26):
	cnt = r[i]
	if cnt > 0:
		rem = max(rem, l[i][cnt - 1])
print(fold * n + rem + 1)

Submission status: Wrong Answer

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

N = len(s)

tN = len(t)

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

# 次の特定の文字が何文字先にあるか
next_string = [[0 for _ in range(27)] for _ in range(len(s))]

def sarch_char(nowindex,c):
    if next_string[nowindex][alpha_index(c)] != 0:
        return next_string[nowindex][alpha_index(c)]
    else:
        for i in range(1, N):
            if s[(i + nowindex) % N] == c:
                next_string[nowindex][alpha_index(c)] = i
                return i
        next_string[nowindex][alpha_index(c)] = -1
        return -1


if not t[0] in s:
    print(-1)
    exit()

ans = s.find(t[0])
index = ans

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

print(ans + 1)

Submission status: Wrong Answer

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

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

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

ans = 0
index = -1
for ti in t:
    s_char = s_index[d[ti]]

    if len(s_char) == 0:
        ans = -1
        break
    next = s_char[bisect_left(s_char, index) % len(s_char)]
    if next == index:
        next = s_char[(bisect_left(s_char, index)+1) % len(s_char)]

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


print(ans)

Submission status: Wrong Answer

Solution code:
def ctoi(char):
  return ord(char)-97

s=input()
ls=len(s)
t=input()
lt=len(t)
pos=[[] for _ in range(26)]
for i in range(ls):
  val=ctoi(s[i])
  pos[val].append(i)
larr=[0]*26
for i in range(26):
  larr[i]=len(pos[i])
pos2=[]
cnt=[0]*26
flag=True
for i in range(lt):
  val=ctoi(t[i])
  if larr[val]==0:
    flag=False
    break
  if larr[val]<=cnt[val]:
    cnt[val]=0
  pos2.append(pos[val][cnt[val]])
  cnt[val]+=1
if flag==False:
  print(-1)
else:
  loop=0
  tmp=pos2[0]
  for i in range(1,lt):
    if pos2[i]<=tmp:
      loop+=1
    tmp=pos2[i]
  ans=loop*ls+(pos2[-1]+1)
  print(ans)

Submission status: Wrong Answer

Solution code:
s = str(input())
t = str(input())
eng = [[] for i in range(26)]
  # print(ord("a"))
num = len(s)
for i in range(num):
    eng[ord(s[i])-ord("a")].append(i+1)
  # print(eng)
if eng[ord(t[0])-ord("a")] == []:
    print("-1")
    exit()
ans = eng[ord(t[0])-ord("a")][0]
now = eng[ord(t[0])-ord("a")][0]
nu = len(t)
  #print(ans)
for i in range(1,nu):
    if eng[ord(t[i])-ord("a")] == []:
        print("-1")
        exit()
    if now > eng[ord(t[i])-ord("a")][-1]:
        ans += eng[ord(t[i])-ord("a")][0]+num-now
        now = eng[ord(t[i])-ord("a")][0]
    else:
        for hu in eng[ord(t[i])-ord("a")]:
            if now < hu:
                ans += (hu-now)
                now = hu
                break
print(ans)
  # print(eng)

Submission status: Wrong Answer

Solution code:
import sys
mod = 1000000007
def get_array(): return list(map(int, sys.stdin.readline().split()))
def get_ints(): return map(int, sys.stdin.readline().split())
def input(): return sys.stdin.readline()
def main():
    s = list(input())
    t = input()
    l = len(s) - 1
    m = len(t) - 1
    flag = True
    if t[0] not in s: flag = False
    if flag:
        j = s.index(t[0])
        x = 1
        while x < m:
            cnt = 0
            if s[j % l] == t[x]:
                x += 1
                cnt += 1
            j += 1
            if cnt == 0 and j > (x + 1) * l: 
                flag = False
                break
    if flag: print(j)
    else: print("-1")
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:

from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline
from math import factorial
import heapq, bisect
import math
import itertools

def main():
    s = list(input())
    t = list(input())

    if len(set(t) - set(s)) > 0:
        print(-1)
        sys.exit()

    s_count = Counter(s)
    t_count = Counter(t)

    abc_123_dic = defaultdict(int)
    for i in range(26):
        aaa = chr(97 + i)
        abc_123_dic[aaa] = i + 1
        abc_123_dic[i + 1] = aaa


    str_dic = [[] for i in range(27)]

    for i in range(len(s)):
        now_ind = abc_123_dic[s[i]]
        str_dic[now_ind].append(i + 1)


    ans_list = [0 for i in range(len(t))]
    pre_ind = 0
    for i in range(len(t)):
        now_str = t[i]
        now_ind = abc_123_dic[now_str]
        insert_ind = bisect.bisect_right(str_dic[now_ind], pre_ind)
        if insert_ind == len(str_dic[now_ind]):
            ans_list[i] = str_dic[now_ind][0]
        else:
            ans_list[i] = str_dic[now_ind][insert_ind]
        pre_ind = ans_list[i]

    pre_ind = 0
    ans = 0
    for i in range(len(t)):
        if pre_ind > ans_list[i]:
           ans += len(s)
        pre_ind = ans_list[i]
        # print(ans)
    ans += ans_list[len(t) - 1]
    print(ans)






if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from bisect import bisect_left
S = input()
T = input()

s_map = defaultdict(list)

for i, s in enumerate(S + S):
    s_map[s].append(i + 1)

ans = 0
current_index = 0
for t in T:
    if not s_map[t]:
        print(-1)
        exit()

    index = bisect_left(s_map[t],current_index)
    current_index = s_map[t][index]

    if current_index > len(S):
        ans += len(S)
        current_index = current_index % len(S)


print(ans + current_index)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
S = input()
T = input()
Ns = len(S)
S = S + S
ABC = [[] for _ in range(26)]
for i, s in enumerate(S):
    s = ord(s) - 97
    ABC[s].append(i)
ans = 0
l_s = 0
for t in T:
    t = ord(t) - 97
    abc = ABC[t]
    l = bisect_left(abc, l_s)
    if l == len(abc):
        print(-1)
        exit()
    l_s = abc[l]
    #print(l_s)
    if l_s >= Ns:
        ans += Ns
        l_s -= Ns
print(ans + l_s + 1)

Submission status: Wrong Answer

Solution code:
import bisect


s = input()
t = input()

memo = {}
alph = "abcdefghijklmnopqrstuvwxyz"

for i, char in enumerate(alph):
    memo[char] = i

li = [[] for i in range(26)]
for i, char in enumerate(s):
    li[memo[char]].append(i)

ans = 0
now_here = -1
# for i, char in enumerate(t):
i = 0
while i < len(t):
    char = t[i]
    if not li[memo[char]]:
        print(-1)
        exit()
    if now_here < li[memo[char]][0]:
        now_here = li[memo[char]][0]
        i += 1
    elif now_here <= li[memo[char]][-1]:
        now_here = bisect.bisect_left(li[memo[char]], now_here)
        i += 1
    else:
        ans += 1
        now_here = -1
print(ans*len(s) + now_here + 1)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_right

s = input()
t = input()

table = {}
for i in range(len(s)):
    if s[i] in table:
        table[s[i]].append(i)
    else:
        table[s[i]] = [i]

dup = 0
prev = 0
for i in range(len(t)):
    if t[i] not in table:
        print(-1)
        exit()

    ind_list = table[t[i]]
    if prev < ind_list[-1]:
        temp = bisect_right(ind_list, prev)
        prev = ind_list[temp]
        continue
    else:
        dup += 1
        prev = ind_list[0]

print(dup * len(s) + prev + 1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from bisect import bisect
s = input()
t = input()
d = defaultdict(list)
for i in range(len(s)):
    d[s[i]].append(i)
for i in range(len(s)):
    d[s[i]].append(d[s[i]][0]+len(s))

ans = 0
idx = 0
for i in range(len(t)):
    if d[t[i]] == []:
        ans = -1
        break
    idx = d[t[i]][bisect(d[t[i]],idx)]
    if idx >= len(s):
        ans += 1
        idx %= len(s)
if ans != -1:
    ans *= len(s)
    ans += idx+1
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
flag = 0
index_list = []
for a in t:
    try:
        index_list.append(s.index(a) + 1)
    except:
        print(-1)
        flag = 1
        break
if flag == 0:
    com = index_list[0]
    ans = 0
    for i in index_list:
        if i < com:
            ans += len(s)
        com = i
    ans += com
    print(ans)

Submission status: Wrong Answer

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

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

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

ans = 0
index = -1
for ti in t:
    s_char = s_index[d[ti]]

    if len(s_char) == 0:
        ans = -1
        break
    next = s_char[bisect_left(s_char, index) % len(s_char)]
    if next == index:
        next = s_char[(bisect_left(s_char, index)+1) % len(s_char)]
    ans += (next - index) % len(s)
    index = next


print(ans)

Submission status: Wrong Answer

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

def main():
    # N = int(input())
    # N, K = map(int, input().split())
    # a = list(map(int, input().split()))
    s = input().strip()
    t = input().strip()
    len_s = len(s)

    dic = {}
    for i in range(len_s):
        if s[i] in dic:
            dic[s[i]].append(i)
        else:
            dic[s[i]] = [i]
    # print(dic)
    ok = True
    now = 0
    ans = 1
    for i in range(len(t)):
        if t[i] not in dic:
            ok = False
            break
        else:
            index = bisect.bisect_left(dic[t[i]], now)
            # print(index)
            if index >= len(dic[t[i]]):# １週回ってからしかない
                # print("plus")
                ans += len_s
                now = dic[t[i]][0]
            else:# １週回るまでに見つかった
                now = dic[t[i]][index]
    if ok:
        print(ans+now)
    else:
        print(-1)

if __name__ == "__main__":
    main()