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

// clear time: 01:55
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;
typedef pair<string, int> bug;
typedef map<string, int> closeMap;

int isOK(string str) {
  // cout << "str:" << str << " " << str.size() << endl;
  for (int i = 0; i < str.size(); i++) {
    if (str[0] != str[i])
      return 0;
  }
  return 1;
}

char cc(char a, char b) {
  // 並び替え
  if (b < a) {
    char tmp = a;
    a = b;
    b = tmp;
  }

  if (a == 'b' && b == 'g') {
    return 'r';
  } else if (a == 'b' && b == 'r') {
    return 'g';
  } else {
    return 'b';
  }
}

void wfs(string str) {
  int cnt = -1;
  string first = str;
  queue<bug> open;
  closeMap close;

  // 初期状態をプッシュ
  open.push(make_pair(str, 0));

  close.insert(closeMap::value_type(str, 1));
  // cout << str << " key:" << close[str] << endl;

  while (!open.empty()) {
    bug b = open.front();
    open.pop();
    string cs = b.first;

    if (isOK(cs)) {
      cnt = b.second;
      break;
    }

    int c = 0;
    for (int i = 1; i < cs.size(); i++) {
      if (cs[i - 1] != cs[i]) {
        string tmp = cs;
        char nc = cc(cs[i - 1], cs[i]);
        tmp[i - 1] = nc;
        tmp[i] = nc;

        if (close[tmp])
          continue;
        open.push(make_pair(tmp, b.second + 1));
        close[tmp] = 1;
        c++;
      }
    }
  }

  if (cnt == -1) {
    cout << "NA" << endl;
  } else {
    cout << cnt << endl;
  }
}

int main() {
  string n;

  while (cin >> n, n != "0") {
    wfs(n);
  }

  return 0;
}

Error: TLE

Fixed submission:

// clear time: 01:55
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;
typedef pair<string, int> bug;

int isOK(string str) {
  // cout << "str:" << str << " " << str.size() << endl;
  for (int i = 0; i < str.size(); i++) {
    if (str[0] != str[i])
      return 0;
  }
  return 1;
}

char cc(char a, char b) {
  // 並び替え
  if (b < a) {
    char tmp = a;
    a = b;
    b = tmp;
  }

  if (a == 'b' && b == 'g') {
    return 'r';
  } else if (a == 'b' && b == 'r') {
    return 'g';
  } else {
    return 'b';
  }
}

int has(vector<string> &list, string str) {
  vector<string>::iterator sit = find(list.begin(), list.end(), str);
  return sit != list.end();
}

void wfs(string str) {
  int cnt = -1;
  string first = str;
  queue<bug> open;
  vector<string> closed;

  // 初期状態をプッシュ
  open.push(make_pair(str, 0));

  closed.push_back(str);

  while (!open.empty()) {
    bug b = open.front();
    open.pop();
    string cs = b.first;

    if (isOK(cs)) {
      cnt = b.second;
      break;
    }

    int c = 0;
    for (int i = 1; i < cs.size(); i++) {
      if (cs[i - 1] != cs[i]) {
        string tmp = cs;
        char nc = cc(cs[i - 1], cs[i]);
        tmp[i - 1] = nc;
        tmp[i] = nc;

        if (has(closed, tmp))
          continue;
        open.push(make_pair(tmp, b.second + 1));
        closed.push_back(tmp);
        c++;
      }
    }
  }

  if (cnt == -1) {
    cout << "NA" << endl;
  } else {
    cout << cnt << endl;
  }
}

int main() {
  string n;

  while (cin >> n, n != "0") {
    wfs(n);
  }

  return 0;
}