Problem p00179 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second

static const double PI = 6 * asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<int, int> P;
static const int INF = 1 << 24;

string s;
string qf;
set<string> vis;

bool check(string a) {
  char t = a[0];
  rep(i, a.size()) {
    if (t != a[i])
      return false;
  }
  return true;
}

int main() {

  while (cin >> s, s[0] != '0') {
    // cout<<s<<endl;
    vis.clear();
    int ans = -1;
    queue<string> qu;
    if (check(s)) {
      cout << "0" << endl;
      continue;
    }
    vis.insert(s);
    qu.push(s);
    bool f1 = false;
    bool f2 = false;
    bool f3 = false;
    while (!qu.empty()) {
      f1 = false;
      f2 = false;
      ans++;
      int qs = qu.size();
      rep(i, qs) {
        qf = qu.front();
        qu.pop();
        rep(j, s.size() - 1) {
          if (qf[j] != qf[j + 1]) {
            string tmp = qf;
            tmp[j] = tmp[j + 1] = 'r' + 'g' + 'b' - qf[j] - qf[j + 1];
            if (vis.find(tmp) == vis.end()) {
              qu.push(tmp);
              vis.insert(tmp);
              f1 = true;
              if (check(tmp))
                f3 = true;
            }
            f2 = true;
          }
        }
      }
      if (f3)
        break;
    }
    if (f3)
      cout << ans + 1 << endl;
    else
      cout << "NA" << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second

static const double PI = 6 * asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<int, int> P;
static const int INF = 1 << 24;

string s;
int qf;

int foo1(int D[]) {
  int res = 0;
  for (int i = 0; i < s.size(); i++) {
    res *= 10;
    res += D[i];
  }
  return res;
}
void foo2(int D[], int m) {
  rep(j, s.size()) { D[j] = 0; }
  for (int i = s.size() - 1; i >= 0; --i) {
    D[i] = m % 10;
    m /= 10;
    rep(j, s.size()) {}
  }
}

int a[30];
int tmp[30];
set<long int> vis;

bool check() {
  int t = a[0];
  rep(i, s.size()) {
    if (t != a[i])
      return false;
  }
  return true;
}

int main() {

  while (cin >> s, s[0] != '0') {
    vis.clear();
    int ans = 0;
    queue<int> qu;
    rep(i, s.size()) {
      if (s[i] == 'r')
        a[i] = 0;
      else if (s[i] == 'g')
        a[i] = 1;
      else if (s[i] == 'b')
        a[i] = 2;
    }
    if (check()) {
      cout << "0" << endl;
      continue;
    }

    qu.push(foo1(a));
    vis.insert(foo1(a));
    bool f = false;
    bool f1 = false;
    int c = 0;
    while (!qu.empty() && !f1) {
      c++;
      if (c > 50)
        break;
      f = false;
      ans++;
      int qs = qu.size();
      rep(i, qs) {
        qf = qu.front();
        qu.pop();
        foo2(a, qf);
        if (check()) {
          cout << ans - 1 << endl;
          f1 = true;
          break;
        }
        rep(j, s.size() - 1) {
          if (a[j] != a[j + 1]) {
            rep(k, s.size()) { tmp[k] = a[k]; }
            if (a[j] * a[j + 1] == 2) {
              tmp[j] = 0;
              tmp[j + 1] = 0;
            } else if (a[j] == 2 || a[j + 1] == 2) {
              tmp[j] = 1;
              tmp[j + 1] = 1;
            } else {
              tmp[j] = 2;
              tmp[j + 1] = 2;
            }
            if (vis.find(foo1(tmp)) == vis.end()) {
              qu.push(foo1(tmp));
              vis.insert(foo1(tmp));
              f = true;
            }
          }
        }
        if (!f)
          break;
      }
      if (f1)
        break;
    }
    if (f1)
      continue;
    if (!f && !f1)
      cout << "NA" << endl;
  }

  return 0;
}