Problem p01863 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ull> vul;
typedef complex<double> P;
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = 1; i < n; ++i)

#define DEBUG(x) cout << #x << ": " << x << endl
#define DEBUG_VEC(v)                                                           \\
  REP(i, v.size()) cout << #v << "[" << i << "]: " << v[i] << endl
#define ALL(a) (a).begin(), (a).end()

// suffix-array????????????????????????????????????????????????

string s;
int len;

const ull B = 100000007;
vul sh(1000001);
vul bs(1000001);
// arihon P.333

void makeHash() {
  bs[0] = 1;
  REPR(i, len + 1) bs[i] = bs[i - 1] * B;
  sh[0] = 0;
  REPR(i, len + 1) sh[i] = sh[i - 1] * B + s[i - 1];
}

ull getHash(int left, int right) {
  if (left == 0)
    return sh[right];
  return sh[right] - sh[left] * bs[right - left];
}

bool equal(int from, int tlen, int to) {
  // ??????????????°???????????\\??????
  ull ah = getHash(from, from + tlen);
  ull bh = getHash(to, to + tlen);
  return ah == bh;
}

bool check(int lena, int lenb) {
  // if(lena<=0 || lenb<=0)return false;

  // A in center
  if (!equal(0, lena, lena + lenb))
    return false;
  if (!equal(0, lena, 2 * (lena + lenb)))
    return false;

  // B
  if (!equal(lena, lenb, 2 * lena + lenb))
    return false;
  return true;
}

int main() {
  cin >> s;
  len = s.size();
  makeHash();

  int minlenab = len;
  string WA = "mitomerarenaiWA";
  int result = len;
  // ABABA -> 3*A+2*B
  REPR(lena, len / 3 + 1) {
    if ((len - (lena * 3)) % 2 != 0)
      continue;
    int lenb = (len - (lena * 3)) / 2;
    if (lena <= 0 || lenb <= 0)
      continue;
    if (lena + lenb > minlenab)
      continue;

    if (check(lena, lenb)) {
      minlenab = lena + lenb;
      result = lena + lenb;
    }
    // bool ok = true;
    // REP(_,2)REP(i,lena){
    //   if(s[i]!=s[(lena+lenb)*(_+1)+i])ok=false;
    //   if(!ok)break;
    // }
    // REP(i,lenb){
    //   if(s[lena+i]!=s[lena*2+lenb+i])ok=false;
    //   if(!ok)break;
    // }
    // if(ok){
    //   minlenab = lena+lenb;
    //   result = lena+lenb;
    // }
  }
  if (result == len)
    cout << WA << endl;
  else
    cout << "Love " << s.substr(0, result) << "!" << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef complex<double> P;
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = 1; i < n; ++i)

#define DEBUG(x) cout << #x << ": " << x << endl
#define DEBUG_VEC(v)                                                           \\
  REP(i, v.size()) cout << #v << "[" << i << "]: " << v[i] << endl
#define ALL(a) (a).begin(), (a).end()

int main() {
  string s;
  cin >> s;
  int len = s.size();
  int minlenab = len;
  string result = "mitomerarenaiWA";
  // ABABA -> 3*A+2*B
  REPR(lena, len / 3 + 1) {
    if ((len - (lena * 3)) % 2 != 0)
      continue;
    int lenb = (len - (lena * 3)) / 2;
    if (lena == 0 || lenb == 0)
      continue;
    if (lena + lenb > minlenab)
      continue;
    bool ok = true;
    REP(_, 2) REP(i, lena) {
      if (s[i] != s[(lena + lenb) * (_ + 1) + i])
        ok = false;
      if (!ok)
        break;
    }
    REP(i, lenb) {
      if (s[lena + i] != s[lena * 2 + lenb + i])
        ok = false;
      if (!ok)
        break;
    }
    if (ok) {
      minlenab = lena + lenb;
      result = s.substr(0, lena + lenb);
    }
  }
  if (result != "mitomerarenaiWA")
    cout << "Love ";
  cout << result;
  if (result != "mitomerarenaiWA")
    cout << "!";
  cout << endl;
  return 0;
}