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

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define rep(i, n) for (i = 0; i < n; i++)
using namespace std;

int n;
int p10[8];
string s[12];
vector<char> chars;
int ad[26];
bool ngZero[26];

int dfs(int dep, int used, int s = 0) {
  if (dep == chars.size()) {
    return s == 0;
  }

  int i;
  int ret = 0;
  rep(i, 10) {
    if ((used >> i) & 1)
      continue;
    if (i == 0 && ngZero[chars[dep] - 'A'])
      continue;
    int res = dfs(dep + 1, used + (1 << i), s + i * ad[chars[dep] - 'A']);
    ret += res;
  }
  return ret;
}

int main() {
  int i, j;
  p10[0] = 1;
  rep(i, 7) p10[i + 1] = p10[i] * 10;

  while (cin >> n) {
    if (!n)
      break;
    rep(i, n) cin >> s[i];

    chars.clear();
    rep(i, 26) ad[i] = 0;
    rep(i, 26) ngZero[i] = false;

    rep(i, n) {
      rep(j, s[i].length()) { chars.push_back(s[i][j]); }
    }
    sort(chars.begin(), chars.end());
    chars.erase(unique(chars.begin(), chars.end()), chars.end());

    // cout << "Chars: "; rep(i, chars.size()) cout << chars[i]; cout << endl;
    // rep(i, 26) { cout << (char)('A' + i) << "| ad = " << ad[i] << ", ngZero =
    // " << ngZero[i] << endl; }
    rep(i, n) {
      int len = s[i].length();
      rep(j, len) {
        if (i < n - 1)
          ad[s[i][j] - 'A'] += p10[len - 1 - j];
        else
          ad[s[i][j] - 'A'] -= p10[len - 1 - j];
        if (j == 0 && len >= 2)
          ngZero[s[i][j] - 'A'] = true;
      }
    }

    int res = dfs(0, 0);
    cout << res << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

int n;
string s[12];
char kigous[12]; //?????????????¨????????¨????
int kigousNum;
bool used[10] = {false}; // used[i] = ??°???i?????¢?????????????????????
int perm[10];            // perm[i] = kigous[i]????????????????????°???

int calc() {
  int i, j, k;
  int toNum[26]; // toNum[moji_id] = ??????moji_id +
                 // 'A'??????????????????????????°?????????????????????
  int ret = 0;

  for (i = 0; i < kigousNum; i++) {
    toNum[kigous[i] - 'A'] = perm[i];
  }

  for (i = 0; i < n; i++) {
    int value = 0;
    for (j = 0; j < s[i].length(); j++) {
      value *= 10;
      int res = toNum[s[i][j] - 'A'];
      if (j == 0 && s[i].length() >= 2 && res == 0)
        return 0; //???????????????
      value += res;
    }
    if (i < n - 1) {
      ret += value;
    } else {
      ret -= value;
    }
  }
  return (ret == 0);
}

bool ngId[10] = {
    false}; // kigou[i]????????????????????????????????§0????????????????????????

int dfs(int id, int maxDep) {
  if (id == maxDep) {
    return calc();
  }
  int ret = 0;
  for (int i = ngId[id]; i < 10; i++) {
    if (used[i])
      continue;
    used[i] = true;
    perm[id] = i;
    ret += dfs(id + 1, maxDep);
    used[i] = false;
  }
  return ret;
}

int main() {
  int i, j;

  while (cin >> n) {
    if (!n) {
      break;
    }

    kigousNum = 0;
    for (i = 0; i < 10; i++)
      ngId[i] = false;

    bool used[26] = {false};
    for (i = 0; i < n; i++)
      cin >> s[i];
    for (i = 0; i < n; i++) {
      for (j = 0; j < s[i].length(); j++) {
        if (!used[s[i][j] - 'A']) {
          used[s[i][j] - 'A'] = true;
          kigous[kigousNum++] = s[i][j];
        }
      }
    }

    //??????????????????0?????´????????????????????????
    int mlen = -1;
    for (i = 0; i < n - 1; i++) {
      mlen = max(mlen, (int)s[i].length());
    }
    if (mlen > s[n - 1].length() || mlen + 2 < s[n - 1].length()) {
      cout << 0 << endl;
      continue;
    }

    for (i = 0; i < n; i++) {
      for (j = 0; j < kigousNum; j++) {
        if (kigous[j] == s[i][0] && s[i].length() >= 2)
          break;
      }
      ngId[j] = true;
    }

    cout << dfs(0, kigousNum) << endl;
  }
}