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

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

struct cmd {
  int line;
  string p;
  vector<string> v;
};
vector<cmd> A;
map<int, int> idx;
map<string, int> var;

bool isNum(const string &a) { return isdigit(a[0]); }
int StoI(const string &a) {
  int res = 0;
  for (int i = 0; i < a.size(); i++)
    res = res * 10 + a[i] - '0';
  return res;
}

int ADD(string &a, string &b) {
  int res = 0;
  res += var.count(a) ? var[a] : StoI(a);
  res += var.count(b) ? var[b] : StoI(b);
  return res;
}

int SUB(string &a, string &b) {
  int res = 0;
  res += var.count(a) ? var[a] : StoI(a);
  res -= var.count(b) ? var[b] : StoI(b);
  return res;
}

int SET(string &a) { return var.count(a) ? var[a] : StoI(a); }

bool start() {
  int pos = 0, cnt = 0;
  while (1) {
    if (pos == (int)A.size())
      return 1;
    if (cnt++ > 50000000)
      return 0;

    cmd &a = A[pos];
    string p = a.p;
    if (p == "HALT")
      return 1;

    if (p == "ADD") {
      int res = ADD(a.v[1], a.v[2]);
      if (res < 0 || res >= 16)
        return 1;
      var[a.v[0]] = res;
    } else if (p == "SUB") {
      int res = SUB(a.v[1], a.v[2]);
      if (res < 0 || res >= 16)
        return 1;
      var[a.v[0]] = res;
    } else if (p == "SET") {
      int res = SET(a.v[1]);
      if (res < 0 || res >= 16)
        return 1;
      var[a.v[0]] = res;
    } else if (p == "IF" && var[a.v[0]]) {
      if (!idx.count(StoI(a.v[1])))
        return 1;
      pos = idx[StoI(a.v[1])] - 1;
    }
    pos++;
  }
}

int main() {
  int n;
  cin >> n;

  A.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> A[i].line >> A[i].p;
    idx[A[i].line] = i;
    if (A[i].p == "HALT")
      ;
    else if (A[i].p == "ADD" || A[i].p == "SUB")
      A[i].v.resize(3);
    else
      A[i].v.resize(2);

    for (int j = 0; j < (int)A[i].v.size(); j++) {
      cin >> A[i].v[j];
      if (!isNum(A[i].v[j]))
        var[A[i].v[j]] = 0;
    }
  }

  if (!start())
    cout << "inf" << endl;
  else
    for (map<string, int>::iterator it = var.begin(); it != var.end(); it++)
      cout << it->first << "=" << it->second << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

struct cmd {
  int line;
  string p;
  vector<string> v;
};
vector<cmd> A;
map<int, int> idx;
map<string, int> var;

bool isNum(const string &a) { return isdigit(a[0]); }
int StoI(const string &a) {
  int res = 0;
  for (int i = 0; i < a.size(); i++)
    res = res * 10 + a[i] - '0';
  return res;
}

int ADD(string &a, string &b) {
  int res = 0;
  res += var.count(a) ? var[a] : StoI(a);
  res += var.count(b) ? var[b] : StoI(b);
  return res;
}

int SUB(string &a, string &b) {
  int res = 0;
  res += var.count(a) ? var[a] : StoI(a);
  res -= var.count(b) ? var[b] : StoI(b);
  return res;
}

int SET(string &a) { return var.count(a) ? var[a] : StoI(a); }

bool start() {
  int pos = 0, cnt = 0;
  while (1) {
    if (pos == (int)A.size())
      return 1;
    if (cnt++ > 1e8)
      return 0;

    cmd &a = A[pos];
    string p = a.p;
    if (p == "HALT")
      return 1;

    if (p == "ADD") {
      int res = ADD(a.v[1], a.v[2]);
      if (res < 0 || res >= 16)
        return 1;
      var[a.v[0]] = res;
    } else if (p == "SUB") {
      int res = SUB(a.v[1], a.v[2]);
      if (res < 0 || res >= 16)
        return 1;
      var[a.v[0]] = res;
    } else if (p == "SET") {
      int res = SET(a.v[1]);
      if (res < 0 || res >= 16)
        return 1;
      var[a.v[0]] = res;
    } else if (p == "IF" && var[a.v[0]]) {
      if (!idx.count(StoI(a.v[1])))
        return 1;
      pos = idx[StoI(a.v[1])] - 1;
    }
    pos++;
  }
}

int main() {
  int n;
  cin >> n;

  A.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> A[i].line >> A[i].p;
    idx[A[i].line] = i;
    if (A[i].p == "HALT")
      ;
    else if (A[i].p == "ADD" || A[i].p == "SUB")
      A[i].v.resize(3);
    else
      A[i].v.resize(2);

    for (int j = 0; j < (int)A[i].v.size(); j++) {
      cin >> A[i].v[j];
      if (!isNum(A[i].v[j]))
        var[A[i].v[j]] = 0;
    }
  }

  if (!start())
    cout << "inf" << endl;
  else
    for (map<string, int>::iterator it = var.begin(); it != var.end(); it++)
      cout << it->first << "=" << it->second << endl;

  return 0;
}