Problem p00742 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <iostream>
#include <vector>
using namespace std;

int t[3628800];

int fact(int n) {
  if (n <= 1)
    return 1;
  return n * fact(n - 1);
}

void make_T(int n) {
  // T1 [initialize]
  int N = fact(n), d = N / 2, m = 2, k, j;
  t[d] = 1;

  while (m != n) {
    // T2 [Loop on m]
    m++;
    d /= m;
    k = 0;

    // T3 [Hunt down]
  T3:
    k += d;
    j = m - 1;
    while (j > 0) {
      t[k] = j;
      k += d;
      j--;
    }

    // T4 [Offset]
    t[k]++;

    // T5 [Hunt up]
    k += d;
    j = 1;
    while (j < m) {
      t[k] = j;
      k += d;
      j++;
    }

    if (k < N)
      goto T3;
  }
}

int alphametrics(vector<int> s, vector<int> F) {
  // A1 [initialize]
  vector<int> a(10);
  for (int i = 0; i < 10; i++)
    a[i] = i;

  int v = 0;
  for (int i = 0; i < 10; i++)
    v += i * s[i];

  int k = 1;

  vector<int> delta(9);
  for (int i = 0; i < 9; i++)
    delta[i] = s[i + 1] - s[i];

  // A2 [Test]
  int res = 0;
  while (true) {
    bool flag = true;
    for (int i = 0; i < F.size(); i++) {
      if (a[F[i]] == 0) {
        flag = false;
        break;
      }
    }

    if (v == 0 && flag)
      res++;

    // A3 [Swap]
    if (k == 3628800)
      return res;
    int j = t[k] - 1;
    v -= (a[j + 1] - a[j]) * delta[j];
    swap(a[j + 1], a[j]);
    k++;
  }
}

int main() {
  make_T(10);

  int n;
  string str[20];
  while (cin >> n, n) {
    vector<int> s(10);
    for (int i = 0; i < 10; i++)
      s[i] = 0;

    vector<int> F;
    vector<char> alpha;

    for (int id = 0; id < n; id++) {
      cin >> str[id];
      for (int i = 0; i < str[id].size(); i++) {
        bool flag = true;
        for (int j = 0; j < alpha.size(); j++) {
          if (str[id][i] == alpha[j]) {
            flag = false;
            break;
          }
        }
        if (flag)
          alpha.push_back(str[id][i]);
      }
    }

    for (int id = 0; id < n; id++) {
      int tmp = 1;
      if (id == n - 1)
        tmp *= -1;

      for (int i = str[id].size() - 1; i >= 0; i--) {
        for (int j = 0; j < alpha.size(); j++) {
          if (str[id][i] == alpha[j]) {
            s[j] += tmp;
            break;
          }
        }
        tmp *= 10;

        if (str[id].size() > 1) {
          for (int i = 0; i < alpha.size(); i++) {
            if (str[id][0] == alpha[i]) {
              F.push_back(i);
              break;
            }
          }
        }
      }
    }

    sort(F.begin(), F.end());
    F.erase(unique(F.begin(), F.end()), F.end());

    cout << alphametrics(s, F) / fact(10 - alpha.size()) << endl;
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cctype>
#include <iostream>
#include <vector>
using namespace std;

int t[3628800];

int fact(int n) {
  if (n <= 1)
    return 1;
  return n * fact(n - 1);
}

void make_T(int n) {
  // T1 [initialize]
  int N = fact(n), d = N / 2, m = 2, k, j;
  t[d] = 1;

  while (m != n) {
    // T2 [Loop on m]
    m++;
    d /= m;
    k = 0;

    // T3 [Hunt down]
  T3:
    k += d;
    j = m - 1;
    while (j > 0) {
      t[k] = j;
      k += d;
      j--;
    }

    // T4 [Offset]
    t[k]++;

    // T5 [Hunt up]
    k += d;
    j = 1;
    while (j < m) {
      t[k] = j;
      k += d;
      j++;
    }

    if (k < N)
      goto T3;
  }
}

int alphametrics(vector<int> s, vector<int> F) {
  // A1 [initialize]
  vector<int> a(10);
  for (int i = 0; i < 10; i++)
    a[i] = i;

  int v = 0;
  for (int i = 0; i < 10; i++)
    v += i * s[i];

  int k = 1;

  vector<int> delta(9);
  for (int i = 0; i < 9; i++)
    delta[i] = s[i + 1] - s[i];

  // A2 [Test]
  int res = 0;
  while (true) {
    bool flag = true;
    for (int i = 0; i < F.size(); i++) {
      if (a[F[i]] == 0) {
        flag = false;
        break;
      }
    }

    if (v == 0 && flag)
      res++;

    // A3 [Swap]
    if (k == 3628800)
      return res;
    int j = t[k] - 1;
    v -= (a[j + 1] - a[j]) * delta[j];
    swap(a[j + 1], a[j]);
    k++;
  }
}

int main() {
  make_T(10);

  int n;
  string str[10];
  while (cin >> n, n) {
    /*
    vector<int> s(10);
    for(int i=0;i<10;i++)s[i] = 0;

    vector<int> F;
    vector<char> alpha;
    */
    for (int id = 0; id < n; id++) {
      cin >> str[id];
      /*
      for(int i=0;i<str[id].size();i++){
        bool flag = true;
        for(int j=0;j<alpha.size();j++){
          if(str[id][i] == alpha[j]){
            flag = false;
            break;
          }
        }
        if(flag)alpha.push_back(str[id][i]);
      }
      */
    }
    /*
    for(int id=0;id<n;id++){
      int tmp = 1;
      if(id==n-1)tmp *= -1;

      for(int i=str[id].size()-1;i>=0;i--){
        for(int j=0;j<alpha.size();j++){
          if(str[id][i] == alpha[j]){
            s[j] += tmp;
            break;
          }
        }
        tmp *= 10;

        if(str[id].size()>1){
          for(int i=0;i<alpha.size();i++){
            if(str[id][0] == alpha[i]){
              F.push_back(i);
              break;
            }
          }
        }
      }
    }

    sort(F.begin(),F.end());
    F.erase(unique(F.begin(),F.end()),F.end());

    cout << alphametrics(s,F)/fact(10-alpha.size()) << endl;
    */
  }
}