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:
#E
import bisect

s = input()
t = input()
lens = len(s)

s_n = {}
for i in range(len(s)):
    if s[i] in s_n:
        s_n[s[i]].append(i+1)
    else:
        s_n[s[i]] = [i+1]
        
print(s_n)

d1=0
d2=0
for i in t:
    if not(i in s_n):
        print(-1)
        break
    else:
        if d1 >= s_n[i][-1]:
            d1 = s_n[i][0]
            d2 += lens
        else:
            index = bisect.bisect(s_n[i],d1)
            d1 = s_n[i][index]
else:
    print(d1+d2)

Submission status: Wrong Answer

Solution code:
#E
import bisect

s = input()
t = input()
lens = len(s)

s_n = {}
for i in range(len(s)):
    if s[i] in s_n:
        s_n[s[i]].append(i+1)
    else:
        s_n[s[i]] = [i+1]
        
#print(s_n)

d1=0
d2=0
for i in t:
    if not(i in s_n):
        print(-1)
        break
    else:
        if d1 >= s_n[i][-1]:
            d1 = s_n[i][0]
            d2 += lens
        else:
            index = bisect.bisect_left(s_n[i],d1)
            d1 = s_n[i][index]
else:
    print(d1+d2)

Submission status: Wrong Answer

Solution code:
#E
import bisect

s = input()
t = input()
lens = len(s)

s_n = {}
for i in range(len(s)):
    if s[i] in s_n:
        s_n[s[i]].append(i+1)
    else:
        s_n[s[i]] = [i+1]
        
#print(s_n)

d1=0
d2=0
for i in t:
    if not(i in s_n):
        print(-1)
        break
    else:
        if d1 > s_n[i][-1]:
            d1 = s_n[i][0]
            d2 += lens
        else:
            index = bisect.bisect_left(s_n[i],d1)
            d1 = s_n[i][index]
else:
    print(d1+d2)

Submission status: Wrong Answer

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

index = -1
rep = 0
length = len(s)
ans = 0
for tar in t:
    kari_index = s[index+1:].find(tar)

    if kari_index == -1:
        tmp = s.find(tar)
        if  tmp == -1:
            ans = -1
        else:
            rep += 1
            index = tmp
    else:
        index += kari_index + 1
        if index+1 == length:
            rep += 1
            index = 0
if ans != -1:
    ans = rep * len(s) + index + 1
print(ans)

Submission status: Wrong Answer

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

index = 0
rep = 0
length = len(s)
ans = 0
for tar in t:
    old_index = index
    kari_index = s[old_index+1:].find(tar)

    if kari_index == -1:
        tmp = s.find(tar)
        if  tmp == -1:
            ans = -1
        else:
            rep += 1
            index = tmp
    else:
        index += kari_index + 1
        if index+1 == length:
            rep += 1
            index = 0
if ans != -1:
    ans = rep * len(s) + index + 1
print(ans)

Submission status: Wrong Answer

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

index = 0
rep = 0
length = len(s)
ans = 0
for tar in t:
    old_index = index
    kari_index = s[old_index+1:].find(tar)

    if kari_index == -1:
        tmp = s.find(tar)
        if  tmp == -1:
            ans = -1
        else:
            rep += 1
            index = tmp
    else:
        index += kari_index + 1
if ans != -1:
    ans = rep * len(s) + index + 1
print(ans)

Submission status: Wrong Answer

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

index = 0
rep = 0
length = len(s)
ans = 0
for tar in t:
    old_index = index
    kari_index = s[old_index+1:].find(tar)

    if kari_index == -1:
        tmp = s.find(tar)
        if  tmp == -1:
            ans = -1
        else:
            rep += 1
            index = tmp
    else:
        index += kari_index + 1

ans = rep * len(s) + index + 1
print(ans)

Submission status: Wrong Answer

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

index = [s.find(tar) for tar in t]

length = len(s)
ans = 0
for i in range(len(index)-1):
    if index[i] == -1:
        ans = -1
        break
    elif index[i] > index[i+1]:
        ans += length

if ans != -1:
    ans += index[-1]+1

print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
from itertools import *
from collections import defaultdict
import sys
import time


#################### library ####################

def classify(iterable):
	dic = defaultdict(list)
	for k, v in iterable:
		dic[k].append(v)
	return dic


#################### naive ####################

def gen_s(s):
	s_ = ""
	for k in count():
		s_ += s[k%len(s)]
		yield s_

def has_substring(s, t):
	q = 0
	for p, c in enumerate(s):
		if c == t[q]:
			q += 1
			if q == len(t):
				return True
	else:
		return False

def f_naive():
	s, t = read_input()
	if any(c not in s for c in t):
		return -1
	
	for i, s_ in enumerate(gen_s(s), 1):
		if has_substring(s_, t):
			return i


#################### process ####################

def read_input():
#	return ("sosson", "sonssnss")
	s = raw_input()
	t = raw_input()
	return (s, t)

def next_pos(c, p, dic):
	def bin_search(first, last, q):
		if last - first == 1:
			if v[first] == q-1:
				return v[first+1]
			else:
				return v[first]
		
		mid = (first + last) / 2
		if v[mid] > q:
			return bin_search(first, mid, q)
		else:
			return bin_search(mid, last, q)
	
	v = dic[c]
	if p == -1 or v[-1] <= p:
		return v[0]
	
	return bin_search(0, len(v), p+1)

def f():
	s, t = read_input()
	if any(c not in s for c in t):
		return -1
	
	L = len(s)
	dic = classify((s[k], k) for k in range(len(s)))
	p = -1
	i = -1
	for c in t:
		q = next_pos(c, p, dic)
		if q == p:
			i += L
		elif q < p:
			i += q - p + L
		else:
			i += q - p
		p = q
#		print c, q, i
	return i+1


#################### main ####################

#print f_naive()
print f()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
from itertools import *
from collections import defaultdict
import sys
import time


#################### library ####################

def classify(iterable):
	dic = defaultdict(list)
	for k, v in iterable:
		dic[k].append(v)
	return dic


#################### naive ####################

def gen_s(s):
	s_ = ""
	for k in count():
		s_ += s[k%len(s)]
		yield s_

def has_substring(s, t):
	q = 0
	for p, c in enumerate(s):
		if c == t[q]:
			q += 1
			if q == len(t):
				return True
	else:
		return False

def f_naive():
	s, t = read_input()
	if any(c not in s for c in t):
		return -1
	
	for i, s_ in enumerate(gen_s(s), 1):
		if has_substring(s_, t):
			return i


#################### process ####################

def read_input():
	return ("sosson", "sonssnss")
	s = raw_input()
	t = raw_input()
	return (s, t)

def next_pos(c, p, dic):
	def bin_search(first, last, q):
		if last - first == 1:
			if v[first] == q-1:
				return v[first+1]
			else:
				return v[first]
		
		mid = (first + last) / 2
		if v[mid] > q:
			return bin_search(first, mid, q)
		else:
			return bin_search(mid, last, q)
	
	v = dic[c]
	if p == -1 or v[-1] <= p:
		return v[0]
	
	return bin_search(0, len(v), p+1)

def f():
	s, t = read_input()
	if any(c not in s for c in t):
		return -1
	
	L = len(s)
	dic = classify((s[k], k) for k in range(len(s)))
	p = -1
	i = -1
	for c in t:
		q = next_pos(c, p, dic)
		if q == p:
			i += L
		elif q < p:
			i += q - p + L
		else:
			i += q - p
		p = q
#		print c, q, i
	return i+1


#################### main ####################

#print f_naive()
print f()

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
ls = len(s)
lt = len(t)

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

else:
  i = 0
  j = 0
  while True:
    if s[i % ls] == t[j]:
      j += 1
  
    else:
      i += 1
  
    if j >= lt:
      break

  print(i + 1)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
from itertools import *
from collections import defaultdict
import sys
import time


#################### library ####################

def classify(iterable):
	dic = defaultdict(list)
	for k, v in iterable:
		dic[k].append(v)
	return dic


#################### naive ####################

def gen_s(s):
	s_ = ""
	for k in count():
		s_ += s[k%len(s)]
		yield s_

def has_substring(s, t):
	q = 0
	for p, c in enumerate(s):
		if c == t[q]:
			q += 1
			if q == len(t):
				return True
	else:
		return False

def f_naive():
	s, t = read_input()
	if any(c not in s for c in t):
		return -1
	
	for i, s_ in enumerate(gen_s(s), 1):
		if has_substring(s_, t):
			return i


#################### process ####################

def read_input():
	s = raw_input()
	t = raw_input()
	return (s, t)

def next_pos(c, p, dic):
	def bin_search(first, last, q):
		if last - first == 1:
			return v[first]
		
		mid = (first + last) / 2
		if v[mid] > q:
			return bin_search(first, mid, q)
		else:
			return bin_search(mid, last, q)
	
	v = dic[c]
	if p == -1 or v[-1] <= p:
		return v[0]
	
	return bin_search(0, len(v), p+1)

def f():
	s, t = read_input()
	if any(c not in s for c in t):
		return -1
	
	L = len(s)
	dic = classify((s[k], k) for k in range(len(s)))
	p = -1
	i = -1
	for c in t:
		q = next_pos(c, p, dic)
		if q == p:
			i += L
		elif q < p:
			i += q - p + L
		else:
			i += q - p
		p = q
	return i+1


#################### main ####################

#print f_naive()
print f()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
from itertools import *
from collections import defaultdict
import sys
import time


#################### library ####################

def classify(iterable):
	dic = defaultdict(list)
	for k, v in iterable:
		dic[k].append(v)
	return dic


#################### naive ####################

def gen_s(s):
	s_ = ""
	for k in count():
		s_ += s[k%len(s)]
		yield s_

def has_substring(s, t):
	q = 0
	for p, c in enumerate(s):
		if c == t[q]:
			q += 1
			if q == len(t):
				return True
	else:
		return False

def f_naive():
	s, t = read_input()
	if any(c not in s for c in t):
		return -1
	
	for i, s_ in enumerate(gen_s(s), 1):
		if has_substring(s_, t):
			return i


#################### process ####################

def read_input():
	s = raw_input()
	t = raw_input()
	return (s, t)

def next_pos(c, p, dic):
	def bin_search(first, last, q):
		if last - first == 1:
			return v[first]
		
		mid = (first + last) / 2
		if v[mid] > q:
			return bin_search(first, mid, q)
		else:
			return bin_search(mid, last, q)
	
	v = dic[c]
	if p == -1 or v[-1] <= p:
		return v[0]
	
	return bin_search(0, len(v), p+1)

def f():
	s, t = read_input()
	if any(c not in s for c in t):
		return -1
	
	L = len(s)
	dic = classify((s[k], k) for k in range(len(s)))
	p = -1
	i = -1
	for c in t:
		q = next_pos(c, p, dic)
		if q < p:
			i += q - p + L
		else:
			i += q - p
		p = q
	return i+1


#################### main ####################

#print f_naive()
print f()

Submission status: Wrong Answer

Solution code:
import bisect


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

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

    IS = [[] for i in range(26)]
    for i in range(n):
        IS[ord(s[i]) - ord('a')].append(i)
    for i in range(n):
        IS[ord(s[i]) - ord('a')].append(i + n)

    p = 0
    ans = 1
    for i in range(m):
        if IS[ord(t[i]) - ord('a')] == []:
            print(-1)
            return
        p_ind = bisect.bisect_left(IS[ord(t[i]) - ord('a')], p)
        p = IS[ord(t[i]) - ord('a')][p_ind] + 1
        if p >= n:
            p -= n
            ans += n
    ans += p
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import bisect


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

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

    IS = [[] for i in range(26)]
    for i in range(n):
        IS[ord(s[i]) - ord('a')].append(i)
    for i in range(n):
        IS[ord(s[i]) - ord('a')].append(i + n)

    p = 0
    ans = 1
    for i in range(m):
        if IS[ord(t[i]) - ord('a')] == []:
            print(-1)
            return
        p_ind = bisect.bisect_left(IS[ord(t[i]) - ord('a')], p)
        p = IS[ord(t[i]) - ord('a')][p_ind]
        if p >= n:
            p -= n
            ans += n
    ans += p
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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


for ch in t:
    if ch not in s:
        print('-1')
        exit()

x = s
for ch in t:
    p = x.find(ch) + 1
    if p == 0:
        ans += len(x)
        p = s.find(ch) + 1
        x = s
    ans += p
    if p >= len(x) :
        p = 0
    x = x[p:]

print(ans)

Submission status: Wrong Answer

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

if set(t).issubset(set(s)):
    x = s
    for ch in t:
        p = x.find(ch) + 1
        if p == 0:
            ans += len(x)
            x = s
        ans += p
        if p >= len(x) :
            p = 0
        x = x[p:]

    print(ans)
else:
    print('-1')

Submission status: Wrong Answer

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

for ch in t:
    if ch not in s:
        print('-1')
        exit()

x = s
for ch in t:
    p = x.find(ch) + 1
    if p == 0:
        ans += len(x)
        x = s
    ans += p
    if p >= len(x) :
        p = 0
    x = x[p:]

print(ans)

Submission status: Wrong Answer

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

for ch in t:
    if ch not in s:
        print('-1')

x = s
for ch in t:
    p = x.find(ch) + 1
    if p == 0:
        ans += len(x)
        x = s
    ans += p
    if p >= len(x) :
        p = 0
    x = x[p:]

print(ans)

Submission status: Wrong Answer

Solution code:
s = raw_input()
t = raw_input()
def longest_subsequence_prefix(s, t):
    f = [[False for _ in range(len(t))] for _ in range(len(s))]

    f[0][0] = s[0] == t[0]

    for i in range(1, len(s)):
        f[i][0] = True if s[i] == t[0] else f[i-1][0]

    for i in range(1, len(s)):
        for j in range(1, len(t)):
            f[i][j] = s[i] == t[j] and f[i-1][j-1] or f[i-1][j]

    # for i in range(len(s)):
    #     print ' '.join(map(str, f[i]))

    for j in range(len(t)-1, -1, -1):
        for i in range(len(s)):
            if f[i][j]:
                return j, i

    return 0

from collections import Counter
import sys
from math import floor

def find_i(s, t):
    cs = Counter(s)
    ct = Counter(t)
    for char in ct:
        if char not in cs:
            print -1
            return

    output = 0
    while len(t) > 0:
        idx_t, idx_s = longest_subsequence_prefix(s, t)
        if idx_t < len(t) - 1:
            output += len(s)
        else:
            output += idx_s + 1
        t = t[idx_t+1:]

    return output

Submission status: Wrong Answer

Solution code:
import sys
from bisect import bisect_left, bisect_right
s=sys.stdin.readline().strip()
t=sys.stdin.readline().strip()

len_s=len(s)


C={}
for i,a in enumerate(s*2):
		if a not in C:
			C[a]=[i]
		else:
			C[a].append(i)

counter=0
mul=0

for x in t:
	try:
		idx=bisect_left(C[x],counter)
	except:
		print -1
		quit()

	counter=C[x][idx]


	if len_s<counter:
			mul+=1
			counter-=len_s


print mul*len_s+counter+1

Submission status: Wrong Answer

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

g=0
pi=-1
for c in t:
    ci=s.find(c)
    if ci==-1: 
        print(-1)
        break
    if pi>=ci:
        g+=1
    pi=ci
else:
    print(ci+g*len(s)+1)

Submission status: Wrong Answer

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

g=0
pi=-1
for c in t:
    ci=s.find(c)
    if ci==-1: 
        print(-1)
        break
    if ci<pi:
        g+=1
    pi=ci
else:
    print(ci+g*len(s)+1)

Submission status: Wrong Answer

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

g=0
pi=-1
for c in t:
    ci=s.find(c)
    if ci==-1: 
        break
    if ci<pi:
        g+=1
    pi=ci

if ci==-1:
    print(-1)
else:
    print(ci+g*len(s)+1)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
sIdx = 0
tIdx = 0
ans = 0
isFind = False
while 1 :
    if s[sIdx] == t[tIdx]:
        tIdx+=1
        isFind = True
    else:
        sIdx+=1
        ans+=1
    if sIdx > len(s) - 1:
        sIdx = 0
        if isFind == True :
            isFind = False
        else :
            print('-1')
            break;

    if tIdx == len(t):
        print(ans+1)
        break

Submission status: Wrong Answer

Solution code:
s = list(input())
t = input()
a = []
try:
    for i in t:
        a.append(s.index(i) + 1)
except:
    print(-1)
    quit()
ans = a[0]
for j in range(1, len(a)):
    if a[j-1] > a[j]:
        ans += (len(s) - a[j-1]) + a[j]
    else:
        ans += a[j] - a[j-1]
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from bisect import bisect_left, bisect_right
s=sys.stdin.readline().strip()
t=sys.stdin.readline().strip()

len_s=len(s)


C={}
for i,a in enumerate(s*2):
		if a not in C:
				C[a]=[i]
		else:
			C[a].append(i)


counter=0
mul=0


for x in t:
	try:
		idx=bisect_right(C[x],counter)
	except:
		print -1
		quit()

	counter=C[x][idx]

	if len_s<=counter:
			mul+=1
			counter-=len_s

print mul*len_s+counter+1

Submission status: Wrong Answer

Solution code:
import sys

def solve():
    s = input()
    t = input()
    alphabet = ["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"]
    convert = dict()
    for i in range(26):
        convert[alphabet[i]] = i

    behind = [{i:-1 for i in range(26)} for _ in range(len(s) + 1)]
    canuse = set()
    for i in reversed(range(len(s))):
        for j in range(26): behind[i][j] = behind[i+1][j]
        behind[i][convert[s[i]]] = i
        canuse |= {s[i]}
    
    k = 0
    counter = 0
    for i in range(len(t)):
        if t[i] not in canuse:
            print(-1)
            break
        else:
            if behind[counter][convert[t[i]]] > -1:
                counter = behind[counter][convert[t[i]]]
            else:
                k += 1
                counter = behind[0][convert[t[i]]]
    else: print(k * len(s) + counter + 1)


    return 0

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
s1 = input()
l = input()
l1 = len(l)
s = s1*1000
#print(s)
sa = set(s1)
la = set(l)
if la.issubset(sa):
    j,su = 0,0
    for i in s:
        if i == l[j]:
            j+=1
        su+=1
        if j>=l1: break;
    print(su)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
s1 = input()
l = input()
l1 = len(l)
s = s1*100
#print(s)
sa = set(s1)
la = set(l)
if la.issubset(sa):
    j,su = 0,0
    for i in s:
        if i == l[j]:
            j+=1
        su+=1
        if j>=l1: break;
    print(su)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import bisect

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

alphabets = [[] for _ in range(26)]

#素材
ingredients = input()
#つくりたいもの
recipe = input()

# 素材のアルファベット分布リストをつくる
for i in range(len(ingredients)):
  alpha_idx = ctoi(ingredients[i])
  alphabets[alpha_idx].append(i)

ingredients_counter = [0] * 26
for i in range(26):
  ingredients_counter[i] = len(alphabets[i])
ingredients_pointer = -1
ingredients_idx_list = []
flag = True
for i in range(len(recipe)):
  alpha_idx = ctoi(recipe[i])
  if ingredients_counter[alpha_idx] == 0:
    flag = False
    break
  #今のingredients_pointerより右側に使える素材があるか探す
  pointer_candidate_idx = \
  	bisect.bisect_right(alphabets[alpha_idx], ingredients_pointer)
  #右側に無かった。ループして先頭のを使ってどうぞ
  if pointer_candidate_idx == ingredients_counter[alpha_idx]:
    ingredients_pointer = alphabets[alpha_idx][0]
  else:
    ingredients_pointer = alphabets[alpha_idx][pointer_candidate_idx]
  ingredients_idx_list.append(ingredients_pointer)

if flag == False:
  print(-1)
else: 
  loop = 0
  idx = ingredients_idx_list[0]
  #idxのリセット分ループを数える。左より右が小さかったらループ
  for i in range(1, len(ingredients_idx_list)):
    if ingredients_idx_list[i] < idx:
      loop += 1
    idx = ingredients_idx_list[i]
  # ループx素材の長さ + つくりたいものの最後の文字のインデックス(に、+１)
  print(loop * len(ingredients) + ingredients_idx_list[-1] + 1)

Submission status: Wrong Answer

Solution code:
import bisect

s = input()
t = input()
 
flg = False
for x in t:
  if not x in s:
    flg = True
  
if flg:
  print(-1)
else:
  dic = {}
  for i in range(len(s)):
    dic[s[i]] = dic.get(s[i], []) + [i]
  for i in range(len(s)):
    dic[s[i]] = dic.get(s[i], []) + [i+len(s)]

  ans = 0
  p = 0
  for i in range(len(t)):
    if not t[i] in dic:
      print(-1)
      break
    else:
      p = dic[t[i]][bisect.bisect_right(dic[t[i]], p)]
      if p>=len(s):
        p -= len(s)
        ans += len(s)
  ans += p+1
  print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

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

alphabets = [[] for _ in range(26)]

#素材
ingredients = input()
#つくりたいもの
recipe = input()

# 素材のアルファベット分布リストをつくる
for i in range(len(ingredients)):
  alpha_idx = ctoi(ingredients[i])
  alphabets[alpha_idx].append(i)

ingredients_pointer = -1
ingredients_idx_list = []
flag = True
for i in range(len(recipe)):
  alpha_idx = ctoi(recipe[i])
  if len(alphabets[alpha_idx]) == 0:
    flag = False
    break
  #今のingredients_pointerより右側に使える素材があるか探す
  pointer_candidate_idx = \
  	bisect.bisect_right(alphabets[alpha_idx], ingredients_pointer)
  #右側に無かった。ループして先頭のを使ってどうぞ
  if pointer_candidate_idx == len(alphabets[alpha_idx]):
    ingredients_pointer = alphabets[alpha_idx][0]
  else:
    ingredients_pointer = alphabets[alpha_idx][pointer_candidate_idx]
  ingredients_idx_list.append(ingredients_pointer)

if flag == False:
  print(-1)
else: 
  loop = 0
  idx = ingredients_idx_list[0]
  #idxのリセット分ループを数える。左より右が小さかったらループ
  for i in range(1, len(ingredients_idx_list)):
    if ingredients_idx_list[i] < idx:
      loop += 1
    idx = ingredients_idx_list[i]
  # ループx素材の長さ + つくりたいものの最後の文字のインデックス(に、+１)
  print(loop * len(ingredients) + ingredients_idx_list[-1] + 1)

Submission status: Wrong Answer

Solution code:
import bisect

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

alphabets = [[] for _ in range(26)]

#素材
ingredients = input()
#つくりたいもの
recipe = input()

# 素材のアルファベット分布リストをつくる
for i in range(len(ingredients)):
  alpha_idx = ctoi(ingredients[i])
  alphabets[alpha_idx].append(i)

ingredients_pointer = -1
ingredients_idx_list = []
for i in range(len(recipe)):
  alpha_idx = ctoi(recipe[i])
  if len(alphabets[alpha_idx]) == 0:
    break
  #今のingredients_pointerより右側に使える素材があるか探す
  pointer_candidate_idx = \
  	bisect.bisect_right(alphabets[alpha_idx], ingredients_pointer)
  #右側に無かった。ループして先頭のを使ってどうぞ
  if pointer_candidate_idx == len(alphabets[alpha_idx]):
    ingredients_pointer = alphabets[alpha_idx][0]
  else:
    ingredients_pointer = alphabets[alpha_idx][pointer_candidate_idx]
  ingredients_idx_list.append(ingredients_pointer)

if len(ingredients_idx_list) == 0:
  print(-1)
else: 
  loop = 0
  idx = ingredients_idx_list[0]
  #idxのリセット分ループを数える。左より右が小さかったらループ
  for i in range(1, len(ingredients_idx_list)):
    if ingredients_idx_list[i] < idx:
      loop += 1
    idx = ingredients_idx_list[i]
  # ループx素材の長さ + つくりたいものの最後の文字のインデックス(に、+１)
  print(loop * len(ingredients) + ingredients_idx_list[-1] + 1)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline

s = input()
n = len(s)
t = input()
s*=2

ans = 0
for c in t:
    d = s[ans%n:].find(c)
    ans += d
print(ans+1)

Submission status: Wrong Answer

Solution code:
import sys
import bisect

ss = input()
t = input()
n = len(ss)
cnt = {}
s = ss*2

for i, x in enumerate(s):
    if x not in cnt:
        cnt[x] = []
    cnt[x].append(i)

idx = 0
ten = 0
#print(cnt)
for z in t:
    if z not in ss:
        print(-1)
        sys.exit()

    idx = cnt[z][bisect.bisect_left(cnt[z], idx)]

    if idx>=n:
        idx -= n
        ten += 1

    #print(idx, z, ten)

suffix = idx + ten * n + 1

print(suffix)

Submission status: Wrong Answer

Solution code:
import sys
import bisect

s = input()
t = input()
n = len(s)
cnt = {}
s = s*2

for i, x in enumerate(s):
    if x not in cnt:
        cnt[x] = []
    cnt[x].append(i)

idx = 0
ten = 0
for z in t:
    if z not in s:
        print(-1)
        sys.exit()

    idx = cnt[z][bisect.bisect_right(cnt[z], idx)]

    if idx>=n:
        idx -= n
        ten += 1

    #print(idx, z, ten)

suffix = idx + ten * n + 1

print(suffix)

Submission status: Wrong Answer

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

wi = {}
for n, ws in enumerate(s):
  wi[n] = {}
  
for n, _ in enumerate(s):
  for k, ws in enumerate(s):
    if n != k:
      if not wi[n].get(ws):
        wi[n][ws] = [k-n if k-n > 0 else k-n+s_l]
      else:
        wi[n][ws].append(k-n if k-n > 0 else k-n+s_l)

ans = 0
x = 0
for b, wt in enumerate(t):
  if b == 0:
    x = s.find(wt)
    if x == -1:
      print(x)
      exit()
    else:
      ans = x + 1
  else:
    if not wi[x].get(wt):
      print(-1)
      exit()
    else:
      ans += wi[x][wt][0]
      x = (x + wi[x][wt][0]) % s_l

print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
 
flg = False
for x in t:
  if not x in s:
    flg = True
  
if flg:
  print(-1)
else:
  dic = {}
  for i in range(len(s)):
    dic[s[i]] = dic.get(s[i], []) + [i]
  for i in range(len(s)):
    dic[s[i]] = dic.get(s[i], []) + [i+len(s)]

  ans = 0
  p = 0
  for i in range(len(t)):
    if not t[i] in dic:
      print(-1)
      break
    else:
      idx = [-1]+dic[t[i]]
      l, r = 1, len(idx)
      while l<=r:
        m = (l+r)//2
        if p>idx[m-1] and p<=idx[m]:
          p = idx[m]+1
          break
        elif p<idx[m-1]:
          r = m-1
        else:
          l = m+1
      if p>=len(s):
        p -= len(s)
        ans += len(s)
  ans += p
  print(ans)

Submission status: Wrong Answer

Solution code:
import sys

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

for i in range(len(t)):
    if t[i] not in s:
        print(-1)
        sys.exit()

count = 0
for j in range(len(t)):
    for k in range(len(s)):
        if count == len(t):
            print(k +  (j * len(s)))
            sys.exit()
            
        #print(a[k +  (j * len(s))], t[count],a, count)
        if s[k] == t[count]:
            count += 1
            
print(-1)

Submission status: Wrong Answer

Solution code:
import sys

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

for i in range(len(t)):
    if t[i] not in s:
        print(-1)
        sys.exit()

count = 0
for j in range(100000):
    for k in range(len(s)):
        if count == len(t):
            print(k +  (j * len(s)))
            sys.exit()
            
        #print(a[k +  (j * len(s))], t[count],a, count)
        if s[k] == t[count]:
            count += 1

Submission status: Wrong Answer

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

x = len(s)
y = len(t)

a = 0
b = x
c = 0
a0 = 0


for i in range(0,y):
  while True:
    #先頭からの位置
    a=s.find(t[i:i+1],a0)+1
    if a <= 0:
      a=s.find(t[i:i+1])+1
    if a <= 0:
      print("-1")
      exit()
    
    if a<a0:
      c=c+a+b
    else:
      c=c+a-a0
    b=x-a
    a0=a
    break
    
print(c)

Submission status: Wrong Answer

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

x = len(s)
y = len(t)

a = 0
b = x
c = 0
a0 = 0


for i in range(0,y):
  while True:
    #先頭からの位置
    a=s.find(t[i:i+1])+1
    if a <= 0:
      print("-1")
      exit()
    
    if a<a0:
      c=c+a+b
    else:
      c=c+a-a0
    b=x-a
    a0=a
    break
    
print(c)

Submission status: Wrong Answer

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

#s = "hello"
#t = "lheo"
s_line = s * 100

s_num = len(s)
t_num = len(t)
t_list = list(t)
ti = 0

for i in range(len(s_line)):
  if s_line[i] == t_list[ti]:
    ti += 1
    if ti == t_num:
      i += 1
      break
  if (s_num == i) and (ti == 0):
    i = -1
    break

print(i)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
before = -1
len_s = len(s)
ans = 0
cnt = 0
moji = [[] for _ in range(26)]
for i,S in enumerate(s):
        moji[ord(S) - 97].append(i)

for T in t:
        if not moji[ord(T) - 97]:
                print(-1)
                exit()
        if moji[ord(T) - 97][-1] <= before:
                cnt += 1
                before = moji[ord(T) - 97][0]
        for num in moji[ord(T) - 97]:
                if num > before:
                        before = num
                        break
ans = cnt * len_s + before + 1
print(ans)

Submission status: Wrong Answer

Solution code:
S = list(input())
T = list(input())
from collections import defaultdict
S_dict=defaultdict(list)
if set(T) <= set(S):
    for i,s in enumerate(S):
        if s in T:
            S_dict[s].append(i+1)
    ans = 0
    idx = 0
    for t in T:
        if max(S_dict[t]) < idx:
            ans += len(S) - idx + S_dict[t][0]
            idx = S_dict[t][0]
        else:
            for point in S_dict[t]:
                if point > idx:
                    ans += point - idx
                    idx = point
                    break
    print(ans)
else:
    print(-1)

Submission status: Wrong Answer

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

c=0
j=-1
k=1

#print(len(s))

#t
for i in range(0,len(t)):
  while True:
    j=j+1
    c=c+1
#    print(j)
#    print(c)
    
    if t[i:i+1]==s[j:j+1]:
#      print("hit")
      #kはもう一度一文字めからカウントし直し
      k=1
      break
    else:
      #hitしなかった場合、index　カウントアップ
      k=k+1
      if j>=len(s)-1:
        #jがお尻まで行ったら先頭に戻る
        j=-1
      #一周回ってもヒットしなかったらエラー
      if k>len(s)+1:
        print("-1")
        exit()
print(c)

Submission status: Wrong Answer

Solution code:
s = input()
t = input()
before = -1
len_s = len(s)
ans = 0
cnt = 0
for T in t:
        if T not in s:
                print(-1)
                exit()
        now = s.index(T)
        if now <= before:
                cnt += 1
        before = now
ans = cnt * len_s + now + 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

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

for i in range(len(t)):
    if t[i] not in s:
        print(-1)
        sys.exit()

i = 0
count = 0

for j in range(35):

    
    for k in range(len(s)):
        if count == len(t):
            print(k +  (j * len(s)))
            sys.exit()
            
        #print(a[k +  (j * len(s))], t[count],a, count)
        if s[k] == t[count]:
            count += 1