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

#include <algorithm>
#include <bitset>
#include <ciso646>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

inline bool cheak(int x, int y, int xMax, int yMax) {
  return x >= 0 && y >= 0 && xMax > x && yMax > y;
}
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef long long ll;
typedef unsigned long long ull;

#define For(i, a, b) for (int(i) = (a); i < (b); (i)++)
#define rep(i, n) For(i, 0, n)
#define clr(a) memset((a), 0, sizeof(a))
#define mclr(a) memset((a), -1, sizeof(a))
#define all(a) (a).begin(), (a).end()
#define sz(a) (sizeof(a))
#define Fill(a, v) fill((int *)a, (int *)(a + (sz(a) / sz(*(a)))), v)

const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1},
          dy[8] = {0, -1, 0, 1, -1, 1, -1, 1};
const int mod = 1000000007;
const int INF = 1e9;

int tb[256];
int td[256];
bool zeroF[256];

bool f[10];

vector<int> d;

int dfs(int m) {
  if (d.size() == m) {
    int tmp = 0;
    rep(i, d.size()) { tmp += td[d[i]] * tb[d[i]]; }
    return (tmp ? 0 : 1);
  }
  int ret = 0;

  for (int i = 0; i <= 9; i++) {
    if (f[i] == false && (i != 0 || !zeroF[d[m]])) {
      tb[d[m]] = i;
      f[i] = true;
      ret += dfs(m + 1);
      f[i] = false;
    }
  }
  return ret;
}

int main() {
  int n;

  while (cin >> n && n) {
    clr(tb);
    clr(td);
    clr(zeroF);
    d.clear();

    set<char> se;
    rep(i, n) {
      string s;
      cin >> s;
      rep(j, s.size()) {
        if (s.size() != 1 && j == 0) {
          zeroF[s[j]] = true;
        }
        se.insert(s[j]);

        if (i == n - 1) {
          td[s[j]] -= pow(10, s.size() - j - 1);
        } else {
          td[s[j]] += pow(10, s.size() - j - 1);
        }
      }
    }
    for (auto i : se) {
      d.push_back(i);
    }

    cout << dfs(0) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <ciso646>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

inline bool cheak(int x, int y, int xMax, int yMax) {
  return x >= 0 && y >= 0 && xMax > x && yMax > y;
}
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef long long ll;
typedef unsigned long long ull;

#define For(i, a, b) for (int(i) = (a); i < (b); (i)++)
#define rep(i, n) For(i, 0, n)
#define clr(a) memset((a), 0, sizeof(a))
#define mclr(a) memset((a), -1, sizeof(a))
#define all(a) (a).begin(), (a).end()
#define sz(a) (sizeof(a))
#define Fill(a, v) fill((int *)a, (int *)(a + (sz(a) / sz(*(a)))), v)

const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1},
          dy[8] = {0, -1, 0, 1, -1, 1, -1, 1};
const int mod = 1000000007;
const int INF = 1e9;

int tb[256];
bool f[10];

vector<int> d;
vector<string> vs;

int dfs(int m) {
  if (d.size() == m) {
    int val1 = 0;
    rep(i, vs.size() - 1) {
      rep(j, vs[i].size()) {
        if (vs[i].size() != 1 && j == 0 && tb[vs[i][j]] == 0) {
          return 0;
        }
        val1 += tb[vs[i][j]] * pow(10, vs[i].size() - j);
      }
    }
    int val2 = 0;
    rep(j, vs[vs.size() - 1].size()) {
      if (vs[vs.size() - 1].size() != 1 && j == 0 &&
          tb[vs[vs.size() - 1][j]] == 0) {
        return 0;
      }
      val2 += tb[vs[vs.size() - 1][j]] * pow(10, vs[vs.size() - 1].size() - j);
    }
    if (val1 == val2) {
      return 1;
    }
    return 0;
  }
  int ret = 0;

  for (int i = 0; i <= 9; i++) {
    if (f[i] == false) {
      tb[d[m]] = i;
      f[i] = true;
      ret += dfs(m + 1);
      f[i] = false;
    }
  }
  return ret;
}

int main() {
  int n;

  while (cin >> n && n) {
    clr(tb);
    clr(f);
    d.clear();
    vs.clear();
    set<char> se;
    rep(i, n) {
      string s;
      cin >> s;
      rep(j, s.size()) { se.insert(s[j]); }
      vs.push_back(s);
    }
    for (auto i : se) {
      d.push_back(i);
    }

    cout << dfs(0) << endl;
  }
  return 0;
}