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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define rp(i, c) rep(i, (c).size())
#define fr(i, c)                                                               \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = 1 << 28;
const double INF = 1e12, EPS = 1e-9;

int n;
char in[12][9];
int m, ans, id[256];
pi co[10];
bool use[10];

void rec(int c, int s) {
  int big = s, sml = s;
  for (int i = c; i < m; i++) {
    if (co[i].first < 0)
      sml += 9 * co[i].first;
    else
      big += 9 * co[i].first;
  }
  if (sml > 0 || big < 0)
    return;

  if (c == m) {
    ans++;
    return;
  }
  rep(i, 10) if (!use[i] && !(co[c].second && i == 0)) {
    use[i] = 1;
    rec(c + 1, s + co[c].first * i);
    use[i] = 0;
  }
}
int main() {
  while (scanf("%d", &n), n) {
    rep(i, n) scanf("%s", in[i]);
    rep(i, 256) id[i] = -1;
    rep(i, 10) co[i] = mp(0, 0), use[i] = 0;
    ans = m = 0;

    rep(i, n) {
      int t = i < n - 1 ? 1 : -1, len = strlen(in[i]);
      for (int j = len - 1; j >= 0; j--) {
        char c = in[i][j];
        if (id[c] < 0)
          id[c] = m++;
        co[id[c]].first += t;
        if (j == 0 && len > 1)
          co[id[c]].second = 1;
        t *= 10;
      }
    }
    sort(co, co + m, greater<pi>());

    rec(0, 0);
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define rp(i, c) rep(i, (c).size())
#define fr(i, c)                                                               \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = 1 << 28;
const double INF = 1e12, EPS = 1e-9;

int n;
char in[12][9];
int m, ans, id[256];
pi co[10];
bool use[10];

void rec(int c, int s) {
  if (c == m) {
    if (s == 0)
      ans++;
    return;
  }
  rep(i, 10) if (!use[i] && !(co[c].second && i == 0)) {
    use[i] = 1;
    rec(c + 1, s + co[c].first * i);
    use[i] = 0;
  }
}
int main() {
  while (scanf("%d", &n), n) {
    rep(i, n) scanf("%s", in[i]);
    rep(i, 256) id[i] = -1;
    rep(i, 10) co[i] = mp(0, 0), use[i] = 0;
    ans = m = 0;

    rep(i, n) {
      int t = i < n - 1 ? 1 : -1, len = strlen(in[i]);
      for (int j = len - 1; j >= 0; j--) {
        char c = in[i][j];
        if (id[c] < 0)
          id[c] = m++;
        co[id[c]].first += t;
        if (j == 0 && len > 1)
          co[id[c]].second = 1;
        t *= 10;
      }
    }
    sort(co, co + m, greater<pi>());
    rec(0, 0);
    printf("%d\\n", ans);
  }
  return 0;
}