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: Wrong Answer)

use std::collections::VecDeque;

fn main() {
    let (r, w) = (std::io::stdin(), std::io::stdout());
    let mut sc = IO::new(r.lock(), w.lock());

    let s: Vec<_> = sc.read::<String>().bytes().collect();
    let t: Vec<_> = sc.read::<String>().bytes().collect();
    let n_s = s.len();
    let n_t = t.len();

    let mut array = t.clone();

    let x = (n_t + n_s - 1) / n_s;
    for _ in 0..x {
        array.extend(s.clone());
    }

    let z_array = z_algorithm::calc_z_array(&array);
    let mut jump = vec![false; n_s];
    for i in 0..n_s {
        jump[i] = z_array[i + n_t] >= n_t;
    }

    let mut ans = vec![None; n_s];
    let mut q = VecDeque::new();
    for i in 0..n_s {
        if !jump[i] {
            ans[i] = Some(0);
            q.push_back(i);
        }
    }
    if q.is_empty() {
        println!("-1");
        return;
    }

    while let Some(pos) = q.pop_front() {
        let prev = (pos + n_t * n_s - n_t) % n_s;
        if ans[prev].is_some() {
            continue;
        }
        if jump[prev] {
            ans[prev] = Some(ans[pos].unwrap() + 1);
        } else {
            ans[prev] = Some(0);
        }
        q.push_back(prev);
    }

    if ans.contains(&None) {
        println!("-1");
        return;
    }

    let ans = ans.into_iter().map(|ans| ans.unwrap()).max().unwrap();
    println!("{}", ans);
}

pub mod z_algorithm {

    pub fn calc_z_array<T: PartialEq>(s: &[T]) -> Vec<usize> {
        let n = s.len();
        let mut z_array = vec![0; n];

        // [l, r) is a window which matches with prefix of s
        let mut l = 0;
        let mut r = 1;
        for i in 1..n {
            if i >= r {
                l = i;
                r = i + 1;
                while r <= n && s[r - 1 - l] == s[r - 1] {
                    r += 1;
                }
                z_array[i] = r - l - 1;
            } else {
                if z_array[i - l] < r - i {
                    z_array[i] = z_array[i - l];
                } else {
                    l = i;
                    while r <= n && s[r - 1 - l] == s[r - 1] {
                        r += 1;
                    }
                    z_array[i] = r - l - 1;
                }
            }
        }
        z_array[0] = n;
        z_array
    }
}

pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>);

impl<R: std::io::Read, W: std::io::Write> IO<R, W> {
    pub fn new(r: R, w: W) -> IO<R, W> {
        IO(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: std::ops::Deref<Target = str>>(&mut self, s: S) {
        use std::io::Write;
        self.1.write(s.as_bytes()).unwrap();
    }
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .0
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}