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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
using namespace std;
//万合以致，向阳而生——背叛时代，忠于时间//
typedef long long ll;
typedef pair<int,int> pi;
typedef vector<int> vi;
typedef unsigned long long ull;

#define f first
#define s second
#define pb push_back
#define mp make_pair
#define forn(i, n) for(register int i=0; i<n; i++)
#define Forn(i, n) for(register int i=1; i<=n; i++)
#define foreach(it, a) for(__typeof((a).begin()) it=(a).begin(); it!=(a).end(); it++)
#define Foreach(it, a) for(__typeof((a).rbegin()) it=(a).rbegin(); it!=(a).rend(); it++)
#define mid l+r>>1
#define lid id<<1|1
#define rid (id<<1)+2
#define lson lid, l, mid
#define rson rid, mid, r

inline int in()
{
    char c=getchar();
    int neg=1, x=0;
    while(!isdigit(c)) (c=='-')?neg=-1, c=getchar():c=getchar();
    while(isdigit(c)) x=(x<<3)+(x<<1)+(c^48), c=getchar();
    return neg*x;
}
inline int lcm(int a, int b){return a*b/__gcd(a, b);}

const int MAX=5e5*4+5;

int N, M, ptr, mark[MAX*5], fin[MAX*5];
char S[MAX], s[MAX*2], t[MAX*2], pos[MAX*2];

class Hash
{
    public:
    int val, mod, seed;
    Hash(int _m=1e9+7, int _s=61):val(0), mod(_m), seed(_s){}
    inline int update(int x)
    { 
        val=(1LL*val*seed+x)%mod; 
        return val;
    }
    inline int v(){ return val; }
}h;

class Hash_suff// const int MAX=???
{
    private:
    Hash a;
    int ptr, mod, hash[MAX*5+5], base[MAX*5+5];
    public:
    Hash_suff() {}
    inline void init(int _m, int _p)
    {
        ptr=0;
        mod=a.mod=_m, a.seed=_p, a.val=0;
        base[0]=1;
        Forn(i, MAX*4)
            base[i]=1LL*base[i-1]*_p%_m;
    }
    inline void update(int x)
    {
        hash[++ptr]=a.update(x);
    }
    inline int get(int l, int r)// (l,r]
    {
        return (hash[r]-1LL*hash[l]*base[r-l]%mod+mod)%mod;
    }
}H;

inline void solve(int len, int len2, int &ret1, int &ret2)
{
    memset(mark, 0, sizeof(mark));
    forn(i, ptr) if(!mark[pos[i]])
    {
        if(pos[i]+M>=len) break;
        int tmp=1;
        mark[pos[i]]=1;
        for(int j=pos[i]+M; j<len; j+=M) 
        {
            if(!fin[j]) break;
            else mark[j]=1, tmp++;
            if(j<len2) ret2=max(ret2, tmp);
            ret1=max(ret1, tmp);
        }
    }
}

int main()
{
    H.init(1e9+7, 61);
    scanf("%s%s", S, t);
    M=strlen(t);
    int n=strlen(S);
    while(N<M) strcat(s, S), N+=n;//, cout<<N<<endl;
    forn(i, N*4) H.update(s[i%N]-'a'+1);
    forn(i, M) h.update(t[i]-'a'+1);
    forn(i, N*4-M) if(H.get(i, i+M)==h.v()) fin[i]=1, pos[ptr++]=i;
    int ret1=0, ret2=0;
    solve(N*3, N*2, ret1, ret2);
    if(ret1!=ret2)
    {
        puts("-1");
        return 0;
    }
    printf("%d\n", ret1);
    return 0;
}