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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define INF 1 << 30
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int n, ans, cnt[26], len;
bool first[26], used[10];
vector<int> v, S[105];
vector<string> s;

void dfs(int i) {
  if (i == v.size()) {
    ll a = 0, b = 0, t = 1;
    rep(j, len) {
      {
        int sz = S[n - 1].size();
        int l = sz - 1 - j;
        if (l >= 0)
          a += cnt[S[n - 1][l]] * t;
      }
      rep(k, n - 1) {
        int sz = S[k].size();
        int l = sz - 1 - j;
        if (l >= 0)
          b += cnt[S[k][l]] * t;
      }
      t *= 10;
      if (a % t != b % t)
        break;
    }

    if (a == b) {
      ans++;
    }

    return;
  }

  int k = v[i];
  rep(j, 10) {
    if (used[j])
      continue;
    if (first[k] && j == 0)
      continue;

    cnt[k] = j;
    used[j] = true;
    dfs(i + 1);
    used[j] = false;
    cnt[k] = -1;
  }
}

int main() {
  while (cin >> n && n) {
    s.clear();
    s.resize(n);
    memset(first, 0, sizeof(first));

    set<char> st;
    len = 0;

    rep(i, n) {
      cin >> s[i];

      S[i].clear();
      len = max(len, int(s[i].size()));

      rep(j, s[i].size()) {
        st.insert(s[i][j]);
        S[i].push_back(s[i][j] - 'A');
      }

      if (s[i].size() >= 2) {
        first[s[i][0] - 'A'] = true;
      }
    }

    v.clear();
    each(it, st) { v.push_back(*it - 'A'); }

    ans = 0;
    memset(cnt, -1, sizeof(cnt));
    memset(used, 0, sizeof(used));

    dfs(0);

    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define INF 1 << 30
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int n, ans, cnt[26];
bool first[26], used[10];
vector<int> v, S[105];
vector<string> s;

void dfs(int i) {
  if (i == v.size()) {
    int res = 0, t = 1;
    for (int k = S[n - 1].size() - 1; k >= 0; k--) {
      res += cnt[S[n - 1][k]] * t;
      t *= 10;
    }

    rep(j, n - 1) {
      t = 1;
      for (int k = S[j].size() - 1; k >= 0; k--) {
        res -= cnt[S[j][k]] * t;
        t *= 10;
      }
      if (res < 0)
        break;
    }

    if (res == 0)
      ans++;

    return;
  }

  int k = v[i];
  rep(j, 10) {
    if (used[j])
      continue;
    if (first[k] && j == 0)
      continue;

    cnt[k] = j;
    used[j] = true;
    dfs(i + 1);
    used[j] = false;
    cnt[k] = -1;
  }
}

int main() {
  while (cin >> n && n) {
    s.clear();
    s.resize(n);
    memset(first, 0, sizeof(first));

    set<char> st;

    rep(i, n) {
      cin >> s[i];
      rep(j, s[i].size()) { st.insert(s[i][j]); }

      if (s[i].size() >= 2) {
        first[s[i][0] - 'A'] = true;
      }

      S[i].clear();
      rep(j, s[i].size()) { S[i].push_back(s[i][j] - 'A'); }
    }

    v.clear();
    each(it, st) { v.push_back(*it - 'A'); }

    ans = 0;
    memset(cnt, -1, sizeof(cnt));
    memset(used, 0, sizeof(used));

    dfs(0);

    cout << ans << endl;
  }
  return 0;
}