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.

Solution in Rust (1.15.1) (judge verdict: Wrong Answer)

#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}
 
#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};
 
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
 
    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}
 
#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };
 
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
 
    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };
 
    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };
 
    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };
 
    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

use std::collections::HashSet;
use std::collections::HashMap;

fn main() {
   input! {
       s: chars,
       t: chars,
   }

   //make t table
    let mut t_list = vec![];
    let mut set = HashSet::new();
    let mut before = t[0];
    let mut count = 1;

   for e in t.iter().skip(1) {
       if before == *e {
           count = count + 1;
           continue;
       } else {
        t_list.push((before, count));
        set.insert(before);
        count = 1;
        before = *e;
       }
   }
   t_list.push((before, count));
   set.insert(before);
   

   // make s table
   let mut s_map = HashMap::<char, Vec<usize>>::new();

   for e in &set {
       s_map.insert(*e, Vec::new());
   }
   for (i, e) in s.iter().enumerate() {
       if s_map.contains_key(e) {
           s_map.get_mut(e).unwrap().push(i);
       }
    } 

   //possible check
   for (_k, v) in &s_map {
       if v.len() == 0 {
          println!("-1");
          return; 
       }
   }
/*
   for (k, v) in &s_map {
       println!("k {} v{:?}" ,k ,v);
   }
   for e in &t_list {
       println!("{:?}", e);
   }
*/
    //solve
    let mut pos = 0;
    let mut tmp;
    let mut index;
    let mut geta = 0;

    let mut sum = t.len();
    //println!("s len {}", s.len());
    while sum != 0 {
        'here:
        for e in t_list.iter_mut() {
            index = 0;
            while index < s_map[&e.0].len() && e.1 != 0 {
                tmp = s_map[&e.0][index] + geta;
                index = index + 1;
                if pos <= tmp {
                    pos = tmp;
                    //println!("pos : {}", pos);
                    e.1 = e.1 - 1;
                    sum = sum - 1;
                } else {
                    break 'here;
                }
            }
        }
        geta = geta + s.len();
    }

    println!("{}", pos+1);
}