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 (GCC 9.2.1) (judge verdict: Accepted)

#include <stdio.h>
#include <string.h>
char s[500001];
char t[500001];
int match[500000];
int par[20][500000];
int tlen, slen;
int main(void){
    int i, j, k;
    int base[] = {2, 3, 5, 7, 9, 11, 13, 17, 19, 23};
    int m = 10;//baseの数
    scanf("%s", s);
    scanf("%s", t);

    tlen = strlen(t);
    slen = strlen(s);

    long long int mod = 1000000007;
    long long int thash[100], shash[100];

    //初期のハッシュ値を求める
    for(j = 0; j < m; j++){
        thash[j] = 0;
        shash[j] = 0;
        for(i = 0; i < tlen; i++){
            shash[j] += s[i%slen];
            thash[j] += t[i];
            if(i == tlen - 1){
                break;
            }
            thash[j] *= base[j];
            thash[j] %= mod;
            shash[j] *= base[j];
            shash[j] %= mod;
        }
    }

    long long int tmp[100];
    //base^(plen-1)%mod の値を求める
    for(j = 0; j < m; j++){
        tmp[j] = 1;
        for(i = 0; i < tlen-1; i++){
            tmp[j] *= base[j];
            tmp[j] %= mod;
        }
    }
    for(i = 0; i < slen; i++){
        match[i] = -1;
    }

    for(i = 0; i < slen; i++){
        int cnt = 0;
        for(j = 0; j < m; j++){
            if(thash[j] == shash[j]){
                cnt++;
            }
            shash[j] = (shash[j] - tmp[j] * s[i])*base[j] + s[(i+tlen)%slen];
            while(shash[j] < 0){
                shash[j] += mod; 
            }
            shash[j] %= mod;
        }
        if(cnt == m){
            match[i] = 0;
        }
    }

    //degub用，
    // for(i = 0; i < slen; i++){
    //     printf("%d\n", match[i]);
    // }

    //parの計算
    int chk[500000];
    for(i = 0; i < slen; i++){
        chk[i] = -1;
    }
    for(i = 0; i < slen; i++){
        if(chk[i] != -1){
            continue;
        }
        if(match[i] == -1){
            chk[i] = 0;
            continue;
        }
        int v = i;
        //前に探索
        int cnt = 1;
        while(1){
            v += tlen;
            v %= slen;
            if(match[v] != 0){
                break;
            }
            chk[v] = 1;
            cnt++;
            if(cnt > 500000){
                printf("-1\n");
                return 0;
            }
            continue;
        }

        //後ろに探索
        v = i;
        while(1){
            v -= tlen;
            while(v < 0){
                v += slen;
            }
            v %= slen;
            if(match[v] != 0){
                break;
            }
            chk[v] = 1;
            cnt++;
            if(cnt > 500000){
                printf("-1\n");
                return 0;
            }
            continue;
        }
        chk[i] = cnt;
    }
    int ans = 0;
    for(i = 0; i < slen; i++){
        ans = ans > chk[i] ? ans : chk[i];
    }
    printf("%d\n", ans);
    return 0;
}