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: Accepted)

#if DEBUG
using Microsoft.VisualStudio.TestTools.UnitTesting;
#endif

using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;

namespace Contest
{
    class Program
    {
        static void Main(string[] args)
        {
            var scanner = new IO.StreamScanner(Console.OpenStandardInput());
            var s = scanner.ScanLine();
            var t = scanner.ScanLine();

            var ans = GetAns(s, t);
            IO.Printer.Out.WriteLine(ans);
            IO.Printer.Out.Flush();
        }

        public static long GetAns(string s, string t)
        {
            var n = s.Length;
            var chars = Enumerable.Range(0, 26).Select(v => new List<int>()).ToList();
            for (int i = 0; i < n; i++)
            {
                var c = s[i] - 'a';
                chars[c].Add(i);
            }

            // exists?
            foreach(var c in t)
            {
                if (!chars[c - 'a'].Any()) return -1;
            }

            // exist!
            var ret = 0L;
            var idx = 0;
            foreach(var c in t)
            {
                var cIdx = c - 'a';
                var cList = chars[cIdx];

                while (true)
                {
                    var nextIdx = GetNextIndex(idx, cList);
                    if (nextIdx < 0 || nextIdx >= cList.Count() || cList[nextIdx] < idx)
                    {
                        // goto next s'
                        ret += (n - idx);
                        idx = 0;
                    }
                    else
                    {
                        // found in current s'
                        ret += (cList[nextIdx] - idx + 1);
                        idx = cList[nextIdx] + 1;
                        break;
                    }
                }
            }

            return ret;
        }

        public static int GetNextIndex(int tgt, List<int> list)
        {
            return LowerBound(list, tgt);
        }

        public static int LowerBound(List<int> ar, int val)
        {
            var lb = -1;
            var ub = ar.Count();

            while (ub - lb > 1)
            {
                var mid = (lb + ub) / 2;
                if (ar[mid] >= val)
                {
                    ub = mid;
                }
                else
                {
                    lb = mid;
                }
            }

            return ub;
        }
    }


#if DEBUG

    [TestClass]
    public class Test
    {

        // 1, 2=>1
        [TestMethod]
        public void GetNextIndex1()
        {
            var idx = 1;
            var list = new List<int> { 1, 2 };
            var exp = 0;
            var ans = Program.GetNextIndex(idx, list);
            Assert.AreEqual(exp, ans);
        }

        [TestMethod]
        public void Test1()
        {
            var s = "contest";
            var t = "son";
            var exp = 10;
            var ans = Program.GetAns(s, t);
            Assert.AreEqual(exp, ans);
        }

        [TestMethod]
        public void Test2()
        {
            var s = "contest";
            var t = "programming";
            var exp = -1;
            var ans = Program.GetAns(s, t);
            Assert.AreEqual(exp, ans);
        }

        [TestMethod]
        public void Test3()
        {
            var s = "contest";
            var t = "sentence";
            var exp = 33;
            var ans = Program.GetAns(s, t);
            Assert.AreEqual(exp, ans);
        }

        [TestMethod]
        public void Test4()
        {
            var s = "aa";
            var t = "aaaaaaaa";
            var exp = 8;
            var ans = Program.GetAns(s, t);
            Assert.AreEqual(exp, ans);
        }

        [TestMethod]
        public void Test5()
        {
            var s = "a";
            var t = "b";
            var exp = -1;
            var ans = Program.GetAns(s, t);
            Assert.AreEqual(exp, ans);
        }

        [TestMethod]
        public void Test6()
        {
            var s = "abcde";
            var t = "aeaeaeaeae";
            var exp = 25;
            var ans = Program.GetAns(s, t);
            Assert.AreEqual(exp, ans);
        }

        [TestMethod]
        public void Test7()
        {
            var s = "abzzzba";
            var t = "abzzzba";
            var exp = 7;
            var ans = Program.GetAns(s, t);
            Assert.AreEqual(exp, ans);
        }

        [TestMethod]
        public void Test8()
        {
            var s = "abba";
            var t = "abba";
            var exp = 4;
            var ans = Program.GetAns(s, t);
            Assert.AreEqual(exp, ans);
        }

        public bool IsEq<T>(List<T> l1, List<T> l2)
        {
            if (l1.Count() != l2.Count()) return false;
            for (int i = 0; i < l1.Count(); i++)
            {
                if (!l1[i].Equals(l2[i])) return false;
            }
            return true;
        }

    }
#endif
}

namespace IO
{
    using System.IO;
    using System.Text;
    using System.Globalization;

    public class Printer : StreamWriter
    {
        static Printer()
        {
            Out = new Printer(Console.OpenStandardOutput()) { AutoFlush = false };
        }

        public static Printer Out { get; set; }

        public override IFormatProvider FormatProvider
        {
            get { return CultureInfo.InvariantCulture; }
        }

        public Printer(Stream stream)
            : base(stream, new UTF8Encoding(false, true))
        {
        }

        public Printer(Stream stream, Encoding encoding)
            : base(stream, encoding)
        {
        }

        public void Write<T>(string format, T[] source)
        {
            base.Write(format, source.OfType<object>().ToArray());
        }

        public void WriteLine<T>(string format, T[] source)
        {
            base.WriteLine(format, source.OfType<object>().ToArray());
        }
    }

    public class StreamScanner
    {
        public StreamScanner(Stream stream)
        {
            str = stream;
        }

        public readonly Stream str;
        private readonly byte[] buf = new byte[1024];
        private int len, ptr;
        public bool isEof;

        public bool IsEndOfStream
        {
            get { return isEof; }
        }

        private byte read()
        {
            if (isEof) return 0;
            if (ptr < len) return buf[ptr++];
            ptr = 0;
            if ((len = str.Read(buf, 0, 1024)) > 0) return buf[ptr++];
            isEof = true;
            return 0;
        }

        public char Char()
        {
            byte b;
            do b = read(); while ((b < 33 || 126 < b) && !isEof);
            return (char)b;
        }

        public string Scan()
        {
            var sb = new StringBuilder();
            for (var b = Char(); b >= 33 && b <= 126; b = (char)read())
                sb.Append(b);
            return sb.ToString();
        }

        public string ScanLine()
        {
            var sb = new StringBuilder();
            for (var b = Char(); b != '\n'; b = (char)read())
                if (b == 0) break;
                else if (b != '\r') sb.Append(b);
            return sb.ToString();
        }

        public long Long()
        {
            if (isEof) return long.MinValue;
            long ret = 0;
            byte b;
            var ng = false;
            do b = read(); while (b != 0 && b != '-' && (b < '0' || '9' < b));
            if (b == 0) return long.MinValue;
            if (b == '-')
            {
                ng = true;
                b = read();
            }
            for (; ; b = read())
            {
                if (b < '0' || '9' < b)
                    return ng ? -ret : ret;
                ret = ret * 10 + b - '0';
            }
        }

        public int Integer()
        {
            return (isEof) ? int.MinValue : (int)Long();
        }

        public double Double()
        {
            var s = Scan();
            return s != "" ? double.Parse(s, CultureInfo.InvariantCulture) : double.NaN;
        }

        static T[] enumerate<T>(int n, Func<T> f)
        {
            var a = new T[n];
            for (int i = 0; i < n; ++i) a[i] = f();
            return a;
        }

        public char[] Char(int n)
        {
            return enumerate(n, Char);
        }

        public string[] Scan(int n)
        {
            return enumerate(n, Scan);
        }

        public double[] Double(int n)
        {
            return enumerate(n, Double);
        }

        public int[] Integer(int n)
        {
            return enumerate(n, Integer);
        }

        public long[] Long(int n)
        {
            return enumerate(n, Long);
        }
    }
}