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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <forward_list>
#include <initializer_list>
#include <tuple>
#include <unordered_map>
#include <unordered_set>

#define cauto const auto &
#else

#endif

using namespace std;

namespace {
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;

#define VV(T) vector<vector<T>>

template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}

template <class F, class T> void convert(const F &f, T &t) {
  stringstream ss;
  ss << f;
  ss >> t;
}

#define reep(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) reep((i), 0, (n))
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(), (v).rend()

#define MOD 1000000007LL
#define EPS 1e-8
static const int INF = 1 << 24;
vint a, b;
int foo(int x, int y, int z) {
  if (x == -1)
    return (y == 0 ? 1 : 0);
  int MA = 0, MI = 0;
  for (int i = x; i >= 0; i--) {
    if (a[i] > 0)
      MA += a[i] * 9;
    else if (a[i] < 0)
      MI += a[i] * 9;
  }
  if (y > 0 && y + MI > 0)
    return 0;
  if (y < 0 && y + MA < 0)
    return 0;
  int ret = 0;
  for (int i = 0; i < 10; i++) {
    if (z & (1 << i) || (!i && b[x]))
      continue;
    ret += foo(x - 1, y + a[x] * i, z | (1 << i));
  }
  return ret;
}

void mainmain() {
  for (int n; cin >> n, n;) {
    vint x(10, 0);
    vint y(300, -1);
    int cnt = 0;
    vint z(10, 0);
    rep(i, n) {
      string s;
      cin >> s;
      int t = (i == n - 1 ? -1 : 1);
      int c = 1;
      for (int j = s.size() - 1; j >= 0; j--) {
        if (y[s[j]] == -1)
          y[s[j]] = cnt++;
        x[y[s[j]]] += t * c;
        c *= 10;
      }
      if (s.size() > 1) {
        z[y[s[0]]] = 1;
      }
    }
    a = x;
    b = z;
    cout << foo(cnt - 1, 0, 0) << endl;
  }
}

} // namespace
main() try { mainmain(); } catch (...) {
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define reep(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) reep((i), 0, (n))
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second
#define mkp make_pair

static const double PI = 6 * asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vint;
static const int INF = 1 << 24;

template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}
vector<string> vs;
int n;
vint ma1, ma2;
vint ma3;
ll ans;
int cou;

bool srtingexist(char c) {
  rep(i, n) {
    rep(j, vs[i].size()) {
      if (vs[i][j] == c)
        return true;
    }
  }
  return false;
}
void stringconv(char c, char b) {
  rep(i, n) {
    rep(j, vs[i].size()) {
      if (vs[i][j] == c) {
        vs[i][j] = b;
      }
    }
  }
}

void rec(int c) {
  // cout<<c<<endl;
  int cc = cou - c;
  bool f = true;
  rep(i, n) {
    if (ma1[vs[i][vs[i].size() - 1] - 'a'] == -1) {
      f = false;
      break;
    }
  }
  if (f) {
    int SUM = 0;
    rep(i, n - 1) SUM += ma1[vs[i][vs[i].size() - 1] - 'a'];
    if (ma1[vs[n - 1][vs[n - 1].size() - 1] - 'a'] != SUM % 10)
      return;
  }
  if (c == 0) {
    vector<string> tmp = vs;
    vint t(vs.size());
    rep(i, n) {
      rep(j, vs[i].size()) { tmp[i][j] = ma1[tmp[i][j] - 'a'] + '0'; }
      // cout<<tmp[i]<<endl;
    }
    // cout<<endl;
    rep(i, n) {
      stringstream ss;
      ss << tmp[i];
      ss >> t[i];
    }
    int S = 0;
    rep(i, n - 1) { S += t[i]; }
    if (S == t[n - 1])
      ans++;
    // cout<<endl;
    return;
  }
  rep(i, 10) {
    if (ma3[i]) {
      // cout<<"hoge"<<endl;
      if (i == 0 && ma2[cc] == 1)
        continue;
      ma1[cc] = i;
      ma3[i] = 0;
      rec(c - 1);
      ma3[i] = 1;
      ma1[cc] = -1;
    }
  }
}

int main() {
  while (cin >> n, n) {
    vs.resize(n);
    rep(i, n) { cin >> vs[i]; }
    int count = 0;
    rep(i, 'z' - 'a' + 1) {
      if (srtingexist(i + 'A')) {
        stringconv('A' + i, 'a' + count);
        count++;
      }
    }
    cou = count;
    ans = 0;
    ma1.resize(cou);
    ma2.resize(cou);
    ma3.resize(10);
    rep(i, cou) {
      ma1[i] = 0;
      ma2[i] = 0;
    }
    rep(i, 10) ma3[i] = 0;
    rep(i, n) {
      if (vs[i].size() != 1)
        ma2[vs[i][0] - 'a'] = 1;
    }
    rep(i, cou) { ma1[i] = -1; }
    rep(i, 10) {
      // ma1[i]=-1;
      ma3[i] = 1;
    }
    rec(count);
    cout << ans << endl;
  }
}