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 Rust (1.15.1) (judge verdict: Accepted)

use std::io::prelude::*;

fn input<T: std::str::FromStr>() -> T {
    let stdin = std::io::stdin();
    let token: String = stdin
        .lock()
        .bytes()
        .map(|c| c.unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().unwrap()
}

fn main() {
    let s: String = input();
    let t: String = input();

    let slen = s.len();
    let tlen = t.len();
    let mut u = t.clone();
    while u.len() < slen + 2 * tlen {
        u.push_str(&s);
    }

    let rh = RollingHash::new(&u);
    let mut succ = vec![slen; slen];
    let mut pred = vec![0; slen];
    for i in 0..slen {
        if rh.is_same(0, tlen, tlen + i, 2 * tlen + i) {
            let j = (i + tlen) % slen;
            succ[i] = j;
            pred[j] += 1;
        }
    }

    let mut dist = vec![-1; slen];
    let mut q = std::collections::VecDeque::new();
    for i in 0..slen {
        if pred[i] == 0 {
            dist[i] = 0;
            q.push_back(i);
        }
    }
    while let Some(i) = q.pop_front() {
        if succ[i] == slen {
            continue;
        }
        let j = succ[i];
        pred[j] -= 1;
        dist[j] = dist[i] + 1;
        if pred[j] == 0 {
            q.push_back(j);
        }
    }

    let mut ok = true;
    let mut ans = 0;
    for i in 0..slen {
        if dist[i] < 0 {
            ok = false;
            break;
        }
        ans = std::cmp::max(ans, dist[i]);
    }

    println!("{}", if ok { ans } else { -1 });
}

struct RollingHash {
    hash: Vec<Vec<i64>>,
    power: Vec<Vec<i64>>,
}

impl RollingHash {
    fn modulus() -> [i64; 3] {
        [1000000007, 1000000009, 1234567891]
    }

    fn radix() -> [i64; 3] {
        [1007, 1009, 1013]
    }

    fn new<S: AsRef<str>>(s: S) -> RollingHash {
        let s = s.as_ref().as_bytes();
        let m = RollingHash::modulus();
        let r = RollingHash::radix();
        let n = s.len();
        let mut h = vec![vec![0; n + 1]; m.len()];
        let mut p = vec![vec![0; n + 1]; m.len()];
        for i in 0..m.len() {
            h[i][0] = 0;
            p[i][0] = 1;
            for j in 0..n {
                h[i][j + 1] = (h[i][j] * r[i] + s[j] as i64) % m[i];
                p[i][j + 1] = p[i][j] * r[i] % m[i];
            }
        }
        RollingHash { hash: h, power: p }
    }

    fn hash(&self, i: usize, start: usize, end: usize) -> i64 {
        let m = RollingHash::modulus();
        let mut r = self.hash[i][end] - self.hash[i][start] * self.power[i][end - start] % m[i];
        if r < 0 {
            r += m[i];
        }
        r
    }

    fn is_same(&self, start1: usize, end1: usize, start2: usize, end2: usize) -> bool {
        if end1 - start1 != end2 - start2 {
            false
        } else {
            let mlen = RollingHash::modulus().len();
            let mut ok = true;
            for i in 0..mlen {
                if self.hash(i, start1, end1) != self.hash(i, start2, end2) {
                    ok = false;
                }
            }
            ok
        }
    }
}