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

S_map = {}
len_S = len(S)
for si in range(len(S)):
    if S_map.get(S[si]) is None:
        S_map[S[si]] = [si+1 + len_S*i for i in range(len_S)]
    else:
        S_map[S[si]] += [si+1 + len_S*i for i in range(len_S)]

for s_key in S_map.keys():
    S_map[s_key].sort()

# print(S_map)
i = -1
for t in T:
    try:
        for si in S_map[t]:
            if si > i:
                i = si
                break
    except Exception:
        print(-1)
        exit()
else:
    print(si)

Submission status: Wrong Answer

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

import bisect
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 = 0
        # if dsl[t[i]][index] == now:
        #     index = bisect.bisect_right(dsl[t[i]],now+1)
        #     if index == len(dsl[t[i]]):
        #         index = 0            
        indexv = dsl[t[i]][index]
        if indexv > now:
            ans+= indexv - now
            now = indexv
        else:
            index = 0
            ans+= dsl[t[i]][0] + (len(s) - now)
            now = dsl[t[i]][0] 

print(ans)

Submission status: Wrong Answer

Solution code:
import collections, bisect
s = tuple(input())
t = tuple(input())
ls = len(s)
idx = collections.defaultdict(list)
dct = collections.defaultdict(list)
ln = -1
loop = 0
for i, v in enumerate(s):
    idx[v].append(i)
for i in t:
    t_idx = idx[i]
    if t_idx:
        bn = bisect.bisect_left(t_idx, ln)
        if bn >= len(t_idx):
            ln = t_idx[0]
            loop += 1
        else:
            ln = t_idx[bn]
    else:
        print(-1)
        exit()
print(ls * loop + ln + 1)

Submission status: Wrong Answer

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

S_map = {}
for si in range(len(S)):
    if S_map.get(S[si]) is None:
        S_map[S[si]] = [si+1]
    else:
        S_map[S[si]] += [si+1]


# print(S_map)
ans = 0
i = -1
len_S = len(S)
for t in T:
    try:
        for si in S_map[t]:
            if si > i:
                # print(t, si, ans+si)
                i = si
                break
        else:
            ans += len_S
            i = S_map[t][0]
    except Exception:
        print(-1)
        exit()
else:
    ans += si

print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
s += s
def next(s,ch,j):
    indexes = [i for i,x in enumerate(s) if (x==ch)and(i>j)]
    return min(indexes)

flag=True
ans=0
index=0
for ch in t:
    if ch in s:
        next_index = next(s,ch,index)
        ans += next_index-index
        index = next_index%(len(s)//2)
    else:
        flag = False

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

Submission status: Wrong Answer

Solution code:
import sys, math, itertools, bisect, copy, re

from collections import Counter, deque, defaultdict
from itertools import accumulate, permutations, combinations, takewhile, compress, cycle
from functools import reduce
from math import ceil, floor, log10, log2, factorial
from pprint import pprint

# 10倍速いらしい
input = sys.stdin.readline

INF = float('inf')
MOD = 10 ** 9 + 7
EPS = 10 ** -7
sys.setrecursionlimit(1000000)

S = input().rstrip()
T = input().rstrip()

sn = len(S)
M = [dict() for _ in range(sn)]
for i, s in enumerate(S):
    for j in range(sn):
        M[(i - j + sn) % sn][s] = min(M[(i - j + sn) % sn].get(s, INF), j)

ans = 0
idx = 0
for t in T:
    if M[idx].get(t, -1) == -1:
        print(-1)
        sys.exit()
    ans += M[idx][t]
    idx = (idx + M[idx][t]) % sn

print(ans + 1)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
l = len(s)
s2 = s*2
if set(s) <= set(t) :
    print(-1)
else :
    lowers = sorted(set(t))
    ldict = {v:i for i, v in enumerate(lowers)}
    dist = [[0 for j in range(l)] for i in range(len(lowers))]
    for j in range(l) :
        for i, b in enumerate(lowers) :
            for k, c in enumerate(s2[j+1:]) :
                if c == b :
                    dist[i][j] = 1+k
                    break
    ans = 0
    last = -1
    for c in t :
        diff = dist[ldict[c]][last]
        ans += diff
        last = (last+diff)%l
    print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
l = len(s)
s2 = s*2
if set(s) <= set(t) :
    print(-1)
else :
    lowers = sorted(set(t))
    ldict = {v:i for i, v in enumerate(lowers)}
    dist = [[0 for j in range(l)] for i in range(len(lowers))]
    for j in range(l) :
        for i, b in enumerate(lowers) :
            for k, c in enumerate(s2[j+1:]) :
                if c == b :
                    dist[i][j] = 1+k
                    break
    print(dist)
    ans = 0
    last = -1
    for c in t :
        diff = dist[ldict[c]][last]
        ans += diff
        last = (last+diff)%l
    print(ans)

Submission status: Wrong Answer

Solution code:
#coding: utf-8
s = str(input())
t = str(input())
ans = 0
ans1 = -1
# s.sort(reversed=True)
# t.sort(reversed=True)
flg = 0

for i in range(len(t)):
	for j in range(ans1+1,len(s)):
		if t[i] == s[j]:
			flg = 1
			# if j > ans1:
			# print('jが大きい'+str(j))
			# ans = ans - ans1 + j + 1
			ans1 = j
			# else:
			# 	print('jが小さい'+str(j))
			# 	ans = ans + len(s)
			# 	ans1 = j
			break
		# elif t[i] < s[j]:
		# 	flg = 0
		# 	break
	if flg == 0:
		for j in range(0,ans1):
			if t[i] == s[j]:
				flg = 1
				# print('jが小さい'+str(j))
				ans = ans + len(s)
				ans1 = j
				break
				
	if flg == 0:
		ans = -1
		break
	else:
		# ans = ans + ans1
		flg = 0
		# ans1 = 0

if ans == -1:
	print(-1)
else:
	print(ans+ans1+1)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
a=input()
b=input()
c=[[]for i in range(26)]
for i in range(len(a)):
  c[ord(a[i])-97].append(i)

m=0
ans=0
roop=0
for i in range(len(b)):
  s=ord(b[i])-97
  if c[s]==[]:
    print(-1)
    exit()
  else:
    p=bisect_left(c[s],m+1)
    if p<len(c[s]):
      m=c[s][p]
    else:
      roop+=1
      m=c[s][0]

print(roop*len(a)+m+1)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left
a=input()
b=input()
c=[[]for i in range(26)]
for i in range(len(a)):
  c[ord(a[i])-97].append(i)

m=0
ans=0
for i in range(len(b)):
  s=ord(b[i])-97
  if c[s]==[]:
    print(-1)
    exit()
  else:
    p=bisect_left(c[s],m+1)
    if p<len(c[s]):
      ans+=c[s][p]-m
      m=c[s][p]
    else:
      ans+=c[s][0]+len(a)-m
      m=c[s][0]

print(ans+1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from bisect import bisect_left

S = input()
dS = defaultdict(list)
T = input()
for i in range(len(S)):
    dS[S[i]].append(i+1)
if not dS[T[0]]:
    print(-1)
    exit()
now = min(dS[T[0]])
ans = now
for i in range(1, len(T)):
    t = T[i]
    if not dS[t]:
        print(-1)
        exit()
    b = bisect_left(dS[t], now)
    if b == len(dS[t]):
        ans += len(S) - now
        now = min(dS[t])
        ans += now
    else:
        ans += dS[t][b] - now
        now = dS[t][b]
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin

s = stdin.readline().rstrip()
t = stdin.readline().rstrip()


alpos = []

for i in range(26):
    alpos.append([])

for i in range(len(s)):
   alpos[ord(s[i])-ord('a')].append(i)

pos = 0
mpos= 0
for i in range(len(t)):
    tn = ord(t[i])-ord('a')
    if ( len(alpos[tn]) == 0 ):
        pos = -2
        break
    minsteps = len(s)
    for j in range(len(alpos[tn])):
        if ( mpos <= alpos[tn][j] ):
            minsteps = min(minsteps, alpos[tn][j]-mpos)
        else:
            minsteps = min(minsteps,  len(s)-mpos+alpos[tn][j])
    pos += minsteps
    mpos = pos%len(s)
        
print(pos+1)

Submission status: Wrong Answer

Solution code:
import bisect 

s = input()
t = input()

lst = [[] for _ in range(30)]
ch = 'abcdefghijklmnopqrstuvwxyz'
for k, i in enumerate(s+s):
    lst[ch.find(i)].append(k)

len_s = len(s)

count = 0
index = -1
for i in t:
    if lst[ch.find(i)] == []:
        print(-1)
        break
    next_index = lst[ch.find(i)][bisect.bisect_left(lst[ch.find(i)], index)]
    count += next_index - index
    if next_index >= len_s:
        next_index -= len_s
    index = next_index
else:
    print(count)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
a = 0
b = 0
j = 0
for i in t:
    if  s.count(i) == 0:
        print('-1')
        break
    else:
        a += 1
if a >= len(t):
    for i in t:
        j += (s[b:]+s).index(i)+1
        b = j%len(s)
        print(b)
        print(s[b:]+s)
    print(j)

Submission status: Wrong Answer

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

# print(s, t)

set_t = set(t)
# print(set_t)

for v in set_t:
    if v not in s:
        print(-1)
        exit()

last = ''
last_index = -1
count = 1
tail = 0
for v in t:
    index = s[last_index + 1:].find(v)
    # print(s, v, last_index, index)
    if index == -1:
        index = s.find(v)
        count += 1
    tail = last_index + 1 + index
    last_index = index

# print('count={}'.format(count))
ans = (count - 1)*len(s)
# print(ans)
ans += tail
print(ans + 1)

Submission status: Wrong Answer

Solution code:
#coding: utf-8
s = str(input())
t = str(input())
ans = 0
ans1 = 0
# s.sort(reversed=True)
# t.sort(reversed=True)
flg = 0

for i in range(len(t)):
	for j in range(ans1,len(s)):
		if t[i] == s[j]:
			flg = 1
			# if j > ans1:
			# print('jが大きい'+str(j))
			# ans = ans - ans1 + j + 1
			ans1 = j
			# else:
			# 	print('jが小さい'+str(j))
			# 	ans = ans + len(s)
			# 	ans1 = j
			break
		# elif t[i] < s[j]:
		# 	flg = 0
		# 	break
	if flg == 0:
		for j in range(0,ans1):
			if t[i] == s[j]:
				flg = 1
				# print('jが小さい'+str(j))
				ans = ans + len(s)
				ans1 = j
				
	if flg == 0:
		ans = -1
		break
	else:
		# ans = ans + ans1
		flg = 0
		# ans1 = 0

if ans == -1:
	print(-1)
else:
	print(ans+ans1+1)

Submission status: Wrong Answer

Solution code:
a=input()
b=input()
c=[[]for i in range(26)]
for i in range(len(a)):
  c[ord(a[i])-97].append(i)

m=0
ans=0
for i in range(len(b)):
  s=ord(b[i])-97
  if c[s]==[]:
    print(-1)
    exit()
  else:
    p=0
    for j,k in enumerate(c[s]):
      if m<k:
        p=j
        break
    if m<=c[s][p]:
      ans+=c[s][p]-m
      m=c[s][p]
    else:
      ans+=c[s][p]+len(a)-m
      m=c[s][p]

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):
    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
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 t:
        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:
from bisect import bisect
import sys
s = input()
t = input()
l = {}
for c in set(t):
    cl = [s.find(c)]
    if cl[0] == -1:
        print("-1")
        sys.exit()
    while True:
        i = s.find(c, cl[-1] + 1)
        if (i == -1):
            break
        cl.append(i)
    l[c] = cl
result = 0
for c in t:
    lens = len(s)
    ofs = result % lens
    ref = bisect(l[c], ofs)
    if ref < len(l[c]):
        result += l[c][ref] - ofs
    else:
        result += lens - ofs + l[c][0]
print(result+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())
ls = len(s)
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 + ls)
            if p > last_no:
                last_no = p
                break
    else:
        print(-1)
        exit()
print(last_no + 1)

Submission status: Wrong Answer

Solution code:
def get_index(s, c):
    index_list = []
    index = 0
    for i in range(s.count(c)):
        newindex = s.find(c, index, len(s))
        index_list.append(newindex)
        index = newindex+1
    return index_list

def main():
    s = input()
    t = input()
    ans = 1
    index_list = {}
    next_index = {}
    for c in t:
        if not c in s:
            print(-1)
            return
        index_list[c] = get_index(s+s, c)
        next_index[c] = []
        for i in range(len(s)):
            if i > index_list[c][0]:
                index_list[c] = index_list[c][1:]
            next_index[c].append(index_list[c][0] - i)
    index = 0
    limit = 1
    for c in t:
        ans += next_index[c][index] 
        index += next_index[c][index] 
        if index >= len(s):
            index -= len(s)
            limit += 1
            if limit > 100:
                print(-1)
                return
    print(ans)
main()

Submission status: Wrong Answer

Solution code:
#

import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop
import sys
sys.setrecursionlimit(1000000000)

def solve(positions, s, t):
    # print('{}, s = {}, t = {}'.format(positions, s, t))
    ans = 0
    while len(t) > 0:
        if len(positions[t[0]]) == 0:
            return -1

        i = 0
        j = 0
        while i < len(s) and j < len(t):
            candidates = positions[t[j]]
            pos = bisect.bisect_right(candidates, i)
            if pos == len(candidates):
                break

            i = candidates[pos] + 1
            j += 1

        if j == len(t):
            ans += i
            break
        else:
            ans += len(s)
            t = t[j:]

    return ans

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

    positions = defaultdict(list)
    for i in range(len(s)):
        positions[s[i]].append(i)

    print(solve(positions, s, t))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
S = input()
T = input()
D = {}
for i in T:
    D[i] = 1
for i in S:
    if i not in D:
        print(-1)
        exit()

N = len(S)
nex = [[N] * 26 for i in range(N + 1)]
for i in range(N - 1, -1, -1):
    for j in range(26):
        nex[i][j] = nex[i + 1][j]
    nex[i][ord(S[i]) - ord('a')] = i


M = len(T)
i = 0
num = 0
j = 0
while j < M:
    if nex[i][ord(T[j]) - ord('a')] < N:
        num += nex[i][ord(T[j]) - ord('a')] - i + 1
        i = nex[i][ord(T[j]) - ord('a')] + 1
        j += 1
    else:
        num += N - i
        i = 0

print(num)

Submission status: Wrong Answer

Solution code:
# E WA
# -*- 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:
s = input()
t = input()
a = 0
b = 0
j = 0
for i in t:
    if  s.count(i) == 0:
        print('-1')
        break
    else:
        a += 1
if a >= len(t):
    for i in t:
        j += (s[b:]+s).index(i)
        b = j%len(s)
    print(j+1)

Submission status: Wrong Answer

Solution code:
def get_index(s, c):
    index_list = []
    index = 0
    for i in range(s.count(c)):
        newindex = s.find(c, index, len(s))
        index_list.append(newindex)
        index = newindex+1
    return index_list

def main():
    s = input()
    t = input()
    ans = 0
    index_list = {}
    next_index = {}
    for c in t:
        if not c in s:
            print(-1)
            return
        index_list[c] = get_index(s+s, c)
        next_index[c] = []
        for i in range(len(s)):
            if i > index_list[c][0]:
                index_list[c] = index_list[c][1:]
            next_index[c].append(index_list[c][0] - i)
    index = 0
    for c in t:
        ans += next_index[c][index] 
        index += next_index[c][index] 
        if index >= len(s):
            index -= len(s)
    print(ans+1)
main()

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 on[i][j+1] != -1 and on[i][j] == -1:
            on[i][j] = on[i][j+1]
print(-1)
exit()
co = 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:
#

import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop


def solve(positions, s, t):

    if not set(t) <= set(s):
        return -1

    loop = 0
    prev = -1

    for tchar in t:
        candidates = positions[tchar]
        pos = bisect.bisect_left(candidates, prev)

        if pos == len(candidates):
            loop += 1
            pos = 0
        prev = candidates[pos]

    return loop * len(s) + prev + 1


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

    positions = defaultdict(list)
    for i in range(len(s)):
        positions[s[i]].append(i)

    print(solve(positions, s, t))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

lst = [[] for _ in range(30)]
ch = 'abcdefghijklmnopqrstuvwxyz'
for k, i in enumerate(s):
    lst[ch.find(i)].append(k)

len_s = len(s)

count = 0
index = -1
for i in t:
    min_a = 10**10
    if lst[ch.find(i)] == []:
        print(-1)
        break
    for j in lst[ch.find(i)]:
        if index >= j:
            a = (len_s - index) + j
            if min_a > a:
                index = j
                min_a = a
        else:
            a = j - index
            if min_a > a:
                index = j
                min_a = a
    count += min_a
else:
    print(count)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
ss = s*2

a_to_z = [chr(i) for i in range(97, 97+26)]
analize_ss = dict()
for c in a_to_z:
    index = -1
    is_first = True
    while True:
        index = ss.find(c, index + 1)
        if is_first and index != -1:
            analize_ss[c] = [index]
            is_first = False
        elif index != -1:
            analize_ss[c].append(index)
        else:
            break

# print(analize_ss)

len_ss = len(ss)
len_ss_count = 0
index = -1
for c in t:
    if not c in analize_ss.keys():
        print(-1)
        exit()
    ana = [x for x in analize_ss[c] if x > index]
    if ana:
        index = ana[0]
    else:
        len_ss_count += 1
        index = -1

print(len_ss * len_ss_count + index + 1)

Submission status: Wrong Answer

Solution code:
import bisect
import sys

sys.setrecursionlimit(1000000)

s=input()
t=input()
dic={}
for v in s:
    dic[v]=True

for u in t:
    if not u in dic:
        print(-1)
        exit()

nex={}
for i, v in enumerate(s+s):
    if not v in nex:
        nex[v]=[i]
    else:
        nex[v].append(i)
#print(s)
#print(t)
#print(nex)
sl=len(s)
tl=len(t)
def func(st, tn, su):
    #print(st, tn, su)
    if tn == tl:
        return su 
    ind=bisect.bisect_left(nex[t[tn]] ,st)
    nm=(nex[t[tn]][ind]-st)
    return func((st+nm)%sl, tn+1, su+nm)


res=func(-1, 0, 0)

print(res)

Submission status: Wrong Answer

Solution code:
#coding: utf-8
s = str(input())
t = str(input())
ans = 0
ans1 = 0
# s.sort(reversed=True)
# t.sort(reversed=True)
flg = 0

for i in range(len(t)):
	for j in range(len(s)):
		if t[i] == s[j]:
			flg = 1
			if j+1 > ans1:
				# print('jが大きい'+str(j))
				# ans = ans - ans1 + j + 1
				ans1 = j + 1
			else:
				# print('jが小さい'+str(j))
				ans = ans + len(s)
				ans1 = j + 1
			break
		# elif t[i] < s[j]:
		# 	flg = 0
		# 	break
	if flg == 0:
		ans = -1
		break
	else:
		# ans = ans + ans1
		flg = 0
		# ans1 = 0

if ans == -1:
	print(-1)
else:
	print(ans+ans1)

Submission status: Wrong Answer

Solution code:
s=input() #数値入力 「N」だけの入力のとき
t=input()  #数値入力 「N」だけの入力のとき
sa=list(s)
ta=list(t)
ans2=0
x=0
y=0
ans= [[] for i in range(26)]
for i in range(len(s)):
    ans[ord(sa[i])-96-1].append(i)
for i in range(len(t)):
    if len(ans[ord(ta[i])-97])==0:
        ans2=-1
        break;
    else:
        x_old=x
        for j in range(len(ans[ord(ta[i])-97])):
            if ans[ord(ta[i])-97][j]>x:
                x=ans[ord(ta[i])-97][j]
                break
        if x_old==x:
            x=0
            y+=1
            for j in range(len(ans[ord(ta[i])-97])):
                if ans[ord(ta[i])-97][j]>x:
                    x=ans[ord(ta[i])-97][j]
                    break
if ans2==-1:
    print(-1)
else:
    print(y*len(s)+x+1)

Submission status: Wrong Answer

Solution code:
abc = ["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"]
s = input()
x = [[0 for i in range(26)] for j in range(len(s))]
for k in range(2):
    for i in range(len(s)-1,-1,-1):
        if i == 0:
            ib = len(s)-1
        else:
            ib = i-1
        for j in range(26):
            if j == ord(s[i])-97:
                x[ib][j] = i
            else:
                x[ib][j] = x[i][j]
t = input()
i = len(s)-1
count = 0
for j in range(len(t)):
    nxt = x[i][ord(t[j])-97]
    if nxt == 0:
        print(-1)
        exit()
    elif nxt <= i:
        count += 1
        i = nxt
    else:
        i = nxt
print(len(s)*(count-1)+i+1)

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 c:
    i.sort()

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:
# -*- 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) + 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
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 = {}
count = 1
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):
    count += 1
    if count > 10 ** 100:
      print(-1)
      exit()
    sumidx += len(s)
    idx = l[0]
  else:
    #print(l[idx])
    idx = l[idx]
sumidx += idx+1
print(sumidx)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
s=input() #数値入力 「N」だけの入力のとき
t=input()  #数値入力 「N」だけの入力のとき
sa=list(s)
ta=list(t)
ans2=0
x=0
y=0
ans= [[] for i in range(26)]
for i in range(len(s)):
    ans[ord(sa[i])-96-1].append(i)
print(ans)
for i in range(len(t)):
    if len(ans[ord(ta[i])-97])==0:
        ans2=-1
        break;
    else:
        x_old=x
        for j in range(len(ans[ord(ta[i])-97])):
            if ans[ord(ta[i])-97][j]>x:
                x=ans[ord(ta[i])-97][j]
                break
        if x_old==x:
            x=0
            y+=1
            for j in range(len(ans[ord(ta[i])-97])):
                if ans[ord(ta[i])-97][j]>x:
                    x=ans[ord(ta[i])-97][j]
                    break
    print(ta[i],x,y)
if ans2==-1:
    print(-1)
else:
    print(y*len(s)+x+1)