Problem p03212 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;
using P = pair<ll, ll>;

const int mod = 1000000007;
const int inf = (1 << 30) - 1;
const ll linf = (1LL << 62LL) - 1;
const double EPS = (1e-10);

#define anyfill(n, s) setw(n) << setfill(s)
#define loop(s) for (int i = 0; s > i; i++)
#define rep(i, q) for (int i = 0; (q) > i; i++)
#define repp(i, n, q) for (int i = n; (q) > i; i++)
#define dep(i, q) for (int i = (q); 0 < i; i--)

#define pb push_back
#define fir first
#define scn second
#define ednl endl

#define YesNo(a) (a ? "Yes" : "No")
#define YESNO(a) (a ? "YES" : "NO")
#define yesno(a) (a ? "yes" : "no")

P ar4[4] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
P ar8[8] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1},
            {0, 1},   {1, -1}, {1, 0},  {1, 1}};
int dp[10][2];
__attribute__((constructor)) void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}
int main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  for (int i = 0; s.size() > i; i++) {
    if (s[i] >= '7') {
      s[i] = '3';
    } else if (s[i] >= '5') {
      s[i] = '2';
    } else if (s[i] >= '3') {
      s[i] = '1';
    } else {
      if (i + 1 != s.size()) {
        s[i + 1]--;
        s[i] = '3';
      } else {
        s.pop_back();
      }
    }
  }
  reverse(s.begin(), s.end());
  string z = "123";
  int a = 1;
  while (z != s) {
    int nw = z.size() - 1;
    while (nw >= 0 && z[nw] == '3')
      nw--;
    if (nw < 0) {
      z = "1" + z;
      for (int i = 0; z.size() > i; i++) {
        z[i] = '1';
      }
    } else {
      z[nw++]++;
      while (nw != z.size()) {
        z[nw++] = '1';
      }
    }
    bool g[3] = {0, 0, 0};
    for (int i = 0; z.size() > i; i++) {
      g[z[i] - '1'] = true;
    }
    if (g[0] && g[1] && g[2])
      a++;
  }
  cout << a << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;
using P = pair<ll, ll>;

const int mod = 1000000007;
const int inf = (1 << 30) - 1;
const ll linf = (1LL << 62LL) - 1;
const double EPS = (1e-10);

#define anyfill(n, s) setw(n) << setfill(s)
#define loop(s) for (int i = 0; s > i; i++)
#define rep(i, q) for (int i = 0; (q) > i; i++)
#define repp(i, n, q) for (int i = n; (q) > i; i++)
#define dep(i, q) for (int i = (q); 0 < i; i--)

#define pb push_back
#define fir first
#define scn second
#define ednl endl

#define YesNo(a) (a ? "Yes" : "No")
#define YESNO(a) (a ? "YES" : "NO")
#define yesno(a) (a ? "yes" : "no")

P ar4[4] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
P ar8[8] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1},
            {0, 1},   {1, -1}, {1, 0},  {1, 1}};
int dp[10][2];
__attribute__((constructor)) void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}
int main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  for (int i = 0; s.size() > i; i++) {
    if (s[i] >= '7') {
      s[i] = '3';
    } else if (s[i] >= '5') {
      s[i] = '2';
    } else if (s[i] >= '3') {
      s[i] = '1';
    } else {
      if (i + 1 != s.size()) {
        s[i + 1]--;
        s[i] = '3';
      } else {
        s.pop_back();
      }
    }
  }
  reverse(s.begin(), s.end());
  string z = "122";
  int a = 0;
  while (z.size() <= s.size() && z != s) {
    int nw = z.size() - 1;
    while (nw >= 0 && z[nw] == '3')
      nw--;
    if (nw < 0) {
      z = "1" + z;
      for (int i = 0; z.size() > i; i++) {
        z[i] = '1';
      }
    } else {
      z[nw++]++;
      while (nw != z.size()) {
        z[nw++] = '1';
      }
    }
    bool g[3] = {0, 0, 0};
    for (int i = 0; z.size() > i; i++) {
      g[z[i] - '1'] = true;
    }
    if (g[0] && g[1] && g[2])
      a++;
  }
  cout << a << endl;
}