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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define EPS 1e-9
#define INF MOD
#define MOD 1000000007LL
#define fir first
#define iss istringstream
#define sst stringstream
#define ite iterator
#define ll long long
#define mp make_pair
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < n; i++)
#define pi pair<int, int>
#define pb push_back
#define sec second
#define sh(i) (1LL << i)
#define sz size()
#define vi vector<int>
#define vc vector
#define vl vector<ll>
#define vs vector<string>

int dp[2][2][4][4];
char c[100010];

int main() {
  while (scanf("%s", c)) {
    if (c[0] == '#')
      break;
    memset(dp, 0, sizeof(dp));
    int n = strlen(c);
    rep2(i, 1, n + 1) {
      int cur = i % 2, pre = 1 - cur, a = (c[i - 1] - '0') % 3;
      if (!a)
        a = 3;
      rep(j, 2) rep(k, 4) rep(l, 4) dp[cur][j][k][l] = INF;
      rep(j, 2) rep2(k, 1, 4) rep2(l, max(k, a), 4) {
        dp[cur][0][a][l] = min(dp[cur][0][a][l], dp[pre][j][k][l] + (j < 1));
      }
      rep(j, 2) rep2(k, 1, a + 1) rep2(l, k, 4) {
        dp[cur][1][k][a] = min(dp[cur][1][k][a], dp[pre][j][k][l] + (j > 0));
      }
    }
    int a = INF;
    rep(i, 2) rep(j, 4) rep(k, 4) a = min(a, dp[n % 2][i][j][k]);
    cout << a << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define EPS 1e-9
#define INF MOD
#define MOD 1000000007LL
#define fir first
#define iss istringstream
#define sst stringstream
#define ite iterator
#define ll long long
#define mp make_pair
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < n; i++)
#define pi pair<int, int>
#define pb push_back
#define sec second
#define sh(i) (1LL << i)
#define sz size()
#define vi vector<int>
#define vc vector
#define vl vector<ll>
#define vs vector<string>

int dp[2][2][4][4];
char c[100010];

int main() {
  while (scanf("%s", c)) {
    if (c[0] == '#')
      break;
    memset(dp, 0, sizeof(dp));
    rep2(i, 1, strlen(c) + 1) {
      int cur = i % 2, pre = 1 - cur, a = (c[i - 1] - '0') % 3;
      if (!a)
        a = 3;
      rep(j, 2) rep(k, 4) rep2(l, k, 4) dp[cur][j][k][l] = INF;
      rep(j, 2) rep2(k, 1, 4) rep2(l, max(k, a), 4) {
        dp[cur][0][a][l] = min(dp[cur][0][a][l], dp[pre][j][k][l] + (j < 1));
      }
      rep(j, 2) rep2(k, 1, a + 1) rep2(l, k, 4) {
        dp[cur][1][k][a] = min(dp[cur][1][k][a], dp[pre][j][k][l] + (j > 0));
      }
    }
    int a = INF;
    rep(i, 2) rep(j, 4) rep(k, 4) a = min(a, dp[strlen(c) % 2][i][j][k]);
    cout << a << endl;
  }
}