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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, a, n) for (i = a; i < (n); i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;

int main(void) {

  int n;
  while (cin >> n, n) {
    vs s(n);
    rep(i, n) cin >> s[i];

    int co[26] = {};
    int c[26] = {}; // leading zeros
    rep(i, n) {
      if (s[i].size() > 1)
        c[s[i][0] - 'A']++;
      int t[26] = {};
      rep(j, s[i].size()) {
        rep(k, 26) t[k] *= 10;
        t[s[i][j] - 'A']++;
      }
      if (i + 1 < n)
        rep(j, 26) co[j] += t[j];
      else
        rep(j, 26) co[j] -= t[j];
    }
    int a[26] = {};
    rep(i, n) rep(j, s[i].size()) a[s[i][j] - 'A']++;

    vi v;
    rep(i, 26) if (a[i]) v.push_back(i);

    while (v.size() < 10)
      v.push_back(-1);
    sort(all(v));
    int ans = 0;

    do {
      int alph[26];
      rep(i, 26) alph[i] = -1;
      rep(i, v.size()) if (v[i] >= 0) alph[v[i]] = i;

      if (v[0] >= 0 && c[v[0]])
        continue;

      int sum = 0;
      rep(i, 26) if (alph[i] >= 0) { sum += co[i] * alph[i]; }

      if (sum == 0) {
        ans++;
      }
    } while (next_permutation(all(v)));

    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, a, n) for (i = a; i < (n); i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;

int alph[26] = {};

int num(string s) {
  if (s.size() > 1 && alph[s[0] - 'A'] == 0)
    return -1;
  int ret = 0;
  rep(i, s.size()) {
    ret *= 10;
    ret += alph[s[i] - 'A'];
  }
  return ret;
}

int main(void) {

  int n;
  while (cin >> n, n) {
    vs s(n);
    rep(i, n) cin >> s[i];
    int cnt[26] = {};
    rep(i, n) rep(j, s[i].size()) cnt[s[i][j] - 'A']++;

    vi v;
    rep(i, 26) if (cnt[i]) v.push_back(i);

    while (v.size() < 10)
      v.push_back(-1);
    sort(all(v));
    int ans = 0;

    do {
      rep(i, v.size()) if (v[i] >= 0) alph[v[i]] = i;
      int sum = 0;
      rep(i, n - 1) {
        int t = num(s[i]);
        if (t == -1) {
          sum = -1;
          break;
        }
        sum += t;
      }

      if (sum >= 0) {
        sum -= num(s[n - 1]);
        if (sum == 0)
          ans++;
      }
    } while (next_permutation(all(v)));

    cout << ans << endl;
  }
}