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

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;

#define INF INT_MAX / 3

int n;
string s[13];
map<char, int> alc, dame;
bool used[13];
int res;

void dfs(int i, int d, int sum) {
  if (i == n - 1 && d == 0) {
    if (alc[s[i][d]] == sum ||
        (alc[s[i][d]] == -1 && sum < 10 && sum != 0 && !used[sum])) {
      res++;
    }
  } else if (i == n - 1) {
    if (alc[s[i][d]] == sum % 10) {
      dfs(0, d - 1, sum / 10);
    } else if (alc[s[i][d]] == -1 && !used[sum % 10] &&
               (sum % 10 != 0 || !dame[s[i][d]])) {
      used[sum % 10] = true;
      alc[s[i][d]] = sum % 10;
      dfs(0, d - 1, sum / 10);
      used[sum % 10] = false;
      alc[s[i][d]] = -1;
    }
  } else if (alc[s[i][d]] == -1) {
    repl(j, dame[s[i][d]], 10) {
      if (used[j])
        continue;
      alc[s[i][d]] = j;
      used[j] = true;
      dfs(i + 1, d, sum + j);
      alc[s[i][d]] = -1;
      used[j] = false;
    }
  } else {
    dfs(i + 1, d, sum + alc[s[i][d]]);
  }
  return;
}

int main() {
  cin.sync_with_stdio(false);
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    alc.clear();
    dame.clear();
    int maxl = 0;
    rep(i, n) {
      cin >> s[i];
      rep(j, s[i].length()) {
        if (j == 0 && s[i].length() != 1)
          dame[s[i][j]] = 1;
        alc[s[i][j]] = -1;
      }
      maxch(maxl, (int)s[i].length());
    }
    rep(i, n) {
      while (s[i].length() < maxl)
        s[i] = "-" + s[i];
    }
    memset(used, 0, sizeof(used));
    alc['-'] = 0;
    res = 0;
    dfs(0, maxl - 1, 0);
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;

#define INF INT_MAX / 3

#define MAX_N 1000

int n;
string s[111];
map<char, int> ms;
bool used[11];

bool calc() {
  int sum = 0;
  rep(i, n) {
    int sum1 = 0;
    rep(j, s[i].length()) {
      sum1 *= 10;
      sum1 += ms[s[i][j]];
    }
    if (i < n - 1)
      sum += sum1;
    else
      return sum == sum1;
  }
  return false;
}

int dfs(int si, int ci) {
  if (si == n)
    return calc();
  if (ms[s[si][ci]] != -1) {
    if (s[si].length() == ci + 1)
      return dfs(si + 1, 0);
    else
      return dfs(si, ci + 1);
  }
  int res = 0;
  rep(i, 10) {
    if (ci == 0 && i == 0 && s[si].length() > 1)
      continue;
    if (used[i])
      continue;
    used[i] = true;
    ms[s[si][ci]] = i;
    if (s[si].length() == ci + 1)
      res += dfs(si + 1, 0);
    else
      res += dfs(si, ci + 1);
    used[i] = false;
    ms[s[si][ci]] = -1;
  }
  return res;
}

int main() {
  cin.sync_with_stdio(false);
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    rep(i, n) cin >> s[i];
    memset(used, 0, sizeof(used));
    ms.clear();
    rep(i, n) rep(j, s[i].length()) ms[s[i][j]] = -1;
    cout << dfs(0, 0) << endl;
  }
  return 0;
}