Problem p03480 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define INF 999999999

using namespace std;

int dy[4] = {0, 0, 1, -1};
int dx[4] = {1, -1, 0, 0};
int dxx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
int dyy[8] = {1, -1, 0, 1, -1, 0, 1, -1};

ll gcd(ll x, ll y) {
  ll m = max(x, y), n = min(x, y);
  if (m % n == 0)
    return n;
  else
    return gcd(m % n, n);
}

int main() {
  string S;
  cin >> S;
  ll l = S.length();
  int h = l / 2;
  bool ok = true;
  int cnt = 1;
  while (ok) {
    if (l % 2 == 1) {
      char a = S[h];
      int k = 1;
      while (k <= h) {
        if (S[h - k] != a || S[h + k] != a) {
          cout << h + k << endl;
          return 0;
        }
        k++;
      }
      cout << l << endl;
      return 0;
    }
    if (l % 2 == 0) {
      char a = S[h];
      char b = S[h - 1];
      if (a != b) {
        cout << h << endl;
        return 0;
      }
      int k = 1;
      while (k <= h) {
        if (S[h - 1] != S[h - 1 - k] || S[h] != S[h + k]) {
          cout << h + k << endl;
          return 0;
        }
      }
      cout << l << endl;
      return 0;
    }
  }
}

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define INF 999999999

using namespace std;

int dy[4] = {0, 0, 1, -1};
int dx[4] = {1, -1, 0, 0};
int dxx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
int dyy[8] = {1, -1, 0, 1, -1, 0, 1, -1};

ll gcd(ll x, ll y) {
  ll m = max(x, y), n = min(x, y);
  if (m % n == 0)
    return n;
  else
    return gcd(m % n, n);
}

int main() {
  string S;
  cin >> S;
  ll l = S.length();
  int h = l / 2;
  bool ok = true;
  int cnt = 1;
  while (ok) {
    if (l % 2 == 1) {
      char a = S[h];
      int k = 1;
      while (k <= h) {
        if (S[h - k] != a || S[h + k] != a) {
          cout << h + k << endl;
          return 0;
        }
        k++;
      }
      cout << l << endl;
      return 0;
    }
    if (l % 2 == 0) {
      char a = S[h];
      char b = S[h - 1];
      if (a != b) {
        cout << h << endl;
        return 0;
      }
      int k = 1;
      while (k <= h) {
        if (S[h - 1] != S[h - 1 - k] || S[h] != S[h + k]) {
          cout << h + k << endl;
          return 0;
        }
        k++;
      }
      cout << l << endl;
      return 0;
    }
  }
}