Problem p02962 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <stdio.h>
#include <vector>

int MP(std::string &S, std::vector<long long int> &A);
int KMP(std::string &S, std::string &word,
        std::vector<long long int> &match_table);

long long int euclid_gcd(long long int x, long long int y);

int main() {

  // read problem
  std::string s, t;
  std::cin >> s;
  std::cin >> t;
  long long int slen = s.length(), tlen = t.length();
  // std::cerr << "s: " << s << std::endl << "t: " << t << std::endl;

  // solve problem
  std::string s_extend = s;
  for (int i = 0; i <= tlen / slen; i++) {
    s_extend = s_extend + s_extend;
  }
  s_extend = s_extend.substr(0, slen + tlen);

  // KMP
  std::vector<long long int> match_table;
  KMP(s_extend, t, match_table);

  // change format of result
  std::vector<long long int> match_index(slen);
  for (long long int i = 0; i < match_table.size(); i++) {
    if (match_table[i] < slen) {
      match_index[match_table[i]] = 1;
    } else {
      break;
    }
  }

  // calculate answer
  long long int gcd_st = euclid_gcd(slen, tlen);
  long long int tmp, ntmp, ntmp_max = 0, start, nstart, eternity_flg;

  for (long long int i = 0; i < gcd_st; i++) {
    tmp = i;
    eternity_flg = 1;
    ntmp = 0;
    nstart = 0;
    for (int j = 0; j < slen / gcd_st; j++) {
      tmp = (tmp + tlen) % slen;
      if (match_index[tmp] == 0) {
        eternity_flg = 0;
        ntmp_max = std::max(ntmp, ntmp_max);
        ntmp = 0;
      } else {
        if (eternity_flg == 1) {
          nstart++;
        }
        ntmp++;
      }
    }

    if (eternity_flg == 1) {
      break;
    }
    ntmp_max = std::max(ntmp_max, nstart + ntmp);
  }

  std::cerr << "Answer:" << std::endl;
  if (eternity_flg == 1) {
    std::cout << -1 << std::endl;
  } else {
    std::cout << ntmp_max << std::endl;
  }

  return 0;
}

int MP(std::string &S, std::vector<long long int> &A) {

  A[0] = -1;
  long long int L = S.length();
  long long int j = -1;

  for (long long int i = 0; i < L; i++) {
    while (j >= 0 && S[i] != S[j])
      j = A[j];
    j++;
    A[i + 1] = j;
  }

  return 0;
}

int KMP(std::string &S, std::string &word,
        std::vector<long long int> &match_table) {

  // preparation: MP calculation
  std::vector<long long int> MP_table(word.length() + 1);
  MP(word, MP_table);
  match_table.clear();

  // make match_table
  long long int m = 0, i = 0, ls = S.length(), lword = word.length();

  while (m + i < ls) {
    if (word[i] == S[m + i]) {
      i++;
      if (i == lword) {
        match_table.push_back(m);
        m = m + i - MP_table[i];
        i = MP_table[i];
      }
    } else {
      m = m + i - MP_table[i];
      if (i > 0) {
        i = MP_table[i];
      }
    }
  }

  return 0;
}

long long int euclid_gcd(long long int x, long long int y) {
  if (y == 0) {
    return x;
  }
  /*else if(x == 0){
    return y;
  }*/
  else {
    return euclid_gcd(y, x % y);
  }
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <stdio.h>
#include <vector>

int MP(std::string &S, std::vector<long long int> &A);
int KMP(std::string &S, std::string &word,
        std::vector<long long int> &match_table);

long long int euclid_gcd(long long int x, long long int y);

int main() {

  // read problem
  std::string s, t;
  std::cin >> s;
  std::cin >> t;
  long long int slen = s.length(), tlen = t.length();
  // std::cerr << "s: " << s << std::endl << "t: " << t << std::endl;

  // solve problem
  std::string s_extend = s;
  for (int i = 0; s_extend.length() < slen + tlen; i++) {
    s_extend = s_extend + s_extend;
  }
  s_extend = s_extend.substr(0, slen + tlen);

  // KMP
  std::vector<long long int> match_table;
  KMP(s_extend, t, match_table);

  // change format of result
  std::vector<long long int> match_index(slen);
  for (long long int i = 0; i < match_table.size(); i++) {
    if (match_table[i] < slen) {
      match_index[match_table[i]] = 1;
    } else {
      break;
    }
  }

  // calculate answer
  long long int gcd_st = euclid_gcd(slen, tlen);
  long long int tmp, ntmp, ntmp_max = 0, start, nstart, eternity_flg;

  for (long long int i = 0; i < gcd_st; i++) {
    tmp = i;
    eternity_flg = 1;
    ntmp = 0;
    nstart = 0;
    for (int j = 0; j < slen / gcd_st; j++) {
      tmp = (tmp + tlen) % slen;
      if (match_index[tmp] == 0) {
        eternity_flg = 0;
        ntmp_max = std::max(ntmp, ntmp_max);
        ntmp = 0;
      } else {
        if (eternity_flg == 1) {
          nstart++;
        }
        ntmp++;
      }
    }

    if (eternity_flg == 1) {
      break;
    }
    ntmp_max = std::max(ntmp_max, nstart + ntmp);
  }

  std::cerr << "Answer:" << std::endl;
  if (eternity_flg == 1) {
    std::cout << -1 << std::endl;
  } else {
    std::cout << ntmp_max << std::endl;
  }

  return 0;
}

int MP(std::string &S, std::vector<long long int> &A) {

  A[0] = -1;
  long long int L = S.length();
  long long int j = -1;

  for (long long int i = 0; i < L; i++) {
    while (j >= 0 && S[i] != S[j])
      j = A[j];
    j++;
    A[i + 1] = j;
  }

  return 0;
}

int KMP(std::string &S, std::string &word,
        std::vector<long long int> &match_table) {

  // preparation: MP calculation
  std::vector<long long int> MP_table(word.length() + 1);
  MP(word, MP_table);
  match_table.clear();

  // make match_table
  long long int m = 0, i = 0, ls = S.length(), lword = word.length();

  while (m + i < ls) {
    if (word[i] == S[m + i]) {
      i++;
      if (i == lword) {
        match_table.push_back(m);
        m = m + i - MP_table[i];
        i = MP_table[i];
      }
    } else {
      m = m + i - MP_table[i];
      if (i > 0) {
        i = MP_table[i];
      }
    }
  }

  return 0;
}

long long int euclid_gcd(long long int x, long long int y) {
  if (y == 0) {
    return x;
  }
  /*else if(x == 0){
    return y;
  }*/
  else {
    return euclid_gcd(y, x % y);
  }
}