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()
ans=0
order=0
judge=True
d={}
preorder=0
length=len(s)

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

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

Submission status: Wrong Answer

Solution code:
import bisect

GG = [[] for i in range(26)]
L = [chr(i) for i in range(97, 123)]

s = input()
t = input()

N = len(s)
M = len(t)

for i in range(N):
	x = ord(s[i]) - 97
	GG[x].append(i)

for i in range(26):
	GG[i] = sorted(GG[i])


jud = [0] * 26
for i in range(26):
	if GG[i] != []:
		jud[i] = 1
j = "Y"
for i in range(M):
	if jud[ord(t[i]) - 97] == 0:
		j = "N"
		break

if j == "N":
	print(-1)
else:
	v = 0
	ans = 0
	for i in range(M):
		x = ord(t[i]) - 97
		y = bisect.bisect_right(GG[x], v)
		if y == len(GG[x]):
			z = GG[x][0]
		else:
			z = GG[x][y]

		if z <= v:
			ans += N
		v = z

	print(ans + v + 1)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
t_long = len(t)
position = [0] * t_long
ans = 0

if set(t) <= set(s):
    position = [s.find(t[i]) for i in range(t_long)]
    for k in range(t_long - 1):
        if position[k + 1] < position[k]:
            ans += 1
    print(ans * len(s) + s.find(t[t_long - 1]) + 1)
else:
    print(-1)

Submission status: Wrong Answer

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

s = input().splitlines()[0]
t = input()

Slen = len(s)
Index = -1
Loop = 0
LUT = [[] for i in range(26)]
Cur = [0 for i in range(26)]
for i, c in enumerate(s):
    v = ord(c) - ord('a')
    LUT[v].append(i)

NotFound = False
for c in t:
    v = ord(c) - ord('a')
    l = len(LUT[v])
    if l == 0:
        NotFound = True
        break
    if LUT[v][Cur[v]] > Index:
        Index = LUT[v][Cur[v]]
        Cur[v] += 1
        if Cur[v] >= l:
            Cur[v] = 0
    else:    
        Cur = [0 for i in range(26)]
        Index = LUT[v][0]
        Loop += 1

if NotFound:
    print(-1)
else:
    print(Loop*Slen+Index+1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import bisect

def main():
    s = input()
    t = input()
    s_set = set(list(s))
    s_dict = {}
    s_len = len(s)
    t_list = list(t)
    t_set = set(t_list)
    c = 0
    ans = []
    ans_append = ans.append
    for i in s_set:
        s_dict[i] = []
    for i in range(s_len):
        s_dict[s[i]].append(i)
    if len(t_set) == len(t_set & s_set):
        for i in t_list:
            idxs = s_dict[i]
            if idxs[-1] <= c:
                c = idxs[0]
                ans_append(c)
            else:
                c = idxs[bisect.bisect(idxs,c)]
                ans_append(c)
        count = 0
        for i in range(len(ans)-1):
            if ans[i] >= ans[i+1]:
                count += 1
        print(s_len*count + ans[-1] + 1)
    else:
        print(-1)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#ABC138 E

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

s = input()
t = 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 i in range(m):
    f2[alpha.index(t[i])] = True
for i in range(26):
    if not f[i] and f2[i]:
        print(-1)
        quit()
x = []
inde = s.index(t[0])
x.append(inde+1)
if m > 1:
    x.append(n-inde-1)
c = [[] for i in range(26)]
for i in range(n):
    c[alpha.index(s[i])].append(i)

for i in range(1,m):
    preinde = s.index(t[i-1])
    for j in c[alpha.index(t[i])]:
        if j > preinde:
            inde = j
            break
    else:
        inde = s.index(t[i])
    if inde > preinde:
        x.pop()
        x.append(inde-preinde)
        if i != m-1:
            x.append(n-inde-1)
    else:
        x.append(inde+1)
        if i != m-1:
            x.append(n-inde-1)
print(sum(x))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict as dd
from bisect import bisect_left as bl
s = input()
t = input()
ind = dd(list)
res = 0
for i in range(len(s)):
  ind[s[i]].append(i)
#print(ind)
for i in range(len(t)):
  if len(ind[t[i]]) == 0:
    print(-1)
    exit(0)
  temp = bl(ind[t[i]], res % len(s))
  #print(temp, res, t[i])
  if res % len(s) == ind[t[i]][min(temp, len(ind[t[i]]) - 1)]:
    temp += 1
  if temp == len(ind[t[i]]):
    res += (len(s) - res % len(s)) + ind[t[i]][0]
  else:
    res += ind[t[i]][temp] - res % len(s)
print(res + 1)

Submission status: Wrong Answer

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

s = input().splitlines()[0]
t = input()

Slen = len(s)
Index = -1
Loop = 0
LUT = [[] for i in range(26)]
Cur = [0 for i in range(26)]
for i, c in enumerate(s):
    v = ord(c) - ord('a')
    LUT[v].append(i)

NotFound = False
for c in t:
    v = ord(c) - ord('a')
    l = len(LUT[v])
    if l == 0:
        NotFound = True
        break
    if LUT[v][Cur[v]] > Index:
        Index = LUT[v][Cur[v]]
        Cur[v] = Cur[v] + 1 if Cur[v] < l-1 else 0
    else:    
        Cur = [0 for i in range(26)]
        Index = LUT[v][0]
        Loop += 1

if NotFound:
    print(-1)
else:
    print(Loop*Slen+Index+1)

Submission status: Wrong Answer

Solution code:
import re
s = input()
t = input()
ans = 0
lens = len(s)
lent = len(t)
a = [[] for _ in range(lent + 1)]
a[0] = [0]

for i in range(lent):
  a[i+1] = [pos for pos, char in enumerate(s) if char == t[i]]
  
if([] in a):
  print(-1)
else:
  for i in range(lent):
    tmpmin = a[i][0]
    flag = 0
    for p in range(len(a[i+1])):      
      if(a[i+1][p] >= tmpmin):
        ans += a[i+1][p] - tmpmin
        flag = 1
        break
    if(flag == 0):
      ans += (lens + a[i+1][0] - tmpmin)
  print(ans+1)

Submission status: Wrong Answer

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
import fractions
from functools import reduce
def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)


s = list(input())

t = list(input())
flag = -1
on = [[-1 for j in range(len(s))] for i in range(26)]

al = [-1 for i in range(26)]
alll = [0 for i in range(26)]
for i in range(len(s)):
    k = ord(s[i]) - 97
    on[k][i] = i
    if al[k] == -1:
        al[k] = i

for i in range(26):
    flag = True
    for j in range(len(s)-2,-1,-1):
        if flag and on[i][j+1] != -1:
            flag = False
            for k in range(j+2,len(s)):
                on[i][k] = on[i][k-1]
        if on[i][j+1] != -1 and on[i][j] == -1:
            on[i][j] = on[i][j+1]

co = 0
su = 0
ppp = 0
t = cl.deque(t)
while t:
    k = t.popleft()
    kkk = al[ord(k)-97]
    ppp = kkk+1
    if kkk == -1:
        print(kkk)
        exit()
    while True:
        if len(t) == 0:
            break
        k = t.popleft()
        kk = on[ord(k)-97][kkk]
        if kkk >= kk:
            t.appendleft(k)
            break
        else:
            ppp = kk+1
            kkk = kk
    if len(t) != 0:
        co += 1

print(len(s)*co + ppp)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
s = str(input())
t = str(input())

next_index = 0
s_len = len(s)
loop_num = 0

for t_char in t:
    if t_char not in s:
        print(-1)
        sys.exit()

for t_char in t:
    next_index = s.find(t_char, next_index, -1) + 1
    if next_index == 0:
        loop_num += 1
        next_index = 0
        next_index = s.find(t_char, next_index, -1) + 1

    if next_index > s_len:
        loop_num += 1
        next_index = 0
        continue
        

print(loop_num * s_len + next_index)

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
ans = -1
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
import re

s = input()
t = input()
sett = set(t)
sets = set(s)
for e in sett:
  if e not in sets:
    print(-1)
    exit()

sumidx = 0
tmps = s
#tの各文字についてs内での出現場所をリストアップする
s_dict = {}
for e in sett:
  s_dict[e] = [m.start() for m in re.finditer(e, s)]
idx = 0
for e in t:
  l = s_dict[e]
  idx = bisect.bisect_left(l, idx)
  #print(idx)
  if idx == len(l):
    sumidx += len(s)
    idx = l[0]
  else:
    #print(l[idx])
    idx = l[idx]
sumidx += idx+1
print(sumidx)

Submission status: Wrong Answer

Solution code:
import re
s = input()
t = input()
ans = 0
lens = len(s)
lent = len(t)
a = [[] for _ in range(lent + 1)]
a[0] = [0]

for i in range(lent):
  a[i+1] = [pos for pos, char in enumerate(s) if char == t[i]]
  
if([] in a):
  print(-1)
else:
  for i in range(lent):
    tmpmin = a[i][0]
    flag = 0
    for p in range(len(a[i+1])):      
      if(a[i+1][p] >= tmpmin):
        ans += a[i+1][p] - tmpmin
        flag = 1
        break
    if(flag == 0):
      ans += (lens + a[i+1][0] - tmpmin)
  print(ans+1)

print(a)

Submission status: Wrong Answer

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

index = 0
s100 = ''
while index  < 100:
    s100 = s100 + s
    index = index + 1

t = list(t)
s100 = list(s100)
# print(t)
# print(s100)

index = 0
index2 = 0
while index < len(t):
    while index2 < len(s100):
        if(t[index] == s100[index2]):break
        if(index2 == len(s100)-1):
            print('-1')
            exit(0)
        index2 = index2 + 1
    index = index+1

print(index2+1)

Submission status: Wrong Answer

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

# 辞書を作る
dictionary = {}
beforeNum = 0
result = 0
sCount = 1
for i in range(len(t)):
    if t[i] in s and sCount <= 100000:
        result += beforeNum + s.index(t[i]) + 1
        if len(t) > i + 1 and t[i + 1] in s:
            if s.index(t[i + 1]) > s.index(t[i]):
                beforeNum = (s.index(t[i]) + 1) * -1
            else:
                beforeNum = len(s) - s.index(t[i]) - 1
                sCount += 1
    else:
        print(-1)
        exit(0)

print(result)

Submission status: Wrong Answer

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

x = 0
start = 0
for t in T:
  if t in S:
    if t in S[start:]:
      start = start+S[start:].index(t)+1
    else:
      x += 1
      start = S.index(t)
  else:
    x = -1
    break
if x == -1:
  print(-1)
else:
  print(x*len(S)+start)

Submission status: Wrong Answer

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

if not all([i in s for i in set(list(t))]):
  print(-1)
else:
  pos = [0] * len(t)
  ppos = 0
  for i in range(len(t)):
    for j in range(ppos + 1, len(s)*2):
      if t[i] == s[j % len(s)]:
        ppos = j % len(s)
        pos[i] = ppos
        break
#  print(pos)
  ans = 0
  pre = pos[0]
  for k in range(1, len(pos)):
#    print(pre, pos[k], pre > pos[k])
    if pre > pos[k]:
      ans += len(s)
    pre = pos[k]
  print(ans + pos[-1] + 1)

Submission status: Wrong Answer

Solution code:
import bisect

s = input()
t = input()
s_length = len(s)
t_length = len(t)

def char2int(char):
    return ord(char) - 97

def search_index(t_atom, last_index, d_list):
    t_atom_int = char2int(t_atom)
    d = d_list[t_atom_int]

    if d == []:
        return -1

    c = bisect.bisect_right(d, last_index)

    if c != len(d):
        return d[c]
    else:
        return d[0]

def solve():
    last_index = 0
    t_index = []
    loop_cost = 1

    d = [[] for _ in range(26)]
    for i in range(s_length):
        char = s[i]
        d[char2int(char)].append(i)

    for i in range(t_length):
        t_atom = t[i]
        t_atom_index = search_index(t_atom, last_index, d)
        if t_atom_index == -1:
            return -1
        else:
            t_index.append(t_atom_index)
            last_index = t_atom_index

    remainder_cost = t_index[-1] + 1

    for i in range(1, t_length):
        if t_index[i - 1] >= t_index[i]:
            loop_cost += 1

    return s_length * (loop_cost - 1) + remainder_cost

print(solve())

Submission status: Wrong Answer

Solution code:
s=input()
t=list(input())
ans=0
flag=0
u=[0 for i in range(len(t))]
for i,j in enumerate(t):
  if j in s:
    u[i]=s.index(j)+1
  else:
    flag=1
    ans=-1
if flag==0:
  ans+=u[0]
  for i in range(len(u)-1):
    if u[i]<u[i+1]:
      ans+=u[i+1]-u[i]

    else:
      ans+=len(s)-u[i]+u[i+1]

print(ans)

Submission status: Wrong Answer

Solution code:
import copy


def main():
    s = input()
    t = input()
    if not set(sorted(t)) <= set(sorted(s)):
        print(-1)
        return
    # print(set(sorted(t)) , set(sorted(s)))
    cnt_s = {}
    cnt_t = {}
    pos = {} #[({}) for x in range(len(s))]
    for i in range(len(s)):
        if not s[i] in cnt_s:
            cnt_s[s[i]] = 0
            pos[s[i]] = []
        cnt_s[s[i]] += 1
        pos[s[i]].append(i)
    for k in pos.keys():
        pos[k].reverse()
    org_pos = copy.deepcopy(pos)
    ret = 0
    tmp = 0
    # print(pos)
    for i in range(len(t)):
        c = t[i]
        flag = False
        # print(c, ret, tmp)
        while len(pos[c]) > 0:
            p = pos[c].pop()
            if tmp < p:
                tmp = p
                flag = True
                break
        if len(pos[c]) == 0 and flag == False:
            ret += len(s)
            pos = copy.deepcopy(org_pos)
            tmp = pos[c].pop()
    ret += tmp+1
    print(ret)
    return

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np
import math
import sys
import bisect
sys.setrecursionlimit(10000000)

def main():
    s = input()
    t = input()
    alphabets = dict.fromkeys(list("abcdefghijklmnopqrstuvwxyz"), None)
    # alphabets = alphabet.copy()
    for key_ in alphabets:
        lists = []
        for i in range(len(s)):
            if key_ == s[i]:
                lists.append(i)
        alphabets[key_] = lists
    # print(alphabets)
    now = -1
    count = 0
    for alpha in t:
        if len(alphabets[alpha]) == 0:
            print(-1)
            sys.exit()
        else:
            lists = alphabets[alpha]
            index = bisect.bisect_left(lists, now)
            if index >= len(lists):
                now = -1
                count += len(s)
                now = lists[0]
            else:
                now = lists[index]
            # print(now, count)
    print(count + now + 1)
            







if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
s = input()*2
t = input()
def f(t,s):
    return s.find(t)
l = 0
for i in t:
    if f(i,s[l:]) ==-1:
        print(-1)
        break
    l = l+f(i,s[l:])+1
else:
    print(l)

Submission status: Wrong Answer

Solution code:
s = list(input())
t = list(input())
res = 0
lens = len(s)
lim = lens*(10**100)

sl = [[] for i in range(26)]

[sl[ord(s[i])-97].append(i) for i in range(len(s))]

x = -1
for i in t:
    if sl[ord(i)-97] == []:
        res = -1
        break
    if max(sl[ord(i)-97]) < x:
        res += lens
        if res >= lim:
            res = -1
            break
        x = -1
        
    for ii in sl[ord(i)-97]:
        if ii > x:
            x = ii
            break

if res == -1:
    print(res)
else:
    print(res + x +1)

Submission status: Wrong Answer

Solution code:
import numpy as np

s = input()
t = input()

N = len(s)

tN = len(t)


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


# 次の特定の文字が何文字先にあるか
next_string = np.zeros((N, 26))


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 = int(ans % N)

print(int(ans + 1))

Submission status: Wrong Answer

Solution code:
import numpy as np

s = input()
t = input()

N = len(s)

tN = len(t)


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


# 次の特定の文字が何文字先にあるか
next_string = np.zeros((N, 26))


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])
    print(nextindex)
    if nextindex == -1:
        print(-1)
        exit()
    ans += nextindex
    index = int(ans % N)

print(int(ans + 1))

Submission status: Wrong Answer

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

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

repeat = 0
focus = 0
ans = 0
for i in range(len(t)):
    c = t[i]
    if c not in dic:
        ans = -1
        break
    if dic[c][-1] <= focus:
        repeat+=1
        focus = dic[c][0]
    else:
        l = 0
        r = len(dic[c])
        while r != l:
            mid = (r+l)//2
            if(dic[c][mid] <= focus):
                l = mid + 1
            else:
                r = mid
        focus = dic[c][l]
print(ans if ans == -1 else repeat*len(s)+focus+1)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_right

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

#s上で各アルファベットがどの位置にあるか
pos_in_s = {}
for i in range(len(s)):
  c = s[i]
  if c not in pos_in_s:
    pos_in_s[c] = []
  pos_in_s[c].append(i)

#t上でどの文字が何回使われているか
pos = 0
rep = 0
#count_on_t = {}
for c in t:
  #文字がなければ終わり
  if c not in pos_in_s:
    pos = -1
    break
    
  #bin search
  right = bisect_right(pos_in_s[c], pos)
  if right == len(pos_in_s[c]):
    pos = pos_in_s[c][0]
    rep += 1
  else:
    pos = pos = pos_in_s[c][right]
    
#print(rep, pos, rep * len(s) + pos + 1)
if pos == -1:
   print(-1)
else:
  print(rep * len(s) + pos + 1)

Submission status: Wrong Answer

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

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

repeat = 0
focus = 0
ans = 0
for i in range(len(t)):
    c = t[i]
    if c not in dic:
        ans = -1
        break
    if dic[c][-1] <= focus:
        repeat+=1
        focus = dic[c][0]
    else:
        l = 0
        r = len(dic[c])
        while r == l:
            mid = (r+l)//2
            if(dic[c][mid] < focus):
                l = mid + 1
            else:
                r = mid
        focus = dic[c][l]
print(repeat*len(s)+focus+1)

Submission status: Wrong Answer

Solution code:
import bisect

S = input()
T = input()

d = [[] for _ in range(27)]
alpha2num = lambda c: ord(c) - ord("a") + 1
ans = 0

for i, s in enumerate(S):
    d[alpha2num(s)].append(i)

idx = -1
for t in T:
    dl = d[alpha2num(t)]
    if not(dl):
        ans = -1
        break
    insert_idx = bisect.bisect_left(dl, idx)
    if insert_idx == len(dl):
        ans += (len(S) - idx + dl[0])
        idx = dl[0]
    else:
        ans += dl[insert_idx] - idx
        idx = dl[insert_idx]

print(ans)

Submission status: Wrong Answer

Solution code:
t = input()
s = input()
# t = "contest"
# s = "son"
list = []
re_i = 0
count = 0

for i in s:
    re_i = t[re_i:].find(i)
    if re_i  >=0:
        ab_i = t.find(i)
        list.append(ab_i)
    else:
        ab_i = t.find(i)
        if ab_i >=0:
            count += 1
            re_i = ab_i
            list.append(ab_i)

if len(list)!= len(s):
    print(-1)
else:
    print((count)*len(t)+list[-1]+1)

Submission status: Wrong Answer

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
import fractions
from functools import reduce
def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)


s = list(input())

t = list(input())
flag = -1
on = [[-1 for j in range(len(s))] for i in range(26)]

al = [-1 for i in range(26)]
alll = [0 for i in range(26)]
for i in range(len(s)):
    k = ord(s[i]) - 97
    on[k][i] = i
    if al[k] == -1:
        al[k] = i

for i in range(26):
    for j in range(len(s)-2,-1,-1):
        if on[i][j+1] != -1 and on[i][j] == -1:
            on[i][j] = on[i][j+1]

co = 0
su = 0
ppp = 0
t = cl.deque(t)
while t:
    k = t.popleft()
    kkk = al[ord(k)-97]
    ppp = kkk+1
    if kkk == -1:
        print(kkk)
        exit()
    while True:
        if len(t) == 0:
            break
        k = t.popleft()
        kk = on[ord(k)-97][kkk]
        if kkk >= kk:
            t.appendleft(k)
            break
        else:
            ppp = kk+1
            kkk = kk
    if len(t) != 0:
        co += 1

print(len(s)*co + ppp)

Submission status: Wrong Answer

Solution code:
def main():
    s = list(input())
    t = list(input())
    ns = len(s)
    nt = len(t)

    tbl = {}
    for i in range(ns):
        if s[i] not in tbl:
            tbl[s[i]] = [i]
        else:
            tbl[s[i]].append(i)

    cnt = 1
    cur = 0
    for i in range(nt):
        tt = t[i]
        if tt not in tbl:
            print(-1)
            return

        next_index = None
        for idx in tbl[tt]:
            if idx > cur:
                next_index = idx
                break
        if next_index is None:
            cur = tbl[tt][0]
            cnt += 1
        else:
            cur = next_index
    ans = ns * (cnt - 1) + cur + 1
    print(ans)


main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#ABC138 E

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

s = input()
t = 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 i in range(m):
    f2[alpha.index(t[i])] = True
for i in range(26):
    if not f[i] and f2[i]:
        print(-1)
        quit()
x = []
inde = s.index(t[0])
x.append(inde+1)
if m > 1:
    x.append(n-inde-1)
for i in range(1,m):
    inde = s.index(t[i])
    preinde = s[::-1].index(t[i])
    if inde > preinde:
        x.pop()
        x.append(inde-preinde)
        if i != m-1:
            x.append(n-inde-1)
    else:
        x.append(inde+1)
        if i != m-1:
            x.append(n-inde-1)
print(sum(x))

Submission status: Wrong Answer

Solution code:
import bisect

GG = [[] for i in range(26)]
L = [chr(i) for i in range(97, 123)]

s = input()
t = input()

N = len(s)
M = len(t)

for i in range(N):
	x = ord(s[i]) - 97
	GG[x].append(i)

for i in range(26):
	GG[i] = sorted(GG[i])


jud = [0] * 26
for i in range(26):
	if GG[i] != []:
		jud[i] = 1
j = "Y"
for i in range(M):
	if jud[ord(t[i]) - 97] == 0:
		j = "N"
		break

if j == "N":
	print(-1)
else:
	v = 0
	ans = 0
	for i in range(M):
		x = ord(t[i]) - 97
		y = bisect.bisect_left(GG[x], v)
		if y == len(GG[x]):
			z = GG[x][0]
		else:
			z = GG[x][y]
		if z <= v:
			ans += N
		v = z

	print(ans + v + 1)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
s = input().rstrip("\n")
t = input().rstrip("\n")
dic = {"a":[],"b":[],"c":[],"d":[],"e":[],"f":[],"g":[],"h":[],"i":[],"j":[],"k":[],"l":[],"m":[],"n":[],"o":[],"p":[],"q":[],"r":[],"s":[],"t":[],"u":[],"v":[],"w":[],"x":[],"y":[],"z":[]}
for i,c in enumerate(s):
    dic[c] += [i]
else:
    l = i+1
#print(dic)
cnt = 0
ind = -1
ans = 0
for c in t:
    d = dic[c]
    if d:
        if d[-1] > i:
    #    print("c=",c,"d=",d)
            for i in d: #iは各文字のindex 昇順
        #        print("i=",i)
                if ind < i: #もし現在位置<iなら
                    ind = i
        #            print("found, ind=",i)
                    break
        else:
    #        print("not found")
            cnt += 1
            ind = d[0]
    else:
        ans = -1
        break

print(cnt*l+ind+1 if ans > -1 else -1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from bisect import bisect_right

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


    str_dict = defaultdict(list)

    def my_index(l, x, default=-1):
        if x in l:
            return l.index(x)
        else:
            return default


    for i in set(t):
        ind = my_index(s, i)
        if ind == -1:
            return '-1'
        str_dict[i] += [ind]




    ans = 0
    for i in t:
        remain = ans % len(s)
        for each_ind in str_dict[i]:
            if each_ind + 1 > remain:
                ans += (each_ind + 1 - remain)
                break
        else:
            ans += len(s) - remain + str_dict[i][0]
    return ans


print(main())