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: Runtime Error

Solution code:
s = input()
t = input()
c = 0
pre = 0
rep = 0
dict = {}
for i, w in enumerate(s):
    if not w in dict.keys():
        dict[w] = []
    dict[w].append(i+1)
for n in t:
    if len(dict[n]) < 1:
        c = -1
        break
    if pre > dict[n][0]:
        rep += 1
    pre = dict[n][0]
    if len(dict[n]) > 1:
        num = dict[n].pop(0)
        dict[n].append(num)
if c == -1:
    print(-1)
else:
    print(len(s) * rep + max(dict[t[-1]]))

Submission status: Runtime Error

Solution code:
s = input()
t = input()
alphabet = 'abcdefghijklmnopqrstuvwxyz'
pre = 0
rep = 0
dict = {word:[] for word in alphabet}
for i, w in enumerate(s):
    dict[w].append(i+1)
for n in t:
    if len(dict[n]) < 1:
        print(-1)
        break
    if pre > dict[n][0]:
        rep += 1
    pre = dict[n][0]
    if len(dict[n]) > 1:
        num = dict[n].pop(0)
        dict[n].append(num)
print(len(s) * rep + max(dict[t[-1]]))

Submission status: Runtime Error

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

def func(S, T):

    S_2 = S+S
    len_s = len(S)
    merge_S = list(set(S))
    merge_T = list(set(T))
    alp2num = {t: merge_T.index(t) for t in merge_T}
    dis_S = [[-1]*len(merge_T) for _ in range(len_s)]
    pt = S.index(T[0])
    ans = pt+1

    for t in merge_T:
        if t not in merge_S:
            return -1

    for t in T[1:]:
        it = alp2num[t]
        if dis_S[pt][it] == -1:
            dis_S[pt][it] = S_2[pt+1:].index(t) + 1
        ans += dis_S[pt][it]
        pt = (pt + dis_S[pt][it]) % len_s

    return ans

print(func(S,T))

Submission status: Runtime Error

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

go = True
index_s = 0
length = 0

can = True
for ith_t in t:
    if not ith_t in s:
        can = False
        break
if can:
    for ith_t in t:
        length += min((i-length)%len(s) if t[i] == ith_t else len(s) for i in range(len(s)))

print(length-1 if can else -1)

Submission status: Runtime Error

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

including = True
for x in t:
    if x  not in s:
        including = False
        break
if not including:
    print(-1)
    return


x = 0
y = 0
while True:
    index = x % len(s)
    if s[index] == t[y]:
        y += 1
        if y == len(t):
            break
    x += 1

print(x + 1)

Submission status: Runtime Error

Solution code:
def ssearch(c, p):
  
  if p >= len(s):
    p = p % len(s)
  
  key = c + str(p)

  if(key in dic):
    pass
  elif s2[p] == c:
    dic[key] = 1
  else:
    dic[key] = ssearch(c, p+1) + 1

#  print('DEBUG '+key+' '+str(dic[key]))
    
  return dic[key]

s  = input()
s2 = s + s
t  = input()

ptr = 0
cnt = 0
dic = {}
for i in range(len(t)):
  if t[i] in s:
    cnt += ssearch(t[i], ptr)
    ptr  = cnt % len(s)
#    print('DEBUG2 '+ str(len(s)) + ' ' + str(ptr))
  else:
    cnt = -1
    break
  
print(cnt)

Submission status: Runtime Error

Solution code:
def ssearch(c, p):
  key = c + str(p)

  if(key in dic):
    pass
  elif s3[p] == c:
    dic[key] = 1
  else:
    dic[key] = ssearch(c, p+1) + 1

#  print('DEBUG '+key+' '+str(dic[key]))
    
  return dic[key]

s  = input()
s2 = s + s
s3 = s2 + s
t  = input()

ptr = 0
cnt = 0
dic = {}
for i in range(len(t)):
  if t[i] in s:
    cnt += ssearch(t[i], ptr)
    ptr  = cnt % len(s)
#    print('DEBUG2 '+ str(len(s)) + ' ' + str(ptr))
  else:
    cnt = -1
    break
  
print(cnt)

Submission status: Runtime Error

Solution code:
def ssearch(c, p):
  key = c + str(p)

  if(key in dic):
    pass
  elif s2[p] == c:
    dic[key] = 1
  else:
    dic[key] = ssearch(c, p+1) + 1

#  print('DEBUG '+key+' '+str(dic[key]))
    
  return dic[key]

s  = input()
s2 = s + s
t  = input()

ptr = 0
cnt = 0
dic = {}
for i in range(len(t)):
  if t[i] in s:
    cnt += ssearch(t[i], ptr)
    ptr  = cnt % len(s)
#    print('DEBUG2 '+ str(len(s)) + ' ' + str(ptr))
  else:
    cnt = -1
    break
  
print(cnt)

Submission status: Runtime Error

Solution code:
b=input().strip()
a=input().strip()
if (b and a) and ((len(a)<=100000) and (len(b)<=100000)):
    a=a.lower()
    b=b.lower()
    e=list(i for i  in set(a) if i in set(b))
    e=str(''.join(sorted(e)))
    c=''.join(sorted(set(a)))
    if e == c:
        i=0
        j=0
        count=0
        while True:
            if a[i] == b[j%7]:
                i+=1
                j+=1
                if i == (len(a)):
                    print(j%1000000007)
                    break
            else:
                j+=1
                count+=1
    else:
        print(-1)

Submission status: Runtime Error

Solution code:
b=input().strip()
a=input().strip()
if (b and a) and ((len(a)<=100000) and (len(b)<=100000)):
    a=a.lower()
    b=b.lower()
    e=list(i for i  in set(a) if i in set(b))
    e=str(''.join(sorted(e)))
    c=''.join(sorted(set(a)))
    if e == c:
        i=0
        j=0
        count=0
        while True:
            if a[i] == b[j%7]:
                i+=1
                j+=1
                if i == (len(a)):
                    print(j)
                    break
            else:
                j+=1
                count+=1
    else:
        print(-1)

Submission status: Runtime Error

Solution code:
# E - Strings of Impurity ★
from collections import defaultdict

s = input()
t = input()

if (set(s) & set(t)) != set(t):
    print('-1')
else:
    sChar = list(s)
        
    str = s
    preIndex = -1
    for c in list(t):
        nowIndex = str.find(c, preIndex + 1)
        while nowIndex == -1:
            str += s
            nowIndex = str.find(c, preIndex + 1)
        preIndex = nowIndex

    print(preIndex + 1)

Submission status: Runtime Error

Solution code:
b=input().strip()
a=input().strip()
if b and a:
    e=list(i for i  in set(a) if i in set(b))
    e=str(''.join(sorted(e)))
    c=''.join(sorted(set(a)))
    if e == c:
        i=0
        j=0
        count=0
        while True:
            if a[i] == b[j%7]:
                i+=1
                j+=1
                if i == (len(a)):
                    print(j)
                    break
            else:
                j+=1
                count+=1
    else:
        print(-1)

Submission status: Runtime Error

Solution code:
b=input().strip()
a=input().strip()
#c=set(a)
#d=set(b)
e=list(i for i  in set(a) if i in set(b))
e=str(''.join(sorted(e)))
c=''.join(sorted(set(a)))
if e == c:
    i=0
    j=0
    count=0
    while True:
        if a[i] == b[j%7]:
            i+=1
            j+=1
            if i == (len(a)):
                print(j)
                break
        else:
            j+=1
            count+=1
else:
    print(-1)

Submission status: Runtime Error

Solution code:
import sys
S = input()
t = list(input())
s = list(S)*len(t)
ans = 0
for i in t:
    try:
        num = s.index(i)
    except ValueError:
        print(-1)
        sys.exit()
    del s[:num+1]
    ans += num+1
print(ans)

Submission status: Runtime Error

Solution code:
import sys
S = input()
t = list(input())
s = list(S)*len(t)
ans = 0
for i in t:
    try:
        num = s.index(i)
        print(num)
    except ValueError:
        sys.exit()
    del s[:num+1]
    ans += num+1
print(ans)

Submission status: Runtime Error

Solution code:
import sys

s = input()
t = input()

len_s = len(s)

atoz = 'abcdefghijklmnopqrstuvwxyz'

next_char_indices = dict()  # [(idx, char): next_idx]
last_indices = {k: -1 for k in atoz}
start_idx = -1
for i, c in enumerate(reversed(s*2)):
    idx = len_s * 2 - 1 - i
    if c == t[0]:
        start_idx = idx
    if idx < len_s:
        for key in atoz:
            next_char_indices[(idx, key)] = last_indices[key]
    last_indices[c] = idx % len_s

ans = start_idx + 1
idx = start_idx
for c in t[1:]:
    next_idx = next_char_indices.get((idx, c), -1)
    if next_idx < 0:
        print(-1)
        sys.exit(-1)
    ans += next_idx - idx
    if next_idx < idx:
        ans += len_s
    idx = next_idx

print(ans)

Submission status: Runtime Error

Solution code:
import sys
import bisect
sys.setrecursionlimit(200000)
input = sys.stdin.readline

S = input().strip()
S = S * 100000
T = input().strip()
l = len(S)

m = {}
for i in range(l):
    c = S[i]
    if m.get(c) == None:
        m[c] = []
    m[c].append(i)

a = [None] * len(T)
for i in range(len(T)):
    if m.get(T[i]) == None:
        print(-1)
        exit()
    a[i] = m[T[i]]

before = min(a[0])
bl = 0
for v in a[1:]:
    index = bisect.bisect_right(v, before)
    if before >= bl * len(S) + v[index]:
        bl += 1
    before = bl * len(S) + v[index] 

print(before + 1)

Submission status: Runtime Error

Solution code:
use std::io::stdin;
use std::time::SystemTime;
use std::collections::HashMap;

#[allow(dead_code)]
fn read_line() -> String {
    let mut s = String::new();
    stdin().read_line(&mut s).unwrap();
    s.trim().to_string()
}

#[allow(dead_code)]
fn read<T>() -> T
where T: std::str::FromStr
{
    read_line().parse::<T>().ok().unwrap()
}

#[allow(dead_code)]
fn read_vec<T>() -> Vec<T>
where T: std::str::FromStr
{
    read_line().split(" ").map(|n| n.parse::<T>().ok().unwrap()).collect()
}

#[allow(dead_code)]
fn read_chars() -> Vec<char>
{
    read_line().chars().collect()
}

#[allow(dead_code)]
fn print_time() -> () {
    println!("{:?}", SystemTime::now());
}

trait BinarySearchLeft<T> {
    fn binary_search_left(&self, x: &T) -> Result<usize, usize>;
}

impl<T> BinarySearchLeft<T> for Vec<T>
where T: std::cmp::Ord + std::fmt::Debug
{
    fn binary_search_left(&self, x: &T) -> Result<usize, usize> {
        let index = self.binary_search(x);
        match index {
            Ok(index) => {
                for i in (1..index+1).rev() {
                    if self[i] != self[i - 1] {
                        return Ok(i - 1)
                    }
                }
                
                Ok(index)
            },
            _ => index,
        }
    }
}

#[allow(unused_variables)]
fn main() {
    let s = read_chars();
    let t = read_chars();

    for p in t.iter() {
        if !s.contains(p) {
            println!("-1");
            return
        }
    }

    let mut index = 0;
    let mut counter = 0;
    let mut ff = false;
    let mut it = t.iter();
    let mut c = it.next();
    while true {
        if ff {
            ff = false;
            c = it.next();
        }

        if let Some(c) = c {
            let mut f = true;
            for v in s.iter().skip(index) {
                index += 1;
                if c == v {
                    f = false;
                    ff = true;
                    break;
                }
            }
            if f {
                index = 0;
                counter += 1;
            }
        } else {
            break;
        }
    }

    println!("{}", counter * s.len() + index);
}

Submission status: Runtime Error

Solution code:
import sys
from collections import defaultdict

def bs(arr, val):
	l, h = 0, len(arr)-1
	ans = -1
	while l <= h:
		m = l + h >> 1
		if arr[m] > val:
			ans = m
			h = m - 1
		else:
			l = m + 1
	return ans

s = sys.stdin.readline()
n = len(s)

t = sys.stdin.readline()

occurence = defaultdict(list)
for i in range(n):
	occurence[s[i]].append(i+1)

folds = 0
cur_ptr = 0
for ele in t:
	indexes = occurence[ele]
	idx = bs(indexes, cur_ptr)
	if idx != -1: 
		cur_ptr = indexes[idx]
	else:
		folds += 1
		cur_ptr = indexes[0]
ans = folds*(n) + cur_ptr
print(ans)

Submission status: Runtime Error

Solution code:
s1 = input()
l = input()
l1 = len(s1)
sa = set(s1)
la = set(l)
if la.issubset(sa):
    j,su = 0,0
    d,li = {},[]
    for i in range(27):
        d[i] = []
    for i in range(l1):
        d[ord(s1[i])-ord('a')].append(i+1)
    for i in range(l1):
        d[ord(s1[i])-ord('a')].append(0)
    #print(d)
    v1,v2 = 0,0
    for i in l:
        v1 = d[ord(i)-ord('a')][j]
        if v1==0:
            j=0
            v1 = d[ord(i)-ord('a')][j]
        if v1<v2:
            j=0
            su+=l1-v2+v1
        else:
            j+=1
            su+=v1-v2
        v2 = v1
    print(su)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
s1 = input()
l = input()
l1 = len(l)
s = s1*10000
#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: Runtime Error

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 == ingredientx_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: Runtime Error

Solution code:
n, q = map(int, input().split())
e = [[int(i) for i in input().split()] for i in range(n-1)] 
p = [[int(i) for i in input().split()] for i in range(q)]

point = [0] * n

for i in range(q):
  point[p[i][0]-1] += p[i][1]

for j in range(n-1):
  point[e[j][1]-1] += point[e[j][0]-1]
  
for h in range(n):
    print(point[h], end = ' ')

Submission status: Runtime Error

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

t_ls = sorted(set(t))

c_dict = {}
    
# 重複を調べる。
for c in t_ls:
    if c not in s:
        print(-1)
        exit()
ind = 0
loop = 0

for c in t:
    try:
        i = s[ind+1:].index()
        ind = i
    except ValueError:
        ind = 0
        loop += 1
        i = s[ind+1:].index()
        ind = i


print(loop*len(s)+ind+1)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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
n = 0
for i in range(len(s)):
    if len(s) == i + n:
        break
    
    if s[i] not in t:
        s.pop(i)
        n += 1

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

print(-1)

Submission status: Runtime Error

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

Submission status: Runtime Error

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
n = 0
for i in range(len(s)):
    if len(s) == i + n + 1:
        break
    
    if s[i] not in t:
        s.pop(i)
        n += 1

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

Submission status: Runtime Error

Solution code:
S = list(input())
T = list(input())
from collections import defaultdict
S_dict=defaultdict(list)
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)

Submission status: Runtime Error

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
a = []
count = 0

for j in range(35):
    for i in range(len(s)):
        a.append(s[i])
    i = 0
    
    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 a[k +  (j * len(s))] == t[count]:
            count += 1

Submission status: Runtime Error

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

c=1
j=0
k=0

#t
for i in range(0,len(t)):
  while True:
    if t[i:i+1]==s[j:j+1]:
      J=J+1
      k=0
      break
    else:
      if j==len(s):
        j=0
      else:
        j=j+1
        k=k+1
        c=c+1
        if k+1==len(s):
          print("-1")
          exit()
        else:
          j=j+1
          k=k+1
          c=c+1
print(c)

Submission status: Runtime Error

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

c=1
j=0
k=0

#t
for i in range(0,len(t)):
  flag = 0
  while True:
    if t[i:i+1]==s[j:j+1]:
      J=J+1
      k=0
      break
    else:
      if j==len(s):
        j=0
      else:
        j=j+1
        k=k+1
        c=c+1
        if k+1==len(s):
          print("-1")
          exit()
        else:
          j=j+1
          k=k+1
          c=c+1
print(c)

Submission status: Runtime Error

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

c=1
j=0
k=0

flag = 0

#t
for i in range(0,len(t)):
  print("i",i)
  flag = 0
  while True:
    print("j",j) 
    if t[i:i+1]==s[j:j+1]:
      J=J+1
      flag = 1
      k=0
      break
    else:
      if j==len(s):
        j=0
      else:
        j=j+1
        k=k+1
        c=c+1
        if k+1==len(s):
          print("-1")
          exit()
        else:
          j=j+1
          k=k+1
          c=c+1
print(c)

Submission status: Runtime Error

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

wi = {}
for w in range(s_l):
  for n, ws in enumerate(ss[w+1:w+s_l]):
    if not wi.get(w):
      wi[w] = {ws:[n+1]}
    else:
      if not wi[w].get(ws):
        wi[w][ws] = [n+1]
      else:
        wi[w][ws].append(n+1)

ans = 0
x = 0
for wt in t:
  if wt == t[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: Runtime Error

Solution code:
from bisect import bisect_right

s = input()
t = input()

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

for i in range(2*len(s)):
        if i<len(s):
                char_list[ord(s[i])-97].append(i)
        else:
                char_list[ord(s[i-len(s)])-97].append(i)

INF = 10**27

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

next_list = [[char_list[j][bisect_right(char_list[j],i)] for j in range(26)] for i in range(len(s))]

flag = False
for i in range(len(s)):
        if t[0]==s[i]:
                flag=True
                x = i
                y = i
                break
if flag:
        flag2 = True
        for i ina range(1,len(t)):
                n = next_list[x][ord(t[i])-97]
                y += n - x
                x = n%(len(s))
                if y >= INF:
                        flag2=False
                        break
        if flag2:
                print(y+1)
        else:
                print(-1)
else:
        print(-1)

Submission status: Runtime Error

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

s = str(input())
t = str(input())
s_modified = ""


for _ in range(100):
	s_modified += s[:-1]

# print(s_modified)	

T = len(t)
p = 0 


for  i in range(T-1):
	if t[i] in s == False :
		print(-1)
		break
	else:
		if p > len(s_modified) - len(s):
			for _ in range(100):
				s_modified += s[:-1]

		p += s_modified[p:].index(t[i]) + 1
print(p)

Submission status: Runtime Error

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

s = str(input())
t = str(input())
s_modified = ""


for _ in range(100):
	s_modified += s[:-1]

T = len(t)
p = 0 


for  i in range(T-1):
	if t[i] in s == False :
		print(-1)
		break
	else:
		if p > len(s_modified) - len(s):
			s_modified += s[:-1]

		p += s_modified[p:].index(t[i]) + 1
print(p)

Submission status: Runtime Error

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

s = str(input())
t = str(input())
s_modified = ""


for _ in range(1000000):
	s_modified += s[:-1]

# print(s_modified)	

T = len(t)
p = 0 


for  i in range(T-1):
	if t[i] in s == False :
		print(-1)
		break
	else:
		p += s_modified[p:].index(t[i]) + 1
print(p)

# print("答えは{}".format(p))

Submission status: Runtime Error

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

s = str(input())
t = str(input())

for _ in range(10**100):
	s_ += s


T = len(t)
p = 0 
counter = 0

for  i in range(T):
	if t[i] in s == False :
		print(-1)
		break
	else:
		p += s[p:].index(t[i]) + 1

print(p)

Submission status: Runtime Error

Solution code:
from bisect import bisect

s = input()
t = input()

if len(set(t) & set(s)) != len(set(t)):
    print(-1)
    exit()

d = dict()

for i, si in enumerate(s):
    if si in d:
        d[si].append(i)
    else:
        d[si] = [i]

ans = 0
res = d[t[0]][0]

for i in range(1, len(t)):
    index = bisect(d[t[i]], res)
    if index == len(d[t[i]]):
        ans += len(s)
        res = d[t[i]][0]
    else:
        res = d[t[i]][index]

if index == len(d[t[-1]]):
    ans += d[t[i]][0] + 1
else:
    ans += d[t[i]][index] + 1

print(ans)

Submission status: Runtime Error

Solution code:
from bisect import bisect

s = input()
t = input()

d = dict()

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

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

ans = 0
res = -1

for i in range(len(t)):
    if d[t[i]] == []:
        print(-1)
        exit()
    index = bisect(d[t[i]], res)
    if index == len(d[t[i]]):
        ans += len(s) - res + d[t[i]][0]
        res = d[t[i]][0]
    else:
        ans += d[t[i]][index] - res
        res = d[t[i]][index]

print(ans)

Submission status: Runtime Error

Solution code:
from bisect import bisect

s = input()
t = input()

d = dict()

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

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

ans = 0
res = -1

for i in range(len(t)):
    if d[t[i]] == []:
        print(-1)
        exit()
    index = bisect(d[t[i]], res)
    if index == len(d[t[i]]):
        ans += len(s)
        res = d[t[i]][0]
    else:
        res = d[t[i]][index]

ans += d[t[-1]][0] + 1

print(ans)

Submission status: Runtime Error

Solution code:
import bisect

s = input()
t = input()
T = set(t)
s_num = len(s)
t_num = len(T)

# make a dictonary whose keys are the alphabets in t
dict = {}
appearing_times = {}

# when each alphabet in t appears in s
for w in T:
    dict[w] = []
    appearing_times[w] = 0
    index = -1
    while True:
        index = s.find(w, index + 1)
        if index == -1 and appearing_times[w] == 0:
            print(-1)
            exit()
        elif index == -1:
            break
        else:
            dict[w].append(index)
            appearing_times[w] += 1

# how many times s is repeated
count = 0
# 0 <= length <= s_num - 1
s_index = 0
check = 0

for w in t:
    idx = bisect.bisect_left(dict[w], s_index)
    if check == 'done' and s_index == dict[w][idx]:
        idx = idx + 1

    if idx < appearing_times[w]:
        s_index = dict[w][idx]
    else:
        count += 1
        s_index = dict[w][0]

    check = 'done'
print(count * s_num + s_index + 1)

Submission status: Runtime Error

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

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

#二分木探索
def search(list_,mark_,sta,gol):
    if  sta == gol:
            return list_[sta]
    else:
            mid = (gol-sta)//2
            if list_[mid]>mark_:
                return search(list_,mark_,sta,mid)
            else:
                return search(list_,mark_,mid+1,gol)

ans = 0
mark = 0
for _ in range(len(t)):
    target = t[_]
    if target not in sdict:
        ans = -1
        break
    else:
        if sdict[target][-1]<=mark:
            mark = sdict[target][0]
            ans += len(s)
        elif sdict[target][0]>mark:
            mark = sdict[target][0]
        else:
            posi = search(sdict[target],mark,0,len(sdict[target])-1)
            mark = posi
        
if ans != -1:
    ans += mark
print(ans)

Submission status: Runtime Error

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

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

#二分木探索
def search(list_,mark_,sta,gol):
    if  sta == gol:
            return list_[sta]
    else:
            mid = (gol-sta)//2
            if list_[mid]>mark_:
                return search(list_,mark_,sta,mid)
            else:
                return search(list_[mid:],mark_,mid+1,gol)

ans = 0
mark = 0
for _ in range(len(t)):
    target = t[_]
    if target not in sdict:
        ans = -1
        break
    else:
        if sdict[target][-1]<=mark:
            mark = sdict[target][0]
            ans += len(s)
        elif sdict[target][0]>mark:
            mark = sdict[target][0]
        else:
            posi = search(sdict[target],mark,0,len(sdict[target])-1)
            mark = posi
        
if ans != -1:
    ans += mark
print(ans)

Submission status: Runtime Error

Solution code:
s = input()
ss = ''.join([s,s])
t = input()
ans = 0
count = 0
dp = [0 for _ in range(len(t))]

for i in range(len(t)):
    if t[i] in s:
        for j in range(len(s)):
            if ss[j+dp[i-1]%7]==t[i]:
                if i==0:
                    dp[i] = j+1
                    break
                else:
                    dp[i] = dp[i-1] + j + 1
                    break
    else:
        ans = -1
        break
#print(dp)
if ans == -1:
    print(ans)
else:
    print(dp[len(t)-1])

Submission status: Runtime Error

Solution code:
s = input()
ss = ''.join([s,s])
t = input()
ans = 0
count = 0
dp = [0 for _ in len(t)]

for i in range(len(t)):
    if t[i] in s:
        for j in range(len(s)):
            if ss[j+dp[i-1]%7]==t[i]:
                if i==0:
                    dp[i] = j+1
                    break
                else:
                    dp[i] = dp[i-1] + j + 1
                    break
    else:
        ans = -1
        break
#print(dp)
if ans == -1:
    print(ans)
else:
    print(dp[len(t)-1])

Submission status: Runtime Error

Solution code:
import sys
input = lambda: sys.stdin.readline()[:-1]
from collections import Counter
from collections import defaultdict
s =input()
lenth=len(s)
ss=s*2
t =input()
place=defaultdict(list)
guide=Counter()
flag=0
for k,l in enumerate(s):
	for word in range(97, 97+26):
		word=chr(word)
		if word==l:
			place[word].append(k)
			place[word].append(k+lenth)
			flag=1

now=0
ans=0
for i in t:
	if not place[i]:
		print(-1)
		exit()
	mt=min((j for j in place[i] if j>now))
	if mt>lenth:
		mt-=lenth
		ans+=lenth
	now=mt
print(ans+now+1)

Submission status: Runtime Error

Solution code:
from collections import Counter
from collections import defaultdict
s =input()
lenth=len(s)
ss=s*2
t =input()
place=defaultdict(set)
guide=Counter()
flag=0
for k,l in enumerate(s):
	for word in range(97, 97+26):
		word=chr(word)
		if word==l:
			place[word].add(k)
			place[word].add(k+lenth)
			flag=1

now=0
ans=0
for i in t:
	if not place[i]:
		print(-1)
		exit()
	mt=min((j for j in place[i] if j>now))
	if mt>lenth:
		mt-=lenth
		ans+=lenth
	now=mt
print(ans+now+1)

Submission status: Runtime Error

Solution code:
import bisect
import sys
sys.setrecursionlimit(10**7)

s = input()
t = input()

abc = "abcdefghijklmnopgrstuvwxyz"

p = {}

for i in abc:
  p[i] = []

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

for i in abc:
  p[i].sort()

l = 0
c = -1

flg = True

for i in t:
  tar = p[i]
  if(len(tar) == 0):
    flg = False
    break
  index = bisect.bisect_left(tar,c)

  if(index == len(tar)):
    index = 0
    l += 1

  elif(c == tar[index]):
    l += 1
  c = tar[index]


if(flg):
  print(l*len(s) + c + 1)

else:
  print("-1")