Problem p02954 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rev(i, n) for (int i = n; i >= 0; i--)
#define Rep(i, m, n) for (int i = m; i < n; i++)
#define repeatrev(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 ll long long
#define pb(a) push_back(a)
#define INF 999999999
#define itn int

using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;
typedef priority_queue<int> Pr;

int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s;
  cin >> s;
  int left = 0;
  rep(i, s.size()) if (s[i] == 'L') left++;
  int a[s.size()], b[s.size()];
  memset(a, 0, sizeof(a));
  memset(b, 0, sizeof(b));
  if (s.size() - left > left) {
    repeatrev(i, s.size() - 1, 0) {
      int count = 0;
      if (s[i] == 'L') {
        if (i < s.size() - 1 && s[i + 1] == 'L') {
          if (s[b[i + 1]] == 'R') {
            a[b[i + 1] + 1]++;
            b[i] = b[i + 1] + 1;
          } else {
            a[b[i + 1] - 1]++;
            b[i] = b[i + 1] - 1;
          }
        } else {
          int j = i;
          while (s[j] != 'R') {
            j--;
            count++;
          }
          if (count % 2 == 0) {
            a[j]++;
            b[i] = j;
          } else {
            a[j + 1]++;
            b[i] = j + 1;
          }
        }
      } else {
        int j = i;
        while (s[j] != 'L') {
          j++;
          count++;
        }
        if (count % 2 == 0) {
          a[j]++;
          b[i] = j;
        } else {
          a[j - 1]++;
          b[i] = j - 1;
        }
      }
    }
  } else {
    rep(i, s.size()) {
      int count = 0;
      if (s[i] == 'L') {
        int j = i;
        while (s[j] != 'R') {
          j--;
          count++;
        }
        if (count % 2 == 0) {
          a[j]++;
          b[i] = j;
        } else {
          a[j + 1]++;
          b[i] = j + 1;
        }
      } else {
        if (i >= 1 && s[i - 1] == 'R') {
          if (s[b[i - 1]] == 'L') {
            a[b[i - 1] - 1]++;
            b[i] = b[i - 1] - 1;
          } else {
            a[b[i - 1] + 1]++;
            b[i] = b[i - 1] + 1;
          }
        } else {

          int j = i;
          while (s[j] != 'L') {
            j++;
            count++;
          }
          if (count % 2 == 0) {
            a[j]++;
            b[i] = j;
          } else {
            a[j - 1]++;
            b[i] = j - 1;
          }
        }
      }
    }
  }

  rep(i, s.size()) cout << a[i] << " ";
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rev(i, n) for (int i = n; i >= 0; i--)
#define Rep(i, m, n) for (int i = m; i < n; i++)
#define repeatrev(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 ll long long
#define pb(a) push_back(a)
#define INF 999999999
#define itn int

using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;
typedef priority_queue<int> Pr;

int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s;
  cin >> s;
  int left = 0;
  rep(i, s.size()) if (s[i] == 'L') left++;
  int a[s.size()], b[s.size()];
  memset(a, 0, sizeof(a));
  memset(b, 0, sizeof(b));
  if (s.size() - left < left) {
    repeatrev(i, s.size() - 1, 0) {
      int count = 0;
      if (s[i] == 'L') {
        if (i < s.size() - 1 && s[i + 1] == 'L') {
          if (s[b[i + 1]] == 'R') {
            a[b[i + 1] + 1]++;
            b[i] = b[i + 1] + 1;
          } else {
            a[b[i + 1] - 1]++;
            b[i] = b[i + 1] - 1;
          }
        } else {
          int j = i;
          while (s[j] != 'R') {
            j--;
            count++;
          }
          if (count % 2 == 0) {
            a[j]++;
            b[i] = j;
          } else {
            a[j + 1]++;
            b[i] = j + 1;
          }
        }
      } else {
        int j = i;
        while (s[j] != 'L') {
          j++;
          count++;
        }
        if (count % 2 == 0) {
          a[j]++;
          b[i] = j;
        } else {
          a[j - 1]++;
          b[i] = j - 1;
        }
      }
    }
  } else {
    rep(i, s.size()) {
      int count = 0;
      if (s[i] == 'L') {
        int j = i;
        while (s[j] != 'R') {
          j--;
          count++;
        }
        if (count % 2 == 0) {
          a[j]++;
          b[i] = j;
        } else {
          a[j + 1]++;
          b[i] = j + 1;
        }
      } else {
        if (i >= 1 && s[i - 1] == 'R') {
          if (s[b[i - 1]] == 'L') {
            a[b[i - 1] - 1]++;
            b[i] = b[i - 1] - 1;
          } else {
            a[b[i - 1] + 1]++;
            b[i] = b[i - 1] + 1;
          }
        } else {

          int j = i;
          while (s[j] != 'L') {
            j++;
            count++;
          }
          if (count % 2 == 0) {
            a[j]++;
            b[i] = j;
          } else {
            a[j - 1]++;
            b[i] = j - 1;
          }
        }
      }
    }
  }

  rep(i, s.size()) cout << a[i] << " ";
}