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

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

int main() {
  int n;
  int q;
  while (cin >> n >> q, n) {
    vector<tuple<int, int, string>> eras(n);
    for (int i = 0; i < n; i++) {
      string eraname;
      int eray;
      int wesy;
      cin >> eraname >> eray >> wesy;
      int startwes = wesy - eray + 1;
      int endwes = wesy;
      eras[i] = make_tuple(startwes, endwes, eraname);
    }
    sort(eras.begin(), eras.end());
    for (int i = 0; i < q; i++) {
      int year;
      cin >> year;
      for (int j = 0; j <= n; j++) {
        if (eras.size() <= j) {
          cout << "Unknown" << endl;
          break;
        }
        if (get<0>(eras[j]) <= year && year <= get<1>(eras[j])) {
          cout << get<2>(eras[j]) << " " << year - get<0>(eras[j]) + 1 << endl;
          break;
        }
      }
    }
  }

  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int n;
  int q;
  while (cin >> n >> q, n) {
    vector<tuple<int, int, string>> eras(n);
    for (int i = 0; i < n; i++) {
      string eraname;
      int eray;
      int wesy;
      cin >> eraname >> eray >> wesy;
      int startwes = wesy - eray + 1;
      int endwes = wesy;
      eras[i] = make_tuple(startwes, endwes, eraname);
    }
    sort(eras.begin(), eras.end());
    for (int i = 0; i < q; i++) {
      int year;
      cin >> year;
      for (int j = 0; j <= eras.size(); j++) {
        if (eras.size() <= j) {
          cout << "Unknown" << endl;
          break;
        }
        if (get<0>(eras[i]) <= year && year <= get<1>(eras[i])) {
          cout << get<2>(eras[i]) << " " << year - get<0>(eras[i]) + 1 << endl;
          break;
        }
      }
    }
  }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#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 it ::iterator
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double ESP = 1e-10;
using namespace std;
int main() {
  int a, b;
  while (cin >> a >> b, a || b) {
    vector<string> in(a);
    vector<int> in1(a);
    vector<int> in2(a);
    rep(i, a) cin >> in[i] >> in1[i] >> in2[i];
    rep(q, b) {
      int n;
      cin >> n;
      bool h = false;
      rep(i, a) {
        if (in2[i] - in1[i] < n && n <= in2[i]) {
          h = true;
          cout << in[i] << " " << n - in2[i] + in1[i] << endl;
          break;
        }
      }
      if (!h)
        cout << "Unknown" << endl;
    }
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#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 it ::iterator
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double ESP = 1e-10;
using namespace std;
int main() {
  int a, b;
  while (cin >> a >> b, a || b) {
    map<int, string> in;
    map<int, int> in1;
    rep(q, a) {
      string s;
      int c, d;
      cin >> s >> c >> d;
      loop(i, d - c + 1, d + 1) {
        in[i] = s;
        in1[i] = i - d + c;
      }
    }
    rep(q, b) {
      int n;
      cin >> n;
      if (in1[n])
        cout << in[n] << " " << in1[n] << endl;
      else
        cout << "Unknown" << endl;
    }
  }
}
Problem p01359 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {
  int n, q;
  while (cin >> n >> q, n) {
    map<int, string> mp;
    map<string, int> m;
    rep(i, n) {
      string s;
      int j, k;
      cin >> s >> j >> k;
      mp[k - j + 1] = s;
      m[s] = j;
    }
    rep(i, q) {
      int d;
      cin >> d;
      bool flag = false;
      for (auto p : mp) {
        if (0 <= d - p.first && d - p.first < m[p.second]) {
          flag = true;
          cout << p.second << ' ' << d - p.first + 1 << endl;
          break;
        }
      }
      if (!flag)
        puts("Unknown");
    }
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {
  int n, q;
  while (scanf("%d%d", &n, &q), n) {
    map<int, string> mp;
    rep(i, n) {
      string s;
      int j, k;
      cin >> s >> j >> k;
      mp[k - j] = s;
    }
    rep(i, q) {
      int d;
      scanf("%d", &d);
      auto it = mp.lower_bound(d);
      it--;
      if (it == --mp.begin()) {
        puts("Unknown");
        continue;
      }
      if ((*it).first == d || (++it != mp.end() && (*++it).first - d == 1)) {
        puts("Unknown");
        continue;
      }
      cout << (*it).second << ' ' << d - (*it).first << endl;
    }
  }
}
Problem p01359 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define all(c) (c).begin(), (c).end()

int main() {
  int N, Q;
  while (cin >> N >> Q, N | Q) {
    typedef tuple<int, int, string> Data;
    vector<Data> era;
    while (N--) {
      string name;
      int eyear, wyear;
      cin >> name >> eyear >> wyear;
      era.emplace_back(wyear - eyear + 1, wyear, name);
    }
    sort(all(era));
    auto comp = [](Data &d, int x) { return get<1>(d) < x; };
    while (Q--) {
      int query, one = 0, known = 0;
      string name;
      cin >> query;
      if (query <= get<1>(era.back()))
        tie(one, known, name) = *lower_bound(all(era), query, comp);
      if (one <= query && query <= known)
        cout << name << " " << query - one + 1 << '\\n';
      else
        cout << "Unknown\\n";
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define all(c) (c).begin(), (c).end()

int main() {
  int N, Q;
  while (cin >> N >> Q, N | Q) {
    typedef tuple<int, int, string> Data;
    vector<Data> era;
    while (N--) {
      string name;
      int eyear, wyear;
      cin >> name >> eyear >> wyear;
      era.emplace_back(wyear - eyear + 1, wyear, name);
    }
    sort(all(era));
    while (Q--) {
      int query, one, known;
      string name;
      cin >> query;
      tie(one, known, name) = *lower_bound(
          all(era), query, [](Data &d, int x) { return get<1>(d) < x; });
      if (one <= query && query <= known)
        cout << name << " " << query - one + 1 << '\\n';
      else
        cout << "Unknown\\n";
    }
  }
  return 0;
}
Problem p01359 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef pair<int, int> pii;
typedef pair<pii, string> piis;

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }
    vector<piis> v(n);
    for (int i = 0; i < n; ++i) {
      cin >> v[i].second >> v[i].first.second >> v[i].first.first;
    }
    sort(v.begin(), v.end());
    while (m--) {
      int y;
      cin >> y;
      vector<piis>::iterator it =
          lower_bound(v.begin(), v.end(), piis(pii(y, 0), ""));
      if (it == v.end()) {
        cout << "Unknown" << endl;
      } else {
        int jy = it->first.second - (it->first.first - y);
        if (jy <= 0) {
          cout << "Unknown" << endl;
        } else {
          cout << it->second << " " << jy << endl;
        }
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>

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

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }
    vector<pis> v(n);
    map<int, int> last;
    for (int i = 0; i < n; ++i) {
      int a, b;
      cin >> v[i].second >> a >> b;
      v[i].first = b - a + 1;
      last[v[i].first] = b;
    }
    sort(v.begin(), v.end());
    while (m--) {
      int y;
      cin >> y;
      vector<pis>::iterator it = lower_bound(v.begin(), v.end(), pis(y, ""));
      if (it == v.begin() && y < it->first) {
        cout << "Unknown" << endl;
      } else if (it->first == y) {
        cout << it->second << " " << 1 << endl;
      } else {
        --it;
        int jy = (y - it->first + 1);
        if (y <= last[it->first]) {
          cout << it->second << " " << jy << endl;
        } else {
          cout << "Unknown" << endl;
        }
      }
    }
  }
  return 0;
}
Problem p01359 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int INF = 1000000000;
#define REP(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, Q, _e, _w, _q;
  string _s;
  while (cin >> N >> Q && N) {
    map<int, string> cvt;
    map<int, int> cvt2;
    int start = INF, last = -1;
    rep(i, N) {
      cin >> _s >> _e >> _w;
      cvt[_w] = _s;
      cvt2[_w] = _w - _e;
      start = min(start, _w - _e + 1);
      last = max(last, _w);
    }
    rep(i, Q) {
      cin >> _q;
      if (_q < start || last < _q)
        cout << "Unknown" << endl;
      else {
        auto lb = cvt.lower_bound(_q);
        string sa = lb->second;
        int ia = _q - cvt2[lb->first];
        if (ia >= 1)
          cout << sa << ' ' << ia << endl;
        else
          cout << "Unknown" << endl;
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int INF = 1000000000;
#define REP(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, Q, _e, _w, _q;
  string _s;
  while (cin >> N >> Q && N) {
    map<int, string> cvt;
    map<int, int> cvt2;
    rep(i, N) {
      cin >> _s >> _e >> _w;
      cvt[_w] = _s;
      cvt2[_w] = _w - _e;
    }
    rep(i, Q) {
      cin >> _q;
      auto lb = cvt.lower_bound(_q);
      pair<string, int> ans;
      ans.first = lb->second;
      ans.second = _q - cvt2[lb->first];
      if (ans.second >= 1)
        cout << ans.first << ' ' << ans.second << endl;
      else
        cout << "Unknown" << endl;
    }
  }
  return 0;
}
Problem p01359 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#define REP(i, a, n) for (int i = (a); i < (n); i++)
#define INF 1000000000000LL
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<long, long> pll;
typedef pair<pll, string> pls;

int N, Q, Y, W;
string Name;
pls E[1001];

int main(void) {
  while (cin >> N >> Q, N || Q) {
    REP(i, 0, N) {
      cin >> Name >> Y >> W;
      E[i] = pls(pll(W - Y + 1, Y), Name);
    }
    E[N] = pls(pll(INF, 0), "");

    sort(E, E + N);

    REP(i, 0, Q) {
      int q;
      cin >> q;
      if (q < E[0].fi.fi) {
        cout << "Unknown" << endl;
        continue;
      }
      REP(j, 0, N) {
        if (E[j].fi.fi <= q && q < E[j + 1].fi.fi) {
          int ans = q - E[j].fi.fi + 1;
          if (ans <= E[j].fi.se) {
            cout << E[j].se << " " << ans << endl;
            goto END;
          }
        }
      }
      cout << "Unknown" << endl;
    END:;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#define REP(i, a, n) for (int i = (a); i < (n); i++)
#define INF 1000000000000LL
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<long, long> pll;
typedef pair<pll, string> pls;

int N, Q, Y, W;
string Name;
pls E[1000];

int main(void) {
  while (cin >> N >> Q, N || Q) {
    REP(i, 0, N) {
      cin >> Name >> Y >> W;
      E[i] = pls(pll(W - Y + 1, Y), Name);
    }
    E[N] = pls(pll(INF, 0), "");

    sort(E, E + N);

    REP(i, 0, Q) {
      int q;
      cin >> q;
      if (q < E[0].fi.fi) {
        cout << "Unknown" << endl;
        continue;
      }
      REP(j, 0, N) {
        if (E[j].fi.fi <= q && q < E[j + 1].fi.fi) {
          int ans = q - E[j].fi.fi + 1;
          if (ans <= E[j].fi.se) {
            cout << E[j].se << " " << ans << endl;
            goto END;
          }
        }
      }
      cout << "Unknown" << endl;
    END:;
    }
  }
  return 0;
}
Problem p01359 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

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

using namespace std;

int main() {
  while (1) {
    int n, q;
    cin >> n >> q;
    if (!n)
      break;
    vector<tuple<int, int, string>> v;
    REP(i, n) {
      string name;
      int era, western;
      cin >> name >> era >> western;
      v.emplace_back(western, era, name);
    }
    sort(begin(v), end(v));
    REP(i, q) {
      int year;
      cin >> year;
      int ue, uw;
      string un;
      auto itr = upper_bound(begin(v), end(v), make_tuple(year, 0, string()));
      if (itr == end(v)) {
        cout << "Unknown" << endl;
        continue;
      }
      tie(uw, ue, un) = *itr;
      if (year <= uw - ue)
        cout << "Unknown" << endl;
      else
        cout << un << ' ' << ue - (uw - year) << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

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

using namespace std;

int main() {
  while (1) {
    int n, q;
    cin >> n >> q;
    if (!n)
      break;
    vector<tuple<int, int, string>> v;
    REP(i, n) {
      string name;
      int era, western;
      cin >> name >> era >> western;
      v.emplace_back(western, era, name);
    }
    sort(begin(v), end(v));
    REP(i, q) {
      int year;
      cin >> year;
      int ue, uw;
      string un;
      tie(uw, ue, un) =
          *upper_bound(begin(v), end(v), make_tuple(year, 0, string()));
      if (year <= uw - ue)
        cout << "Unknown" << endl;
      else
        cout << un << ' ' << ue - (uw - year) << endl;
    }
  }
  return 0;
}
Problem p01360 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)

const int INF = 1 << 25;
int dx[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
int dy[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
struct Pos {
  int x, y;
  Pos(int x = 0, int y = 0) {
    this->x = x;
    this->y = y;
  }
};
Pos p[9];
int N;
Pos ary[1000000];

int func(bool flg, Pos l, Pos r) {
  int res = 0;
  REP(i, N) {
    if (flg) { // right foot
      if (ary[i].x < l.x) {
        l = ary[i];
        flg = !flg;
        res++;
      } else {
        if (r.x == ary[i].x && r.y == ary[i].y)
          flg != flg;
        r = ary[i];
      }
    } else { // left foot
      if (ary[i].x > r.x) {
        r = ary[i];
        flg = !flg;
        res++;
      } else {
        if (l.x == ary[i].x && l.y == ary[i].y)
          flg != flg;
        l = ary[i];
      }
    }
    flg = !flg;
  }
  return res;
}

int solve() {
  string str;
  getline(cin, str);
  if (str == "#")
    return -1;

  N = str.length();
  REP(i, N) { ary[i] = p[str[i] - '1']; }
  int ans = INF;
  REP(i, 9) REP(j, 9) {
    ans = min(ans, min(func(false, p[i], p[j]), func(true, p[i], p[j])));
  }
  return ans;
}

int main() {
  REP(i, 9) { p[i] = Pos(dx[i], dy[i]); }

  int ans;
  while ((ans = solve()) >= 0) {
    printf("%d\\n", ans);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)

const int INF = 1 << 25;
int dx[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
int dy[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
struct Pos {
  int x, y;
  Pos(int x = 0, int y = 0) {
    this->x = x;
    this->y = y;
  }
};
Pos p[9];
int N;
Pos ary[10000000];

int func(bool flg, Pos l, Pos r) {
  int res = 0;
  REP(i, N) {
    if (flg) { // right foot
      if (ary[i].x < l.x) {
        l = ary[i];
        flg = !flg;
        res++;
      } else {
        if (r.x == ary[i].x && r.y == ary[i].y)
          flg != flg;
        r = ary[i];
      }
    } else { // left foot
      if (ary[i].x > r.x) {
        r = ary[i];
        flg = !flg;
        res++;
      } else {
        if (l.x == ary[i].x && l.y == ary[i].y)
          flg != flg;
        l = ary[i];
      }
    }
    flg = !flg;
  }
  return res;
}

string str;
int solve() {
  getline(cin, str);
  if (str == "#")
    return -1;

  N = str.length();
  REP(i, N) { ary[i] = p[str[i] - '1']; }
  int ans = INF;
  REP(i, 9) REP(j, 9) {
    ans = min(ans, min(func(false, p[i], p[j]), func(true, p[i], p[j])));
  }
  return ans;
}

int main() {
  REP(i, 9) { p[i] = Pos(dx[i], dy[i]); }

  int ans;
  while ((ans = solve()) >= 0) {
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p01360 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
using ll = int64_t;

enum { LEFT, RIGHT };

bool isOK(ll left_sq, ll right_sq) {
  if (left_sq == -1 || right_sq == -1) {
    return true;
  }

  ll l = (left_sq % 3 + 2) % 3;
  ll r = (right_sq % 3 + 2) % 3;
  return l <= r;
}

int main() {
  string s;
  while (cin >> s) {
    if (s == "#") {
      break;
    }

    ll ans = INT_MAX;

    for (ll turn : {LEFT, RIGHT}) {
      ll left_sq = -1, right_sq = -1;
      ll tmp = 0;

      for (char c : s) {
        int sq = c - '0';

        if (turn == RIGHT) {
          if (isOK(left_sq, sq)) {
            right_sq = sq;
            turn = LEFT;
          } else {
            tmp++;
            left_sq = sq;
            turn = RIGHT;
          }
        } else {
          if (isOK(sq, right_sq)) {
            left_sq = sq;
            turn = RIGHT;
          } else {
            tmp++;
            right_sq = sq;
            turn = LEFT;
          }
        }
      }

      ans = min(ans, tmp);
    }

    cout << ans << endl;
  }
}


Error: MLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>

using std::min;

const int N = 100000 + 10;
const int D[10] = {2, 1, 2, 3, 1, 2, 3, 1, 2, 3};
const int INF = 1 << 29;

char string[N];
int dp[N][10][10][2];

void update(int &dest, int src) {
  if (dest == -1 || src < dest) {
    dest = src;
  }
}

int main() {
  while (scanf("%s", string + 1) != EOF && string[1] != '#') {
    int n = strlen(string + 1);
    memset(dp, -1, sizeof(dp));
    int x = string[1] - '0';
    if (D[x] <= D[0]) {
      dp[1][x][0][0] = 0;
    }
    if (D[x] >= D[0]) {
      dp[1][0][x][1] = 0;
    }
    for (int i = 1; i < n; ++i) {
      for (int a = 0; a <= 9; ++a) {
        for (int b = 0; b <= 9; ++b) {
          for (int k = 0; k < 2; ++k) {
            if (dp[i][a][b][k] != -1) {
              int x = string[i + 1] - '0';
              if (D[x] <= D[b]) {
                update(dp[i + 1][x][b][0], dp[i][a][b][k] + (k == 0));
              }
              if (D[x] >= D[a]) {
                update(dp[i + 1][a][x][1], dp[i][a][b][k] + (k == 1));
              }
            }
          }
        }
      }
    }
    int answer = INF;
    for (int a = 0; a <= 9; ++a) {
      for (int b = 0; b <= 9; ++b) {
        for (int k = 0; k < 2; ++k) {
          if (dp[n][a][b][k] != -1) {
            answer = min(answer, dp[n][a][b][k]);
          }
        }
      }
    }
    printf("%d\\n", answer);
  }
  return 0;
}
Problem p01360 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

int dp[2][2][9];

bool valid(int left, int right) {
  if (left == right)
    return false;
  left %= 3;
  right %= 3;
  return left <= right;
}

int main() {
  string str;
  while (true) {
    cin >> str;
    if (str == "#")
      break;
    rep(i, 2) rep(j, 9) {
      if (i == 0) {
        if (valid(str[0] - '1', j))
          dp[0][i][j] = 0;
        else
          dp[0][i][j] = 1e9;
      } else {
        if (valid(j, str[0] - '1'))
          dp[0][i][j] = 0;
        else
          dp[0][i][j] = 1e9;
      }
    }
    rep(i, str.size() - 1) {
      rep(j, 2) rep(k, 9) dp[1][j][k] = 1e9;
      rep(j, 2) rep(k, 9) {
        if (j == 0) {
          if (valid(str[i + 1] - '1', k))
            dp[1][j][k] = dp[0][j][k] + 1;
          else
            dp[1][j][k] = 1e9;
          if (valid(str[i + 1] - '1', str[i] - '1')) {
            rep(l, 9) dp[1][j][k] = min(dp[1][j][k], dp[0][j ^ 1][l]);
          }
        } else {
          if (valid(k, str[i + 1] - '1'))
            dp[1][j][k] = dp[0][j][k] + 1;
          else
            dp[1][j][k] = 1e9;
          if (valid(str[i] - '1', str[i + 1] - '1')) {
            rep(l, 9) dp[1][j][k] = min(dp[1][j][k], dp[0][j ^ 1][l]);
          }
        }
      }
      rep(j, 2) rep(k, 9) dp[0][j][k] = dp[1][j][k];
    }
    int res = 1e9;
    rep(i, 2) rep(j, 9) res = min(dp[0][i][j], res);
    cout << res << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

int dp[2][2][9];

bool valid(int left, int right) {
  if (left == right)
    return false;
  left %= 3;
  right %= 3;
  return left <= right;
}

int main() {
  string str;
  while (true) {
    cin >> str;
    if (str == "#")
      break;
    rep(i, 2) rep(j, 9) {
      if (i == 0) {
        if (valid(str[0] - '1', j))
          dp[0][i][j] = 0;
        else
          dp[0][i][j] = 1e9;
      } else {
        if (valid(j, str[0] - '1'))
          dp[0][i][j] = 0;
        else
          dp[0][i][j] = 1e9;
      }
    }
    rep(i, str.size() - 1) {
      rep(j, 2) rep(k, 9) dp[1][i][j] = 1e9;
      rep(j, 2) rep(k, 9) {
        if (j == 0) {
          if (valid(str[i + 1] - '1', k))
            dp[1][j][k] = dp[0][j][k] + 1;
          else
            dp[1][j][k] = 1e9;
          if (valid(str[i + 1] - '1', str[i] - '1')) {
            rep(l, 9) dp[1][j][k] = min(dp[1][j][k], dp[0][j ^ 1][l]);
          }
        } else {
          if (valid(k, str[i + 1] - '1'))
            dp[1][j][k] = dp[0][j][k] + 1;
          else
            dp[1][j][k] = 1e9;
          if (valid(str[i] - '1', str[i + 1] - '1')) {
            rep(l, 9) dp[1][j][k] = min(dp[1][j][k], dp[0][j ^ 1][l]);
          }
        }
      }
      rep(j, 2) rep(k, 9) dp[0][j][k] = dp[1][j][k];
    }
    int res = 1e9;
    rep(i, 2) rep(j, 9) res = min(dp[0][i][j], res);
    cout << res << endl;
  }
  return 0;
}