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

#include <iostream>
#include <queue>
#include <set>
#include <string>

using namespace std;

typedef pair<string, int> P;

bool hantei(string a) {
  for (int i = 1; i < a.size(); i++) {
    if (a[0] == a[i]) {
    } else {
      return false;
    }
  }
  return true;
}

int main() {
  string a;
  string abc = "rgb";
  while (getline(cin, a)) {
    if (a[0] == '0') {
      break;
    }
    queue<P> q;
    q.push(P(a, 0));
    bool han = true;
    int t;
    set<string> s;
    if (hantei(a)) {
      han = false;
      cout << 0 << endl;
    }
    while (!q.empty()) {
      P p = q.front();
      q.pop();
      string pp = p.first;
      for (int i = 0; i < pp.size() - 1; i++) {
        if (!han) {
          break;
        }
        if (pp[i] == pp[i + 1]) {
        } else {
          string qq = pp;
          int v, w;
          for (int j = 0; j < abc.size(); j++) {
            if (abc[j] == pp[i]) {
              v = j;
            }
            if (abc[j] == pp[i + 1]) {
              w = j;
            }
          }
          qq[i] = qq[i + 1] = abc[3 - (v + w)];
          if (hantei(qq)) {
            han = false;
            cout << p.second + 1 << endl;
            break;
          }
          if (s.find(qq) == s.end()) {
            s.insert(qq);
            q.push(P(qq, p.second + 1));
          }
        }
      }
    }
    if (han) {
      cout << "NA" << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

typedef pair<string, int> P;

char keisan(char a, char b) {
  if (a == b) {
    return a;
  } else {
    string abc = "rbg";
    int aa, bb;
    for (int i = 0; i < 3; i++) {
      if (a == abc[i]) {
        aa = i;
      }
      if (b == abc[i]) {
        bb = i;
      }
    }
    return abc[3 - (aa + bb)];
  }
}

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

int main() {
  string a;
  while (cin >> a) {
    if (a[0] == '0') {
      break;
    }
    if (hantei(a)) {
      cout << 0 << endl;
    } else {
      queue<P> q;
      vector<string> hyou;
      hyou.push_back(a);
      bool hantei2 = false;
      for (int i = 0; i < a.size() - 1; i++) {
        string c;
        c = a;
        c[i] = keisan(a[i], a[i + 1]);
        c[i + 1] = keisan(a[i], a[i + 1]);
        if (hantei(c)) {
          cout << 1 << endl;
          hantei2 = true;
          break;
        } else {
          P p;
          p.first = c;
          p.second = 2;
          q.push(p);
          hyou.push_back(c);
        }
      }
      while (!q.empty()) {
        if (hantei2) {
          break;
        }
        P p = q.front();
        q.pop();
        string b = p.first;
        for (int i = 0; i < b.size() - 1; i++) {
          string c;
          c = b;
          c[i] = keisan(b[i], b[i + 1]);
          c[i + 1] = keisan(b[i], b[i + 1]);
          if (hantei(c)) {
            cout << p.second << endl;
            hantei2 = true;
            break;
          } else {
            bool hantei3 = true;
            for (int i = 0; i < hyou.size(); i++) {
              if (c == hyou[i]) {
                hantei3 = false;
                break;
              }
            }
            if (hantei3) {
              P pp;
              pp.first = c;
              pp.second = p.second + 1;
              q.push(pp);
              hyou.push_back(c);
            }
          }
        }
      }
      if (!hantei2) {
        cout << "NA" << endl;
      }
    }
  }
  return 0;
}