Problem p01796 (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"

int N, K;
vector<vector<int>> orders;
int memo[1 << 16][16][6];

int getperm(const int now, const vector<int> &perm) {
  vector<int> a(N - K);
  int n = 0;
  for (auto e : perm) {
    if (find(orders[now].begin(), orders[now].end(), e) == orders[now].end()) {
      a[n++] = e;
    }
  }
  if (N - K == 3) {
    if (a[0] < a[1]) {
      if (a[1] < a[2])
        return 0;
      else if (a[0] < a[2])
        return 1;
      else
        return 4;
    } else {
      if (a[1] > a[2])
        return 5;
      else if (a[0] < a[2])
        return 2;
      else
        return 3;
    }
  } else if (N - K == 2) {
    return a[0] < a[1];
  } else {
    return 0;
  }
}
vector<int> getans(bitset<16> &rest, const vector<int> &perm) {
  for (int i = 0; i < N; ++i) {
    if (rest[i]) {

      const int hash = getperm(i, perm);
      if (memo[rest.to_ulong()][i][hash])
        continue;
      const int n(i);
      vector<int> aperm(perm);

      auto it = aperm.begin();
      for (int o = 0; o < orders[0].size(); ++o) {
        it = find(it, aperm.end(), orders[n][o]);
      }
      if (it == aperm.end())
        continue;
      else {
        if (rest.count() == 1) {
          return vector<int>(1, n);
        } else {
          for (int p = 0; p < orders[0].size(); ++p) {
            auto it = find(aperm.begin() + p, aperm.end(), orders[n][p]);
            aperm.erase(it);
            aperm.insert(aperm.begin(), orders[n][p]);
          }
          rest[i] = false;
          vector<int> v = getans(rest, aperm);
          rest[i] = true;
          if (!v.empty()) {
            v.push_back(n);
            return v;
          }
        }
      }
      memo[rest.to_ulong()][i][hash] = true;
    }
  }
  return vector<int>(0);
}
int main() {
  memset(memo, 0, sizeof(memo));
  cin >> N >> K;
  orders.resize(N);
  for (int i = 0; i < N; ++i) {
    string st;
    cin >> st;
    for (int j = 0; j < st.size(); ++j) {
      orders[i].push_back(st[j] - 'A');
    }
  }
  string ans = "ZZZZZZZZZZZZZZZZZZZZ";
  for (int i = 0; i < N; ++i) {
    vector<int> perm(orders[i]);
    vector<int> aa;
    for (int j = 0; j < N; ++j) {
      if (find(perm.begin(), perm.end(), j) == perm.end()) {
        aa.push_back(j);
      }
    }
    sort(aa.begin(), aa.end());
    do {

      bitset<16> bs(0);
      for (int i = 0; i < N; ++i) {
        bs[i] = true;
      }
      vector<int> aperm(perm);
      aperm.insert(aperm.end(), aa.begin(), aa.end());
      vector<int> v = getans(bs, aperm);
      if (!v.empty()) {
        if (v.size() != N)
          assert(false);
        reverse(v.begin(), v.end());
        string nans;
        for (auto a : v) {
          nans.push_back(a + 'A');
        }
        ans = min(ans, nans);
      }
    } while (next_permutation(aa.begin(), aa.end()));
  }
  cout << ans << endl;
  return 0;
}

Error: -6
9124349f-a18e-4402-8f40-fdebcaec8d2a.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p01796/C++/s509698335.cpp:114: int main(): Assertion `false' failed.


Fixed submission:

#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"

int N, K;
vector<vector<int>> orders;
int memo[1 << 16][16][6];

int getperm(const int now, const vector<int> &perm) {
  vector<int> a(N - K);
  int n = 0;
  for (auto e : perm) {
    if (find(orders[now].begin(), orders[now].end(), e) == orders[now].end()) {
      a[n++] = e;
    }
  }
  if (N - K == 3) {
    if (a[0] < a[1]) {
      if (a[1] < a[2])
        return 0;
      else if (a[0] < a[2])
        return 1;
      else
        return 4;
    } else {
      if (a[1] > a[2])
        return 5;
      else if (a[0] < a[2])
        return 2;
      else
        return 3;
    }
  } else if (N - K == 2) {
    return a[0] < a[1];
  } else {
    return 0;
  }
}
vector<int> getans(bitset<16> &rest, const vector<int> &perm) {
  for (int i = 0; i < N; ++i) {
    if (rest[i]) {

      const int hash = getperm(i, perm);
      if (memo[rest.to_ulong()][i][hash])
        continue;
      const int n(i);
      vector<int> aperm(perm);

      auto it = aperm.begin();
      for (int o = 0; o < orders[0].size(); ++o) {
        it = find(it, aperm.end(), orders[n][o]);
      }
      if (it == aperm.end())
        continue;
      else {
        if (rest.count() == 1) {
          return vector<int>(1, n);
        } else {
          for (int o = 0; o < orders[0].size(); ++o) {
            auto it = find(aperm.begin() + o, aperm.end(), orders[n][o]);
            aperm.erase(it);
            aperm.insert(aperm.begin(), orders[n][o]);
          }
          rest[i] = false;
          vector<int> v = getans(rest, aperm);
          if (!v.empty()) {
            v.push_back(n);
            return v;
          }
          rest[i] = true;
        }
      }
      memo[rest.to_ulong()][i][hash] = true;
    }
  }
  return vector<int>(0);
}
int main() {
  memset(memo, 0, sizeof(memo));
  cin >> N >> K;
  orders.resize(N);
  for (int i = 0; i < N; ++i) {
    string st;
    cin >> st;
    for (int j = 0; j < st.size(); ++j) {
      orders[i].push_back(st[j] - 'A');
    }
  }
  bitset<16> bs(0);
  for (int i = 0; i < N; ++i) {
    bs[i] = true;
  }
  string ans = "ZZZZZZZZZZZZZZZZZZZZ";
  for (int i = 0; i < N; ++i) {
    vector<int> perm(orders[i]);
    vector<int> aa;
    for (int j = 0; j < N; ++j) {
      if (find(perm.begin(), perm.end(), j) == perm.end()) {
        aa.push_back(j);
      }
    }
    sort(aa.begin(), aa.end());
    do {
      vector<int> aperm(perm);
      aperm.insert(aperm.end(), aa.begin(), aa.end());
      vector<int> v = getans(bs, aperm);
      if (!v.empty()) {
        if (v.size() != N)
          assert(false);
        reverse(v.begin(), v.end());
        string nans;
        for (auto a : v) {
          nans.push_back(a + 'A');
        }
        ans = min(ans, nans);
      }
    } while (next_permutation(aa.begin(), aa.end()));
  }
  cout << ans << endl;
  return 0;
}