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

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define df 0
int dc=10000;
#define MAX 500020
int gcd(int a,int b){
  if(b==0)return a;
  return gcd(b,a%b);
}

int pre[MAX]={};   // tl
int match[MAX]={}; // sl
int ans[MAX]={};
void make_pre(char* t,int tl);
void make_match(char* s,int sl,char* t,int tl);
void print(char* s,char* t,int i,int temps,int flag);
int make_ans(int i,int sl,int tl);
int period(char* t,int tl,int d);

int main(){
  char s[MAX],t[MAX];
  scanf(" %s %s",s,t);
  int sl=strlen(s),tl=strlen(t),d=gcd(sl,tl);
  if(period(s,sl,d) && period(t,tl,d)) {
    printf("-1");
    return 0;
  }

  make_pre(t,tl);
  make_match(s,sl,t,tl);
  int i,m=0;
  for(i=0;i<sl;i++){
    dc-=df;if(dc<0){printf("err. A\n");return 0;}
    int temp= make_ans(i,sl,tl);
    if(m<temp)m=temp;
  }

    

  printf("%d",m);
  return 0;
}

void make_pre(char* t,int tl){
  dc-=df;if(dc<0){printf("err in make_pre\n");return;}
  pre[0]=0;
  int i,temp;
  for(i=1;i<tl;i++){
  dc-=df;if(dc<0){printf("err in make_pre\n");return;}
    temp=pre[i-1];
    while(t[i]!=t[temp]){
  dc-=df;if(dc<0){printf("err in make_pre\n");return;}
      if(temp==0){
	temp=-1;
	break;
      }
      int T=temp-pre[temp-1];
      while(temp+1-pre[temp]==T){
  dc-=df;if(dc<0){printf("err in make_pre\n");return;}
	temp%=T;
	if(temp==0){
	  temp=T;
	  break;
	}
	T=temp-pre[temp-1];
      }
      temp-=T;
    }
    pre[i]=temp+1;
  }
  if(df){
    for(i=0;i<tl;i++){
  dc-=df;if(dc<0){printf("err in make_pre\n");return;}
      printf("%d",i%10);
    }
    printf("\n%s\n",t);
    for(i=0;i<tl;i++){
  dc-=df;if(dc<0){printf("err in make_pre\n");return;}
      printf("%d",pre[i]);
    }
    printf("\n");
  }
}

void make_match(char* s,int sl,char* t,int tl){
  int i=0,temps=0;
  while(temps<sl) {
  dc-=df;if(dc<0){printf("err in make_match\n");return;}
    while(i<tl && temps<sl) {
  dc-=df;if(dc<0){printf("err in make_match\n");return;}
      if(df)print(s,t,i,temps,1);
      if(s[(temps+i)%sl]==t[i]){
	i++;
	continue;
      }
      if(i==0){
	temps++;
	continue;
      }
      int T=i-pre[i-1];
      while(i+1-pre[i]==T){
  dc-=df;if(dc<0){printf("err in make_match\n");return;}
	temps+=i/T*T;
	i%=T;
	if(df)print(s,t,i,temps,-2);
	if(i==0)break;
	T=i-pre[i-1];
      }
      if(i==0)continue;
      temps+=T;
      i=pre[i-1];
      if(df)print(s,t,i,temps,-3);
      if(temps>=sl)break;
    }
    if(i==tl){
      match[temps]=1;
      temps+=i-pre[i-1];
      i=pre[i-1];
      if(df)print(s,t,i,temps,-4);
    }
  }
  
}

void print(char* s,char* t,int i,int temps,int flag){
  int k;
  printf("check %d\n%s\n",flag,s);
  for(k=0;k<temps+i;k++)    printf(" ");
  printf("l check!\n");
  
  for(k=0;k<temps;k++)    printf(" ");
  printf("%s\n",t);

}
int make_ans(int i,int sl,int tl){
  if(match[i]==0)return 0;
  if(ans[i]) return ans[i];
  int s=make_ans((i+tl)%sl,sl,tl)+1;
  ans[i]=s;
  return s;
}
int period(char* t,int tl,int d){
  int i,flag=1;
  for(i=0;i<tl-d;i++){
    if(t[i]!=t[i+d])flag=0;
  }
  return flag;
}
/// confirm df==0 ///