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

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

string s[N], s1[N], s2[N], s3[N];
set<char> S;
int n, num;
int d[N], p;
int var[N];

int get_num(string str) {
  if ('a' <= str[0] && str[0] <= 'z')
    return var[(int)str[0]];
  int res = 0;
  rep(i, str.size()) res = res * 10 + str[i] - '0';
  return res;
}

void solve() {
  int f = 0, cnt = 50000000;
  while (1) {
    cnt--;
    if (cnt == 0)
      break;
    if (s[p] == "ADD") {
      int var2 = get_num(s2[p]);
      int var3 = get_num(s3[p]);
      if (var2 + var3 >= 16) {
        f = 1;
        break;
      }
      var[(int)s1[p][0]] = var2 + var3;
    }
    if (s[p] == "SUB") {
      int var2 = get_num(s2[p]);
      int var3 = get_num(s3[p]);
      if (var2 - var3 < 0) {
        f = 1;
        break;
      }
      var[(int)s1[p][0]] = var2 - var3;
    }
    if (s[p] == "SET") {
      int var2 = get_num(s2[p]);
      var[(int)s1[p][0]] = var2;
    }
    if (s[p] == "IF") {
      int var1 = get_num(s1[p]);
      int var2 = get_num(s2[p]);
      if (var1 != 0) {
        if (d[var2] == INF) {
          f = 1;
          break;
        }
        p = d[var2];
        continue;
      }
    }
    if (s[p] == "HALT") {
      f = 1;
      break;
    }
    p++;
    if (p == n) {
      f = 1;
      break;
    }
  }
  if (f) {
    set<char>::iterator ite = S.begin();
    while (ite != S.end()) {
      cout << (*ite) << "=" << var[(int)(*ite)] << endl;
      ite++;
    }
  } else
    cout << "inf" << endl;
}

void add(char c) {
  if ('a' <= c && c <= 'z')
    S.insert(c);
}

int main() {
  cin >> n;
  rep(i, N) d[i] = INF;
  rep(i, n) {
    cin >> num;
    d[num] = i;
    cin >> s[i];
    if (s[i] == "HALT")
      continue;
    cin >> s1[i] >> s2[i];
    if (s[i] == "ADD" || s[i] == "SUB")
      cin >> s3[i], add(s3[i][0]);
    add(s1[i][0]);
    add(s2[i][0]);
  }
  solve();
  return 0;
}

Error: TLE

Fixed submission:

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

string s[N], s1[N], s2[N], s3[N];
set<char> S;
int n, num;
int d[N], p;
int var[N];

int get_num(string str) {
  if ('a' <= str[0] && str[0] <= 'z')
    return var[(int)str[0]];
  int res = 0;
  rep(i, str.size()) res = res * 10 + str[i] - '0';
  return res;
}

void solve() {
  int f = 0, cnt = 60000000;
  while (1) {
    cnt--;
    if (cnt == 0)
      break;
    if (s[p] == "ADD") {
      int var2 = get_num(s2[p]);
      int var3 = get_num(s3[p]);
      if (var2 + var3 >= 16) {
        f = 1;
        break;
      }
      var[(int)s1[p][0]] = var2 + var3;
    }
    if (s[p] == "SUB") {
      int var2 = get_num(s2[p]);
      int var3 = get_num(s3[p]);
      if (var2 - var3 < 0) {
        f = 1;
        break;
      }
      var[(int)s1[p][0]] = var2 - var3;
    }
    if (s[p] == "SET") {
      int var2 = get_num(s2[p]);
      var[(int)s1[p][0]] = var2;
    }
    if (s[p] == "IF") {
      int var1 = get_num(s1[p]);
      int var2 = get_num(s2[p]);
      if (var1 != 0) {
        if (d[var2] == INF) {
          f = 1;
          break;
        }
        p = d[var2];
        continue;
      }
    }
    if (s[p] == "HALT") {
      f = 1;
      break;
    }
    p++;
    if (p == n) {
      f = 1;
      break;
    }
  }
  if (f) {
    set<char>::iterator ite = S.begin();
    while (ite != S.end()) {
      cout << (*ite) << "=" << var[(int)(*ite)] << endl;
      ite++;
    }
  } else
    cout << "inf" << endl;
}

void add(char c) {
  if ('a' <= c && c <= 'z')
    S.insert(c);
}

int main() {
  cin >> n;
  rep(i, N) d[i] = INF;
  rep(i, n) {
    cin >> num;
    d[num] = i;
    cin >> s[i];
    if (s[i] == "HALT")
      continue;
    cin >> s1[i] >> s2[i];
    if (s[i] == "ADD" || s[i] == "SUB")
      cin >> s3[i], add(s3[i][0]);
    add(s1[i][0]);
    add(s2[i][0]);
  }
  solve();
  return 0;
}