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

#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

// const long long int MOD = 1000000007;
const int MOD = 1000000007;
// const int MOD = 998244353;
// const long long int MOD = 998244353;

// long long int N, M, K, H, W, L, R;
int N, M, K, H, W, L, R;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  while (cin >> N, N) {
    vector<string> s(N);
    for (auto &i : s)
      cin >> i;
    map<char, int> m;
    for (auto i : s) {
      for (auto j : i)
        m[j] = 0;
    }
    int cnt = 0;
    for (auto &i : m)
      i.second = cnt++;
    for (auto &i : s) {
      for (auto &j : i)
        j = m[j];
    }
    M = m.size();
    vector<int> v(10);
    for (int i = 0; i < 10; i++)
      v[i] = i;
    int ans = 0;
    do {
      int sum = 0;
      bool flag = true;
      for (int i = 0; i < s.size() - 1; i++) {
        if (s[i].size() > 1 && !v[s[i][0]]) {
          flag = false;
          break;
        }
        int add = 0;
        for (auto j : s[i]) {
          add *= 10;
          add += v[j];
        }
        sum += add;
      }
      int fin = 0;
      if (s.back().size() > 1 && !v[s.back()[0]])
        flag = false;
      for (auto i : s.back()) {
        fin *= 10;
        fin += v[i];
      }
      if (sum == fin && flag)
        ans++;
    } while (next_permutation(v.begin(), v.end()));
    for (int i = 1; i <= 10 - m.size(); i++)
      ans /= i;
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

// const long long int MOD = 1000000007;
const int MOD = 1000000007;
// const int MOD = 998244353;
// const long long int MOD = 998244353;

// long long int N, M, K, H, W, L, R;
int N, M, K, H, W, L, R;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  while (cin >> N, N) {
    vector<string> s(N);
    for (auto &i : s)
      cin >> i;
    map<char, int> m;
    for (auto i : s) {
      for (auto j : i)
        m[j] = 0;
    }
    int cnt = 0;
    for (auto &i : m)
      i.second = cnt++;
    for (auto &i : s) {
      for (auto &j : i)
        j = m[j];
    }
    M = m.size();
    vector<int> v(10);
    for (int i = 0; i < 10; i++)
      v[i] = i;
    int ans = 0;
    do {
      int sum = 0;
      bool flag = true;
      for (int i = 0; i < s.size() - 1; i++) {
        if (s[i].size() > 1 && !v[s[i][0]])
          flag = false;
        int add = 0;
        for (auto j : s[i]) {
          add *= 10;
          add += v[j];
        }
        sum += add;
      }
      int fin = 0;
      if (s.back().size() > 1 && !v[s.back()[0]])
        flag = false;
      for (auto i : s.back()) {
        fin *= 10;
        fin += v[i];
      }
      if (sum == fin && flag)
        ans++;
    } while (next_permutation(v.begin(), v.end()));
    for (int i = 1; i <= 10 - m.size(); i++)
      ans /= i;
    cout << ans << endl;
  }
}