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>
#define For(i, a, b) for(int (i)=(a); (i)<(b); ++(i))
#define rFor(i, a, b) for(int (i)=(a)-1; (i)>=(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef pair<int, int> pii;

int gcd(int p, int q){
    if(p < q) swap(p, q);
    while(q){
        int r = p % q;
        p = q;
        q = r;
    }
    return p;
}

const lint b1 = 1009;
const lint b2 = 1007;
const lint b3 = 997;
const lint h1 = 1e9 + 7;
const lint h2 = 1e9 + 9;
const lint h3 = 998244353;

lint powmod(lint x, lint n, lint mod){
  lint ret = 1;
  while(n > 0){
    if(n&1) ret *= x, ret %= mod, n -= 1;
    else x *= x, x %= mod, n >>= 1;
  }
  return ret;
}

int main(){
    string s, t;
    cin >> s >> t;
    string u = s;
    while(s.size() < t.size()) s += u;
    s += s;
    int n = s.size();
    int m = t.size();
    int l = gcd(n, m);
    u = t.substr(0, l);
    bool flag = false;
    int loop = 0;
    while(loop < n){
        if(s.substr(loop, l) != u){
            flag = true;
            break;
        }
        loop += l;
    }
    if(!flag){
        printf("-1\n");
        return 0;
    }
    lint th1 = 0;
    lint th2 = 0;
    lint th3 = 0;
    rep(i, m){
        th1 = (th1 * b1 + (t[i] - 'a')) % h1;
        th2 = (th2 * b2 + (t[i] - 'a')) % h2;
        th3 = (th3 * b3 + (t[i] - 'a')) % h3;
    }
    //printf("%d %d %d\n", th1, th2, th3);
    lint sh1 = 0;
    lint sh2 = 0;
    lint sh3 = 0;
    rep(i, m){
        sh1 = (sh1 * b1 + (s[i] - 'a')) % h1;
        sh2 = (sh2 * b2 + (s[i] - 'a')) % h2;
        sh3 = (sh3 * b3 + (s[i] - 'a')) % h3;
    }
    int num[n-m+1];
    rep(i, n-m+1) num[i] = 0;
    if(sh1 == th1 && sh2 == th2 && sh3 == th3) ++num[0];
    lint p1 = powmod(b1, m, h1);
    lint p2 = powmod(b2, m, h2);
    lint p3 = powmod(b3, m, h3);
    rep(i, n-m+1){
        sh1 = (sh1 * b1 % h1 - p1 * (s[i] - 'a') % h1 + (s[i+m] - 'a') + h1) % h1;
        sh2 = (sh2 * b2 % h2 - p2 * (s[i] - 'a') % h2 + (s[i+m] - 'a') + h2) % h2;
        sh3 = (sh3 * b3 % h3 - p3 * (s[i] - 'a') % h3 + (s[i+m] - 'a') + h3) % h3;
        if(sh1 == th1 && sh2 == th2 && sh3 == th3) ++num[i+1];
        //printf("%d %d %d\n", sh1, sh2, sh3);
    }
    rrep(i, n-m+1)if(num[i] && i-m >= 0 && num[i-m]){
        num[i-m] += num[i];
    }
    int ans = 0;
    rep(i, n-m+1) ans = max(ans, num[i]);
    printf("%d\n", ans);
}