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

#include <cstring>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
using namespace std;

#define index foo

map<char, int> index;
int ch_count;
int coef[11];
int as[11];
bool not_zero[11];

int pow10(int n) {
  int ret = 1;
  for (int i = 0; i < n; i++)
    ret *= 10;
  return ret;
}

bool check() {
  int sum = 0;
  for (int i = 1; i < ch_count; i++) {
    sum += as[i] * coef[i];
  }
  // if (sum == 0) {
  //     for (int i = 1; i < ch_count; i++) {
  //         cout << as[i] << ' ' << coef[i] << endl;
  //     }
  //     cout << endl;
  // }
  return sum == 0;
}

int cnt(int cur = 1, int used = 0) {
  if (cur == ch_count)
    return check() ? 1 : 0;
  int ret = 0;
  for (int i = 0; i < 10; i++) {
    if (i == 0 && not_zero[cur])
      continue;
    if (used & (1 << i))
      continue;
    as[cur] = i;
    ret += cnt(cur + 1, used | (1 << i));
    as[cur] = 0;
  }
  return ret;
}

int main() {
  int n;
  while (cin >> n, n) {
    int cur = 1;
    index = map<char, int>();
    memset(coef, 0, sizeof(coef));
    memset(as, 0, sizeof(as));
    memset(not_zero, 0, sizeof(not_zero));
    for (int i = 0; i < n - 1; i++) {
      string e;
      cin >> e;
      for (int j = 0; j < e.size(); j++) {
        if (index[e[j]] == 0)
          index[e[j]] = cur++;
        coef[index[e[j]]] += pow10(e.size() - 1 - j);
      }
      if (e.size() != 1)
        not_zero[index[e[0]]] = true;
    }
    string obj;
    cin >> obj;
    for (int j = 0; j < obj.size(); j++) {
      if (index[obj[j]] == 0)
        index[obj[j]] = cur++;
      coef[index[obj[j]]] -= pow10(obj.size() - 1 - j);
    }
    if (obj.size() != 1)
      not_zero[index[obj[0]]] = true;
    ch_count = cur;
    cout << cnt() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
using namespace std;

int ch_count;
map<char, int> ch_index;
bool not_zero[11];
int as[11];
vector<string> eq;

int tr(string s) {
  int ret = 0;
  for (int i = 0; i < s.size(); i++) {
    ret *= 10;
    ret += as[ch_index[s[i]]];
  }
  return ret;
}

bool check() {
  vector<int> value(eq.size());
  for (int i = 0; i < eq.size(); i++) {
    value[i] = tr(eq[i]);
  }
  int sum = 0;
  for (int i = 0; i < eq.size() - 1; i++) {
    sum += value[i];
  }
  if (value.back() == sum) {
    // for (int i = 0; i < value.size(); i++) {
    //     cout << value[i] << ' ';
    // }
    // cout << endl;
    return true;
  }
  return false;
}

int cnt(int cur = 1, int used = 0) {
  if (cur == ch_count)
    return (check() ? 1 : 0);
  int ret = 0;
  for (int i = 0; i < 10; i++) {
    if (i == 0 && not_zero[cur])
      continue;
    if (used & (1 << i))
      continue;
    as[cur] = i;
    ret += cnt(cur + 1, used | (1 << i));
    as[cur] = 0;
  }
  return ret;
}

int main() {
  int n;
  while (cin >> n, n) {
    memset(not_zero, 0, sizeof(not_zero));
    memset(as, 0, sizeof(as));
    eq = vector<string>(n);
    ch_index = map<char, int>();
    ch_count = 1;
    for (int i = 0; i < n; i++) {
      cin >> eq[i];
      for (int j = 0; j < eq[i].size(); j++) {
        if (ch_index[eq[i][j]] == 0)
          ch_index[eq[i][j]] = ch_count++;
      }
      if (eq[i].size() != 1)
        not_zero[ch_index[eq[i][0]]] = true;
    }
    // for (int i = 1; i < ch_count; i++) {
    //     cout << not_zero[i] << ' ';
    // } cout << endl;
    // for (int i = 'A'; i <= 'Z'; i++) {
    //     cout << (char)i << ' ' << ch_index[i] << endl;
    // }
    cout << cnt() << endl;
  }
  return 0;
}