atcoder#ABC135F. [ABC135F] Strings of Eternity

[ABC135F] Strings of Eternity

Score : 600600 points

Problem Statement

Given are two strings ss and tt consisting of lowercase English letters. Determine if the number of non-negative integers ii satisfying the following condition is finite, and find the maximum value of such ii if the number is finite.

  • There exists a non-negative integer jj such that the concatenation of ii copies of tt is a substring of the concatenation of jj copies of ss.

Notes

  • A string aa is a substring of another string bb if and only if there exists an integer xx (0xba)(0 \leq x \leq |b| - |a|) such that, for any yy (1ya)(1 \leq y \leq |a|), ay=bx+ya_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 ss and tt, i=0i = 0 satisfies the condition in the problem statement.

Constraints

  • 1s5×1051 \leq |s| \leq 5 \times 10^5
  • 1t5×1051 \leq |t| \leq 5 \times 10^5
  • ss and tt consist of lowercase English letters.

Input

Input is given from Standard Input in the following format:

ss

tt

Output

If the number of non-negative integers ii satisfying the following condition is finite, print the maximum value of such ii; if the number is infinite, print -1.

abcabab
ab
3

The concatenation of three copies of tt, ababab, is a substring of the concatenation of two copies of ss, abcabababcabab, so i=3i = 3 satisfies the condition.

On the other hand, the concatenation of four copies of tt, abababab, is not a substring of the concatenation of any number of copies of ss, so i=4i = 4 does not satisfy the condition.

Similarly, any integer greater than 44 does not satisfy the condition, either. Thus, the number of non-negative integers ii satisfying the condition is finite, and the maximum value of such ii is 33.

aa
aaaaaaa
-1

For any non-negative integer ii, the concatenation of ii copies of tt is a substring of the concatenation of 4i4i copies of ss. Thus, there are infinitely many non-negative integers ii that satisfy the condition.

aba
baaab
0

As stated in Notes, i=0i = 0 always satisfies the condition.