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

#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
#include<inttypes.h>
#include<string.h>
#include<math.h>

typedef int32_t i32;
typedef int64_t i64;

#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define ABS(a) ((a)>(0)?(a):-(a))
#define ALLOC(size,type) ((type*)calloc((size),sizeof(type)))
#define SORT(a,num,cmp) qsort((a),(num),sizeof(*(a)),cmp)

#define F 4

const i32 mod[F] = {1000001011,1000001021,1000001053,1000001087};
const i32 rad = 100000;

i32 mod_pow (i32 r, i32 n, i32 mod) {
  i64 t = 1;
  i64 s = r;
  while (n > 0) {
    if (n & 1) t = t * s % mod;
    s = s * s % mod;
    n >>= 1;
  }
  return t;
}

void query (i32 **dp, i32 l, i32 r, i32 *res) {
  for (i32 i = 0; i < F; ++i) {
    res[i] = (dp[l][i] - (i64) dp[r][i] * mod_pow (rad, r - l, mod[i]) % mod[i] + mod[i]) % mod[i];
  }
}

i32 equal (i32 *a, i32 *b) {
  for (i32 i = 0; i < F; ++i) {
    if (a[i] != b[i]) {
      return 0;
    }
  }
  return 1;
}

void run (void) {
  const i32 LEN = 500000;
  char *s = ALLOC (3 * LEN + 1, char);
  char *t = ALLOC (LEN + 1, char);
  scanf ("%s%s", s, t);
  i32 n = strlen (s);
  i32 m = strlen (t);
  const i32 d = n;
  while (n < 2 * m || n < 2 * d) {
    memcpy (s + n, s, sizeof (char) * d);
    n += d;
  }
  i32 **hash = ALLOC (n + 1, i32 *);
  {
    i32 *buf = ALLOC (F * (n + 1), i32);
    for (i32 i = 0; i <= n; ++i) {
      hash[i] = buf;
      buf += F;
    }
  }
  for (i32 i = n - 1; i >= 0; --i) {
    for (i32 j = 0; j < F; ++j) {
      hash[i][j] = ((i64) rad * hash[i + 1][j] + s[i] - 'a' + 1) % mod[j];
    }
  }
  i32 hash_t[F] = {0, 0, 0, 0};
  for (i32 i = m - 1; i >= 0; --i) {
    for (i32 j = 0; j < F; ++j) {
      hash_t[j] = ((i64) rad * hash_t[j] + t[i] - 'a' + 1) % mod[j];
    }
  }
  i32 *dp = ALLOC (n, i32);
  for (i32 i = 0; i < n; ++i) {
    dp[i] = -1;
  }
  i32 ans = 0;
  for (i32 i = 0; i < d; ++i) {
    if (dp[i] >= 0) continue;
    dp[i] = -2;
    i32 now = i;
    i32 k = 0;
    i32 hash_s[F];
    query (hash, i, i + m, hash_s);
    while (equal (hash_s, hash_t)) {
      ++k;
      now = (now + m) % d;
      if (dp[now] >= 0) {
        k += dp[now];
        break;
      }
      if (dp[now] == -2) {
        puts ("-1");
        return;
      }
      dp[now] = -2;
      query (hash, now, now + m, hash_s);
    }
    ans = MAX (ans, k);
    dp[i] = k;
    query (hash, i, i + m, hash_s);
    now = i;
    while (equal (hash_s, hash_t)) {
      --k;
      now = (now + m) % d;
      if (dp[now] >= 0) break;
      dp[now] = k;
      query (hash, now, now + m, hash_s);
    }
    if (dp[now] < 0) dp[now] = 0;
  }
  printf ("%" PRIi32 "\n", ans);
}

int main (void) {
  run();
  return 0;
}