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

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):
                r = mid
            else:
                l = mid + 1
        focus = dic[c][l]
print(repeat*len(s)+focus+1)

Submission status: Wrong Answer

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

ss = set(s)
ts = set(t)
if not ts.issubset(ss):
    print(-1)
else:
    # mapping
    sd = {}
    for i in range(len(s)):
        if s[i] in sd:
            sd[s[i]].append(i)
        else:
            sd[s[i]] = [i]

    c = 0
    lt = len(t)
    i = 0
    co = {}
    p = -1
    while True:
        if t[i] in sd:
            k = 0
            if not t[i] in co:
                if p < sd[t[i]][0]:
                    k = sd[t[i]][0]
                    co[t[i]] = 1
                    p = k
                else:
                    c = c + 1
                    p = -1
                    co = {}
                    continue
            elif len(sd[t[i]]) > co[t[i]]:
                if p < sd[t[i]][co[t[i]] - 1]:
                    k = sd[t[i]][co[t[i]] - 1]
                    co[t[i]] += 1
                    p = k
                else:
                    c = c + 1
                    p = -1
                    co = {}
                    continue
            else:
                c = c + 1
                p = -1
                co.clear()
                continue
            if i == lt - 1:
                print(c * len(s) + k + 1)
                break
            i = i + 1
        else:
            c = c + 1
            p = -1
            co.clear()

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)
    else:
      x += 1
      start = S.index(t)
  else:
    x = -1
    break
if x == -1:
  print(-1)
else:
  print(x*len(S)+start+1)

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:
            list.append(ab_i)
            count += 1
if len(list)< len(s):
    print(-1)
else:
    print((count-1)*len(t)+list[-1]+1)

Submission status: Wrong Answer

Solution code:
import bisect

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

s_dic = {}
for i,s in enumerate(S):
    if s not in s_dic.keys():
        s_dic[s] = [i]
    else:
        s_dic[s].append(i)
        
def main():
    pre, n_repeat = 0, 0
    cur = 0
    for t in T:
        if t not in s_dic.keys():
            return -1
        i = bisect.bisect_left(s_dic[t], cur)
        if i>= len(s_dic[t]):
            cur = s_dic[t][0]
            n_repeat += 1
        else:
            cur = s_dic[t][i]
            if cur <= pre:
                n_repeat += 1
        pre = cur

    return n_repeat * len(S) + cur + 1

print(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.index(t[i-1])
    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:
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 < 100:
        if(t[index] == s100[index2]):break
        if(index2 == 99):
            print('-1')
            exit(0)
        index2 = index2 + 1
    index = index+1

print(index2+1)

Submission status: Wrong Answer

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

if set(t).issubset(set(ori_s)):
    idx = 0
    n_times = 0
    current_idx = 0
    s = ori_s
    for i in t:
        idx = s[idx:].find(i)
        if idx == -1:
            # 初期化
            s = ori_s
            current_idx = 0
            n_times += 1
            # 再検索
            idx = s.find(i)
            current_idx += idx
        else:
            s = s[idx+1:]
            current_idx += (idx+1)
    idx = current_idx + n_times * len(ori_s)
    print(idx)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import sys

s = input()
t = input()
if not set(t).issubset(set(s)):
    print(-1)
    sys.exit(0)
count = 0
now_idx = 0
for i,t_ in enumerate(t):
    if t_ in s[now_idx+1:]:
        next_idx = (now_idx+1)+ s[now_idx+1:].index(t_)
    else:
        next_idx = s.index(t_)
        count += 1
        if i == (len(t)-1):
            next_idx += 1
    now_idx = next_idx
print(count*len(s) + now_idx+1)

Submission status: Wrong Answer

Solution code:
S=input()
T=input()
dp=[[10**6]*len(S) for i in range(26)]
for i,s in enumerate(S):
    dp[ord(s)-ord('a')][i]=0
for i in range(len(S)-1,-1,-1):
    for j in range(26):
        dp[j][i-1]=min(dp[j][i-1],dp[j][i]+1)
for i in range(len(S)-1,-1,-1):
    for j in range(26):
        dp[j][i-1]=min(dp[j][i-1],dp[j][i]+1)
x=1
i=0
for t in T:
    ti=ord(t)-ord('a')
    if dp[ti][i]<1e6:
        x=x+dp[ti][i] if dp[ti][i]>0 else x+len(S)
        i=(i+max(dp[ti][i],1))%len(S)
    else:
        x=-1
        break
print(x)

Submission status: Wrong Answer

Solution code:
import sys

if __name__ == "__main__":
    S = list(sys.stdin.readline().rstrip())
    T = list(sys.stdin.readline().rstrip())

    # 巻き戻る → 文字数分プラスする
    tmp = 0
    L = [S.index(t) if t in S else -1 for t in T]

    if -1 in L:
        print(-1)
        exit()

    prev = L[0]
    ans = 0
    Slen = len(S)

    for l in L[1::]:
        if prev >= l:
            ans += Slen
        prev = l

    if len(L) > 1:
        if L[-2] < L[-1]:
            ans += (L[len(L) - 1] + 1)
    else:
        ans += (L[len(L) - 1] + 1)

    print(ans)

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)
        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)
            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:
s = input()
t = input()
len_s = len(s)
len_t = len(t)
ans = 0
ans2 = 0
i = 0
break_flg = 0
index = -1
while i < len_t:
    ans2 = 0
    for j in range(len_t-i):
        index = s[index+1:].find(t[i+j])
        if index == -1:
            break_flg = 1
            break
        ans2 += index+1
    if j == 0 and break_flg == 1:
        print(-1)
        exit()
    break_flg = 0
    if j == len_t-i-1:
        print(ans+ans2)
        exit()
    ans += len_s
    i += j

Submission status: Wrong Answer

Solution code:
s=input()
list_s=[]
for i in range(len(s)):
    list_s.append(s[i])
set(list_s)
t=input()
list_t=[]
for i in range(len(t)):
    list_t.append(t[i])
set(list_t)

flag=True
for i in list_t:
    if(i not in list_s):
        flag=False

cnt=0
ans=0
temp=0
if not flag:
    print(-1)
else:
    while(cnt<len(t)):
        for i in range(len(s)):
            if cnt>=len(t):
                break
            if t[cnt]==s[i]:
                cnt+=1
                temp=i+1
            if i==len(s)-1:
                ans+=1
    print(ans*len(s)+temp)

Submission status: Wrong Answer

Solution code:
s = list(input())
t = list(input())
res = 0
lens = len(s)

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
        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:
s = input()
t = input()
len_s = len(s)
len_t = len(t)
a = [-100]
ans = 0
ng_count = 0
s_raw = s
d = []
for i in range(len_t):
    b = s.find(t[i])
    # print(b)
    if b == -1:
        e = s_raw.find(t[i])
        if e!=-1:
            d.append(e)
        else:
            print(-1)
            exit()
    else:
        if b!=len_t:
            s = s[:b] + "?" + s[b+1:]
        else:
            s = s[:b] + "?"
        # print(t)
        d.append(b)
        # print(d)

# print(d)

if len(d) == 1:
    print(d[0])
else:
    for i in range(1,len_t):
        if d[i-1] < d[i]:
            ans = ng_count*len_s + d[i]
        else:
            ng_count += 1
            ans = ng_count*len_s + d[i]

print(ans+1)

Submission status: Wrong Answer

Solution code:
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; cnt = 0; ansli = []; tmpli = []; candili = []; stillset = set()
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]#WideIntPoint
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def s_list(): return list(input())
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]#HeightString
def mi(n): return [wi() for _ in range(n)]#MatrixInt
def num__grid(n): return [[int(i) for i in sys.stdin.readline().split()[0]] for _ in range(n)]#NumberGrid
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]
def grid(n): return [s_list() for _ in range(n)]


s = input()
t = input()
ns = len(s)
nt = len(t)
alpha = "abcdefghijklmnopqrstuvwxyz"
aldi = {}
for i in alpha:
    aldi[i] = []

for i in range(ns):
    aldi[s[i]].append(i + 1)


num = []
if aldi[t[0]] == []:
    print("-1")
    exit()
mae = aldi[t[0]][0]
num.append(mae)
for i in range(1, nt):
    if aldi[t[i]] == []:
        print("-1")
        exit()
    else:
        kk = bisect_right(aldi[t[i]], mae)
        if kk == len(aldi[t[i]]):
            kk = aldi[t[i]][0]
        else:
            kk = aldi[t[i]][kk]
        mae = kk
        num.append(kk)

ans += num[0]
for i in range(1, len(num)):
    if num[i] < num[i - 1]:
        ans += ns - num[i - 1] + num[i]
    else:
        ans += num[i] - num[i - 1]

print(ans)

Submission status: Wrong Answer

Solution code:
import copy

s=list(input())
s_len=len(s)
t=list(input())
s_alphabets=[]
s_reference={}
ref_length=0
for i,alphabet in enumerate(s):
    s_alphabets.append(alphabet)
    s_reference.setdefault(alphabet,[]).append(i)

copied_reference=copy.deepcopy(s_reference)
prev_checked=0
ans=0
flag=True
for i in t:
    try:
        checked=copied_reference[i].pop(0)
        if copied_reference[i]==[]:
            hoju=copy.copy(s_reference[i])
            copied_reference[i]=hoju
        if checked<prev_checked:
            ans+=s_len
            copied_reference=copy.deepcopy(s_reference)
            checked=copied_reference[i].pop(0)
            if copied_reference[i]==[]:
                hoju=copy.copy(s_reference[i])
                copied_reference[i]=hoju
        prev_checked=checked
    except KeyError:
        print(-1)
        flag=False
        break
if flag:
    print(ans+checked+1)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_right
from collections import defaultdict

S = input()
S_set = set(S)
d = defaultdict(list)
for i,s in enumerate(S):
    d[s].append(i)
# print(d)

T = input()
T_set = set(T)
for t in T_set:
    if t not in S_set:
        print(-1)
        exit()

cnt = 0
loop = 0
prev = 0

for t in T:
    num = d[t]
    a = bisect_right(num,prev)
    # print(prev,a,num)
    if a >= len(num):
        loop += 1
        a = 0
    prev = num[a]

print(prev+1+(loop)*len(S))

Submission status: Wrong Answer

Solution code:
#
# Yet I'm feeling like
# 	There is no better place than right by your side
# 		I had a little taste
# 			And I'll only spoil the party anyway
# 				'Cause all the girls are looking fine
# 					But you're the only one on my mind


import sys
import re
# inf = float("inf")
#sys.setrecursionlimit(1000000)

# abc='abcdefghijklmnopqrstuvwxyz'
# abd={'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}
# mod,MOD=1000000007,998244353
# vow=['a','e','i','o','u']
# dx,dy=[-1,1,0,0],[0,0,1,-1]

# from cmath import sqrt
from collections import deque, Counter, OrderedDict,defaultdict
from heapq import nsmallest, nlargest, heapify,heappop ,heappush, heapreplace
# from math import ceil,floor,log,sqrt,factorial,pow,pi,gcd,log10,atan,tan
from bisect import bisect_left,bisect_right
# import numpy as np

def dfs(curr,parent):
    visited[curr]=True
    value[curr]+=value[parent]
    for i in mydict[curr]:
        if not visited[i]:
            dfs(i,curr)


def get_array(): return list(map(int , sys.stdin.readline().strip().split()))
def get_ints(): return map(int, sys.stdin.readline().strip().split())
def input(): return sys.stdin.readline().strip()

string=input()
length=len(string)
T=input()
mydict=defaultdict(list)
for i in range(length):
    mydict[string[i]].append(i+1)
flag=0;last=0;total=0
for i in range(len(T)):
    if T[i] not in mydict:
        flag=1
        break
    else:
        new=bisect_right(mydict[T[i]],last)
        if new==len(mydict[T[i]]):
            value=mydict[T[i]][-1]
            total+=(length-last+value)
            last=value
        else:
            value=mydict[T[i]][new]
            total+=(value-last)
            last=value
        if last==length:
            last=0
if flag==0:
    print(total)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
dic = {}
index = 10 ** 5 + 1
ans = 0
for i in range(len(s)):
    if s[i] not in dic:
        dic[s[i]] = []
    dic[s[i]].append(i)

for i in range(len(t)):
    if t[i] in dic:
        for j in range(len(dic[t[i]])):
            number = dic[t[i]][j]
            if number > index:
                ans += number - index
                index = number
                break
            if j == len(dic[t[i]])-1:
                if i > 0:
                    ans += len(s) - index + dic[t[i]][0]
                else:
                    ans += number + 1
                index = dic[t[i]][0]
    else:
        print(-1)
        exit()

print(ans)

Submission status: Wrong Answer

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

len_s_count = 0
same_word_count = 0
pre_c = 'A'
pre_index = -1
for i,c in enumerate(t):
    index = s.find(c, pre_index + 1)
    if c == pre_c:
        if index == -1:
            len_s_count += 1
    else:
        if index == -1:
            len_s_count += 1
            index = s.find(c)
        if index == -1:
            print(-1)
            exit()
    pre_index = index
    pre_c = c

print(len(s) * len_s_count + index + 1)

Submission status: Wrong Answer

Solution code:
s=list(input())
t=list(input())
counter=0
s_n=s[:]
i=0

for m in t:
    if m in s:
        counter+=1

if counter==len(t):
    counter=0
    while i<len(t)-1:
        if t[i] in s_n:
            y=s_n.index( t[i] )+1
            i+=1
            del s_n[ :y ]

        elif not t[i] in s_n:
            counter+=1
            s_n=s[:]
    print( counter*len(s)+ s.index(t[i])+1 )

else:
    print(-1)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
from collections import defaultdict
from bisect import bisect_left
d = defaultdict(list)
for i, e in enumerate(s):
  d[e].append(i)

for e in t:
  if e not in d:
    print(-1)
    exit()
cur = -1
loop = 0
for e in t:
  found = False
  idxt = bisect_left(d[e], cur-loop*len(s))
  #print(d[e], idxt)
  if len(d[e])>idxt:
    #print(11, idxt, d[e][idxt])
    idx = d[e][idxt]
    cur = (loop*len(s))+idx
    #print(12, idx)
    found = True
  else:
    loop += 1
    idxt = bisect_left(d[e], cur-loop*len(s))
    #print(d[e], idxt)
    idx = d[e][idxt]
    cur = (loop*len(s))+idx
    found = True
print(cur+1)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
index = [0]*len(t)
counter = 1
for i in range(len(t)):
    try:
        index[i] = s.index(t[i])
    except ValueError:
        print('-1')
        exit()
    else:
        if i == 0:
            pass
        elif index[i] < index[i-1]:
            counter += 1

else:
    print((counter-1)*len(s)+index[len(t)-1]+1)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
sa = len(s)
ta = len(t)
count = 0
n = 0
for i in range(ta):
    try:
        if i > 0:
            sc = s
            del sc[:deln + 1]
            scs = sc + s
            count += scs.index(t[i]) + 1
            if sa - (deln + 1) >= scs.index(t[i]) + 1:
                deln += scs.index(t[i]) + 1
            elif sa - (deln + 1) < scs.index(t[i]) + 1:
                deln = sa - (deln + 1) - scs.index(t[i])
        elif i == 0:
            count += s.index(t[i]) + 1
            deln = s.index(t[i])
        
    except:
        count = -1
        break
print(count)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import math
import bisect

def main():
    s = list(input())
    t = list(input())
    lenth = len(s)
    if len(set(t)-set(s))!=0:
        print(-1)
    else:
        dc = defaultdict(list)
        for i,j in enumerate(s):
            dc[j].append(i)
        ans = 0
        now = 0
        for i,j in enumerate(t):
            next_ = bisect.bisect_left(dc[j],now)
            if  len(dc[j])==next_:
                ans += lenth-(now+1)
                now = dc[j][0]
                ans += now+1
            else:
                ans += dc[j][next_]-now
                now = dc[j][next_]
        print(ans+1)
if __name__ =="__main__":
    main()

Submission status: Wrong Answer

Solution code:
import bisect

s = input()
t = input()
dic = {}

for i in "abcdefghijklmnopqrstuvwxyz":
    dic[i] = []

for i in range(len(s)):
    dic[s[i]].append(i)

f = 0
ans = 0
for moji in t:
    if len(dic[moji]) == 0:
        print(-1)
        exit()

    index = bisect.bisect_left(dic[moji], f)
    if index == len(dic[moji]):
        ans += len(s)
        f = dic[moji][0]
    else:
        f = dic[moji][index]



ans += f + 1
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
n = len(s)
m = len(t)
i = 0
j = 0
while j != m:
  now = t[j]
  for _ in range(n):
    if s[i%n] == now:
      break
    else:
      i += 1
  else:
    print(-1)
    break
  j += 1
else:
  print(i+1)

Submission status: Wrong Answer

Solution code:
import collections, bisect

s = input()
t = input()

n = len(s)
d = collections.defaultdict(list)
for i, c in enumerate(s):
    d[c].append(i + 1)

m = len(t)
x = [0] * (m + 1) # 長さを記録
y = [0] * (m + 1) # 添え字を記録

for i in range(1, m + 1):
    c = t[i - 1]
    if not c in d:
        print(-1)
        exit()
    
    a = y[i - 1]
    ptr = bisect.bisect_right(d[c], y[i - 1])
    if ptr == len(d[c]):
        b = d[c][-1]
    else:
        b = d[c][ptr]
    
    y[i] = b
    if a < b:
        x[i] = x[i - 1] + b - a
    else:
        x[i] = x[i - 1] + n - a + b
    
print(x[-1])

Submission status: Wrong Answer

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

flag = 0
count = 0
rest_s = s
n = len(s)

for w in t:
  if w not in s:
    flag = 1
    print(-1)
    break

if flag == 0:
  for w in t:
    index = rest_s.find(w)
    #print(index)
    if (index != -1):
      count += (index+1)
      rest_s = s[count%n+1:]
    else:
      index = s.find(w)
      count = (count // n + 1) * n + index
      rest_s = s[count%n+1:]
  print(count+1)

Submission status: Wrong Answer

Solution code:
import sys
import bisect
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

s = S()
t = S()
N = len(s)

dic = {}

for i in range(N):
  if s[i] in dic:
    dic[s[i]].append(i+1)
  else:
    dic[s[i]] = [i+1]

tmp = 0
cnt = 0
for i in t:
  if i not in dic:
    print(-1)
    exit()
  else:
    l = dic[i]
    n = bisect.bisect_left(l,tmp)
    if n > len(l)-1:
      tmp = l[0]
      cnt += 1
    else:
      tmp = l[n]

print(cnt*N+tmp)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
a = 'abcdefghijklmnopqrstuvwxyz'
c = {}
ans = 0
for i in range(len(a)):
	indexes = [n for n, x in enumerate(s) if x == a[i]]
	c[a[i]] = indexes
for i in range(len(t)):
	if len(c[t[i]]) == 0:
		print('-1')
		exit()

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)
x.append(n-inde-1)
for i in range(1,m):
    inde = s.index(t[i])
    preinde = s.index(t[i-1])
    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 numpy as np
s = input()
t = input()
i = 0
r = 1
n = len(s)

d = dict()
for x in t:
    if x not in d:
        d[x] = np.where(np.array(list(s)) == x)[0]
        if len(d[x]) == 0:
            r = -1
            break
    
    y = np.where(d[x]>i)[0]
    if len(y) == 0:
        r += n - i
        i = d[x][0]
        r += i
    else:
        j = d[x][y[0]]
        r += j - i
        i = j

print(r)

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

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

  ln=len(s)

  a=set(s)
  b=set(t)

  for x in b:
    if x not in a:
      return -1

  q=['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']
  r=[[] for _ in range(len(q))]

  for i,x in enumerate(s):
    r[q.index(x)].append(i)

  ans=1
  if s[0]==t[0]:
    ans=0
  i=0
  for x in t:
    while True:
      if i>=len(s):
        i=0
      _l=r[q.index(x)]
      a=bisect.bisect_right(_l,i)
      __i=_l[a-1]
      if a==0:
        _i=_l[0]
      elif a==len(_l):
        _i=_l[0]
      else:
        _i=__i

      if i==_i:
        ans+=1
        break

      if _i<i:
        ans+=ln-i-1
        ans+=_i+1
      else:
        ans+=_i-i
      i=_i
      break

  return ans

# main()
print(main())

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])
        mid = (r+l)//2
        while r == l:
            if(dic[c][mid] > focus):
                l = mid
            else:
                r = mid
        focus = dic[c][mid-1]
print(repeat*len(s)+focus+1)

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict

def main():
    input = sys.stdin.readline
    S = str(input().strip())
    T = str(input().strip())

    if not set(T) <= set(S):
        return -1

    n = len(S)
    d = defaultdict(list)
    for i, s in enumerate(S, start=1):
        d[s].append(i)

    cnt = 0
    pos = 0
    for t in T:
        if pos > d[t][-1]:
            cnt += 1
            pos = d[t][0]
        else:
            left = 0
            right = len(d[t])
            while left != right:
                mid = (left + right) // 2
                np = d[t][mid]
                if np < pos:
                    left = mid + 1
                else:
                    right = mid
            pos = d[t][left]

    return n * cnt + pos


if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
import string
import bisect

S, T = input(), input()
lS, lT = len(S), len(T)

l = {s: [] for s in string.ascii_lowercase}
for i in range(lS):
    l[S[i]].append(i)

id, ans = 0, 0
for i in range(lT):
    if len(l[T[i]]) == 0:
        print(-1)
        exit()
    else:
        tmp = l[T[i]][(bisect.bisect(l[T[i]], id)) % len(l[T[i]])]
        if id >= tmp:
            ans += lS
        id = tmp

print(ans + id + 1)

Submission status: Wrong Answer

Solution code:
def main():
    from collections import defaultdict
    import sys
    
    s = input()
    t = input()
    ds = defaultdict(int)
    di = {}

    for i in range(len(s)):
        di[i] = defaultdict(int)
    
    for a in s:
        ds[a] = 1
        
    flag = 0
    for b in t:
        if ds[b] == 0:
            flag = 1
    if flag == 1:
        print(-1)
        sys.exit()

    s2 = s[::-1]
    for i in range(2*len(s)):
        for j in di[len(s) - (i-1)%len(s)-1]:
            di[len(s) - i%len(s)-1][j] = di[len(s) - (i-1)%len(s)-1][j] + 1
        di[len(s) - i%len(s) - 1][s2[(i-1)%len(s)]] = 1
    tmp = t[0]
    print(di)

    for k in range(len(s)):
        if tmp == s[k]:
            answer = k+1
            break
    for k in range(len(t)-1):
        print(answer)
        l = k+1
        answer += di[(answer-1)%len(s)][t[l]]
        tmp = t[l]
    print(answer)
      

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

len_s_count = 0
same_word_count = 0
pre_c = 'A'
pre_index = -1
for i,c in enumerate(t):
    if c == pre_c:
        index = s.find(c, pre_index + 1)
        if index == -1:
            len_s_count += 1
    else:
        index = s.find(c)
        if index == -1:
            print(-1)
            exit()
        elif index < pre_index:
            len_s_count += 1
    pre_index = index
    pre_c = c

print(len(s) * len_s_count + index + 1)

Submission status: Wrong Answer

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

ans = -1

print(ans)