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

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt"

const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
map<pair<int, int>, pair<int, int>> mp;
struct aa {
  int x;
  int y;
  int way;
  int foot;
};
int main() {
  while (1) {
    mp.clear();
    int N;
    cin >> N;
    if (!N)
      break;
    for (int i = 0; i < N; ++i) {
      int x, y;
      char c;
      cin >> x >> y >> c;
      if (c == 'x') {
        mp[make_pair(x, y)] = make_pair(2, -1);
        mp[make_pair(x + 1, y)] = make_pair(0, -1);
      } else {
        mp[make_pair(x, y)] = make_pair(1, -1);
        mp[make_pair(x, y + 1)] = make_pair(3, -1);
      }
    }
    bool ok = true;
    for (auto &m : mp) {
      if (m.second.second == -1) {
        m.second.second = 0;
        queue<aa> que;
        que.push(aa{m.first.first, m.first.second, m.second.first, 0});

        while (!que.empty()) {
          aa atop(que.front());
          que.pop();
          const int nowx = atop.x;
          const int nowy = atop.y;
          const int nowf = atop.foot;
          for (int way = 0; way < 4; ++way) {
            const int newx = nowx + dx[way];
            const int newy = nowy + dy[way];
            const int newf = way == atop.way ? !nowf : nowf;
            if (mp.find(make_pair(newx, newy)) != mp.end()) {
              if (mp[make_pair(newx, newy)].second == -1) {
                mp[make_pair(newx, newy)].second = newf;
                que.push(aa{newx, newy, mp[make_pair(newx, newy)].first, newf});
              } else {
                if (mp[make_pair(newx, newy)].second != newf)
                  ok = false;
              }
            }
          }
        }
      }
    }
    if (ok)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }

  return 0;
}

Error: -6
b8233cca-5506-4d1d-8e58-4a9e92503fc0.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p01296/C++/s818647483.cpp:389: int main(): Assertion `k == alphanum' failed.


Fixed submission:

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

const int mod = 1000000007;

struct Mod {
  int num;
  Mod() : num(0) { ; }
  Mod(long long int n) : num((n % mod + mod) % mod) { ; }
  Mod(int n) : num((n % mod + mod) % mod) { ; }
  operator int() { return num; }
};

Mod operator+(Mod a, Mod b) { return Mod((a.num + b.num) % mod); }
Mod operator+(long long int a, Mod b) { return Mod(a) + b; }
Mod operator-(Mod a, Mod b) { return Mod((mod + a.num - b.num) % mod); }
Mod operator-(long long int a, Mod b) { return Mod(a) - b; }
Mod operator*(Mod a, Mod b) { return Mod(((long long)a.num * b.num) % mod); }
Mod operator*(long long int a, Mod b) { return Mod(a) * b; }
Mod operator+=(Mod &a, Mod b) { return a = a + b; }
Mod operator+=(long long int &a, Mod b) { return a = a + b; }
Mod operator-=(Mod &a, Mod b) { return a = a - b; }
Mod operator-=(long long int &a, Mod b) { return a = a - b; }
Mod operator*=(Mod &a, Mod b) { return a = a * b; }
Mod operator*=(long long int &a, Mod b) { return a = a * b; }
Mod operator^(Mod a, int n) {
  if (n == 0)
    return Mod(1);
  Mod res = (a * a) ^ (n / 2);
  if (n % 2)
    res = res * a;
  return res;
}
Mod inv(Mod a) { return a ^ (mod - 2); }
Mod operator/(Mod a, Mod b) {
  assert(b != 0);
  return a * inv(b);
}
Mod operator/(long long int a, Mod b) {
  assert(b != 0);
  return Mod(a) * inv(b);
}

#define MAX_N 1024000

Mod fact[MAX_N], factinv[MAX_N];
void init() {
  fact[0] = Mod(1);
  factinv[0] = 1;
  for (int i = 0; i < MAX_N - 1; ++i) {
    fact[i + 1] = fact[i] * Mod(i + 1);
    factinv[i + 1] = factinv[i] / Mod(i + 1);
  }
}
Mod comb(int a, int b) { return fact[a] * factinv[b] * factinv[a - b]; }

vector<map<vector<int>, Mod>> dp;

vector<vector<Mod>> keisann(const vector<vector<Mod>> l,
                            const vector<vector<Mod>> r) {
  vector<vector<Mod>> ans(l.size(), vector<Mod>(r[0].size()));
  assert(l[0].size() == r.size());
  for (int h = 0; h < l.size(); ++h) {
    for (int i = 0; i < r.size(); ++i) {
      for (int w = 0; w < r[0].size(); ++w) {

        ans[h][w] += l[h][i] * r[i][w];
      }
    }
  }
  return ans;
}

vector<vector<Mod>> powgyou(vector<vector<Mod>> a, const long long int n) {
  if (n == 1)
    return a;
  else {
    vector<vector<Mod>> ans(a.size(), vector<Mod>(a[0].size(), 0));
    ans = powgyou(a, n / 2);
    ans = keisann(ans, ans);
    if (n % 2) {
      ans = keisann(ans, a);
    }
    return ans;
  }
}

/*int N; cin >> N;
vector<pair<int, int>>cs;
for (int i = 0; i < N; ++i) {
        int x, r; cin >> x >> r;
        int l = x - r; int r = x + r;
        cs.emplace_back({ l,r });
}
sort(cs.begin(), cs.end(), [](const pair<int, int>l, const int pair<int, int>r)
{ return l.first == r.first ? l.second < r.second : l.first < r.first; }));
return 0;*/

/*int H, W; cin >> W >> H;
dp.resize(H+1);
vector<int>s(W,0);
s[0] = Mod(1);
dp[0][s] = Mod(1);
for (int i = 1; i <= H; ++i) {
        for (auto it = dp[i - 1].begin(); it != dp[i - 1].end(); ++it) {
                vector<int>pre((*it).first);
                for (int j = 0; j < (1 <<( W)); ++j) {
                        bitset<6>bs(j);
                        vector<int>now(W);
                        int num = 2;
                        for (int k = 0; k < W; ++k) {
                                if (bs[k]) {
                                        now[k] = num;
                                        num++;
                                }
                        }
                        for (int k = 0; k < W; ++k) {
                                if (now[k] && pre[k]) {
                                        now[k] = pre[k];
                                }
                        }
                        for (int k = 0; k < W - 1; ++k) {
                                if (now[k] && now[k + 1]) {
                                        int amin = min(now[k], now[k + 1]);
                                        now[k] = amin;
                                        now[k + 1] = amin;
                                }
                        }
                        for (int k = W - 1; k > 0; --k) {
                                if (now[k] && now[k - 1]) {
                                        int amin = min(now[k], now[k - 1]);
                                        now[k] = amin;
                                        now[k - 1] = amin;
                                }
                        }
                        int nnum = 2;
                        for (int k = 0; k < W; ++k) {
                                if (now[k]>=2) {
                                        int aa = now[k];
                                        for (int l = 0; l < W; ++l) {
                                                if (now[l] == aa)now[l] = nnum;
                                        }
                                        nnum++;
                                }
                        }
                        bool isok = false;
                        for (int k = 0; k < W; ++k) {
                                if (now[k] == 1) {
                                        isok = true;
                                }
                        }
                        if (isok) {

                                dp[i][now] += (*it).second;
                        }

                }
        }
}
Mod ans(0);
for (auto it = dp[H].begin(); it != dp[H].end(); ++it) {
        if ((*it).first[W - 1] == 1) {
                ans += (*it).second;
        }
}
cout << ans << endl;
return 0;*/

int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};

int dx8[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dy8[8] = {-1, 0, 1, 1, 1, 0, -1, -1};

template <class BidirectionalIterator>
bool next_combination(BidirectionalIterator first1, BidirectionalIterator last1,
                      BidirectionalIterator first2,
                      BidirectionalIterator last2) {
  if ((first1 == last1) || (first2 == last2)) {
    return false;
  }
  BidirectionalIterator m1 = last1;
  BidirectionalIterator m2 = last2;
  --m2;
  while (--m1 != first1 && !(*m1 < *m2)) {
  }
  bool result = (m1 == first1) && !(*first1 < *m2);
  if (!result) {
    // ?
    while (first2 != m2 && !(*m1 < *first2)) {
      ++first2;
    }
    first1 = m1;
    std::iter_swap(first1, first2);
    ++first1;
    ++first2;
  }
  if ((first1 != last1) && (first2 != last2)) {
    // ?
    m1 = last1;
    m2 = first2;
    while ((m1 != first1) && (m2 != last2)) {
      std::iter_swap(--m1, m2);
      ++m2;
    }
    // ?
    std::reverse(first1, m1);
    std::reverse(first1, last1);
    std::reverse(m2, last2);
    std::reverse(first2, last2);
  }
  return !result;
}

template <class BidirectionalIterator>
bool next_combination(BidirectionalIterator first, BidirectionalIterator middle,
                      BidirectionalIterator last) {
  return next_combination(first, middle, middle, last);
}

int main(void) {
  /*int N; cin >> N;
  vector<pair<int, int>>cs;
  for (int i = 0; i < N; ++i) {
          int x, r; cin >> x >> r;
          int l = x - r; int r = x + r;
          cs.emplace_back({ l,r });
  }
  sort(cs.begin(), cs.end(), [](const pair<int, int>l, const int pair<int,
  int>r) { return l.first == r.first ? l.second < r.second : l.first < r.first;
  })); return 0;*/

  /*int H, W; cin >> W >> H;
  dp.resize(H+1);
  vector<int>s(W,0);
  s[0] = Mod(1);
  dp[0][s] = Mod(1);
  for (int i = 1; i <= H; ++i) {
          for (auto it = dp[i - 1].begin(); it != dp[i - 1].end(); ++it) {
                  vector<int>pre((*it).first);
                  for (int j = 0; j < (1 <<( W)); ++j) {
                          bitset<6>bs(j);
                          vector<int>now(W);
                          int num = 2;
                          for (int k = 0; k < W; ++k) {
                                  if (bs[k]) {
                                          now[k] = num;
                                          num++;
                                  }
                          }
                          for (int k = 0; k < W; ++k) {
                                  if (now[k] && pre[k]) {
                                          now[k] = pre[k];
                                  }
                          }
                          for (int k = 0; k < W - 1; ++k) {
                                  if (now[k] && now[k + 1]) {
                                          int amin = min(now[k], now[k + 1]);
                                          now[k] = amin;
                                          now[k + 1] = amin;
                                  }
                          }
                          for (int k = W - 1; k > 0; --k) {
                                  if (now[k] && now[k - 1]) {
                                          int amin = min(now[k], now[k - 1]);
                                          now[k] = amin;
                                          now[k - 1] = amin;
                                  }
                          }
                          int nnum = 2;
                          for (int k = 0; k < W; ++k) {
                                  if (now[k]>=2) {
                                          int aa = now[k];
                                          for (int l = 0; l < W; ++l) {
                                                  if (now[l] == aa)now[l] =
  nnum;
                                          }
                                          nnum++;
                                  }
                          }
                          bool isok = false;
                          for (int k = 0; k < W; ++k) {
                                  if (now[k] == 1) {
                                          isok = true;
                                  }
                          }
                          if (isok) {

                                  dp[i][now] += (*it).second;
                          }

                  }
          }
  }
  Mod ans(0);
  for (auto it = dp[H].begin(); it != dp[H].end(); ++it) {
          if ((*it).first[W - 1] == 1) {
                  ans += (*it).second;
          }
  }
  cout << ans << endl;
  return 0;*/

  /*int k = 0;
  while (1) {

          k++;
          int a; cin >> a;
          if (a) {
                  cout << "Case " << k << ": " << a << endl;
          }
          else {
                  return 0;
          }
  }
  return 0;*/

  while (1) {
    int N;
    cin >> N;
    if (!N)
      break;
    vector<string> sts;
    for (int i = 0; i < N; ++i) {
      string st;
      cin >> st;
      sts.emplace_back(st);
    }
    int alphanum = 0;
    vector<bool> ex(26, false);
    vector<bool> start(26, false);
    bool ok = true;
    for (int i = 0; i < N; ++i) {
      if (sts[i].size() != 1)
        start[sts[i][0] - 'A'] = true;
      for (int j = 0; j < sts[i].size(); ++j) {
        if (ex[sts[i][j] - 'A']) {

        } else {
          alphanum++;
          if (alphanum > 10) {
            ok = false;
          }
          ex[sts[i][j] - 'A'] = true;
        }
      }
    }
    vector<int> perm(10);
    for (int i = 0; i < 10; ++i) {
      perm[i] = i;
    }

    int ansnum = 0;
    do {
      map<char, int> mp;
      int k = 0;
      bool isok = true;
      vector<int> to(26);
      for (int i = 0; i < 26; ++i) {
        if (ex[i]) {
          if (perm[k] == 0) {
            if (start[i])
              isok = false;
          }
          to[i] = perm[k];
          k++;
        }
      }
      assert(k == alphanum);
      if (!isok)
        continue;
      long long int ans = 0;
      for (int i = 0; i < N - 1; ++i) {
        int num = 1;
        for (int j = sts[i].size() - 1; j >= 0; --j) {
          ans += num * to[sts[i][j] - 'A'];
          num *= 10;
        }
      }
      int num = 1;
      for (int j = sts[N - 1].size() - 1; j >= 0; --j) {
        ans -= num * to[sts[N - 1][j] - 'A'];
        num *= 10;
      }
      if (!ans) {
        ansnum++;
      }
    } while (next_permutation(perm.begin(), perm.end()));
    for (int j = 10 - alphanum; j != 0; --j) {
      ansnum /= j;
    }
    cout << ansnum << endl;
  }
  return 0;
}