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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll MOD = 1000000007;
const int INF = 1 << 29;

int N;
vector<string> strs;

set<int> non_zero;
int mapped_num[26];
bool used[10];
int max_len;

int dfs(int x, int y, int s) {
  if (x >= max_len) {
    if (s == 0)
      return 1;
    else
      return 0;
  }
  if (y == N - 1) {
    if (x >= strs[y].size())
      return 0;
    int d = strs[y][strs[y].size() - 1 - x] - 'A';
    if (mapped_num[d] == -1) {
      int i = s % 10;
      if (used[i])
        return 0;
      if (i == 0 && non_zero.count(d) > 0)
        return 0;
      used[i] = true;
      mapped_num[d] = i;
      int ret = dfs(x + 1, 0, s / 10);
      mapped_num[d] = -1;
      used[i] = false;
      return ret;
    } else {
      if (mapped_num[d] != s % 10)
        return 0;
      return dfs(x + 1, 0, s / 10);
    }
  } else {
    if (x >= strs[y].size())
      return dfs(x, y + 1, s);
    int d = strs[y][strs[y].size() - 1 - x] - 'A';
    if (mapped_num[d] != -1)
      return dfs(x, y + 1, s + mapped_num[d]);
    int ret = 0;
    for (int i = 0; i < 10; i++) {
      if (used[i])
        continue;
      if (i == 0 && non_zero.count(d) > 0)
        continue;
      used[i] = true;
      mapped_num[d] = i;
      ret += dfs(x, y + 1, s + i);
      mapped_num[d] = -1;
      used[i] = false;
    }
    return ret;
  }
}

int main() {
  while (true) {
    cin >> N;
    if (N == 0)
      break;

    strs.resize(N);
    non_zero.clear();
    max_len = 0;
    for (int i = 0; i < N; i++) {
      cin >> strs[i];
      if (strs[i].size() > 1)
        non_zero.insert(strs[i][0] - 'A');
      max_len = max(max_len, int(strs[i].size()));
    }

    memset(mapped_num, -1, sizeof(mapped_num));
    memset(used, 0, sizeof(used));
    cout << dfs(0, 0, 0) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll MOD = 1000000007;
const int INF = 1 << 29;

int mapped_num[26];
int N;
vector<string> strs;
vector<char> chars;
set<char> non_zero;
bool used[10];

bool check() {
  int total = 0;
  int p = 1;
  for (int i = 0; i < N - 1; i++) {
    p = 1;
    for (int j = strs[i].size() - 1; j >= 0; j--) {
      total += p * mapped_num[strs[i][j] - 'A'];
      p *= 10;
    }
  }
  int sum_line = 0;
  p = 1;
  for (int j = strs[N - 1].size() - 1; j >= 0; j--) {
    sum_line += p * mapped_num[strs[N - 1][j] - 'A'];
    p *= 10;
  }
  return sum_line == total;
}

int dfs(int pos) {
  if (pos == chars.size()) {
    if (check())
      return 1;
    else
      return 0;
  }
  int ret = 0;
  for (int i = 0; i < 10; i++) {
    if (used[i])
      continue;
    if (i == 0 && non_zero.count(chars[pos]) > 0)
      continue;
    used[i] = true;
    mapped_num[chars[pos] - 'A'] = i;
    ret += dfs(pos + 1);
    used[i] = false;
  }
  return ret;
}

int main() {
  while (true) {
    cin >> N;
    if (N == 0)
      break;

    strs.resize(N);
    non_zero.clear();
    set<char> chars_set;
    for (int i = 0; i < N; i++) {
      cin >> strs[i];
      if (strs[i].size() > 1)
        non_zero.insert(strs[i][0]);
      for (int j = 0; j < strs[i].size(); j++) {
        if (chars_set.count(strs[i][j]) == 0) {
          chars_set.insert(strs[i][j]);
        }
      }
    }
    chars = vector<char>(chars_set.begin(), chars_set.end());
    memset(used, 0, sizeof(used));
    cout << dfs(0) << endl;
  }

  return 0;
}