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

#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define reps(i, j, k) for (int i = (j); i < (k); i++)
#define rep(i, j) reps(i, 0, j)
#define fs first
#define sc second
#define pb push_back
#define mk make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> p) {
  return out << "(" << p.fs << ", " << p.sc << ")";
}

template <class T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << "{";
  rep(i, v.size()) out << v[i] << ", ";
  return out << "}" << endl;
}

int main() {
  int n;
  while (cin >> n, n) {
    vector<string> v(n);
    vector<char> c;
    vi num(10);
    rep(i, 10) num[i] = i;
    rep(i, n) {
      cin >> v[i];
      rep(j, v[i].length()) {
        char ch = v[i][j];
        rep(k, c.size()) if (ch == c[k]) {
          ch = 0;
          break;
        }
        if (ch)
          c.pb(ch);
      }
    }
    while (c.size() < 10)
      c.pb(0);
    sort(c.begin(), c.end());
    int cnt = 0;
    vi istop(256);
    rep(i, n) {
      if (v[i].length() == 1)
        continue;
      istop[v[i][0]] = 1;
    }
    vi keisu(256);
    rep(i, n) {
      int t = 1;
      reverse(v[i].begin(), v[i].end());
      rep(j, v[i].length()) {
        keisu[v[i][j]] += i != n - 1 ? t : -t;
        t *= 10;
      }
    }
    do {
      if (istop[c[0]])
        continue;
      int sum = 0;
      rep(i, 10) sum += keisu[c[i]] * i;
      cnt += sum == 0;
    } while (next_permutation(c.begin(), c.end()));
    cout << cnt << endl;
    ;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define reps(i, j, k) for (int i = (j); i < (k); i++)
#define rep(i, j) reps(i, 0, j)
#define fs first
#define sc second
#define pb push_back
#define mk make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> p) {
  return out << "(" << p.fs << ", " << p.sc << ")";
}

template <class T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << "{";
  rep(i, v.size()) out << v[i] << ", ";
  return out << "}" << endl;
}

int main() {
  int n;
  while (cin >> n, n) {
    vector<string> v(n);
    vector<char> c;
    vi num(10);
    rep(i, 10) num[i] = i;
    rep(i, n) {
      cin >> v[i];
      rep(j, v[i].length()) {
        char ch = v[i][j];
        rep(k, c.size()) if (ch == c[k]) {
          ch = 0;
          break;
        }
        if (ch)
          c.pb(ch);
      }
    }
    while (c.size() < 10)
      c.pb(0);
    sort(c.begin(), c.end());
    int cnt = 0;
    unordered_map<char, int> istop;
    rep(i, n) {
      if (v[i].length() == 1)
        continue;
      istop[v[i][0]] = 1;
    }
    unordered_map<char, int> keisu;
    rep(i, n) {
      int t = 1;
      reverse(v[i].begin(), v[i].end());
      rep(j, v[i].length()) {
        keisu[v[i][j]] += i != n - 1 ? t : -t;
        t *= 10;
      }
    }
    do {
      if (istop[c[0]])
        continue;
      int sum = 0;
      rep(i, 10) sum += keisu[c[i]] * i;
      cnt += sum == 0;
    } while (next_permutation(c.begin(), c.end()));
    cout << cnt << endl;
    ;
  }
  return 0;
}