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 C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Linq;
using System.Collections.Generic;

namespace Contest
{
    class Program
    {
        static void Main(string[] args)
        {
            ABC138.E();
        }
        class ABC138
        {
            public static void A()
            {
                var A = Read.Int();
                var S = Read.Str();
                Console.WriteLine(A >= 3200 ? S : "red");
            }
            public static void B()
            {
                var N = Read.Int();
                var A = Read.IntN();
                Console.WriteLine(1.0 / A.Select(x => 1.0 / x).Sum());
            }
            public static void C()
            {
                var N = Read.Int();
                var V = Read.IntN().OrderBy(x => x).ToArray();
                double ans = V[0];
                for(int i = 1; i < N; ++i)
                {
                    ans = (ans + V[i]) / 2.0;
                }
                Console.WriteLine(ans);
            }
            public static void D()
            {
                var N = Read.Int();
                var Q = Read.Int();
                var Adj = Read.AdjacencyList(N, N - 1);
                var PX = Read.TupleInt(Q);
                var X = new long[N + 1];
                foreach(var px in PX)
                {
                    X[px.Item1] += px.Item2;
                }
                var res = Enumerable.Range(0, N + 1).Select(i => -1L).ToArray();
                var queue = new Queue<Tuple<int, long>>();
                queue.Enqueue(new Tuple<int, long>(1, X[1]));
                while (queue.Count > 0)
                {
                    var t = queue.Dequeue();
                    var v = t.Item1;
                    var x = t.Item2;
                    res[v] = x;
                    foreach(var w in Adj[v])
                    {
                        if (res[w] >= 0) { continue; }
                        queue.Enqueue(new Tuple<int, long>(w, x + X[w]));
                    }
                }
                Console.WriteLine(String.Join(" ", res.Skip(1)));
            }
            public static void E()
            {
                var S = Read.Str();
                var T = Read.Str();
                var N = S.Length;
                var next = Enumerable.Range(0, 26).Select(i => Enumerable.Range(0, N).Select(j => -1).ToArray()).ToArray();
                var idx = new int[26];
                var SS = S + S;
                for(int i = 1; i < N * 2; ++i)
                {
                    var c = SS[i] - 'a';
                    for(int j = idx[c]; j < Math.Min(N, i); ++j)
                    {
                        next[c][j] = i;
                    }
                    idx[c] = i;
                }
                long current = 0;
                long count = 0;
                foreach(var t in T)
                {
                    current = next[t - 'a'][current];
                    if (current < 0) { Console.WriteLine(-1); return; }
                    if (current >= N)
                    {
                        count++;
                        current -= N;
                    }
                }
                Console.WriteLine(count * N + current + 1);
            }
            public static void F()
            {
            }
            static MOD F_Calc(long K)
            {
                var P3 = new MOD[60];
                var ans = (MOD)0;
                P3[0] = 0;
                P3[1] = 1;
                P3[2] = 4;
                for(int i = 0; i < 60; ++i)
                {
                    if (i > 2) { P3[i] = P3[i - 1] * 3 + 1; }
                    if ((K & (1L << i)) > 0)
                    {
                        ans = P3[i] + ans * 2 + 1;
                    }
                }
                return ans.V;
            }
        }



        class Util
        {


        }
        struct MOD
        {
            public static long P = 1000000007;
            public readonly long V;

            public MOD(long a) { this.V = a; }
            public static implicit operator MOD(long a) { return new MOD(a); }
            public static MOD operator +(MOD a, MOD b) { return new MOD((a.V + b.V) % P); }
            public static MOD operator -(MOD a, MOD b) { return new MOD((a.V - b.V + P) % P); }
            public static MOD operator *(MOD a, MOD b) { return new MOD((a.V * b.V) % P); }
            public static MOD operator /(MOD a, MOD b) { return new MOD((a.V * b.Inverse().V) % P); }
            //public MOD Inverse() { return Util.Inverse(V, P); }
            public MOD Inverse()
            {
                long a = V, p = P;
                long b = p, c = 1, d = 0, e = 0, f = 0;
                while (b != 0)
                {
                    e = a / b;
                    f = b; b = a - e * b; a = f;
                    f = d; d = c - e * d; c = f;
                }
                c %= p;
                return c + (c < 0 ? p : 0);
            }
            //public MOD Power(long n) { return Util.Power(V, n, P); }
            public MOD Power(long n)
            {
                long a = V, p = P;
                long res = 1;
                long mult = a;
                for (int i = 0; i < 40; ++i)
                {
                    if ((n & ((long)1 << i)) > 0)
                    {
                        res = (res * mult) % p;
                    }
                    mult = (mult * mult) % p;
                }
                return res;
            }

            public override string ToString() { return V.ToString(); }
        }


        static class Read
        {
            static Queue<string> queue = new Queue<string>();
            static void Enqueue() { var ss = Console.ReadLine().Split(' '); foreach (var s in ss) { queue.Enqueue(s); } }
            public static int Int() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt32(queue.Dequeue()); }
            public static long Long() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt64(queue.Dequeue()); }
            public static string Str() { if (queue.Count == 0) { Enqueue(); } return queue.Dequeue(); }
            public static int[] IntN() { return Console.ReadLine().Split(' ').Select(s => Convert.ToInt32(s)).ToArray(); }
            public static long[] LongN() { return Console.ReadLine().Split(' ').Select(s => Convert.ToInt64(s)).ToArray(); }
            public static string[] StrN() { return Console.ReadLine().Split(' '); }
            public static Tuple<int, int>[] TupleInt(int n) { List<Tuple<int, int>> list = new List<Tuple<int, int>>(); for (int i = 0; i < n; ++i) { var ab = Read.IntN(); list.Add(new Tuple<int, int>(ab[0], ab[1])); } return list.ToArray(); }
            public static Tuple<long, long>[] TupleLong(int n) { List<Tuple<long, long>> list = new List<Tuple<long, long>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<long, long>(ab[0], ab[1])); } return list.ToArray(); }
            public static Tuple<string, string>[] TupleStr(int n) { List<Tuple<string, string>> list = new List<Tuple<string, string>>(); for (int i = 0; i < n; ++i) { var ab = Read.StrN(); list.Add(new Tuple<string, string>(ab[0], ab[1])); } return list.ToArray(); }
            public static Tuple<double, double>[] TupleDouble(int n) { List<Tuple<double, double>> list = new List<Tuple<double, double>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<double, double>(ab[0], ab[1])); } return list.ToArray(); }
            public static long[,] LongMatrix(int r, int c)
            {
                var mat = new long[r, c];
                for (int i = 0; i < r; ++i)
                {
                    var x = Read.LongN();
                    for (int j = 0; j < c; ++j)
                    {
                        mat[i, j] = x[j];
                    }
                }
                return mat;
            }
            public static Dictionary<int, HashSet<int>> AdjacencyList(int N, int M, bool isDirectional = false)
            {
                Dictionary<int, HashSet<int>> dict = new Dictionary<int, HashSet<int>>();
                for (int i = 0; i <= N; ++i) { dict.Add(i, new HashSet<int>()); }
                for (int i = 0; i < M; ++i)
                {
                    var ab = Read.IntN(); dict[ab[0]].Add(ab[1]);
                    if (!isDirectional) { dict[ab[1]].Add(ab[0]); }
                }
                return dict;
            }
            public static Dictionary<int, Dictionary<int, long>> AdjacencyListWithWeight(int N, int M)
            {
                Dictionary<int, Dictionary<int, long>> dict = new Dictionary<int, Dictionary<int, long>>();
                for (int i = 0; i <= N; ++i)
                {
                    dict.Add(i, new Dictionary<int, long>());
                }
                for (int i = 0; i < M; ++i)
                {
                    var ab = Read.IntN();
                    dict[ab[0]].Add(ab[1], ab[2]);
                    dict[ab[1]].Add(ab[0], ab[2]);
                }
                return dict;
            }
        }
    }
}