Problem p04018 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <utility>
#include <vector>

using namespace std;

template <typename T, typename S>
istream &operator>>(istream &is, pair<T, S> &p) {
  is >> p.first >> p.second;
  return is;
}

template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &x : v) {
    is >> x;
  }
  return is;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto &x : v) {
    os << x << " ";
  }
  os << "\n";
  return os;
}

vector<int> get_pf(const string &s) {
  int n = (int)s.length();
  vector<int> pi(n);
  for (int i = 0; i < n; ++i)
    for (int k = 0; k <= i; ++k)
      if (s.substr(0, k) == s.substr(i - k + 1, k))
        pi[i] = k;
  return pi;
}

vector<int> get_goodness(const string &s) {
  vector<int> result(s.size() + 1, 0);
  auto pf = get_pf(s);
  for (int i = 2; i <= s.size(); ++i) {
    result[i] = int(pf[i - 1] > 0 && (i % (i - pf[i - 1])) == 0);
  }
  return result;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  string s;
  cin >> s;
  int n = s.size();
  {
    bool is_all_one_c = true;
    for (int i = 1; i < n && is_all_one_c; ++i) {
      if (s[i] != s[0])
        is_all_one_c = false;
    }
    if (is_all_one_c) {
      cout << n << "\n" << 1 << "\n";
      return 0;
    }
  }
  // vector<int> pref_good(n + 1, false), suff_good(n + 1, false);
  auto pref_good = get_goodness(s);
  reverse(s.begin(), s.end());
  auto suff_good = get_goodness(s);
  // cout << pref_good << suff_good;
  /*
  for (int k = 1; k < n; ++k) {
      // if (n % k > 0) continue;
      if (!suff_good[k]) {
          for (int st_i = k; st_i + k <= n; ++st_i) {
              if (st_i == k || suff_good[st_i]) {
                  suff_good[st_i + k] = true;
                  for (int i = 0; i < k && suff_good[st_i + k]; ++i) {
                      if (s[n - 1 - i] != s[n - 1 - st_i - i]) suff_good[st_i +
  k] = false;
                  }
              }
          }
      }
      if (!pref_good[k]) {
          for (int st_i = k; st_i + k <= n; ++st_i) {
              if (st_i == k || pref_good[st_i]) {
                  pref_good[st_i + k] = true;
                  for (int i = 0; i < k && pref_good[st_i + k]; ++i) {
                      if (s[i] != s[st_i + i]) pref_good[st_i + k] = false;
                  }
              }
          }
      }
  }
  // cout << pref_good << suff_good;
  */
  if (!suff_good[n]) {
    cout << "1\n1";
  } else {
    cout << "2\n";
    int ans = 0;
    for (int l = 1; l < n; ++l) {
      if (!pref_good[l] && !suff_good[n - l])
        ++ans;
    }
    cout << ans;
  }
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <utility>
#include <vector>

using namespace std;

template <typename T, typename S>
istream &operator>>(istream &is, pair<T, S> &p) {
  is >> p.first >> p.second;
  return is;
}

template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &x : v) {
    is >> x;
  }
  return is;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto &x : v) {
    os << x << " ";
  }
  os << "\n";
  return os;
}

vector<int> get_pf(const string &s) {
  int n = (int)s.length();
  vector<int> pi(n);
  for (int i = 1; i < n; ++i) {
    int j = pi[i - 1];
    while (j > 0 && s[i] != s[j])
      j = pi[j - 1];
    if (s[i] == s[j])
      ++j;
    pi[i] = j;
  }
  return pi;
}

vector<int> get_goodness(const string &s) {
  vector<int> result(s.size() + 1, 0);
  auto pf = get_pf(s);
  for (int i = 2; i <= s.size(); ++i) {
    result[i] = int(pf[i - 1] > 0 && (i % (i - pf[i - 1])) == 0);
  }
  return result;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  string s;
  cin >> s;
  int n = s.size();
  {
    bool is_all_one_c = true;
    for (int i = 1; i < n && is_all_one_c; ++i) {
      if (s[i] != s[0])
        is_all_one_c = false;
    }
    if (is_all_one_c) {
      cout << n << "\n" << 1 << "\n";
      return 0;
    }
  }
  // vector<int> pref_good(n + 1, false), suff_good(n + 1, false);
  auto pref_good = get_goodness(s);
  reverse(s.begin(), s.end());
  auto suff_good = get_goodness(s);
  // cout << pref_good << suff_good;
  /*
  for (int k = 1; k < n; ++k) {
      // if (n % k > 0) continue;
      if (!suff_good[k]) {
          for (int st_i = k; st_i + k <= n; ++st_i) {
              if (st_i == k || suff_good[st_i]) {
                  suff_good[st_i + k] = true;
                  for (int i = 0; i < k && suff_good[st_i + k]; ++i) {
                      if (s[n - 1 - i] != s[n - 1 - st_i - i]) suff_good[st_i +
  k] = false;
                  }
              }
          }
      }
      if (!pref_good[k]) {
          for (int st_i = k; st_i + k <= n; ++st_i) {
              if (st_i == k || pref_good[st_i]) {
                  pref_good[st_i + k] = true;
                  for (int i = 0; i < k && pref_good[st_i + k]; ++i) {
                      if (s[i] != s[st_i + i]) pref_good[st_i + k] = false;
                  }
              }
          }
      }
  }
  // cout << pref_good << suff_good;
  */
  if (!suff_good[n]) {
    cout << "1\n1";
  } else {
    cout << "2\n";
    int ans = 0;
    for (int l = 1; l < n; ++l) {
      if (!pref_good[l] && !suff_good[n - l])
        ++ans;
    }
    cout << ans;
  }
}