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

#include <bits/stdc++.h>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair

#define loop(i, a, b) for (int i = (a); i < ull(b); ++i)
#define rep(i, n) loop(i, 0, n)

const double eps = 1e-10;
const double pi = acos(-1.0);
const double inf = (int)1e8;

bool check(string s) {
  for (int i = 0; i < s.size() - 1; i++)
    if (s[i] != s[i + 1])
      return false;
  return true;
}

int main() {
  string s;
  while (cin >> s, s != "0") {
    queue<pair<string, int>> que;
    que.push(make_pair(s, 0));
    int ret = 100;

    map<string, int> m;
    while (que.size()) {
      s = que.front().first;
      int count = que.front().second;
      que.pop();

      if (ret < count)
        continue;
      if (check(s))
        ret = count;

      if (m[s] == 0 || count + 1 < m[s]) {
        string tmp = s;
        m[s] = count + 1;
        string t = "rgb";
        vector<int> v(3);
        v[0] = 0;
        v[1] = 1;
        v[2] = 2;
        map<string, int> mp;
        mp["r"] = 0;
        mp["g"] = 1;
        mp["b"] = 2;
        do {
          for (int i = 0; i < s.size(); i++) {
            s[i] = t[v[mp[s.substr(i, 1)]]];
          }
          m[s] = count + 1;
        } while (next_permutation(v.begin(), v.end()));
        s = tmp;
      } else
        continue;

      for (int i = 0; i < s.size() - 1; i++) {
        if (s[i] != s[i + 1]) {
          string t = s.substr(i, 2);
          sort(t.begin(), t.end());
          if (t == "br")
            t = "gg";
          else if (t == "bg")
            t = "rr";
          else if (t == "gr")
            t = "bb";
          que.push(make_pair(s.substr(0, i) + t + s.substr(i + 2), count + 1));
        }
      }
    }
    if (ret == 100)
      cout << "NA" << endl;
    else
      cout << ret << endl;
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair

#define loop(i, a, b) for (int i = (a); i < ull(b); ++i)
#define rep(i, n) loop(i, 0, n)

const double eps = 1e-10;
const double pi = acos(-1.0);
const double inf = (int)1e8;

bool check(string s) {
  for (int i = 0; i < s.size() - 1; i++)
    if (s[i] != s[i + 1])
      return false;
  return true;
}

vector<string> v;

bool emergence(string s) {
  for (int i = 0; i < v.size(); i++)
    if (v[i] == s)
      return true;
    else
      return false;
}

int main() {
  string s;
  while (cin >> s, s != "0") {
    v.clear();
    queue<pair<string, int>> que;
    que.push(make_pair(s, 0));
    int ret = 9;
    while (que.size()) {
      s = que.front().first;
      int count = que.front().second;
      que.pop();

      if (ret < count)
        continue;
      if (check(s))
        ret = count;
      if (emergence(s))
        continue;
      else
        v.push_back(s);

      for (int i = 0; i < s.size() - 1; i++) {
        if (s[i] != s[i + 1]) {
          string t = s.substr(i, 2);
          sort(t.begin(), t.end());
          if (t == "br")
            t = "gg";
          else if (t == "bg")
            t = "rr";
          else if (t == "gr")
            t = "bb";
          que.push(make_pair(s.substr(0, i) + t + s.substr(i + 2), count + 1));
        }
      }
    }
    if (ret == 9)
      cout << "NA" << endl;
    else
      cout << ret << endl;
  }
}