alexjercan/bugnet huggingface dataset:
Problem p00741 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int H, W;
bool fld[50][50];
void del(int x, int y) {
  fld[x][y] = 0;
  for (int i = -1; i <= 1; i++) {
    for (int j = -1; j <= 1; j++) {
      int nx = x + i, ny = y + j;
      if (nx >= 0 && nx < W && ny >= 0 && ny < H && fld[nx][ny])
        del(nx, ny);
    }
  }
}
int main() {
  while (cin >> W >> H, W, H) {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> fld[j][i];
      }
    }
    int cnt = 0;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (fld[j][i]) {
          cnt++;
          fld[j][i] = 0;
          del(j, i);
        }
      }
    }
    cout << cnt << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int H, W;
bool fld[50][50];
int main() {
  queue<pair<int, int>> p;
  while (cin >> W >> H, W, H) {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> fld[j][i];
      }
    }
    int cnt = 0;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (fld[j][i]) {
          cnt++;
          fld[j][i] = 0;
          p.push(make_pair(j, i));
          while (!p.empty()) {
            int x = p.front().first, y = p.front().second;
            fld[x][y] = 0;
            p.pop();
            for (int dx = -1; dx <= 1; dx++)
              for (int dy = -1; dy <= 1; dy++) {
                int nx = x + dx, ny = y + dy;
                if (nx >= 0 && nx < W && ny >= 0 && ny < H && fld[nx][ny]) {
                  p.push(make_pair(nx, ny));
                }
              }
          }
        }
      }
    }
    cout << cnt << endl;
  }
}
Problem p00741 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int map[114][514];
void dfs(int i, int j) {
  map[i][j] = 0;
  for (int a = -1; a < 2; a++) {
    for (int b = -1; b < 2; b++) {
      if (map[i + a][b + j] == 1)
        dfs(i + a, b + j);
    }
  }
  return;
}
int main() {
  int h, w;
  while (cin >> h >> w, h, w) {
    for (int i = 1; i <= w; i++) {
      for (int j = 1; j <= h; j++) {
        cin >> map[i][j];
      }
    }
    int ans = 0;
    for (int i = 1; i <= w; i++) {
      for (int j = 1; j <= h; j++) {
        if (map[i][j]) {
          dfs(i, j);
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;
int map[114][514];
int h, w;
void dfs(int i, int j) {
  map[i][j] = 0;
  if (i < h) {
    if (j < w) {
      if (map[i - 1][j - 1] == 1)
        dfs(i - 1, j - 1);
    }
    if (map[i - 1][j] = 1)
      dfs(i - 1, j);
    if (j > 0) {
      if (map[i - 1][j + 1] == 1)
        dfs(i - 1, j + 1);
    }
  }
  if (j < w) {
    if (map[i][j - 1] == 1)
      dfs(i, j - 1);
  }
  if (j > 0) {
    if (map[i][j + 1] == 1)
      dfs(i, j + 1);
  }
  if (i > 0) {
    if (j < w) {
      if (map[i + 1][j - 1] == 1)
        dfs(i + 1, j - 1);
    }
    if (map[i + 1][j] = 1)
      dfs(i + 1, j);
    if (j > 0) {
      if (map[i + 1][j + 1] == 1)
        dfs(i + 1, j + 1);
    }
  }
  return;
}
int main() {
  while (cin >> h >> w) {
    if (!h && !w)
      return 0;
    int INF = 0;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        cin >> map[i][j];
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        if (map[i][j] == 1) {
          INF++;
          dfs(i, j);
        }
    cout << INF << endl;
  }
}
Problem p00742 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int n, m, a[10], f[10], u[10];

int rec(int k, int t) {
  if (k == m)
    return t == 0 ? 1 : 0;
  int mx = 0, mn = 0;
  for (int i = k; i < m; i++) {
    if (a[i] > 0)
      mx += a[i] * 9;
    else
      mn += a[i] * 9;
  }
  if (t + mx < 0 || t + mn > 0)
    return 0;
  int ans = 0;
  rep(i, 10) if (u[i] == 0) {
    if (i == 0 && f[k])
      continue;
    u[i] = 1;
    ans += rec(k + 1, t + a[k] * i);
    u[i] = 0;
  }
  return ans;
}

int main() {
  for (;;) {
    cin >> n;
    if (n == 0)
      return 0;
    memset(a, 0, sizeof(a));
    memset(f, 0, sizeof(f));
    map<char, int> of;
    m = 0;
    rep(k, n) {
      string s;
      cin >> s;
      int t = 1;
      for (int i = s.size() - 1; i >= 0; i--) {
        if (of.count(s[i]) == 0)
          of[s[i]] = m++;
        a[of[s[i]]] += k < n - 1 ? t : -t;
        if (s.size() > 1 && i == 0)
          f[of[s[i]]] = 1;
        t *= 10;
      }
    }
    printf("%d\\n", rec(0, 0));
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int n, m, a[10], f[10], u[10];

int rec(int k, int t) {
  if (k == m)
    return t == 0 ? 1 : 0;
  int ans = 0;
  rep(i, 10) if (u[i] == 0) {
    if (i == 0 && f[k])
      continue;
    u[i] = 1;
    ans += rec(k + 1, t + a[k] * i);
    u[i] = 0;
  }
  return ans;
}

int main() {
  for (;;) {
    cin >> n;
    if (n == 0)
      return 0;
    memset(a, 0, sizeof(a));
    memset(f, 0, sizeof(f));
    map<char, int> of;
    m = 0;
    rep(k, n) {
      string s;
      cin >> s;
      int t = 1;
      for (int i = s.size() - 1; i >= 0; i--) {
        if (of.count(s[i]) == 0)
          of[s[i]] = m++;
        a[of[s[i]]] += k < n - 1 ? t : -t;
        if (s.size() > 1 && i == 0)
          f[of[s[i]]] = 1;
        t *= 10;
      }
    }
    printf("%d\\n", rec(0, 0));
  }
}
Problem p00742 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#define REP(i, a, n) for (int i = (a); i < (n); i++)
using namespace std;

int main(void) {
  long fact[11];
  fact[0] = 1;
  REP(i, 1, 11) fact[i] = fact[i - 1] * i;

  for (int N; cin >> N, N > 0;) {
    string S[12];
    int a[256];

    REP(i, 0, 256) a[i] = 0;
    REP(i, 0, N) {
      cin >> S[i];
      REP(j, 0, S[i].length()) a[S[i][j]] = 1;
    }

    int p = 0;
    REP(i, 0, 256) {
      if (a[i] == 1)
        a[i] = p++;
      else
        a[i] = -1;
    }

    int v[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    long cnt = 0;
    int k, s, t, u;
    while (next_permutation(v, v + 10)) {
      k = 0;
      REP(i, 0, N - 1) k += v[a[S[i][S[i].length() - 1]]];
      k -= v[a[S[N - 1][S[N - 1].length() - 1]]];
      if (k % 10 != 0)
        goto CONTINUE;
      s = 0;
      REP(i, 0, N - 1) {
        if (S[i].length() > 1 && v[a[S[i][0]]] == 0)
          goto CONTINUE;
        t = 0;
        REP(j, 0, S[i].length()) t = t * 10 + v[a[S[i][j]]];
        s += t;
      }
      if (S[N - 1].length() > 1 && v[a[S[N - 1][0]]] == 0)
        goto CONTINUE;
      u = 0;
      REP(i, 0, S[N - 1].length()) u = u * 10 + v[a[S[N - 1][i]]];
      if (s == u)
        cnt++;
    CONTINUE:
      continue;
    }

    cout << (p <= 10 ? cnt / fact[10 - p] : cnt) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#define REP(i, a, n) for (int i = (a); i < (n); i++)
using namespace std;

int main(void) {
  int fact[11];
  fact[0] = 1;
  REP(i, 1, 11) fact[i] = fact[i - 1] * i;

  for (int N; cin >> N, N > 0;) {
    string S[12];
    int a[256];

    REP(i, 0, N) {
      cin >> S[i];
      REP(j, 0, S[i].length()) a[S[i][j]] = 1;
    }

    int p = 0;
    REP(i, 0, 256) {
      if (a[i] == 1)
        a[i] = p++;
      else
        a[i] = -1;
    }

    int v[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    long cnt = 0;
    int s, t, u;
    while (next_permutation(v, v + 10)) {
      s = 0;
      REP(i, 0, N - 1) {
        if (S[i].length() > 1 && v[a[S[i][0]]] == 0)
          goto CONTINUE;
        t = 0;
        REP(j, 0, S[i].length()) t = t * 10 + v[a[S[i][j]]];
        s += t;
      }
      if (S[N - 1].length() > 1 && v[a[S[N - 1][0]]] == 0)
        goto CONTINUE;
      u = 0;
      REP(i, 0, S[N - 1].length()) u = u * 10 + v[a[S[N - 1][i]]];
      if (s == u)
        cnt++;
    CONTINUE:
      continue;
    }

    cout << (cnt / fact[10 - p]) << endl;
  }
}
Problem p00742 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>

using namespace std;

int main() {
  const int d[9] = {1,      10,      100,      1000,     10000,
                    100000, 1000000, 10000000, 100000000};

  for (int N; cin >> N, N;) {
    string n[12];
    for (int i = 0; i < N; i++)
      cin >> n[i];

    map<char, int> m;
    for (int i = 0; i < N; i++)
      for (int j = 0; j < n[i].size(); j++)
        if (m.find(n[i][j]) == m.end())
          m.insert(make_pair(n[i][j], m.size()));
    int M = m.size();

    int c[10] = {}, z[10] = {};
    for (int i = 0; i < N; i++) {
      if (n[i].size() > 1)
        z[m[n[i][0]]] = 1;
      reverse(n[i].begin(), n[i].end());
      for (int j = 0; j < n[i].size(); j++)
        c[m[n[i][j]]] += i == N - 1 ? -d[j] : d[j];
    }

    int p[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    int res = 0;
    do {
      int x = 0;
      for (int i = 0; i < M; i++) {
        if (p[i] == 0 && z[i] == 1) {
          x = -1;
          break;
        }
        x += c[i] * p[i];
      }
      if (x == 0)
        res++;
      reverse(p + M, p + 10);
    } while (next_permutation(p, p + 10));

    cout << res << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <numeric>
#include <set>

using namespace std;

int main() {
  int d[9] = {1};
  for (int i = 1; i < 9; i++)
    d[i] = d[i - 1] * 10;

  for (int N; cin >> N, N;) {
    int c[128] = {};
    string n[12];
    for (int i = 0; i < N; i++)
      cin >> n[i];

    int z[128] = {}; // 0ツづーツ環づィツ督鳴づづァツづェツづ按つ「ツづつォ1
    for (int i = 0; i < N; i++) {
      if (n[i].size() > 1)
        z[n[i][0]] = 1;
      for (int j = 0; j < n[i].size(); j++) {
        int t = d[n[i].size() - 1 - j];
        c[n[i][j]] += (i == N - 1 ? -t : t);
      }
    }

    string s = accumulate(n, n + N, string());
    sort(s.begin(), s.end());
    s.erase(unique(s.begin(), s.end()), s.end());

    int p[10];
    for (int i = 0; i < 10; i++)
      p[i] = i;

    int res = 0;
    do {
      int x = 0;
      for (int i = 0; i < s.size(); i++) {
        if (p[i] == 0 && z[s[i]] == 1) {
          x = -1;
          break;
        }
        x += c[s[i]] * p[i];
      }
      if (x == 0)
        res++;
      reverse(p + s.size(), p + 10);
    } while (next_permutation(p, p + 10));

    cout << res << endl;
  }

  return 0;
}
Problem p00742 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int n, cnt, a[13][9], l[13], p[11], used[11], zero[11];
char c[11];
int solve(int depth) {
  if (depth == cnt) {
    int ret = 0;
    for (int i = 0; i < n; i++) {
      int res = 0;
      for (int j = l[i] - 1; j >= 0; j--)
        res = res * 10 + p[a[i][j]];
      if (i + 1 == n)
        ret -= res;
      else
        ret += res;
    }
    return ret ? 0 : 1;
  }
  int ret = 0;
  for (int i = 0; i < 10; i++) {
    if (!used[i]) {
      used[i] = 1, p[depth] = i;
      if (i || zero[depth])
        ret += solve(depth + 1);
      used[i] = 0;
    }
  }
  return ret;
}
int main() {
  string s;
  while (cin >> n, n) {
    cnt = 0;
    memset(c, 0, 11);
    memset(zero, -1, 44);
    for (int i = 0; i < n; i++) {
      cin >> s;
      l[i] = s.size();
      reverse(s.begin(), s.end());
      for (int j = 0; j < s.size(); j++) {
        int flag = 0;
        for (int k = 0; k < cnt; k++) {
          if (s[j] == c[k]) {
            a[i][j] = k, flag = 1;
            break;
          }
        }
        if (!flag)
          a[i][j] = cnt, c[cnt++] = s[j];
      }
      if (s.size() > 1)
        zero[a[i][s.size() - 1]] = 0;
    }
    cout << solve(0) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int n, cnt, a[13][9], l[13], p[11], used[11];
char c[11];
int solve(int depth) {
  if (depth == cnt) {
    int b[13][9], ret = 0;
    memset(b, 0, 468);
    for (int i = 0; i < n; i++) {
      int res = 0;
      for (int j = l[i] - 1; j >= 0; j--)
        res = res * 10 + p[a[i][j]];
      if (i + 1 == n)
        ret -= res;
      else
        ret += res;
    }
    return ret ? 0 : 1;
  }
  int ret = 0;
  for (int i = 0; i < 10; i++) {
    if (!used[i]) {
      used[i] = 1, p[depth] = i;
      ret += solve(depth + 1);
      used[i] = 0;
    }
  }
  return ret;
}
int main() {
  string s;
  while (cin >> n, n) {
    cnt = 0;
    memset(c, 0, 11);
    for (int i = 0; i < n; i++) {
      cin >> s;
      l[i] = s.size();
      reverse(s.begin(), s.end());
      for (int j = 0; j < s.size(); j++) {
        int flag = 0;
        for (int k = 0; k < cnt; k++) {
          if (s[j] == c[k]) {
            a[i][j] = k, flag = 1;
            break;
          }
        }
        if (!flag)
          a[i][j] = cnt, c[cnt++] = s[j];
      }
    }
    cout << solve(0) << endl;
  }
}
Problem p00742 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

int N;
string S[20];
int mem[256];
bool used[10];

int solve(int x, int y, int t) {
  if (x == 8) {
    return t == 0;
  }

  if (y == N) {
    if (t < 0 || t % 10)
      return false;
    return solve(x + 1, 0, t / 10);
  }

  if (S[y].size() <= x)
    return solve(x, y + 1, t);

  int ret = 0;
  for (int i = 0; i < 10; i++) {
    if (S[y].size() > 1 && x + 1 == S[y].size() && i == 0)
      continue;
    if (mem[S[y][x]] != -1) {
      if (mem[S[y][x]] != i)
        continue;
      ret += solve(x, y + 1, t + i * (y == N - 1 ? -1 : 1));
    } else if (!used[i]) {
      mem[S[y][x]] = i;
      used[i] = 1;
      ret += solve(x, y + 1, t + i * (y == N - 1 ? -1 : 1));
      mem[S[y][x]] = -1;
      used[i] = 0;
    }
  }
  return ret;
}

signed main() {
  memset(mem, -1, sizeof(mem));
  while (cin >> N, N) {
    rep(i, N) cin >> S[i];
    rep(i, N) reverse(all(S[i]));
    cout << solve(0, 0, 0) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

int N;
string S[20];
int mem[256];
bool used[10];
int solve(int y, int x, int t, int l) {
  if (y == N) {
    return t == 0;
  }

  if (x == S[y].size())
    return solve(y + 1, 0, t + l * ((y == N - 1) ? -1 : 1), 0);

  int ret = 0;
  for (int i = 0; i < 10; i++) {
    if (S[y].size() > 1 && x == 0 && i == 0)
      continue;
    if (mem[S[y][x]] != -1) {
      if (mem[S[y][x]] != i)
        continue;
      ret += solve(y, x + 1, t, l * 10 + i);
    } else if (!used[i]) {
      mem[S[y][x]] = i;
      used[i] = 1;
      ret += solve(y, x + 1, t, l * 10 + i);
      mem[S[y][x]] = -1;
      used[i] = 0;
    }
  }

  return ret;
}

signed main() {
  memset(mem, -1, sizeof(mem));
  while (cin >> N, N) {
    rep(i, N) cin >> S[i];
    cout << solve(0, 0, 0, 0) << endl;
  }
  return 0;
}
Problem p00742 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

map<char, int> m;
vector<int> v = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
set<char> Top;
string S;

int dfs(int n, int sum) {
  if (n == m.size())
    return sum == 0;

  int res = 0;
  for (int i = 0; i < v.size(); i++) {
    if (v[i] == 0 && Top.find(S[n]) != Top.end())
      continue;
    int tmp = v[i];
    v.erase(v.begin() + i);
    res += dfs(n + 1, sum + (m[S[n]] * tmp));
    v.insert(v.begin() + i, tmp);
  }

  return res;
}

int main() {
  int n;
  while (cin >> n, n) {
    set<char> s;
    m.clear();
    Top.clear();
    for (int i = 0; i < n; i++) {
      string str;
      cin >> str;
      int k = 1;
      if (str.size() > 1)
        Top.insert(str[0]);
      for (int j = str.size() - 1, k = 1; j >= 0; j--, k *= 10) {
        s.insert(str[j]);
        if (m.find(str[j]) == m.end()) {
          if (i == n - 1)
            m[str[j]] = -k;
          else
            m[str[j]] = k;
        } else {
          if (i == n - 1)
            m[str[j]] -= k;
          else
            m[str[j]] += k;
        }
      }
    }

    S = "";
    for (auto i : s) {
      S += i;
    }

    cout << dfs(0, 0) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

vector<string> s;
string S;
int ss;
int ssm;
vector<int> strsize;
map<char, int> m;
vector<int> v = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int res;

void dfs(int n) {

  if (n == m.size()) {
    if (strsize[ssm] > 1 && m[s[ssm][0]] == 0)
      return;
    int sum = 0, k;
    for (int i = 0; i < ssm; i++) {
      k = 1;
      if (strsize[i] > 1 && m[s[i][0]] == 0)
        return;
      for (int j = strsize[i] - 1; j >= 0; j--) {
        sum += m[s[i][j]] * k;
        k *= 10;
      }
    }

    k = 1;
    for (int i = strsize[ssm] - 1; i >= 0; i--) {
      sum -= (m[s[ssm][i]] * k);
      k *= 10;
    }

    if (!sum)
      res++;
  } else {
    for (int ii = 0; ii < v.size(); ii++) {
      int tmp = m[S[n]] = v[ii];
      v.erase(v.begin() + ii);
      dfs(n + 1);
      v.insert(v.begin() + ii, tmp);
    }
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    s.resize(n);
    strsize.resize(n);
    set<char> sc;
    for (int i = 0; i < n; i++) {
      cin >> s[i];
      strsize[i] = s[i].size();
      for (int j = 0; j < s[i].size(); j++) {
        sc.insert(s[i][j]);
      }
    }

    S = "";
    m.clear();
    for (auto i : sc) {
      S += i;
      m[i] = -1;
    }

    ss = s.size();
    ssm = ss - 1;
    res = 0;
    dfs(0);
    cout << res << endl;
  }

  return 0;
}
Problem p00742 (C++) — buggy submission, verdict: Time Limit Exceeded

// #include<bits/stdc++.h>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define rep(i, j) for (int i = 0; i < (int)j; i++)
#define repeat(i, j, k) for (int i = (j); i < (k); i++)
#define all(v) begin(v), end(v)

const int INF = 1 << 30; // 10E10

//????????????
int rec(map<char, int>::iterator itr, map<char, int>::iterator end,
        const set<char> &tops, vector<bool> &used, int sum) {
  if (itr == end)
    return sum == 0;
  char c = itr->first;
  int weight = itr->second;
  itr++;
  int ret = 0;
  rep(i, 10) {
    if (used[i])
      continue;
    if (i == 0 and tops.count(c))
      continue;
    used[i] = true;
    ret += rec(itr, end, tops, used, sum + weight * i);
    used[i] = false;
  }
  return ret;
}

int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;

    map<char, int> coefficient;
    set<char> tops;

    rep(i, n) {
      string s;
      cin >> s;
      if (s.size() > 1)
        tops.insert(s.front());

      for (int j = s.size() - 1, k = 1; j >= 0; j--, k *= 10) {
        coefficient[s[j]] += k * (i != n - 1 ? 1 : -1);
      }
    }
    vector<bool> used(10);
    int ans = rec(coefficient.begin(), coefficient.end(), tops, used, 0);

    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

// #include<bits/stdc++.h>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef int64_t i64;
typedef int32_t i32;
typedef i64 int__;
#define rep(i, j) for (int__ i = 0; i < j; i++)
#define repeat(i, j, k) for (int__ i = (j); i < (k); i++)
#define all(v) begin(v), end(v)

const i32 INF = 1 << 30; // 10E10

int coefficients[26] = {};
bool is_top[26] = {}, is_used[26] = {};

//????????????
int rec(int n, vector<bool> used, int sum) {
  if (n == 26)
    return sum == 0 ? 1 : 0;
  int ans = 0;
  if (!is_used[n])
    return rec(n + 1, used, sum);
  rep(i, 10) {
    if (used[i])
      continue;
    if (is_top[n] and i == 0)
      continue;
    used[i] = true;
    ans += rec(n + 1, used, sum + i * coefficients[n]);
    used[i] = false;
  }
  return ans;
}

int main() {

  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    rep(i, 26) {
      coefficients[i] = 0;
      is_top[i] = 0;
      is_used[i] = 0;
    }

    rep(i, n) {
      string s;
      cin >> s;
      rep(j, s.size()) {
        if (j == 0 and s.size() > 1)
          is_top[s[j] - 'A'] = true;
        coefficients[s[j] - 'A'] +=
            pow(10, s.size() - j - 1) * (i == n - 1 ? -1 : 1);
        is_used[s[j] - 'A'] = true;
      }
    }

    int ans = rec(0, vector<bool>(10, false), 0);

    cout << ans << endl;
  }
}