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

#include <stdio.h>
#include <string.h>

int n, f[10], num[10], len[12], s[12][8], w[10], allen, c0;
char al[11];

int dfs(int c) {
  int i, j, k, ret = 0, t;
  if (c == c0 + 1) {
    k = 1;
    for (j = 1; j < len[n - 1]; j++)
      k *= 10;
    t = k * (num[s[n - 1][0]] + 1);
    for (i = 0; i < n - 1; i++) {
      k = 1;
      for (j = 1; j < len[i]; j++)
        k *= 10;
      t -= k * num[s[i][0]];
      if (t < 0)
        return 0;
    }
  }
  if (c == allen) {
    int r;
    t = 0;
    for (k = 0; k < len[n - 1]; k++)
      t = t * 10 + num[s[n - 1][k]];
    r = t;
    for (j = 0; j < n - 1; j++) {
      t = 0;
      for (k = 0; k < len[j]; k++)
        t = t * 10 + num[s[j][k]];
      r -= t;
      if (r < 0)
        break;
    }
    return r == 0 ? 1 : 0;
  }
  for (i = 0; i < 10; i++) {
    if (!(w[c] && i == 0) && !f[i]) {
      f[i] = 1;
      num[c] = i;
      ret += dfs(c + 1);
      f[i] = 0;
    }
  }
  return ret;
}

int main(void) {
  int i, j, k, cnt, m;
  char c, str[12][9];
  while (1) {
    scanf("%d%*c", &n);
    if (!n)
      break;
    for (i = 0; i < n; i++) {
      j = 0;
      while ((c = getchar()) != '\\n')
        str[i][j++] = c;
      str[i][j] = '\\0';
    }
    for (i = 0; i < n; i++)
      len[i] = strlen(str[i]);
    allen = 0;
    m = 0;
    do {
      for (j = 0; j < n; j++) {
        if (len[j] > m) {
          for (k = 0; k < allen; k++)
            if (str[j][m] == al[k])
              break;
          if (k == allen) {
            al[k] = str[j][m], al[k + 1] = '\\0';
            allen++;
          }
          s[j][m] = k;
        }
      }
      cnt = 0;
      for (i = 0; i < n; i++)
        if (len[i] > m)
          cnt++;
      m++;
    } while (cnt);
    memset(w, 0, sizeof(w));
    for (i = 0; i < n; i++)
      if (len[i] > 1)
        w[s[i][0]] = 1;
    c0 = 0;
    for (i = 0; i < n; i++)
      if (s[i][0] > c0)
        c0 = s[i][0];
    memset(f, 0, sizeof(f));
    printf("%d\\n", dfs(0));
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#include <string.h>

int n, f[10], num[10], len[12], s[12][8], w[10], allen;
char al[11];

int dfs(int c) {
  int i, ret = 0;
  if (c == allen) {
    int j, k, r, t;
    t = 0;
    for (k = 0; k < len[n - 1]; k++)
      t = t * 10 + num[s[n - 1][k]];
    r = t;
    for (j = 0; j < n - 1; j++) {
      t = 0;
      for (k = 0; k < len[j]; k++)
        t = t * 10 + num[s[j][k]];
      r -= t;
      if (r < 0)
        break;
    }
    return r == 0 ? 1 : 0;
  }
  for (i = 0; i < 10; i++) {
    if (!(w[c] && i == 0) && !f[i]) {
      f[i] = 1;
      num[c] = i;
      ret += dfs(c + 1);
      f[i] = 0;
    }
  }
  return ret;
}

int main(void) {
  int i, j, k;
  char c;
  while (1) {
    scanf("%d%*c", &n);
    if (!n)
      break;
    al[0] = '\\0';
    allen = 0;
    for (i = 0; i < n; i++) {
      for (j = 0; ((c = getchar()) != '\\n'); j++) {
        for (k = 0; k < allen; k++)
          if (c == al[k])
            break;
        if (k == allen) {
          al[k] = c, al[k + 1] = '\\0';
          allen++;
        }
        s[i][j] = k;
      }
      len[i] = j;
    }
    memset(w, 0, sizeof(w));
    for (i = 0; i < n; i++)
      if (len[i] > 1)
        w[s[i][0]] = 1;
    memset(f, 0, sizeof(f));
    printf("%d\\n", dfs(0));
  }
  return 0;
}