alexjercan/bugnet huggingface dataset:
Problem p00179 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;

struct worm {
  int c;
  string s;
};
queue<worm> que;
bool str_check(string str) {
  for (int i = 1; i < str.size(); ++i) {
    if (str[0] != str[i])
      return false;
  }
  return true;
}
int f() {
  map<string, int> check;
  while (que.size()) {
    worm w = que.front(), tmp;
    que.pop();
    int n = w.c;
    string str = w.s;
    if (str_check(str)) {
      return n;
    }
    for (int i = 0; i < str.size() - 1; ++i) {
      if (str[i] != str[i + 1]) {
        if ((str[i] == 'r' && str[i + 1] == 'b') ||
            (str[i] == 'b' && str[i + 1] == 'r')) {
          str[i] = str[i + 1] = 'g';
        } else if ((str[i] == 'g' && str[i + 1] == 'b') ||
                   (str[i] == 'b' && str[i + 1] == 'g')) {
          str[i] = str[i + 1] = 'r';
        } else {
          str[i] = str[i + 1] = 'b';
        }

        if (!check.count(str)) {
          tmp.s = str;
          tmp.c = n + 1;
          que.push(tmp);
          check[str] = n + 1;
        }
        str = w.s;
      }
    }
  }
  return -1;
}
int main() {
  string str;
  worm w;
  while (cin >> str) {
    if (str == "0")
      break;
    w.s = str;
    w.c = 0;
    que.push(w);
    int n = f();
    if (n == -1)
      cout << "NA\\n";
    else
      cout << n << endl;
    while (que.size()) {
      que.pop();
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
#include <string>
using namespace std;

struct worm {
  int c;
  string s;
};
queue<worm> que;
bool str_check(string str) {
  for (int i = 1; i < str.size(); ++i) {
    if (str[0] != str[i])
      return false;
  }
  return true;
}
int f() {
  while (que.size()) {
    worm w = que.front(), tmp;
    que.pop();
    int n = w.c;
    if (n > 9)
      break;
    string str = w.s;
    if (str_check(str)) {
      return n;
    }
    for (int i = 0; i < str.size() - 1; ++i) {
      if (str[i] != str[i + 1]) {
        if ((str[i] == 'r' && str[i + 1] == 'b') ||
            (str[i] == 'b' && str[i + 1] == 'r')) {
          str[i] = str[i + 1] = 'g';
        } else if ((str[i] == 'g' && str[i + 1] == 'b') ||
                   (str[i] == 'b' && str[i + 1] == 'g')) {
          str[i] = str[i + 1] = 'r';
        } else {
          str[i] = str[i + 1] = 'b';
        }
        tmp.s = str;
        tmp.c = n + 1;
        que.push(tmp);
        str = w.s;
      }
    }
  }
  return -1;
}
int main() {
  string str;
  worm w;
  while (cin >> str) {
    if (str == "0")
      break;
    w.s = str;
    w.c = 0;
    que.push(w);
    int n = f();
    if (n == -1)
      cout << "NA\\n";
    else
      cout << n << endl;
    while (que.size()) {
      que.pop();
    }
  }
  return 0;
}
Problem p00179 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <map>
#include <queue>
#define INF 10000000
using namespace std;
typedef pair<string, int> P;
int cnt;

int bfs(string worm) {
  map<string, int> used;
  queue<P> que;
  que.push(P(worm, 0));
  string next;

  while (!que.empty()) {
    char tmp;
    P p = que.front();
    que.pop();
    if (used[p.first] == 1)
      continue;
    bool f = true;
    string str = p.first;
    for (int i = 0; i < p.first.size() - 1; i++) {
      if (str[i] != str[i + 1])
        f = false;
    }
    if (f)
      return p.second;
    used[p.first] = 1;
    for (int i = 0; i < worm.size() - 1; i++) {
      if (str[i] != str[i + 1]) {
        if (str[i] != 'r' && str[i + 1] != 'r') {
          tmp = 'r';
        } else if (str[i] != 'g' && str[i + 1] != 'g') {
          tmp = 'g';
        } else {
          tmp = 'b';
        }
        next = str;
        next[i] = next[i + 1] = tmp;
        que.push(P(next, p.second + 1));
      }
    }
  }
  return -1;
}

int main() {
  string worm;
  while (cin >> worm) {
    if (worm[0] == '0')
      break;
    cnt = 0;
    int ret = bfs(worm);
    if (ret == -1) {
      cout << "NA" << endl;
    } else
      cout << ret << endl;
  }
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#define INF 10000000
using namespace std;
typedef pair<string, int> P;
int cnt;

int bfs(string worm) {
  queue<P> que;
  que.push(P(worm, cnt));
  string next;
  char tmp;
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    bool f = true;
    for (int i = 0; i < p.first.size() - 1; i++) {
      if (worm[i] != worm[i + 1])
        f = false;
    }
    if (f)
      return p.second;
    for (int i = 0; i < worm.size() - 1; i++) {
      if (worm[i] != worm[i + 1]) {
        if (worm[i] != 'r' && worm[i + 1] != 'r') {
          tmp = 'r';
        } else if (worm[i] != 'g' && worm[i + 1] != 'g') {
          tmp = 'g';
        } else {
          tmp = 'b';
        }
        next = worm;
        next[i] = next[i + 1] = tmp;
        que.push(P(next, cnt + 1));
      }
    }
  }
  return -1;
}

int main() {
  string worm;
  while (cin >> worm) {
    if (worm[0] == '0')
      break;
    cnt = 0;
    int ret = bfs(worm);
    if (ret == -1) {
      cout << "NA" << endl;
    } else
      cout << ret << endl;
  }
  return 0;
}
Problem p00179 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

const int MOD = 1e9 + 7;
const int iINF = 1000000000;
const long long int llINF = 1000000000000000000;
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
using ll = long long int;
using P = pair<string, int>;
using edge = struct {
  int to;
  int cost;
};

int wfs(string worm) {
  queue<P> que;
  que.push(P(worm, 0));

  map<string, int> mp;

  while (!que.empty()) {
    P p = que.front();
    que.pop();

    string tmp = p.first;

    if (mp[p.first])
      continue;

    bool flag = true;

    for (int i = 0; i < tmp.size() - 1; i++) {
      if (tmp[i] != tmp[i + 1]) {
        flag = false;
        break;
      }
    }

    if (flag)
      return p.second;

    mp[p.first] = p.second;

    for (int i = 0; i < tmp.size() - 1; i++) {
      if (tmp[i] == tmp[i + 1])
        continue;

      string nworm = tmp;

      if (tmp[i] != 'g' && tmp[i + 1] != 'g') {
        nworm[i] = 'g';
        nworm[i + 1] = 'g';
      } else if (tmp[i] != 'b' && tmp[i + 1] != 'b') {
        nworm[i] = 'b';
        nworm[i + 1] = 'b';
      } else {
        nworm[i] = 'r';
        nworm[i + 1] = 'r';
      }

      que.push(P(nworm, p.second + 1));
    }
  }
  return -1;
}

int main() {
  string input;

  while (cin >> input && input != "0") {
    cout << (wfs(input) != -1 ? to_string(wfs(input)) : "NA") << endl;
  }

  return 0;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

const int MOD = 1e9 + 7;
const int iINF = 1000000000;
const long long int llINF = 1000000000000000000;
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
using ll = long long int;
using P = pair<string, int>;
using edge = struct {
  int to;
  int cost;
};

int wfs(string worm) {
  queue<P> que;
  que.push(P(worm, 0));

  map<string, int> mp;

  while (!que.empty()) {
    P p = que.front();
    que.pop();

    string tmp = p.first;

    // if(mp[p.first]) continue;

    bool flag = true;

    for (int i = 0; i < tmp.size() - 1; i++) {
      if (tmp[i] != tmp[i + 1]) {
        flag = false;
        break;
      }
    }

    if (flag)
      return p.second;

    // mp[p.first] = p.second;

    for (int i = 0; i < tmp.size() - 1; i++) {
      if (tmp[i] == tmp[i + 1])
        continue;

      string nworm = tmp;

      if (tmp[i] != 'g' && tmp[i + 1] != 'g') {
        nworm[i] = 'g';
        nworm[i + 1] = 'g';
      } else if (tmp[i] != 'b' && tmp[i + 1] != 'b') {
        nworm[i] = 'b';
        nworm[i + 1] = 'b';
      } else {
        nworm[i] = 'r';
        nworm[i + 1] = 'r';
      }

      que.push(P(nworm, p.second + 1));
    }
  }
  return -1;
}

int main() {
  string input;

  while (cin >> input && input != "0") {
    cout << (wfs(input) ? to_string(wfs(input)) : "NA") << endl;
  }

  return 0;
}
Problem p00179 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;

typedef pair<string, int> P;

int bfs(string worm) {
  queue<P> que;
  que.push(P(worm, 0));

  map<string, int> d;

  while (!que.empty()) {
    P p = que.front();
    que.pop();

    // 重複パターンはcontinue
    if (d[p.first])
      continue;

    string str = p.first;

    // 全色同じかどうかのチェック
    bool flag = false;
    for (int i = 0; i < str.size() - 1; i++) {
      if (str[i] != str[i + 1]) {
        flag = true;
        break;
      }
    }
    // 同じならreturn
    if (!flag)
      return p.second;

    // 重複パターン判別用
    d[p.first] = p.second;

    for (int i = 0; i < str.size() - 1; i++) {
      // 隣り合わせで色が異なるとき
      if (str[i] != str[i + 1]) {
        char temp;
        string next;

        // tempに含まれない一色を代入
        if (str[i] != 'b' && str[i + 1] != 'b') {
          temp = 'b';
        } else if (str[i] != 'g' && str[i + 1] != 'g') {
          temp = 'g';
        } else {
          temp = 'r';
        }

        // 変化後の順番をnextに詰め込む
        next = str;
        next[i] = next[i + 1] = temp;

        // キューにプッシュ
        que.push(P(next, p.second + 1));
      }
    }
  }
  return -1;
}

int main() {
  string worm;
  while (cin >> worm && worm != "0") {
    int ret = bfs(worm);
    if (ret >= 0)
      cout << ret << endl;
    else
      cout << "NA" << endl;
  }
}

Error: MLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <map>
#include <queue>
using namespace std;

typedef pair<string, int> P;

int bfs(string worm) {
  queue<P> que;
  que.push(P(worm, 0));

  map<string, int> d;

  while (!que.empty()) {
    P p = que.front();
    que.pop();

    if (d[p.first])
      continue;

    string str = p.first;

    bool flag = true;
    for (int i = 0; i < str.size() - 1; i++) {
      if (str[i] != str[i + 1]) {
        flag = false;
        break;
      }
    }

    if (flag)
      return p.second;

    for (int i = 0; i < str.size() - 1; i++) {
      if (str[i] != str[i + 1]) {
        string next;
        char tmp;
        if (str[i] != 'r' && str[i + 1] != 'r') {
          tmp = 'r';
        } else if (str[i] != 'b' && str[i + 1] != 'r') {
          tmp = 'b';
        } else {
          tmp = 'g';
        }

        next = str;
        next[i] = next[i + 1] = tmp;
        que.push(P(next, p.second + 1));
      }
    }
  }
  return -1;
}

int main(void) {
  string worm;
  while (cin >> worm && worm != "0") {
    int ans = bfs(worm);
    if (ans >= 0)
      cout << ans << endl;
    else
      cout << "NA" << endl;
  }

  return 0;
}
Problem p00179 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <queue>
#include <string>

using namespace std;

const int inf = 99999;

struct St {
  string worm;
  int cnt;
};

typedef map<string, bool> Mp;

bool check(string str) {
  bool f = true;

  for (int i = 1; i < str.size(); i++) {
    if (str[i] != str[0]) {
      f = false;
      break;
    }
  }

  return f;
}

char getC(char s, char t) {
  if ((s == 'r' && t == 'g') || (s == 'g' && t == 'r')) {
    return 'b';
  }
  if ((s == 'r' && t == 'b') || (s == 'b' && t == 'r')) {
    return 'g';
  }

  return 'r';
}

int bfs(string str) {
  int r = inf + 1;
  queue<St> que;
  map<string, bool> mpp;
  St st1, st2;
  st1.worm = str;
  st1.cnt = 0;
  que.push(st1);
  mpp[str] = true;

  while (!que.empty()) {
    st1 = que.front();
    que.pop();
    if (check(st1.worm) || st1.cnt > inf) {
      r = st1.cnt;
      break;
    }

    for (int i = 0; i < (st1.worm).size() - 1; i++) {
      string s1 = st1.worm;
      if (s1[i] != s1[i + 1]) {
        s1[i] = s1[i + 1] = getC(s1[i], s1[i + 1]);
        if (!mpp[s1]) {
          mpp[s1] = true;
          st2.worm = s1;
          st2.cnt = st1.cnt + 1;
          que.push(st2);
        }
      }
    }
  }

  return r;
}

int main() {
  int t;
  string str;

  while (cin >> str && str != "0") {
    t = bfs(str);
    if (t > inf) {
      cout << "NA" << endl;
    } else {
      cout << t << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

struct St {
  string worm;
  int cnt;
};

bool check(string str) {
  bool f = true;

  for (int i = 1; i < str.size(); i++) {
    if (str[i] != str[0]) {
      f = false;
      break;
    }
  }

  return f;
}

int bfs(string str) {
  int r = 101;
  vector<St> vec;
  St stt, stt2;
  stt.worm = str;
  stt.cnt = 0;

  vec.push_back(stt);
  while (!vec.empty()) {
    stt = vec[0];
    vec.erase(vec.begin());

    if (check(stt.worm) || stt.cnt > 10) {
      r = stt.cnt;
      break;
    }

    for (int i = 0; i < (stt.worm).size() - 1; i++) {
      string s = stt.worm;
      if (s[i] != s[i + 1]) {
        if ((s[i] == 'r' && s[i + 1] == 'g') ||
            (s[i] == 'g' && s[i + 1] == 'r')) {
          s[i] = s[i + 1] = 'b';
        } else if ((s[i] == 'r' && s[i + 1] == 'b') ||
                   (s[i] == 'b' && s[i + 1] == 'r')) {
          s[i] = s[i + 1] = 'g';
        } else {
          s[i] = s[i + 1] = 'r';
        }

        bool f = true;
        for (int j = 0; j < vec.size(); j++) {

          if (vec[j].worm == s) {
            f = false;
            break;
          }
        }

        if (f) {
          stt2.worm = s;
          stt2.cnt = stt.cnt + 1;
          vec.push_back(stt2);
        }
      }
    }
  }

  return r;
}

int main() {
  int t;
  string str;

  while (cin >> str && str != "0") {

    t = bfs(str);
    if (t > 10) {
      cout << "NA" << endl;
    } else {
      cout << t << endl;
    }
  }

  return 0;
}
Problem p00179 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <string>
using namespace std;
class Data {
public:
  string bc;
  int t;
  Data(string bc, int t) : bc(bc), t(t) {}
  Data() {}
};
bool judge(string s, int l) {
  for (int i = 0; i < l - 1; ++i) {
    if (s[i] != s[i + 1])
      return false;
  }
  return true;
}
char cj(char a, char b) {
  if (a == 'b' && b == 'g')
    return 'r';
  if (a == 'b' && b == 'r')
    return 'g';
  if (a == 'g' && b == 'r')
    return 'b';
  swap(a, b);
  if (a == 'b' && b == 'g')
    return 'r';
  if (a == 'b' && b == 'r')
    return 'g';
  if (a == 'g' && b == 'r')
    return 'b';
}
int main() {
  string s;
  while (cin >> s, s != "0") {
    int l = s.length();
    int res = -1;
    queue<Data> Q;
    set<string> visited;
    Q.push(Data(s, 0));
    while (!Q.empty()) {
      Data t = Q.front();
      Q.pop();
      if (visited.count(t.bc))
        continue;
      if (judge(t.bc, l)) {
        res = t.t;
        break;
      }
      visited.insert(t.bc);
      for (int i = 0; i < l - 1; ++i) {
        if (t.bc[i] == t.bc[i + 1])
          continue;
        char nc = cj(t.bc[i], t.bc[i + 1]);
        string nbc = t.bc;
        nbc[i] = nbc[i + 1] = nc;
        Q.push(Data(nbc, t.t + 1));
      }
    }
    if (res == -1)
      cout << "NA" << endl;
    else
      cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <string>
using namespace std;
class Data {
public:
  string bc;
  int t;
  Data(string bc, int t) : bc(bc), t(t) {}
  Data() {}
};
bool judge(string s, int l) {
  for (int i = 0; i < l - 1; ++i) {
    if (s[i] != s[i + 1])
      return false;
  }
  return true;
}
char cj(char a, char b) {
  if (a == 'b' && b == 'g')
    return 'r';
  if (a == 'b' && b == 'r')
    return 'g';
  if (a == 'g' && b == 'r')
    return 'b';
  swap(a, b);
  if (a == 'b' && b == 'g')
    return 'r';
  if (a == 'b' && b == 'r')
    return 'g';
  if (a == 'g' && b == 'r')
    return 'b';
}
int main() {
  string s;
  while (cin >> s, s != "s") {
    int l = s.length();
    int res = -1;
    queue<Data> Q;
    set<string> visited;
    Q.push(Data(s, 0));
    while (!Q.empty()) {
      Data t = Q.front();
      Q.pop();
      if (visited.count(t.bc))
        continue;
      if (judge(t.bc, l)) {
        res = t.t;
        break;
      }
      visited.insert(t.bc);
      for (int i = 0; i < l - 1; ++i) {
        if (t.bc[i] == t.bc[i + 1])
          continue;
        char nc = cj(t.bc[i], t.bc[i + 1]);
        string nbc = t.bc;
        nbc[i] = nbc[i + 1] = nc;
        Q.push(Data(nbc, t.t + 1));
      }
    }
    if (res == -1)
      cout << "NA" << endl;
    else
      cout << res << endl;
  }
  return 0;
}
Problem p00179 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

const int INF = 1e+9;
typedef pair<string, int> P;

int ans = 0;
int s_size;

string change(string s, int a, int b) {
  if ((s[a] == 'r' && s[b] == 'g') || (s[a] == 'g' && s[b] == 'r'))
    s[b] = s[a] = 'b';
  else if ((s[a] == 'b' && s[b] == 'g') || (s[a] == 'g' && s[b] == 'b'))
    s[b] = s[a] = 'r';
  else
    s[b] = s[a] = 'g';

  return s;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  string str;
  while (cin >> str) {
    if (str == "0")
      break;
    s_size = str.size();
    ans = INF;
    queue<P> que;
    map<string, bool> visited;
    que.push(P(str, 0));
    while (!que.empty()) {
      P p = que.front();
      que.pop();
      string s = p.first;
      if (visited[s])
        continue;
      visited[s] = true;
      bool flag = false;
      for (int i = 0; i < s_size - 1; ++i) {
        if (s[i] != s[i + 1])
          break;
        if (i == s_size - 2) {
          ans = min(ans, p.second);
          flag = true;
          break;
        }
      }
      if (flag)
        continue;
      for (int i = 0; i < s_size - 1; ++i) {
        if (s[i] != s[i + 1]) {
          string t = change(s, i, i + 1);
          que.push(P(t, p.second + 1));
        }
      }
    }
  end:
    if (ans == INF)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

const int INF = 1e+9;
typedef pair<int, int> P;

int ans = 0;
int s_size;

string change(string s, int a, int b) {
  if ((s[a] == 'r' && s[b] == 'g') || (s[a] == 'g' && s[b] == 'r'))
    s[b] = s[a] = 'b';
  else if ((s[a] == 'b' && s[b] == 'g') || (s[a] == 'g' && s[b] == 'b'))
    s[b] = s[a] = 'r';
  else
    s[b] = s[a] = 'g';

  return s;
}

void bfs(string s, int res) {
  if (res >= ans)
    return;
  for (int i = 0; i < s_size - 1; ++i) {
    if (s[i] != s[i + 1])
      break;
    if (i == s_size - 2) {
      ans = min(res, ans);
      return;
    }
  }
  if (res >= s_size)
    return;
  for (int i = 0; i < s_size - 1; ++i) {
    if (s[i] != s[i + 1]) {
      string str = change(s, i, i + 1);
      bfs(str, res + 1);
    }
  }
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  string s;
  while (cin >> s) {
    if (s == "0")
      break;
    s_size = s.size();
    ans = INF;
    bfs(s, 0);
    if (ans == INF)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}
Problem p00179 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
#include <string>
using namespace std;
queue<int> q;
int t[59049];
int INF = 100000;
int encode(vector<int> vc) {
  int p = 0;
  for (int i = 0; i < vc.size(); i++) {
    p = p * 3 + vc[i];
  }
  return p;
}
vector<int> decode(int p, int n) {
  vector<int> vc(n);
  for (int i = 0; i < n; i++) {
    vc[n - i - 1] = p % 3;
    p /= 3;
  }
  return vc;
}
int main() {
  while (true) {
    for (int i = 0; i < 59049; i++)
      t[i] = INF;
    string s;
    cin >> s;
    if (s == "0")
      break;
    int n = s.size(), start = 0, goal = 1;
    for (int i = 0; i < n; i++)
      goal *= 3;
    goal -= 1;
    for (int i = 0; i < n; i++) {
      if (s[i] == 'r')
        start = start * 3 + 2;
      if (s[i] == 'g')
        start = start * 3 + 1;
      if (s[i] == 'b')
        start = start * 3;
    }
    q.push(start);
    t[start] = 0;
    int ok = -1;
    while (q.size()) {
      int p = q.front();
      if (p == 0 || p == goal / 2 || p == goal) {
        ok = p;
        while (q.size())
          q.pop();
        break;
      }
      q.pop();
      vector<int> vc = decode(p, n);
      for (int i = 0; i < n - 1; i++) {
        if (vc[i] == vc[i + 1])
          continue;
        vector<int> vc_c = vc;
        vc_c[i] = vc_c[i + 1] = ((vc[i] + vc[i + 1]) * 2) % 3;
        int hoge = encode(vc_c);
        if (t[hoge] != INF)
          continue;
        q.push(hoge);
        t[hoge] = t[p] + 1;
      }
    }
    if (ok == -1)
      cout << "NA" << endl;
    else
      cout << t[ok] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <string>
using namespace std;
queue<int> q;
int t[59049];
int INF = 100000;
int encode(vector<int> vc) {
  int p = 0;
  for (int i = 0; i < vc.size(); i++) {
    p = p * 3 + vc[i];
  }
  return p;
}
vector<int> decode(int p, int n) {
  vector<int> vc(n);
  for (int i = 0; i < n; i++) {
    vc[n - i - 1] = p % 3;
    p /= 3;
  }
  return vc;
}
int main() {
  while (true) {
    for (int i = 0; i < 59049; i++)
      t[i] = INF;
    string s;
    cin >> s;
    int n = s.size(), start = 0, goal = 1;
    for (int i = 0; i < n; i++)
      goal *= 3;
    goal -= 1;
    for (int i = 0; i < n; i++) {
      if (s[i] == 'r')
        start = start * 3 + 2;
      if (s[i] == 'g')
        start = start * 3 + 1;
      if (s[i] == 'b')
        start = start * 3;
    }
    q.push(start);
    t[start] = 0;
    int ok = -1;
    while (q.size()) {
      int p = q.front();
      if (p == 0 || p == goal / 2 || p == goal) {
        ok = p;
        while (q.size())
          q.pop();
        break;
      }
      q.pop();
      vector<int> vc = decode(p, n);
      for (int i = 0; i < n - 1; i++) {
        if (vc[i] == vc[i + 1])
          continue;
        vector<int> vc_c = vc;
        vc_c[i] = vc_c[i + 1] = ((vc[i] + vc[i + 1]) * 2) % 3;
        int hoge = encode(vc_c);
        if (t[hoge] != INF)
          continue;
        q.push(hoge);
        t[hoge] = t[p] + 1;
      }
    }
    if (ok == -1)
      cout << "NA" << endl;
    else
      cout << t[ok] << endl;
  }
  return 0;
}