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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

vector<int> table(26, -1);
vector<bool> used(10, false);
int dfs(int d, vector<string> &str, vector<char> &clist, vector<bool> &nonzero,
        vector<long long int> &num) {
  if (d == 0) {
    long long int sum = 0;
    for (int i = 0; i < (int)clist.size(); i++) {
      sum += num[i] * table[clist[i] - 'A'];
    }
    if (sum == 0) {
      return 1;
    } else {
      return 0;
    }
  }

  int ret = 0;
  for (int i = 0; i <= 9; i++) {
    if (used[i])
      continue;
    if (i == 0 && nonzero[clist[d - 1] - 'A'])
      continue;
    used[i] = true;
    table[clist[d - 1] - 'A'] = i;
    ret += dfs(d - 1, str, clist, nonzero, num);
    table[clist[d - 1] - 'A'] = -1;
    used[i] = false;
  }
  return ret;
}

int main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;

    vector<string> str(n);
    vector<char> clist;
    vector<bool> nonzero(26, false);
    for (int i = 0; i < n; i++) {
      cin >> str[i];
      for (int j = 0; j < (int)str[i].length(); j++) {
        clist.push_back(str[i][j]);
      }
      if ((int)str[i].length() != 1) {
        nonzero[str[i][0] - 'A'] = true;
      }
    }
    sort(clist.begin(), clist.end());
    clist.erase(unique(clist.begin(), clist.end()), clist.end());

    int numc = clist.size();
    vector<long long int> num(numc, 0);
    for (int d = 0; d < numc; d++) {
      for (int i = 0; i < n; i++) {
        long long int sub = 0;
        for (int j = 0; j < (int)str[i].length(); j++) {
          sub *= 10;
          if (str[i][j] == clist[d])
            sub += 1;
        }
        if (i == n - 1)
          num[d] -= sub;
        else
          num[d] += sub;
      }
    }
    cout << dfs(numc, str, clist, nonzero, num) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

vector<int> table(26, -1);
vector<bool> used(10, false);
int dfs(int d, vector<string> &str, vector<char> &clist,
        vector<bool> &nonzero) {
  if (d == 0) {
    int n = str.size();
    vector<long long int> num(n, 0);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < (int)str[i].length(); j++) {
        num[i] *= 10;
        num[i] += table[str[i][j] - 'A'];
      }
    }
    long long int sum = 0;
    for (int i = 0; i < n - 1; i++) {
      sum += num[i];
    }
    if (num[n - 1] == sum) {
      return 1;
    } else {
      return 0;
    }
  }

  int ret = 0;
  for (int i = 0; i <= 9; i++) {
    if (used[i])
      continue;
    if (i == 0 && nonzero[clist[d - 1] - 'A'])
      continue;
    used[i] = true;
    table[clist[d - 1] - 'A'] = i;
    ret += dfs(d - 1, str, clist, nonzero);
    table[clist[d - 1] - 'A'] = -1;
    used[i] = false;
  }
  return ret;
}

int main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;

    vector<string> str(n);
    vector<char> clist;
    vector<bool> nonzero(26, false);
    for (int i = 0; i < n; i++) {
      cin >> str[i];
      for (int j = 0; j < (int)str[i].length(); j++) {
        clist.push_back(str[i][j]);
      }
      if ((int)str[i].length() != 1) {
        nonzero[str[i][0] - 'A'] = true;
      }
    }
    sort(clist.begin(), clist.end());
    clist.erase(unique(clist.begin(), clist.end()), clist.end());
    cout << dfs((int)clist.size(), str, clist, nonzero) << endl;
  }
  return 0;
}