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

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
// #include<cctype>
#include <climits>
#include <iostream>
#include <map>
#include <string>
#include <vector>
// #include<list>
#include <algorithm>
#include <deque>
#include <queue>
// #include<numeric>
#include <complex>
#include <utility>
// #include<memory>
#include <cassert>
#include <functional>
#include <random>
#include <set>
#include <stack>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

inline int calc(const vi &v, const vi &perm) {
  int ret = 0;
  for (int el : v)
    ret = ret * 10 + perm[el];
  return ret;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  while (cin >> N) {
    if (N == 0)
      break;
    vector<char> vs;
    vector<string> ss(N);
    for (int i = 0; i < N; i++) {
      cin >> ss[i];
      for (char c : ss[i])
        vs.push_back(c);
    }
    sort(vs.begin(), vs.end());
    vs.erase(unique(vs.begin(), vs.end()), vs.end());
    vector<vi> nums;
    for (int i = 0; i < N; i++) {
      vi v;
      for (char c : ss[i]) {
        v.push_back(lower_bound(vs.begin(), vs.end(), c) - vs.begin());
      }
      nums.push_back(v);
    }
    vector<int> perm(10);
    for (int i = 0; i < 10; i++)
      perm[i] = i;
    int ans = 0;

    do {
      bool muri = false;
      for (int i = 0; i < N; i++)
        if (nums[i].size() > 1 && perm[nums[i][0]] == 0) {
          muri = true;
          break;
        }
      if (muri)
        continue;
      int target = calc(nums[N - 1], perm), sum = 0;
      for (int i = 0; i < N - 1; i++) {
        if (sum > target)
          break;
        sum += calc(nums[i], perm);
      }
      if (sum == target)
        ans++;
    } while (next_permutation(perm.begin(), perm.end()));

    int cnt = 10 - vs.size();
    int div = 1;
    for (int i = 1; i <= cnt; i++)
      div *= i;
    cout << ans / div << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
// #include<cctype>
#include <climits>
#include <iostream>
#include <map>
#include <string>
#include <vector>
// #include<list>
#include <algorithm>
#include <deque>
#include <queue>
// #include<numeric>
#include <complex>
#include <utility>
// #include<memory>
#include <cassert>
#include <functional>
#include <random>
#include <set>
#include <stack>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

inline int trans(const string &s, const map<char, int> &mp) {
  int ret = 0;
  for (char c : s) {
    ret = ret * 10 + mp.find(c)->second;
  }
  return ret;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  while (cin >> n) {
    vector<string> ss(n);
    for (int i = 0; i < n; i++)
      cin >> ss[i];
    vector<int> perm(10);
    for (int i = 0; i < 10; i++)
      perm[i] = i;
    map<char, int> mp;
    map<char, char> flags;
    for (string s : ss) {
      flags[s[0]] = true;
      for (char c : s)
        mp[c] = 0;
    }
    int ans = 0;
    do {
      int cnt = 0;
      bool ng = false;
      for (auto &p : mp) {
        if (flags[p.first] && perm[cnt] == 0)
          ng = true;
        p.second = perm[cnt++];
      }
      if (ng)
        continue;
      int sum = 0, target = trans(ss[n - 1], mp);
      for (int i = 0; i < n - 1; i++) {
        if (sum > target)
          break;
        sum += trans(ss[i], mp);
      }
      if (sum == target)
        ans++;
    } while (next_permutation(perm.begin(), perm.end()));
    int cnt = 10 - mp.size();
    int div = 1;
    for (int i = 1; i <= cnt; i++)
      div *= i;
    cout << ans / div << endl;
  }
  return 0;
}