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

#include <bits/stdc++.h>

using namespace std;

typedef vector<int> Vec;

int N, maxi;
vector<string> v;
Vec num, l;
set<Vec> st;

int solve(int k, int idx, int sum, int S, int p) {
  if (k == maxi) {
    if (st.count(num))
      return 0;
    st.insert(num);
    return 1;
  }
  int res = 0;
  if (idx == N - 1) {
    int val = (int)(v[idx][k] - 'A');
    int ns = (sum + p) % 10;
    int np = (sum + p) / 10;
    if (k + 1 == maxi && np != 0)
      return 0;
    if (num[val] != -1) {
      if (l[idx] > 0 && k == l[idx] && num[val] == 0) {
        return 0;
      }
      if (ns == num[val]) {
        res = solve(k + 1, 0, 0, S, np);
      }
    } else {
      for (int i = 0; i < 10; i++) {
        if (S >> i & 1)
          continue;
        num[val] = i;
        if (ns == i && !(l[idx] > 0 && k == l[idx] && i == 0)) {
          res += solve(k + 1, 0, 0, S | (1 << i), np);
        }
        num[val] = -1;
      }
    }
  } else {
    if (v[idx][k] == '*') {
      res = solve(k, idx + 1, sum, S, p);
    } else {
      int val = (int)(v[idx][k] - 'A');
      if (num[val] != -1) {
        if (l[idx] > 0 && k == l[idx] && num[val] == 0) {
          return 0;
        }
        res += solve(k, idx + 1, sum + num[val], S, p);
      } else {
        for (int i = 0; i < 10; i++) {
          if (S >> i & 1)
            continue;
          num[val] = i;
          if (!(l[idx] > 0 && k == l[idx] && i == 0)) {
            res += solve(k, idx + 1, sum + i, S | (1 << i), p);
          }
          num[val] = -1;
        }
      }
    }
  }
  return res;
}

int main() {
  while (cin >> N, N) {
    v.resize(N);
    l.resize(N);
    maxi = 0;
    Vec len(N);
    for (int i = 0; i < N; i++) {
      cin >> v[i];
      l[i] = len[i] = v[i].size();
      l[i]--;
      maxi = max(maxi, len[i]);
      reverse(v[i].begin(), v[i].end());
    }
    if (len[N - 1] < maxi) {
      cout << 0 << endl;
      continue;
    }
    for (int i = 0; i < N; i++) {
      while (len[i] != maxi) {
        v[i] += '*';
        len[i]++;
      }
    }
    num.resize(26, -1);
    st.clear();
    cout << solve(0, 0, 0, 0, 0) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int N, l[12], res, S;
string str[12];
map<char, int> mp;
map<int, char> mp2;
map<ll, ll> mp3;
set<char> not_zero;

void init() {
  S = 0;
  mp.clear();
  mp2.clear();
  mp3.clear();
  not_zero.clear();
}

void check(ll v) {
  ll sum = 0LL;
  vector<ll> vec;
  while (v > 0LL) {
    vec.push_back(v % 10LL);
    v /= 10LL;
  }
  while ((int)vec.size() != S) {
    vec.push_back(0);
  }
  reverse(vec.begin(), vec.end());

  ll num;
  for (int i = 0; i < N - 1; i++) {
    num = 0LL;
    for (int j = 0; j < l[i]; j++) {
      num *= 10LL;
      num += vec[mp[str[i][j]]];
    }
    sum += num;
  }
  num = 0LL;
  for (int i = 0; i < l[N - 1]; i++) {
    num *= 10LL;
    num += vec[mp[str[N - 1][i]]];
  }
  if (sum == num) {
    ++res;
  }
}

void make(int idx, ll v, int nS) {
  if (idx == S) {
    check(v);
    return;
  }
  for (int i = 0; i <= 9; i++) {
    if ((nS >> i) & 1) {
      continue;
    }
    if (i == 0 && not_zero.count(mp2[idx])) {
      continue;
    }
    ll nv = v;
    nv *= 10LL;
    nv += (ll)i;
    make(idx + 1, nv, nS | (1 << i));
  }
}

int main() {
  while (cin >> N, N) {
    init();
    for (int i = 0; i < N; i++) {
      cin >> str[i];
      l[i] = str[i].size();
      for (int j = 0; j < l[i]; j++) {
        if (mp.find(str[i][j]) == mp.end()) {
          mp[str[i][j]] = S;
          mp2[S] = str[i][j];
          S++;
        }
      }
      if (l[i] > 1) {
        not_zero.insert(str[i][0]);
      }
    }
    res = 0;
    make(0, 0LL, 0);
    cout << res << endl;
  }
  return 0;
}