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

#include <iostream>
#include <map>
#include <set>
#include <vector>

using namespace std;

bool not_zero[10];
int coefficient[10];

struct S {
  int value, used;
  S(int value, int used) : value(value), used(used) {}
  bool operator<(const S &s) const {
    return value != s.value ? value < s.value : used < s.used;
  }
};

void Dfs(int depth, int alphabet_size, int used, int lr, map<S, int> &m) {
  if (depth == alphabet_size) {
    ++m[S(lr, used)];
  } else {
    for (int i = not_zero[depth] ? 1 : 0; i < 10; ++i) {
      if (used & (1 << i))
        continue;
      Dfs(depth + 1, alphabet_size, used | (1 << i),
          lr + coefficient[depth] * i, m);
    }
  }
}

int main() {
  int N;
  while (cin >> N, N) {
    vector<string> v(N);
    set<char> alph;
    for (int i = 0; i < N; ++i) {
      cin >> v[i];
      for (int j = 0; j < v[i].size(); ++j)
        alph.insert(v[i][j]);
    }

    string alphabet(alph.begin(), alph.end());

    vector<vector<int>> words(N);
    for (int i = 0; i < v.size(); ++i)
      for (int j = 0; j < v[i].size(); ++j)
        words[i].push_back(alphabet.find(v[i][j]));

    fill(coefficient, coefficient + 10, 0);
    for (int i = 0; i < N - 1; ++i)
      for (int j = words[i].size() - 1, ten = 1; j >= 0; --j, ten *= 10)
        coefficient[words[i][j]] += ten;
    for (int i = words.back().size() - 1, ten = 1; i >= 0; --i, ten *= 10)
      coefficient[words.back()[i]] -= ten;

    fill(not_zero, not_zero + 10, false);
    for (int i = 0; i < v.size(); ++i)
      if (v[i].size() > 1)
        not_zero[alphabet.find(v[i][0])] = true;

    map<S, int> head;
    Dfs(0, alphabet.size() / 2, 0, 0, head);
    map<S, int> tail;
    Dfs(alphabet.size() / 2, alphabet.size(), 0, 0, tail);

    vector<pair<S, int>> h(head.begin(), head.end());
    vector<pair<S, int>> t(tail.rbegin(), tail.rend());

    int answer = 0;
    for (int i = 0, j = 0; i < h.size() && j < t.size();) {
      int hv = h[i].first.value, tv = t[j].first.value, value = hv + tv;
      if (value < 0) {
        ++i;
      } else if (value > 0) {
        ++j;
      } else {
        for (int k = i; k < h.size() && h[k].first.value == hv; ++k) {
          for (int l = j; l < t.size() && t[l].first.value == tv; ++l) {
            if ((h[k].first.used & t[l].first.used) == 0) {
              answer += h[k].second * t[l].second;
            }
          }
        }
        while (i < h.size() && h[i].first.value == hv)
          ++i;
        while (j < t.size() && t[j].first.value == tv)
          ++j;
      }
    }
    cout << answer << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>

#define ll long long int

using namespace std;

const int INF = (1 << 30) - 1;

int to_int(string &s, vector<char> &digits) {
  if (s.size() != 1 && s[0] == digits[0])
    return -1;

  int l = s.size();
  int ret = 0;
  int x = 1;
  for (int i = l - 1; 0 <= i; i--) {
    for (int j = 0; j < digits.size(); j++) {
      if (s[i] == digits[j])
        ret += j * x;
    }
    x *= 10;
  }
  return ret;
}

bool check(vector<string> &strings, vector<char> &digits) {
  int sum = 0;
  for (int i = 0; i < strings.size() - 1; i++) {
    string &s = strings[i];
    int x = to_int(s, digits);
    if (x < 0)
      return false;
    sum += x;
  }
  int y = to_int(strings[strings.size() - 1], digits);
  if (y < 0)
    return false;
  return sum == y;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int N;
  while (cin >> N, N) {
    vector<string> strings(N);
    for (auto &s : strings)
      cin >> s;

    vector<char> digits;
    for (auto &s : strings)
      for (auto &c : s)
        digits.push_back(c);

    sort(digits.begin(), digits.end());
    digits.erase(unique(digits.begin(), digits.end()), digits.end());

    while (digits.size() != 10)
      digits.push_back('_');

    int ans = 0;
    do {
      ans += check(strings, digits);
    } while (next_permutation(digits.begin(), digits.end()));

    cout << ans << endl;
  }

  return 0;
}