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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge {
  int u, v;
  ll w;
};

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

int powten[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000};

int f(int i, int j, vector<string> &s, vector<int> &a, map<char, int> &m,
      vector<bool> &b) {
  if (j == 8)
    return 1;
  int N = s.size();
  if (i == N) {
    /*for (int i = 0; i < N; i++)
            cout << a[i] << ' ';
    cout << endl;
    string x; cin >> x;*/
    int sum = 0;
    for (int i = 0; i < N - 1; i++)
      sum += a[i];
    if ((sum - a[N - 1]) % powten[j + 1])
      return 0;
    return f(0, j + 1, s, a, m, b);
  }
  if (j >= s[i].length())
    return f(i + 1, j, s, a, m, b);
  char c = s[i][s[i].length() - j - 1];
  if (m[c] == -1) {
    int sum = 0, x = (s[i].length() >= 2 && s[i].length() - j - 1 == 0 ? 1 : 0);
    for (int k = x; k < 10; k++) {
      if (b[k])
        continue;
      b[k] = true;
      m[c] = k;
      a[i] += k * powten[j];
      sum += f(i + 1, j, s, a, m, b);
      b[k] = false;
      m[c] = -1;
      a[i] -= k * powten[j];
    }
    return sum;
  } else {
    if (m[c] == 0 && s[i].length() >= 2 && s[i].length() - j - 1 == 0)
      return 0;
    a[i] += m[c] * powten[j];
    int res = f(i + 1, j, s, a, m, b);
    a[i] -= m[c] * powten[j];
    return res;
  }
}

int main() {
  for (;;) {
    int N;
    cin >> N;
    if (N == 0)
      break;
    vector<string> s(N);
    for (int i = 0; i < N; i++)
      cin >> s[i];
    vector<int> a(N);
    map<char, int> m;
    for (char c = 'A'; c <= 'Z'; c++)
      m[c] = -1;
    vector<bool> b(10);
    cout << f(0, 0, s, a, m, b) << endl;
  }
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge {
  int u, v;
  ll w;
};

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

int foo(vector<int> &v, string &s) {
  int x = 0;
  for (int j = 0; j < s.length(); j++)
    x = x * 10 + v[s[j]];
  return x;
}

int dfs(int n, vector<int> &v, vector<string> &s) {
  int N = s.size();
  if (v.size() == n) {
    int sum = 0;
    for (int i = 0; i < N - 1; i++)
      sum += foo(v, s[i]);
    return sum == foo(v, s[N - 1]) ? 1 : 0;
  }
  int cnt = 0;
  for (int k = 0; k < 10; k++) {
    if (find(v.begin(), v.end(), k) != v.end())
      continue;
    if (k == 0) {
      bool flag = true;
      for (int i = 0; i < N; i++)
        if (s[i].length() >= 2 && s[i][0] == v.size())
          flag = false;
      if (!flag)
        continue;
    }
    v.push_back(k);
    cnt += dfs(n, v, s);
    v.erase(v.end() - 1);
  }
  return cnt;
}

int main() {
  for (;;) {
    int N;
    cin >> N;
    if (N == 0)
      break;
    vector<string> s(N);
    for (int i = 0; i < N; i++)
      cin >> s[i];
    int maxi = 0;
    for (int i = 0; i < N - 1; i++)
      maxi = max(maxi, (int)s[i].length());
    int x = s[N - 1].length();
    if (x - maxi < 0 || x - maxi > 2) {
      cout << 0 << endl;
      continue;
    }
    int n = 0;
    vector<int> a(128, -1);
    for (int i = 0; i < N; i++)
      for (int j = 0; j < s[i].length(); j++) {
        char &c = s[i][j];
        if (a[c] == -1)
          a[c] = n++;
        c = a[c];
      }
    vector<int> v;
    cout << dfs(n, v, s) << endl;
  }
}