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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl;
int n, k;
string s[12];
int f[12][8], dig[12];
int a[10], now[12], p10[8], al[26];
bool nozero[10], use[10];
int mxnum(int id, int cnt) {
  int ret = 0;
  rep(i, dig[id]) {
    ret *= 10;
    if (f[id][dig[id] - 1 - i] < cnt)
      ret += a[f[id][dig[id] - 1 - i]];
    else
      ret += 9;
  }
  return ret;
}
int mnnum(int id, int cnt) {
  int ret = 0;
  rep(i, dig[id]) {
    ret *= 10;
    if (f[id][dig[id] - 1 - i] < cnt)
      ret += a[f[id][dig[id] - 1 - i]];
    else
      ret += 0;
  }
  return ret;
}
int dfs(int cnt) {
  if (cnt == k) {
    int sum = 0;
    rep(i, n - 1) sum += mxnum(i, cnt);
    if (sum == mxnum(n - 1, cnt))
      return 1;
  }
  int sum = 0;
  rep(i, n - 1) sum += mxnum(i, cnt);
  if (sum < mnnum(n - 1, cnt))
    return 0;
  sum = 0;
  rep(i, n - 1) sum += mnnum(i, cnt);
  if (sum > mxnum(n - 1, cnt))
    return 0;

  int ret = 0;
  rep(i, 10) {
    if (use[i])
      continue;
    if (i == 0 && nozero[cnt])
      continue;
    a[cnt] = i;
    use[i] = true;
    ret += dfs(cnt + 1);
    use[i] = false;
  }
  return ret;
}
int main() {
  p10[0] = 1;
  rep(i, 7) p10[i + 1] = p10[i] * 10;
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    rep(i, 10) nozero[i] = 0;
    rep(i, 26) al[i] = -1;
    rep(i, n) cin >> s[i];
    rep(i, n) dig[i] = s[i].size();
    int it = 0;
    for (int i = n - 1; i >= 0; i--) {
      rep(j, dig[i]) {
        if (al[s[i][j] - 'A'] == -1)
          al[s[i][j] - 'A'] = it, it++;
        f[i][dig[i] - 1 - j] = al[s[i][j] - 'A'];
      }
    }
    rep(i, n) if (dig[i] != 1) nozero[f[i][dig[i] - 1]] = 1;
    k = it;
    cout << dfs(0) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl;
int n, k;
string s[12];
int f[12][8], dig[12];
vector<int> ash;
int a[10];
bool nozero[10], use[10];
int num(int id) {
  int ret = 0;
  rep(i, dig[id]) {
    ret *= 10;
    ret += a[f[id][dig[id] - 1 - i]];
  }
  return ret;
}
int dfs(int cnt) {
  if (cnt == k) {
    int sum = 0;
    rep(i, n - 1) sum += num(i);
    if (sum == num(n - 1))
      return 1;
  }
  int ret = 0;
  rep(i, 10) {
    if (use[i])
      continue;
    if (i == 0 && nozero[cnt])
      continue;
    a[cnt] = i;
    use[i] = true;
    ret += dfs(cnt + 1);
    use[i] = false;
  }
  return ret;
}
int main() {
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    ash.clear();
    rep(i, 10) nozero[i] = 0;
    rep(i, n) cin >> s[i];
    rep(i, n) rep(j, s[i].size()) ash.pb(s[i][j]);
    rep(i, n) dig[i] = s[i].size();
    sort(all(ash));
    ash.erase(unique(all(ash)), ash.end());
    rep(i, n) rep(j, 8) f[i][j] = -1;
    rep(i, n) rep(j, dig[i]) f[i][dig[i] - 1 - j] =
        lower_bound(all(ash), s[i][j]) - ash.begin();
    rep(i, n) if (dig[i] != 1) nozero[f[i][dig[i] - 1]] = 1;
    k = ash.size();
    cout << dfs(0) << endl;
  }
}