alexjercan/bugnet huggingface dataset:
Problem p00178 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
#define MH 5000
using namespace std;
bool ck(int *f) {
  rep(i, 5) if (!f[i]) return 0;
  return 1;
}
int main() {
  int n;
  while (cin >> n, n) {
    int d, l, x, y, c = 0;
    int field[MH][5] = {0};
    rep(i, n) {
      cin >> d >> l >> x, y = MH, x--, d--;
      for (; y > 0; y--)
        rep(j, (d ? 1 : l)) if (field[y - 1][x + j]) goto END;
    END:
      rep(j, l) field[y + d * j][x + (1 - d) * j] = 1;
      rep(i, MH) while (ck(field[i])) {
        rep(j, MH - i - 1) rep(k, 5) field[j + i][k] = field[j + i + 1][k];
        rep(k, 5) field[MH - 1][k] = 0;
      }
    }
    rep(i, MH) rep(j, 5) c += field[i][j];
    cout << c << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
#define MH 100
using namespace std;
bool ck(int *f) {
  rep(i, 5) if (!f[i]) return 0;
  return 1;
}
int main() {
  int n;
  while (cin >> n, n) {
    int d, l, x, y, c = 0;
    int field[MH][5] = {0};
    rep(i, n) {
      cin >> d >> l >> x, y = MH, x--, d--;
      for (; y > 0; y--)
        rep(j, (d ? 1 : l)) if (field[y - 1][x + j]) goto END;
    END:
      rep(j, l) field[y + d * j][x + (1 - d) * j] = 1;
      rep(i, MH) while (ck(field[i])) {
        rep(j, MH - i - 1) rep(k, 5) field[j + i][k] = field[j + i + 1][k];
        rep(k, 5) field[MH - 1][k] = 0;
      }
    }
    rep(i, MH) rep(j, 5) c += field[i][j];
    cout << c << endl;
  }
  return 0;
}
Problem p00178 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)

int n, hmax;
bool maps[6000][5];

int serch(int x, int l) {
  for (int i = hmax; i >= 0; i--) {
    REP(j, l) if (maps[i][x + j]) return i + 1;
  }
  return 0;
}

int check(int y, int l) {
  REP(i, l) {
    bool flag = true;
    REP(j, 5) if (!maps[y + i][j]) flag = false;
    if (flag)
      return y + i;
  }
  return -1;
}

void bset(int d, int p, int q) {
  int hl = d == 2 ? p : 1;
  int xl = d == 1 ? p : 1;
  int hpos = serch(q - 1, xl);

  if (d == 1)
    REP(i, p) maps[hpos][q + i - 1] = true;
  else
    REP(i, p) maps[hpos + i][q - 1] = true;
  hmax = max(hmax, hpos + hl);

  int tmp;
  while ((tmp = check(hpos, hl)) != -1) {
    REP(i, 5) maps[tmp][i] = false;
    FOR(i, tmp, hmax) { REP(j, 5) swap(maps[i][j], maps[i + 1][j]); }
    hmax--;
  }
}

int main() {
  while (cin >> n, n) {
    memset(maps, 0, sizeof(maps));
    hmax = 0;

    while (n--) {
      int d, p, q;
      scanf("%d %d %d", &d, &p, &q);
      bset(d, p, q);
    }
    cout << count(maps[0], maps[6000], true) << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)

int n, hmax;
bool maps[6000][5];

int serch(int x, int l) {
  for (int i = hmax; i >= 0; i--) {
    REP(j, l) if (maps[i][x + j]) return i + 1;
  }
  return 0;
}

int check(int y, int l) {
  FOR(i, y, hmax) {
    bool flag = true;
    REP(j, 5) if (!maps[i][j]) flag = false;
    if (flag)
      return i;
  }
  return -1;
}

void bset(int d, int p, int q) {
  int hl = d == 1 ? p : 1;
  int hpos = serch(q - 1, hl);

  if (d == 1)
    REP(i, p) maps[hpos][q + i - 1] = true;
  else
    REP(i, p) maps[hpos + i][q - 1] = true;
  hmax += max(hmax, hpos + hl);

  int tmp;
  while ((tmp = check(hpos, hl)) != -1) {
    REP(i, 5) maps[tmp][i] = false;
    FOR(i, tmp, hmax) { REP(j, 5) swap(maps[i][j], maps[i + 1][j]); }
    hmax--;
  }
}

int main() {
  while (cin >> n, n) {
    memset(maps, 0, sizeof(maps));
    hmax = 0;

    while (n--) {
      int d, p, q;
      scanf("%d %d %d", &d, &p, &q);
      bset(d, p, q);

      cout << count(maps[0], maps[6000], true) << endl;
    }
  }
  return 0;
}
Problem p00178 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

char stage[2][5000][5];

int main() {
  int n;

  while (scanf("%d", &n) && n) {
    int height[5] = {0};
    int turn = 0;
    int ct = 0;
    memset(stage, 0, sizeof(stage));
    for (int rep = 0; rep < n; rep++) {
      int d, p, q;
      scanf("%d %d %d", &d, &p, &q);
      --q;
      if (d == 1) {
        int mh = *max_element(height + q, height + p + q);
        for (int j = q; j < p + q; j++) {
          stage[turn][mh][j] = 1;
          height[j] = mh + 1;
        }
      } else {
        for (int j = 0; j < p; j++) {
          stage[turn][height[q]++][q] = 1;
        }
      }

      int look = *max_element(height, height + 5);
      memset(stage[1 - turn], 0, sizeof(stage[1 - turn]));
      memset(height, 0, sizeof(height));
      ct = 0;
      int nh = 0;
      for (int i = 0; i < look; i++) {
        bool ok = true;
        for (int j = 0; j < 5; j++) {
          if (stage[turn][i][j] == 0)
            ok = false;
        }
        if (!ok) {
          for (int j = 0; j < 5; j++) {
            stage[1 - turn][nh][j] = stage[turn][i][j];
            if (stage[1 - turn][nh][j]) {
              height[j] = nh + 1;
              ct++;
            }
          }
          nh++;
        }
      }
      turn = 1 - turn;
    }

    printf("%d\\n", ct);
  }

  return (0);
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

char stage[2][5001][5];

void place(int *height, int col, int d, int len, int pos, int turn) {
  if (len == 0)
    return;
  if (d == 1) {
    int mh = *max_element(height + pos, height + pos + len);
    for (int j = pos; j < pos + len; j++) {
      stage[turn][mh][j] = col;
      height[j] = mh + 1;
    }
  } else {
    for (int j = 0; j < len; j++) {
      stage[turn][height[pos]++][pos] = col;
    }
  }
  return;
}

int main() {
  int n;

  while (scanf("%d", &n) && n) {
    int height[5] = {0};
    int d[1001] = {0}, len[1001] = {0}, pos[1001] = {0};

    memset(stage, 0, sizeof(stage));

    for (int i = 1; i <= n; i++) {
      scanf("%d %d %d", d + i, len + i, pos + i);
      --pos[i];
      place(height, i, d[i], len[i], pos[i], 0);
    }

    int turn = 0;
    for (int i = 0; i < n; i++) {
      int ht = *max_element(height, height + 5);

      for (int j = 0; j < ht; j++) {
        bool ok = true;
        for (int k = 0; k < 5; k++) {
          stage[1 - turn][j][k] = 0;
          if (!stage[turn][j][k])
            ok = false;
        }
        if (ok) {
          for (int k = 0; k < 5; k++) {
            len[stage[turn][j][k]]--;
          }
        }
      }

      memset(height, 0, sizeof(height));
      for (int j = 1; j <= n; j++) {
        place(height, j, d[j], len[j], pos[j], 1 - turn);
      }
      turn = 1 - turn;
    }

    int ret = 0;
    int h = *max_element(height, height + 5);
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < 5; j++) {
        ret += !!stage[turn][i][j];
      }
    }

    printf("%d\\n", ret);
  }

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

#include <bits/stdc++.h>

#define FOR(i, n, m) for (int i = (n); i < (m); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define PII pair<int, int>

using namespace std;

string worm;

int all(string a) {
  char b = a[0];
  int cnt = 0;
  for (int i = 1; a[i] != '\\0'; i++) {
    if (a[i] != b)
      cnt++;
  }
  return cnt;
}

int solve() {
  queue<pair<string, int>> que;
  map<string, int> m;
  que.push(pair<string, int>(worm, 0));

  if (all(worm) == 0) {
    return 0;
  }

  while (que.size()) {
    string worm1 = que.front().first;
    int num = que.front().second;
    que.pop();

    if (m[worm1]) {
      continue;
    }
    if (all(worm1) == 0) {
      return num;
    }

    m[worm1] = num;

    REP(i, worm1.size() - 1) {
      string tmp = worm1;
      if (tmp[i] != tmp[i + 1]) {
        if (tmp[i] != 'r' && tmp[i + 1] != 'r')
          tmp[i] = tmp[i + 1] = 'r';
        else if (tmp[i] != 'g' && tmp[i + 1] != 'g')
          tmp[i] = tmp[i + 1] = 'g';
        else
          tmp[i] = tmp[i + 1] = 'b';

        que.push(pair<string, int>(tmp, num + 1));
      }
    }
  }

  return -1;
}

int main(void) {
  cin >> worm;
  while (worm[0] != '0') {
    int result = solve();
    if (result >= 0) {
      cout << result << endl;
    } else {
      cout << "NA" << endl;
    }
    cin >> worm;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

#define FOR(i, n, m) for (int i = (n); i < (m); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define PII pair<int, int>

using namespace std;

string worm;

int all(string a) {
  char b = a[0];
  int cnt = 0;
  for (int i = 1; a[i] != '\\0'; i++) {
    if (a[i] != b)
      cnt++;
  }
  return cnt;
}

int solve() {
  queue<pair<string, int>> que;
  map<string, int> m;
  que.push(pair<string, int>(worm, 0));
  string worm1, tmp;
  int num;

  if (all(worm) == 0) {
    return 0;
  }

  while (que.size()) {
    worm1 = que.front().first;
    num = que.front().second;
    que.pop();

    if (m[worm1])
      continue;

    if (all(worm1) == 1) {
      continue;
    } else if (all(worm1) == 0) {
      return num;
    }

    if (num >= 10) {
      return -1;
    }
    for (int i = 0; i < strlen(worm1.c_str()) - 1; i++) {
      tmp = worm1;
      if (worm1[i] != worm1[i + 1]) {
        if ((worm1[i] == 'b' && worm1[i + 1] == 'g') ||
            (worm1[i] == 'g' && worm1[i + 1] == 'b')) {
          tmp[i] = tmp[i + 1] = 'r';
          que.push(pair<string, int>(tmp, num + 1));
        } else if ((worm1[i] == 'b' && worm1[i + 1] == 'r') ||
                   (worm1[i] == 'r' && worm1[i + 1] == 'b')) {
          tmp[i] = tmp[i + 1] = 'g';
          que.push(pair<string, int>(tmp, num + 1));
        } else {
          tmp[i] = tmp[i + 1] = 'b';
          que.push(pair<string, int>(tmp, num + 1));
        }
      }
    }
  }

  return -1;
}

int main(void) {
  cin >> worm;
  while (worm[0] != '0') {
    int result = solve();
    if (result >= 0) {
      cout << result << endl;
    } else {
      cout << "NA" << endl;
    }
    cin >> worm;
  }

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

#include <iostream>
#include <queue>
#include <set>
#include <string>
using namespace std;
char conv(char c1, char c2) {
  if (c1 == 'r') {
    if (c2 == 'g')
      return 'b';
    if (c2 == 'b')
      return 'g';
  } else if (c1 == 'g') {
    if (c2 == 'r')
      return 'b';
    if (c2 == 'b')
      return 'r';
  } else if (c1 == 'b') {
    if (c2 == 'r')
      return 'g';
    if (c2 == 'g')
      return 'r';
  }
}
int bfs(string s) {
  queue<pair<int, string>> qs;
  set<string> vis;
  qs.push(make_pair(0, s));
  while (!qs.empty()) {
    bool bFound = false;
    int ret = qs.front().first;
    string now = qs.front().second;
    qs.pop();

    // cout << vis.size() << endl;
    if (vis.find(now) != vis.end()) {
      continue;
    }
    vis.insert(now);
    for (int i = 0; i < now.length() - 1; ++i) {
      if (now[i] != now[i + 1]) {
        bFound = true;
        string next = now;
        char nc = conv(now[i], now[i + 1]);
        next[i] = next[i + 1] = nc;
        qs.push(make_pair(ret + 1, next));
      }
    }
    if (!bFound) {
      // cout << now << endl;
      return ret;
    }
  }
  return -1;
}
int main() {
  while (true) {
    string s;
    cin >> s;
    if (s == "0")
      break;
    int ans = bfs(s);
    if (ans < 0)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <string>
using namespace std;
char conv(char c1, char c2) {
  if (c1 == 'r') {
    if (c2 == 'g')
      return 'b';
    if (c2 == 'b')
      return 'g';
  } else if (c1 == 'g') {
    if (c2 == 'r')
      return 'b';
    if (c2 == 'b')
      return 'r';
  } else if (c1 == 'b') {
    if (c2 == 'r')
      return 'g';
    if (c2 == 'g')
      return 'r';
  }
}
int bfs(string s) {
  queue<pair<int, string>> qs;
  qs.push(make_pair(0, s));
  while (!qs.empty()) {
    bool bFound = false;
    int ret = qs.front().first;
    string now = qs.front().second;
    if (ret > s.length())
      break;
    qs.pop();
    for (int i = 0; i < now.length() - 1; ++i) {
      if (now[i] != now[i + 1]) {
        bFound = true;
        string next = now;
        char nc = conv(now[i], now[i + 1]);
        next[i] = next[i + 1] = nc;
        qs.push(make_pair(ret + 1, next));
      }
    }
    if (!bFound) {
      // cout << now << endl;
      return ret;
    }
  }
  return -1;
}
int main() {
  while (true) {
    string s;
    cin >> s;
    if (s == "0")
      break;
    int ans = bfs(s);
    if (ans < 0)
      cout << "NA" << endl;
    else
      cout << ans << 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> bug;

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

char change(char a, char b) {
  if (a > b)
    swap(a, b);
  if (a == 'b' && b == 'g')
    return 'r';
  else if (a == 'b' && b == 'r')
    return 'g';
  else
    return 'b';
}

void bfs(string str) {
  map<string, int> close;
  queue<bug> open;
  open.push(make_pair(str, 0));
  close.insert(make_pair(str, 1));
  int cnt = -1;
  while (!open.empty()) {
    bug b = open.front();
    open.pop();
    string insect = b.first;
    if (check(insect)) {
      cnt = b.second;
      break;
    }

    for (int i = 0; i < insect.size() - 1; i++) {
      string tmp = insect;
      if (tmp[i] != tmp[i + 1]) {
        char nc = change(tmp[i], tmp[i + 1]);
        tmp[i] = nc;
        tmp[i + 1] = nc;
        if (close[tmp])
          continue;
        open.push(make_pair(tmp, b.second + 1));
        close[tmp] = 1;
      }
    }
  }

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

int main() {
  string str;
  while (cin >> str, str != "0") {
    bfs(str);
  }
  return 0;
}

Error: MLE

Fixed submission:

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

string insect = "brg";
string str, str2;
int cnt;
queue<string> Q;

bool bfs(string str) {
  int l = Q.size();
  while (!Q.empty()) {

    str = Q.front();
    Q.pop();
    l--;
    // cout << "l" << " " << l << endl;
    int len = str.size();

    for (int i = 0; i < len - 1; i++) {
      str2 = str;
      for (int j = 0; j < 3; j++) {
        if (str2[i] != str2[i + 1] && str2[i] != insect[j] &&
            str2[i + 1] != insect[j]) {
          str2[i] = insect[j];
          str2[i + 1] = insect[j];

          Q.push(str2);
          int flag = 0;
          for (int k = 0; k < len - 1; k++) {
            if (str2[k] != str2[k + 1])
              flag = 1;
          }
          // cout << "i" << " " << i << " " << "j" << " " << j << endl;
          // cout << "cnt" << " " << cnt << endl;
          // cout << "str2" << " " << str2 << endl;
          // cout << endl;

          if (flag == 0)
            return 1;
        }
      }
    }
    if (l == 0)
      return 0;
  }
  return 0;
}

int main() {

  while (1) {

    cin >> str;
    int length = str.size();
    int f = 0;
    for (int i = 0; i < length - 1; i++) {
      if (str[i] != str[i + 1])
        f = 1;
    }

    if (str == "0")
      break;
    while (!Q.empty())
      Q.pop();
    cnt = 0;
    Q.push(str);
    while (1) {
      if (f == 0) {
        cout << 0 << endl;
        break;
      }
      cnt++;
      if (cnt == 10) {
        cout << "NA" << endl;
        break;
      }
      if (bfs(str)) {
        cout << cnt << endl;
        break;
      }
    }
  }
  return 0;
}
Problem p00179 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef pair<string, int> P;

//????????????????????????????????????
bool judge(string str) {
  for (int i = 0; i < str.size(); ++i) {
    if (str[0] != str[i])
      return false;
  }
  return true;
}

//??£?????????2?????????????????§?????????????????????
char color(char a, char b) {
  if ((a == 'g' && b == 'r') || (a == 'r' && b == 'g'))
    return 'b';
  else if ((a == 'b' && b == 'r') || (a == 'r' && b == 'b'))
    return 'g';
  else if ((a == 'g' && b == 'b') || (a == 'b' && b == 'g'))
    return 'r';
}

int solve(string s) {

  queue<P> q;
  map<string, int> m;
  q.push(P(s, 0)); //?????\\??????????????¶????????\\??????

  while (!q.empty()) { //?????\\??????????????¶?????????????????§
    P p = q.front();
    q.pop();
    if (m[p.first])
      continue; //??????????????????
    string now = p.first;
    if (judge(now))
      return p.second;
    m[p.first] = p.second;
    for (int i = 0; i < now.size() - 1; ++i) {
      if (now[i] != now[i + 1]) { //??£?????????2????????°????????¨???
        char tmp = color(now[i], now[i + 1]);
        string D = now;
        D[i] = tmp;
        D[i + 1] = tmp;
        q.push(P(D, p.second + 1));
      }
    }
  }
  return -1;
}

int main() {
  string worm;
  while (cin >> worm) {
    if (worm == "0")
      break;
    int count = solve(worm);
    if (count == -1)
      cout << "NA" << endl;
    else
      cout << count << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef pair<string, int> P;

//????????????????????????????????????
bool judge(string str) {
  for (int i = 0; i < str.size(); ++i) {
    if (str[0] != str[i])
      return false;
  }
  return true;
}

//??£?????????2?????????????????§?????????????????????
char color(char a, char b) {
  if ((a == 'g' && b == 'r') || (a == 'r' && b == 'g'))
    return 'b';
  else if ((a == 'b' && b == 'r') || (a == 'r' && b == 'b'))
    return 'g';
  else if ((a == 'g' && b == 'b') || (a == 'b' && b == 'g'))
    return 'r';
}

int solve(string s) {

  queue<P> q;
  map<string, int> m;
  q.push(P(s, 0)); //?????\\??????????????¶????????\\??????

  while (!q.empty()) { //?????\\??????????????¶?????????????????§
    P p = q.front();
    q.pop();
    if (m[p.first])
      continue; //??????????????????
    string now = p.first;
    if (judge(now))
      return p.second;
    for (int i = 0; i < now.size() - 1; ++i) {
      if (now[i] != now[i + 1]) { //??£?????????2????????°????????¨???
        char tmp = color(now[i], now[i + 1]);
        string D = now;
        D[i] = tmp;
        D[i + 1] = tmp;
        q.push(P(D, p.second + 1));
      }
    }
  }
  return -1;
}

int main() {
  string worm;
  while (cin >> worm) {
    if (worm == "0")
      break;
    int count = solve(worm);
    if (count == -1)
      cout << "NA" << endl;
    else
      cout << count << endl;
  }
  return 0;
}
Problem p00179 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <climits>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

map<int, bool> isre;
map<int, int> dp;

int code(string s) {
  int ret = 0, kake = 1;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'r')
      ret += kake * 1;
    else if (s[i] == 'g')
      ret += kake * 2;
    else
      ret += kake * 3;
    kake *= 4;
  }
  return ret;
}

int main() {
  string in;
  for (;;) {
    cin >> in;
    if (in == "0")
      break;
    map<int, bool> isre;
    queue<string> nq;
    nq.push(in);
    int ret = -1;
    int cnt = 0;
    while (!nq.empty()) {
      int sz = nq.size();
      for (int i = 0; i < sz; i++) {
        string now = nq.front();
        nq.pop();
        int co = code(now);
        if (isre.find(co) != isre.end() && isre[co])
          continue;
        isre[co] = true;
        bool is = true;
        for (int j = 0; j < now.size(); j++) {
          if (now[j] != now[0])
            is = false;
        }
        if (is) {
          ret = cnt;
          goto EXIT;
        }
        for (int j = 1; j < now.size(); j++) {
          string ne = now;
          if (now[j - 1] != now[j]) {
            if (now[j - 1] != 'r' && now[j] != 'r') {
              ne[j - 1] = ne[j] = 'r';
            } else if (now[j - 1] != 'g' && now[j] != 'g') {
              ne[j - 1] = ne[j] = 'g';
            } else {
              ne[j - 1] = ne[j] = 'b';
            }
            nq.push(ne);
          }
        }
      }
      cnt++;
    }
  EXIT:;
    if (ret == -1)
      cout << "NA" << endl;
    else
      cout << ret << endl;
  }
}

Error: TLE

Fixed submission:

#include <climits>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

map<string, bool> isre;
map<string, int> dp;

int solve(string st) {
  if (dp.find(st) == dp.end()) {
    bool is = true;
    for (int i = 0; i < st.size(); i++) {
      if (st[i] != st[0])
        is = false;
    }
    if (is)
      return dp[st] = 0;
    if (isre[st])
      return dp[st] = -1;
    isre[st] = true;
    int miti = INT_MAX;
    for (int i = 1; i < st.size(); i++) {
      string ne = st;
      if (st[i - 1] != st[i]) {
        if (st[i - 1] != 'r' && st[i] != 'r') {
          ne[i - 1] = ne[i] = 'r';
        } else if (st[i - 1] != 'g' && st[i] != 'g') {
          ne[i - 1] = ne[i] = 'g';
        } else {
          ne[i - 1] = ne[i] = 'b';
        }
      }
      int ret = solve(ne);
      if (ret != -1)
        miti = min(miti, ret + 1);
    }
    isre[st] = false;
    if (miti == INT_MAX)
      return dp[st] = -1;
    else
      return dp[st] = miti;
  } else
    return dp[st];
}

int main() {
  string in;
  for (;;) {
    cin >> in;
    if (in == "0")
      break;
    isre = map<string, bool>();
    dp = map<string, int>();
    int ret = solve(in);
    if (ret == -1)
      cout << "NA" << endl;
    else
      cout << ret << endl;
  }
}
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);
int main() {
  string worm;
  while (cin >> worm, worm != "0") {
    int rep = bfs(worm);
    if (rep >= 0)
      cout << rep << endl;
    else
      cout << "NA" << endl;
  }
  return 0;
}

int bfs(string worm) {
  queue<P> Q;
  map<string, bool> M;
  Q.push(make_pair(worm, 0));

  while (Q.size()) {
    P p = Q.front();
    Q.pop();
    if (M[p.first])
      continue;
    M[p.first] = true;
    string str = p.first;
    bool flag = false;
    for (int i = 0; i < str.size() - 1; i++)
      if (str[i] != str[i + 1])
        flag = true;

    if (!flag)
      return p.second;

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

        next[i] = next[i + 1] = temp;
        Q.push(make_pair(next, p.second + 1));
      }
    }
  }

  return -1;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
char p[3][12] = {"rrrrrrrrrr", "bbbbbbbbbb", "gggggggggg"};
typedef struct worm {
  char b[12];
  int s;
} worm;
using namespace std;
int main() {
  char a[15];
  while (cin >> a, a[0] != '0') {
    int len = strlen(a);
    queue<worm> Q;
    worm o;
    strcpy(o.b, a);
    o.s = 0;
    Q.push(o);
    bool f = true;
    while (Q.size()) {
      worm gar = Q.front();
      Q.pop();
      bool f2 = false;
      for (int i = 0; i < 3; i++)
        if (!strncmp(gar.b, p[i], len))
          f2 = true;
      if (f2) {
        cout << gar.s << endl;
        f = false;
        break;
      }
      if (gar.s > 9)
        break;
      for (int i = 0; i < len - 1; i++) {
        char gar2[12];
        strcpy(gar2, gar.b);

        if (gar2[i] != gar2[i + 1]) {
          if (gar2[i] != 'g' && gar2[i + 1] != 'g')
            strncpy(gar2 + i, p[2], 2);
          else if (gar2[i] != 'r' && gar2[i + 1] != 'r')
            strncpy(gar2 + i, p[0], 2);
          else if (gar2[i] != 'b' && gar2[i + 1] != 'b')
            strncpy(gar2 + i, p[1], 2);
        }
        if (strcmp(gar.b, gar2)) {
          worm gar3;
          strcpy(gar3.b, gar2);
          gar3.s = gar.s + 1;
          Q.push(gar3);
        }
      }
    }
    if (f)
      cout << "NA" << endl;
  }

  return 0;
}