Problem p04018 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
long long MOD = 1000000000 + 7;
constexpr long long INF = numeric_limits<LL>::max();
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr << #x << ": " << x << '\n'
typedef pair<LL, LL> Pll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll {
  LL first, second, third;
};
struct Fll {
  LL first, second, third, fourd;
};
typedef pair<LL, Tll> Ptll;
#define rep(i, rept) for (LL i = 0; i < rept; i++)
#define Mfor(i, mf) for (LL i = mf - 1; i >= 0; i--)
LL h, w, n, m, k, t, s, p, q, last, first, cnt, sum, ans, dp[200000], a[300000],
    b[300000];
string str, ss;
bool f;
char c, ch[100][100];
int di[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
struct Edge {
  LL to, cost;
};
vector<Edge> vec[200000];
vector<LL> v;
map<LL, LL> ma;
multiset<LL> st;
void YN(bool f) {
  if (f)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void yn(bool f) {
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

vector<int> Z_algorithm(string S) {
  vector<int> A(S.size());
  A[0] = S.size();
  int i = 1, j = 0;
  while (i < S.size()) {
    while (i + j < S.size() && S[j] == S[i + j])
      ++j;
    A[i] = j;
    if (j == 0) {
      ++i;
      continue;
    }
    int k = 1;
    while (i + k < S.size() && k + A[k] < j)
      A[i + k] = A[k], ++k;
    i += k;
    j -= k;
  }
  return A;
}

int main() {
  cin >> str;
  rep(i, str.size() - 1) {
    if (str[i] != str[i + 1])
      f = 1;
  }
  if (f == 0) {
    cout << str.size() << endl;
    cout << 1 << endl;
    return 0;
  }
  for (int i = 1; i <= str.size() / 2; i++) {
    if (str.size() % i == 0) {
      string wo;
      rep(j, i) { wo.push_back(str[j]); }
      bool F = 0;
      rep(j, str.size() / i - 1) {
        rep(k, i) {
          if (str[(j + 1) * i + k] != wo[k]) {
            F = 1;
            break;
          }
        }
      }
      if (F == 0) {
        ss = wo;
        break;
      }
    }
  }
  if (ss == "") {
    cout << 1 << endl;
    cout << 1 << endl;
    return 0;
  }
  vector<int> zal = Z_algorithm(str);
  bool flag[200000];
  rep(i, str.size()) { flag[i] = 1; }
  for (int i = 1; i < zal.size(); i++) {
    for (int p = 2; (p - 1) * i <= zal[i]; p++) {
      flag[p * i] = 0;
    }
  }
  reverse(str.begin(), str.end());
  zal = Z_algorithm(str);
  for (int i = 1; i < zal.size(); i++) {
    for (int p = 2; (p - 1) * i <= zal[i]; p++) {
      flag[str.size() - p * i] = 0;
    }
  }
  rep(i, str.size()) if (flag[i]) ans++;
  cout << 2 << endl;
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
long long MOD = 1000000000 + 7;
constexpr long long INF = numeric_limits<LL>::max();
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr << #x << ": " << x << '\n'
typedef pair<LL, LL> Pll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll {
  LL first, second, third;
};
struct Fll {
  LL first, second, third, fourd;
};
typedef pair<LL, Tll> Ptll;
#define rep(i, rept) for (LL i = 0; i < rept; i++)
#define Mfor(i, mf) for (LL i = mf - 1; i >= 0; i--)
LL h, w, n, m, k, t, s, p, q, last, first, cnt, sum, ans, dp[200000], a[300000],
    b[300000];
string str, ss;
bool f;
char c, ch[100][100];
int di[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
struct Edge {
  LL to, cost;
};
vector<Edge> vec[200000];
vector<LL> v;
map<LL, LL> ma;
multiset<LL> st;
void YN(bool f) {
  if (f)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
void yn(bool f) {
  if (f)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

vector<int> Z_algorithm(string S) {
  vector<int> A(S.size());
  A[0] = S.size();
  int i = 1, j = 0;
  while (i < S.size()) {
    while (i + j < S.size() && S[j] == S[i + j])
      ++j;
    A[i] = j;
    if (j == 0) {
      ++i;
      continue;
    }
    int k = 1;
    while (i + k < S.size() && k + A[k] < j)
      A[i + k] = A[k], ++k;
    i += k;
    j -= k;
  }
  return A;
}

int main() {
  cin >> str;
  rep(i, str.size() - 1) {
    if (str[i] != str[i + 1])
      f = 1;
  }
  if (f == 0) {
    cout << str.size() << endl;
    cout << 1 << endl;
    return 0;
  }
  for (int i = 1; i <= str.size() / 2; i++) {
    if (str.size() % i == 0) {
      string wo;
      rep(j, i) { wo.push_back(str[j]); }
      bool F = 0;
      rep(j, str.size() / i - 1) {
        rep(k, i) {
          if (str[(j + 1) * i + k] != wo[k]) {
            F = 1;
            break;
          }
        }
      }
      if (F == 0) {
        ss = wo;
        break;
      }
    }
  }
  if (ss == "") {
    cout << 1 << endl;
    cout << 1 << endl;
    return 0;
  }
  vector<int> zal = Z_algorithm(str);
  bool flag[600000];
  rep(i, str.size()) { flag[i] = 1; }
  for (int i = 1; i < zal.size(); i++) {
    for (int p = 2; (p - 1) * i <= zal[i]; p++) {
      flag[p * i] = 0;
    }
  }
  reverse(str.begin(), str.end());
  zal = Z_algorithm(str);
  for (int i = 1; i < zal.size(); i++) {
    for (int p = 2; (p - 1) * i <= zal[i]; p++) {
      flag[str.size() - p * i] = 0;
    }
  }
  rep(i, str.size()) if (flag[i]) ans++;
  cout << 2 << endl;
  cout << ans << endl;
  return 0;
}