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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef complex<double> P;
static const double EPS = 1e-8;
static const int INF = (int)1e8;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOREQ(i, k, n) for (int i = (k); i <= (int)(n); ++i)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define FIND(m, w) ((m).find(w) != (m).end())
void dfs(int &result, int pos, const vector<int> &m, const vector<string> &ss,
         vector<int> &assoc, vector<int> &visited, vector<int> &cof) {
  int sum = 0;
  REP(i, cof.size()) if (assoc[i] != -1) sum += cof[i] * assoc[i];
  if (pos == assoc.size()) {
    /*
    vector<int> vs(ss.size());
    REP(i,ss.size()){
      REP(j,ss[i].size()){
        vs[i] = vs[i] * 10 + assoc[m[ss[i][j]]];
      }
    }
    if(vs.back() == accumulate(vs.begin(), vs.end()-1, 0)){
      ++result;
    }
    */
    if (sum == 0)
      ++result;
    return;
  } else {
    if (sum > 0) {
      FOR(i, pos, assoc.size()) {
        if (cof[i] < 0)
          sum += 9 * cof[i];
      }
      if (sum > 0)
        return;
    } else {
      FOR(i, pos, assoc.size()) {
        if (cof[i] > 0)
          sum += 9 * cof[i];
      }
      if (sum < 0)
        return;
    }

    REP(k, 10) {
      if (visited[k])
        continue;
      if (k == 0) {
        REP(i, ss.size()) if (ss[i].size() > 1 && m[ss[i][0]] == pos) {
          goto NEXT;
        }
      }
      visited[k] = true;
      assoc[pos] = k;
      dfs(result, pos + 1, m, ss, assoc, visited, cof);
      assoc[pos] = -1;
      visited[k] = false;
    NEXT:;
    }
  }
}
int main(void) {
  int n;
  while (cin >> n, n) {
    vector<string> ss(n);
    REP(i, n) cin >> ss[i];
    vector<int> m(256, -1);
    int p = 0;
    int ans = 0;
    REP(i, ss.size())
    REP(j, ss[i].size()) if (m[ss[i][j]] < 0) m[ss[i][j]] = p++;
    vector<int> visited(10, 0);
    vector<int> assoc(p, -1);
    vector<int> cof(p, 0);
    REP(i, ss.size()) REP(j, ss[i].size()) {
      cof[m[ss[i][j]]] +=
          pow(10, ss[i].size() - j - 1) * ((i == ss.size() - 1) ? (-1) : 1);
    }
    /*
    REP(i,cof.size())FOR(j,i+1,cof.size())if(abs(cof[i])<abs(cof[j])){
      swap(cof[i],cof[j]);
      REP(k,m.size()){
        if(m[k]==i)m[k]=j;
        if(m[k]==j)m[k]=i;
      }
    }
    */
    dfs(ans, 0, m, ss, assoc, visited, cof);
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef complex<double> P;
static const double EPS = 1e-8;
static const int INF = (int)1e8;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOREQ(i, k, n) for (int i = (k); i <= (int)(n); ++i)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define FIND(m, w) ((m).find(w) != (m).end())
void dfs(int &result, int pos, const vector<int> &m, const vector<string> &ss,
         vector<int> &assoc, vector<int> &visited, vector<int> &cof) {
  if (pos == assoc.size()) {
    /*
    vector<int> vs(ss.size());
    REP(i,ss.size()){
      REP(j,ss[i].size()){
        vs[i] = vs[i] * 10 + assoc[m[ss[i][j]]];
      }
    }
    if(vs.back() == accumulate(vs.begin(), vs.end()-1, 0)){
      ++result;
    }
    */
    int sum = 0;
    REP(i, cof.size()) sum += cof[i] * assoc[i];
    if (sum == 0)
      ++result;
    return;
  } else {
    REP(k, 10) {
      if (visited[k])
        continue;
      if (k == 0) {
        REP(i, ss.size()) if (ss[i].size() > 1 && m[ss[i][0]] == pos) {
          goto NEXT;
        }
      }
      visited[k] = true;
      assoc[pos] = k;
      dfs(result, pos + 1, m, ss, assoc, visited, cof);
      assoc[pos] = -1;
      visited[k] = false;
    NEXT:;
    }
  }
}

int main(void) {
  int n;
  while (cin >> n, n) {
    vector<string> ss(n);
    REP(i, n) cin >> ss[i];
    vector<int> m(256, -1);
    int p = 0;
    int ans = 0;
    REP(i, ss.size())
    REP(j, ss[i].size()) if (m[ss[i][j]] < 0) m[ss[i][j]] = p++;
    vector<int> visited(10, 0);
    vector<int> assoc(p, -1);
    vector<int> cof(p, 0);
    REP(i, ss.size()) REP(j, ss[i].size()) {
      cof[m[ss[i][j]]] +=
          pow(10, ss[i].size() - j - 1) * ((i == ss.size() - 1) ? (-1) : 1);
    }
    dfs(ans, 0, m, ss, assoc, visited, cof);
    cout << ans << endl;
  }
  return 0;
}