Problem p04018 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fr first
#define sc second
#define pb push_back
#define ll long long
#define maxheap priority_queue<int>
#define minheap priority_queue<int, vector<int>, greater<int>>
const long double eps = 1e-2;
const long double pi = acos(-1.0);
using namespace std;

vector<int> z_function(string s) {
  int n = (int)s.length();
  vector<int> z(n);
  for (int i = 1, l = 0, r = 0; i < n; ++i) {
    if (i <= r)
      z[i] = min(r - i + 1, z[i - l]);
    while (i + z[i] < n && s[z[i]] == s[i + z[i]])
      ++z[i];
    if (i + z[i] - 1 > r)
      l = i, r = i + z[i] - 1;
  }
  return z;
}

vector<bool> get(string s) {
  int n = s.length();
  vector<bool> bol(n, false);
  vector<int> z = z_function(s);
  for (int i = 1; i < n; i++) {
    for (int j = 2 * i - 1; j < n; j += i) {
      if (z[j - i + 1] < i)
        break;
      bol[j] = true;
    }
  }
  return bol;
}

void solve() {
  string s;
  cin >> s;
  bool ok = false;
  for (int i = 0; i < s.length() - 1; i++)
    if (s[i] != s[i + 1])
      ok = true;

  if (!ok) {
    cout << s.length() << endl;
    cout << 1 << endl;
    return;
  }

  vector<bool> bol1, bol2;
  bol1 = get(s);
  reverse(s.begin(), s.end());
  bol2 = get(s);

  if (bol1[s.length() - 1] == false) {

    cout << 1 << endl;
    cout << 1 << endl;
    return;
  }

  int ans = 0;
  for (int i = 0; i < s.length() - 1; i++) {
    if (!bol1[i] && !bol2[s.length() - i - 2])
      ans++;
  }
  cout << 2 << endl;
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0);
  cout.setf(ios::fixed), cout.precision(3);
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  int step;
  step = 1;
  for (int i = 1; i <= step; i++) {
    solve();
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define fr first
#define sc second
#define pb push_back
#define ll long long
#define maxheap priority_queue<int>
#define minheap priority_queue<int, vector<int>, greater<int>>
const long double eps = 1e-2;
const long double pi = acos(-1.0);
using namespace std;

vector<int> z_function(string s) {
  int n = (int)s.length();
  vector<int> z(n);
  for (int i = 1, l = 0, r = 0; i < n; ++i) {
    if (i <= r)
      z[i] = min(r - i + 1, z[i - l]);
    while (i + z[i] < n && s[z[i]] == s[i + z[i]])
      ++z[i];
    if (i + z[i] - 1 > r)
      l = i, r = i + z[i] - 1;
  }
  return z;
}

vector<bool> get(string s) {
  int n = s.length();
  vector<bool> bol(n, false);
  vector<int> z = z_function(s);
  for (int i = 1; i < n; i++) {
    for (int j = 2 * i - 1; j < n; j += i) {
      if (z[j - i + 1] < i)
        break;
      bol[j] = true;
    }
  }
  return bol;
}

void solve() {
  string s;
  cin >> s;
  bool ok = false;
  for (int i = 0; i < s.length() - 1; i++)
    if (s[i] != s[i + 1])
      ok = true;

  if (!ok) {
    cout << s.length() << endl;
    cout << 1 << endl;
    return;
  }

  vector<bool> bol1, bol2;
  bol1 = get(s);
  reverse(s.begin(), s.end());
  bol2 = get(s);

  if (bol1[s.length() - 1] == false) {

    cout << 1 << endl;
    cout << 1 << endl;
    return;
  }

  int ans = 0;
  for (int i = 0; i < s.length() - 1; i++) {
    if (!bol1[i] && !bol2[s.length() - i - 2])
      ans++;
  }
  cout << 2 << endl;
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0);
  cout.setf(ios::fixed), cout.precision(3);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  int step;
  step = 1;
  for (int i = 1; i <= step; i++) {
    solve();
  }
}