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::cmp;
use std::cmp::max;
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 mut s = sc.chars();
    let mut t = sc.chars();

    while s.len() < t.len() {
        let t = s.clone();
        s.extend(t);
    }
    let x = s.clone();
    s.extend(x);

    let n_s = s.len() / 2;
    let n_t = t.len();
    t.extend(s);

    let z_array = z_algorithm::calc_z_array(&t);
    let mut length = vec![0; n_s];
    for i in 0..n_s {
        length[i] = cmp::min(z_array[i + n_t], n_t);
    }

    let mut vis = vec![false; n_s];
    for i in 0..n_s {
        if vis[i] {
            continue;
        }
        vis[i] = true;
        if length[i] != n_t {
            continue;
        }
        let mut cur = (i + n_t) % n_s;
        while !vis[cur] {
            vis[cur] = true;
            if length[cur] != n_t {
                break;
            }
            cur = (cur + n_t) % n_s;
        }
        if cur == i {
            println!("-1");
            return;
        }
    }

    let mut max_i = 0;
    for i in 0..n_s {
        if length[i] == n_t {
            continue;
        }
        let mut cur = i;
        let mut ans = 0;
        loop {
            let prev = (cur + n_s - n_t) % n_s;
            if length[prev] == n_t {
                cur = prev;
                ans += 1;
            } else {
                break;
            }
        }
        max_i = max(max_i, ans);
    }
    println!("{}", max_i);
}

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;
                r -= 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;
                    r -= 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) -> Self {
        IO(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: ToString>(&mut self, s: S) {
        use std::io::Write;
        self.1.write(s.to_string().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()
    }
}