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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
int main() {
  map<string, string> q;
  q["bg"] = q["gb"] = "rr";
  q["gr"] = q["rg"] = "bb";
  q["rb"] = q["br"] = "gg";
  string s;
  while (cin >> s, s != "0") {
    char qqq = s[0];
    bool h = true;
    rep(i, s.size()) if (s[i] != qqq) h = false;
    if (h) {
      cout << 0 << endl;
      continue;
    }
    vector<string> in;
    map<string, bool> tmp;
    in.pb(s);
    int co = 0;
    while (1) {
      vector<string> out;
      bool h = false;
      rep(i, in.size()) {
        string s = in[i];
        tmp[s] = true;
        // h=true;
        // char qq=in[i][0];
        // rep(j,s.size())if(qq!=in[i][j])h=false;
        // if(h)break;
        rep(j, s.size() - 1) if (s[j] != s[j + 1]) {
          string www = s.substr(j, 2);
          string w = q[www];
          string ww = s;
          ww[j] = w[0];
          ww[j + 1] = w[1];
          h = true;
          char qq = ww[0];
          rep(k, ww.size()) if (qq != ww[k]) h = false;
          if (h)
            break;
          if (!tmp[ww]) {
            out.pb(ww);
            tmp[ww] = true;
          }
        }
        if (h)
          break;
      }
      // cout<<endl;
      // rep(i,out.size())cout<<out[i]<<endl;
      // cout<<tmp.size()<<endl;
      // cout<<out.size()<<endl;
      if (h)
        break;
      co++;
      in = out;
      // cout<<" "<<in.size()<<endl;
      if (co == 100)
        break;
    }
    if (co == 100)
      cout << "NA" << endl;
    else
      cout << co + 1 << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
int main() {
  map<string, string> q;
  q["bg"] = q["gb"] = "rr";
  q["gr"] = q["rg"] = "bb";
  q["rb"] = q["br"] = "gg";
  string s;
  while (cin >> s, s != "0") {
    vector<string> in;
    map<string, bool> tmp;
    in.pb(s);
    int co = 0;
    while (1) {
      vector<string> out;
      bool h;
      rep(i, in.size()) {
        string s = in[i];
        tmp[s] = true;
        h = true;
        char qq = in[i][0];
        rep(j, s.size()) if (qq != in[i][j]) h = false;
        if (h)
          break;
        rep(j, s.size() - 1) if (s[j] != s[j + 1]) {
          string www = s.substr(j, 2);
          string w = q[www];
          string ww = s;
          ww[j] = w[0];
          ww[j + 1] = w[1];
          if (tmp[ww] == false)
            out.pb(ww);
        }
      }
      if (h)
        break;
      co++;
      in = out;
      // cout<<" "<<in.size()<<endl;
      if (co == 10)
        break;
    }
    if (co == 11)
      cout << "NA" << endl;
    else
      cout << co << endl;
  }
}