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 self::rolling_hash::*;
use std::cmp;

const BASE: u64 = 1_000_000_007;
const N: usize = 1e6 as usize + 10;

fn main() {
    let (r, w) = (std::io::stdin(), std::io::stdout());
    let mut sc = IO::new(r.lock(), w.lock());
    let s = sc
        .read::<String>()
        .chars()
        .map(|c| c as u8 - 'a' as u8)
        .collect::<Vec<_>>();
    let t = sc
        .read::<String>()
        .chars()
        .map(|c| c as u8 - 'a' as u8)
        .collect::<Vec<_>>();

    let mut pow = vec![1; N + 1];
    for i in 0..N {
        pow[i + 1] = mod_mul(pow[i], BASE);
    }

    let s_hash = RollingHash::new(&s);
    let t_hash = RollingHash::new(&t);

    let mut s_tandem = vec![0; N + 1];
    s_tandem[1] = s_hash.get_hash(0, s.len());
    for i in 1..N {
        s_tandem[i + 1] = modulo(mod_mul(s_tandem[1], mod_pow(BASE, s.len() * i)) + s_tandem[i]);
    }
    let mut t_tandem = vec![0; N + 1];
    t_tandem[1] = t_hash.get_hash(0, t.len());
    for i in 1..N {
        t_tandem[i + 1] = modulo(mod_mul(t_tandem[1], mod_pow(BASE, t.len() * i)) + t_tandem[i]);
    }

    let mut ans = 0;
    for t_start_pos in 0..s.len() {
        if is_ok(
            &s,
            &t,
            s.len(),
            t_start_pos,
            &s_tandem,
            &t_tandem,
            &pow,
            &s_hash,
        ) {
            println!("-1");
            return;
        }

        let mut ok = 0;
        let mut ng = s.len();
        while ng - ok > 1 {
            let tandem = (ng + ok) / 2;
            if is_ok(
                &s,
                &t,
                tandem,
                t_start_pos,
                &s_tandem,
                &t_tandem,
                &pow,
                &s_hash,
            ) {
                ok = tandem;
            } else {
                ng = tandem
            }
        }

        ans = cmp::max(ans, ok);
    }

    println!("{}", ans);
    return;
}

fn is_ok(
    s: &[u8],
    t: &[u8],
    tandem: usize,
    start_pos: usize,
    s_tandem: &[u64],
    t_tandem: &[u64],
    pow: &[u64],
    s_hash: &RollingHash,
) -> bool {
    let length = tandem * t.len();
    let t_hash = t_tandem[tandem];
    let suffix = s.len() - start_pos;

    let s_hash = if length < suffix {
        s_hash.get_hash(start_pos, start_pos + length)
    } else {
        let full_s_count = (length - suffix) / s.len();
        let prefix = length - suffix - full_s_count * s.len();

        s_hash.get_hash(0, prefix)
            + mod_mul(pow[prefix], s_tandem[full_s_count])
            + mod_mul(
                s_hash.get_hash(s.len() - suffix, s.len()),
                mod_pow(BASE, length - suffix),
            )
    };
    modulo(t_hash) == modulo(s_hash)
}

fn mod_pow(x: u64, mut e: usize) -> u64 {
    let mut result = 1;
    let mut cur = x;
    while e > 0 {
        if e & 1 == 0 {
            result = mod_mul(result, cur);
        }
        cur = mod_mul(cur, cur);
        e >>= 1;
    }
    result
}

pub mod rolling_hash {
    use super::BASE;

    const MASK_30: u64 = (1 << 30) - 1;
    const MASK_31: u64 = (1 << 31) - 1;
    const MOD: u64 = (1 << 61) - 1;

    pub struct RollingHash {
        hash: Vec<u64>,
        pow: Vec<u64>,
    }

    impl RollingHash {
        pub fn new(s: &[u8]) -> RollingHash {
            let n = s.len();
            let base = BASE;
            let mut hash: Vec<u64> = vec![0; n + 1];
            let mut pow: Vec<u64> = vec![0; n + 1];
            pow[0] = 1;
            for i in 0..n {
                pow[i + 1] = modulo(mod_mul(pow[i], base));
                hash[i + 1] = modulo(mod_mul(hash[i], base) + s[i] as u64);
            }
            RollingHash {
                hash: hash,
                pow: pow,
            }
        }

        /// Get hash of [l, r)
        pub fn get_hash(&self, l: usize, r: usize) -> u64 {
            modulo(self.hash[r] + MOD - mod_mul(self.hash[l], self.pow[r - l]))
        }
    }

    pub fn mod_mul(a: u64, b: u64) -> u64 {
        let (a_prefix, a_suffix) = (a >> 31, a & MASK_31);
        let (b_prefix, b_suffix) = (b >> 31, b & MASK_31);
        let m = a_suffix * b_prefix + a_prefix * b_suffix;
        modulo(a_prefix * b_prefix * 2 + (m >> 30) + ((m & MASK_30) << 31) + a_suffix * b_suffix)
    }

    pub fn modulo(v: u64) -> u64 {
        let v = (v & MOD) + (v >> 61);
        if v >= MOD {
            v - MOD
        } else {
            v
        }
    }
}

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: 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()
    }
}