Problem:
Score : 600 points

Problem Statement

Given are two strings s and t consisting of lowercase English letters. Determine if the number of non-negative integers i satisfying the following condition is finite, and find the maximum value of such i if the number is finite.

There exists a non-negative integer j such that the concatenation of i copies of t is a substring of the concatenation of j copies of s.

Notes

A string a is a substring of another string b if and only if there exists an integer x (0 \leq x \leq |b| - |a|) such that, for any y (1 \leq y \leq |a|), a_y = b_{x+y} holds.

We assume that the concatenation of zero copies of any string is the empty string. From the definition above, the empty string is a substring of any string. Thus, for any two strings s and t, i = 0 satisfies the condition in the problem statement.

Constraints

1 \leq |s| \leq 5 \times 10^5

1 \leq |t| \leq 5 \times 10^5

s and t consist of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s
t

Output

If the number of non-negative integers i satisfying the following condition is finite, print the maximum value of such i; if the number is infinite, print -1.

Sample Input 1

abcabab
ab

Sample Output 1

3

The concatenation of three copies of t, ababab, is a substring of the concatenation of two copies of s, abcabababcabab, so i = 3 satisfies the condition.

On the other hand, the concatenation of four copies of t, abababab, is not a substring of the concatenation of any number of copies of s, so i = 4 does not satisfy the condition.

Similarly, any integer greater than 4 does not satisfy the condition, either. Thus, the number of non-negative integers i satisfying the condition is finite, and the maximum value of such i is 3.

Sample Input 2

aa
aaaaaaa

Sample Output 2

-1

For any non-negative integer i, the concatenation of i copies of t is a substring of the concatenation of 4i copies of s. Thus, there are infinitely many non-negative integers i that satisfy the condition.

Sample Input 3

aba
baaab

Sample Output 3

0

As stated in Notes, i = 0 always satisfies the condition.

Solution in C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace A
{
    partial class Program
    {

        public IEnumerable<string> Solve()
        {
            var s = Str;
            var t = Str;

            var slen = s.Length;
            var tlen = t.Length;

            var res = 0;
            var si = 0;
            // si, ti
            List<int> dp = new List<int>();

            while (true)
            {
                var ndp = new List<int>();

                var simod = si % slen;
                var first = s[simod] == t[0];
                if (first)
                    ndp.Add(0);

                foreach (var prev in dp)
                {
                    var timod = (prev + 1) % tlen;
                    if (s[simod] == t[timod])
                        ndp.Add(prev + 1);
                }

                if (ndp.Count > 0)
                {
                    var cand = ndp[ndp.Count - 1] + 1;
                    if (cand % tlen == 0)
                        res = Math.Max(cand / tlen, res);

                    if (cand % tlen == 1 && first)
                    {
                        res = -1;
                        break;
                    }
                }

                if (slen <= tlen && si > slen + tlen)
                    break;

                if (slen > tlen && si > slen * 2)
                    break;

                dp = ndp;
                si++;
            }


            yield return res.ToString();
        }
    }

    class StartingPoint
    {
        static void Main(string[] args)
        {
            if (args?.Any() == true)
            {
                var assembly = Assembly.GetEntryAssembly();
                var resourceName = "AtCoderCSharp.In.txt";
                var stream = assembly.GetManifestResourceStream(resourceName);
                var streamReader = new StreamReader(stream, Encoding.UTF8);
                var str = streamReader.ReadToEnd();
                var textReader = new StringReader(str);
                Console.SetIn(textReader);
            }

            try
            {
                foreach (var s in new Program().Solve())
                {
                    Console.WriteLine(s);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                if (args?.Any() != true)
                    throw e;
            }

            if (args?.Any() == true)
                Console.ReadKey();
        }
    }

    partial class Program
    {
        public List<int> ScanInts(int count) => Enumerable.Range(0, count).Select(_ => ScanInt).ToList();
        public List<int> ScanInts(long count) => ScanInts((int)count);
        public List<long> ScanLongs(int count) => Enumerable.Range(0, count).Select(_ => Scan).ToList();
        public List<long> ScanLongs(long count) => ScanLongs((int)count);
        public int ScanInt => int.Parse(Str);
        public long Scan => long.Parse(Str);
        public double ScanDouble => (double)Scan;

        StringBuilder sb = new StringBuilder();

        public string Str => ScanStr();
        public string ScanStr()
        {
            sb.Clear();

            int r = 0;
            bool? b = null;
            do
            {
                r = Console.Read();
                b = IsBreak(r);
            } while (b == true);

            if (b == null)
                throw new Exception("input error: unexpected end of stream");

            do
            {
                sb.Append((char)r);
                r = Console.Read();
                b = IsBreak(r);
            } while (b == false);

            return sb.ToString();
        }

        bool? IsBreak(int c)
        {
            switch (c)
            {
                case -1:
                    return null;
                case (int)' ':
                case (int)'\n':
                case (int)'\r':
                    return true;
                default:
                    return false;
            }
        }

        public IEnumerable<int> Loop(int count) => Enumerable.Range(0, count);
        public IEnumerable<int> Loop(int from, int to) => Enumerable.Range(from, to - from + 1);

        public string YesNo(bool isYes) => isYes ? "Yes" : "No";

        public HashSet<T> ToHashSet<T>(IEnumerable<T> ts) => new HashSet<T>(ts);

        public int Gcm(int a, int b)
        {
            if (a < b)
                return GcmImpl(b, a);
            else
                return GcmImpl(a, b);
        }

        int GcmImpl(int a, int b)
        {
            var remainder = a % b;
            if (remainder == 0)
                return b;
            else
                return GcmImpl(b, remainder);
        }

        public int Lcm(int a, int b)
        {
            return a / Gcm(a, b) * b;
        }

        int GetFirstIndexGreater(long x, ref List<long> listOrdered)
        {
            var count = listOrdered.Count;

            if (count == 0)
                return 0;

            if (listOrdered[count - 1] <= x)
                return count;

            int low = 0;
            int high = listOrdered.Count - 1;

            while (low < high)
            {
                var mid = (low + high) / 2;

                if (listOrdered[mid] > x)
                    high = mid;
                else
                    low = mid + 1;
            }

            return low;
        }

        int GetFirstIndexGreater(int x, ref List<int> listOrdered)
        {
            var count = listOrdered.Count;

            if (count == 0)
                return 0;

            if (listOrdered[count - 1] <= x)
                return count;

            int low = 0;
            int high = listOrdered.Count - 1;

            while (low < high)
            {
                var mid = (low + high) / 2;

                if (listOrdered[mid] > x)
                    high = mid;
                else
                    low = mid + 1;
            }

            return low;
        }

        int GetLastIndexLess(long x, ref List<long> listOrdered)
        {
            var count = listOrdered.Count;

            if (count == 0)
                return -1;

            if (listOrdered[0] >= x)
                return -1;

            int low = 0;
            int high = listOrdered.Count - 1;

            while (low < high)
            {
                var mid = (low + high + 1) / 2;

                if (listOrdered[mid] < x)
                    low = mid;
                else
                    high = mid - 1;
            }

            return low;
        }

        int GetLastIndexLess(int x, ref List<int> listOrdered)
        {
            var count = listOrdered.Count;

            if (count == 0)
                return -1;

            if (listOrdered[0] >= x)
                return -1;

            int low = 0;
            int high = listOrdered.Count - 1;

            while (low < high)
            {
                var mid = (low + high + 1) / 2;

                if (listOrdered[mid] < x)
                    low = mid;
                else
                    high = mid - 1;
            }

            return low;
        }
    }

    class Mod
    {
        readonly int _divider = 1000000007;
        public Mod(int divider = 1000000007)
        {
            _divider = divider;
        }

        public int Add(int a, int b) => (int)Add((long)a, (long)b);
        public int Sub(int a, int b) => (int)Sub((long)a, (long)b);
        public int Mul(int a, int b) => (int)Mul((long)a, (long)b);
        public int Div(int a, int b) => (int)Div((long)a, (long)b);
        public int Pow(int a, int p) => (int)Pow((long)a, (long)p);

        public long Add(long a, long b) => (a + b) % _divider;

        public long Sub(long a, long b)
        {
            var t = (a - b) % _divider;
            if (t < 0)
                t += _divider;
            return t;
        }

        public long Mul(long a, long b) => (a * b) % _divider;

        public long Div(long a, long b) => Mul(a, Inv(b));

        public long Pow(long a, long p)
        {
            switch (p)
            {
                case 0: return 1;
                case 1: return a;
                default:
                    var halfP = p / 2;
                    var halfPowered = Pow(a, halfP);
                    var powered = Mul(halfPowered, halfPowered);
                    return p % 2 == 0 ? powered : Mul(powered, a);
            }
        }

        readonly Dictionary<long, long> invCache = new Dictionary<long, long>();
        long Inv(long a)
        {
            long cache = 0L;
            if (invCache.TryGetValue(a, out cache))
                return cache;

            var result = Pow(a, _divider - 2);
            invCache.Add(a, result);
            return result;
        }

        readonly List<int> facCache = new List<int>() { 1 };
        public int Fac(int a)
        {
            if (a < facCache.Count)
                return facCache[a];

            var val = facCache.Last();
            var start = facCache.Count;
            for (int i = start; i <= a; i++)
            {
                val = Mul(val, i);
                facCache.Add(val);
            }

            return val;
        }

        public int Perm(int n, int r)
        {
            if (n < r)
                return 0;

            if (r <= 0)
                return 1;

            int nn = Fac(n);
            int nr = Fac(n - r);
            return Div(nn, nr);
        }

        public int Comb(int n, int r)
        {
            if (n < r)
                return 0;

            if (n == r)
                return 1;

            int nn = Fac(n);
            int nr = Fac(n - r);
            int rr = Fac(r);
            return Div(Div(nn, nr), rr);
        }
    }
}