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

// g++ -std=c++11 a.cpp
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define FOR(i, a) for (auto i : a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
// #define int ll
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
#define mt make_tuple
typedef tuple<int, int, int, int> tp;
typedef vector<tp> vt;
template <typename A, typename B> bool cmin(A &a, const B &b) {
  return a > b ? (a = b, true) : false;
}
template <typename A, typename B> bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}
// template<class C>constexpr int size(const C &c){return (int)c.size();}
// template<class T,size_t N> constexpr int size(const T
// (&xs)[N])noexcept{return (int)N;}
const double PI = acos(-1);
const double EPS = 1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9 + 10;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
#define it vector<int>::iterator
bool next_combination(it first1, it last1, it first2, it last2) {
  if ((first1 == last1) || (first2 == last2))
    return false;
  it m1 = last1, m2 = last2;
  m2--;
  while (--m1 != first1 && !(*m1 < *m2))
    ;
  bool result = (m1 == first1) && !(*first1 < *m2);
  if (!result) {
    while (first2 != m2 && !(*m1 < *first2))
      first2++;
    first1 = m1;
    iter_swap(first1, first2);
    first1++;
    first2++;
  }
  if ((first1 != last1) && (first2 != last2)) {
    m1 = last1;
    m2 = first2;
    while ((m1 != first1) && (m2 != last2))
      iter_swap(--m1, m2++);
    reverse(first1, m1);
    reverse(first1, last1);
    reverse(m2, last2);
    reverse(first2, last2);
  }
  return !result;
}
bool next_combination(it first, it middle, it last) {
  return next_combination(first, middle, middle, last);
}
int main() {
  int n;
  while (cin >> n, n) {
    vs in(n);
    rep(i, n) cin >> in[i];
    map<int, int> ma;
    rep(i, n) rep(j, in[i].size()) ma[in[i][j]] = 2;
    int N = ma.size();
    int t = 0;
    for (auto &ite : ma)
      ite.second = t++;

    vi w(N);
    rep(i, n) {
      int t = 1;
      for (int j = in[i].size() - 1; j >= 0; j--)
        w[ma[in[i][j]]] += t * (i == n - 1 ? -1 : 1), t *= 10;
    }
    vi dame(N);
    rep(i, n) if (in[i].size() > 1) dame[ma[in[i][0]]] = 1;

    vi pr(10);
    rep(i, 10) pr[i] = i;
    int co = 0;
    do {
      do {
        bool h = true;
        rep(i, N) if (pr[i] == 0 && dame[i]) h = false;
        if (!h)
          continue;

        int sum = 0;
        rep(i, N) sum += pr[i] * w[i];
        if (sum == 0)
          co++;
      } while (next_permutation(pr.begin(), pr.begin() + N));
    } while (next_combination(pr.begin(), pr.begin() + N, pr.end()));
    cout << co << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
int n;
vector<string> in, tmp;
int out;
vi visit, fil, num;
void f() {
  rep(i, 27) {
    if (i == 26) {
      //		rep(j,26)cout<<" "<<num[j];cout<<endl;
      tmp = in;
      bool h = true;
      rep(i, n) rep(j, tmp[i].size()) {
        tmp[i][j] = (char)(num[tmp[i][j] - 'A'] + '0');
      }
      rep(i, n) if (tmp[i].size() > 1 && tmp[i][0] == '0') h = false;
      if (!h)
        continue;
      int sum = 0;
      rep(i, n - 1) sum += atoi(&tmp[i][0]);
      //			cout<<sum<<" "<<atoi(&tmp[n-1][0])<<endl;
      if (sum == atoi(&tmp[n - 1][0]))
        out++;
    } else if (visit[i] == 1) {
      rep(j, 10) if (!fil[j]) {
        fil[j] = true;
        visit[i] = 2;
        num[i] = j;
        f();
        visit[i] = 1;
        fil[j] = false;
      }
      break;
    }
  }
}
int main() {
  while (cin >> n, n) {
    in = vector<string>(n);
    rep(i, n) cin >> in[i];
    num = visit = vi(26);
    fil = vi(10);
    rep(i, n) rep(j, in[i].size()) visit[in[i][j] - 'A'] = true;
    out = 0;
    f();
    cout << out << endl;
  }
}