Problem p03350 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <map>
#include <queue>
using namespace std;
int N, K;
char X[22][(1 << 20) + 5];
struct State {
  int len, conf, start;
};
queue<State> Q;
State C[((1 << 20) + 5) * 20 + 5];
int DP[((1 << 20) + 5) * 20 + 5];
int cnt;
int Sum[35], Mask[35];
void precalcSum() {
  Sum[0] = 1;
  for (int i = 1; i <= N; i++) {
    int val = (1 << i);
    val *= (i + 1);
    Sum[i] = Sum[i - 1] + val;
  }
}
int toC(State x) {
  int len = x.len, conf = x.conf, start = x.start;
  int val = 0;
  if (len > 0)
    val = Sum[len - 1];
  int ans = val + conf * (len + 1) + start + 2;
  // C[ans ] = x;
  return ans;
}
void precalcNodes() {
  for (int i = 0; i <= N; i++) {
    int p = (1 << i);
    for (int conf = 0; conf < p; conf++) {
      for (int j = 0; j <= i; j++) {
        State x = {i, conf, j - 1};
        int pos = toC(x);
        C[pos] = x;
      }
    }
  }
}

void Solve() {

  for (int i = 0; i <= N; i++) {
    cin.getline(X[i] + 1, (1 << 20) + 5);
    for (int j = 1; X[i][j] != 0; j++)
      if (X[i][j] == '1') {
        State x = {i, j - 1, i - 1};
        int pos = toC(x);
        DP[pos] = 1;
        // Q.push(x);
      }
  }
  pair<int, int> ans = make_pair(0, 0);
  for (int start = N - 1; start >= -1; start--) {
    for (int len = N; len > start; len--) {
      int p = (1 << len);
      for (int conf = 0; conf < p; conf++) {
        if (start == -1) {
          State x = {len, conf, start};
          int pos = toC(x);
          if (DP[pos] >= K) {
            if (ans.first == len)
              ans.second = min(ans.second, conf);
            if (ans.first < len) {
              ans.first = len;
              ans.second = conf;
            }
          }

          continue;
        }
        State x = {len, conf, start};
        int pos = toC(x);
        int mask = (conf & ((1 << (start + 1)) - 1));
        int mask2 = ((conf - mask) >> (start + 1));
        for (int i = start; i >= 0; i--)
          if (mask & (1 << i)) {
            mask2 = mask2 * 2 + 1;
            mask = (conf & ((1 << i) - 1));
            State p;
            p.len = len - (start - i + 1) + 1;
            p.conf = (mask2 << i) + mask;
            p.start = i - 1;
            int aux = toC(p);
            /*if(DP[aux] == 0)
                Q.push(aux);*/
            DP[aux] += DP[pos];
            break;
          }
        mask = (conf & ((1 << (start + 1)) - 1));
        mask2 = ((conf - mask) >> (start + 1));
        for (int i = start; i >= 0; i--)
          if ((mask & (1 << i)) == 0) {
            mask2 = mask2 * 2;
            mask = (conf & ((1 << i) - 1));
            State p;
            p.len = len - (start - i + 1) + 1;
            p.conf = (mask2 << i) + mask;
            p.start = i - 1;
            int aux = toC(p);
            /*if(DP[aux] == 0)
                Q.push(aux);*/
            DP[aux] += DP[pos];
            break;
          }
        mask2 = ((conf - mask) >> (start + 1));
        State a;
        a.len = len - (start + 1);
        a.conf = mask2;
        a.start = -1;
        int aux = toC(a);
        DP[aux] += DP[pos];
      }
    }
  }
  for (int j = ans.first - 1; j >= 0; j--) {
    if ((ans.second & (1 << j)) != 0)
      cout << "1";
    else
      cout << "0";
  }
  cout << "\n";
}
int main() {
  cin >> N >> K;
  cin.get();
  precalcSum();
  precalcNodes();
  Solve();
  return 0;
}


Fixed submission:
#include <iostream>
#include <map>
#include <queue>
using namespace std;
int N, K;
char X[22][(1 << 20) + 5];
struct State {
  int len, conf, start;
};
queue<State> Q;
State C[((1 << 20) + 5) * 80 + 5];
int DP[((1 << 20) + 5) * 80 + 5];
int cnt;
int Sum[35], Mask[35];
void precalcSum() {
  Sum[0] = 1;
  for (int i = 1; i <= N; i++) {
    int val = (1 << i);
    val *= (i + 1);
    Sum[i] = Sum[i - 1] + val;
  }
}
int toC(State x) {
  int len = x.len, conf = x.conf, start = x.start;
  int val = 0;
  if (len > 0)
    val = Sum[len - 1];
  int ans = val + conf * (len + 1) + start + 2;
  // C[ans ] = x;
  return ans;
}
void precalcNodes() {
  for (int i = 0; i <= N; i++) {
    int p = (1 << i);
    for (int conf = 0; conf < p; conf++) {
      for (int j = 0; j <= i; j++) {
        State x = {i, conf, j - 1};
        int pos = toC(x);
        C[pos] = x;
      }
    }
  }
}

void Solve() {

  for (int i = 0; i <= N; i++) {
    cin.getline(X[i] + 1, (1 << 20) + 5);
    for (int j = 1; X[i][j] != 0; j++)
      if (X[i][j] == '1') {
        State x = {i, j - 1, i - 1};
        int pos = toC(x);
        DP[pos] = 1;
        // Q.push(x);
      }
  }
  pair<int, int> ans = make_pair(0, 0);
  for (int start = N - 1; start >= -1; start--) {
    for (int len = N; len > start; len--) {
      int p = (1 << len);
      for (int conf = 0; conf < p; conf++) {
        if (start == -1) {
          State x = {len, conf, start};
          int pos = toC(x);
          if (DP[pos] >= K) {
            if (ans.first == len)
              ans.second = min(ans.second, conf);
            if (ans.first < len) {
              ans.first = len;
              ans.second = conf;
            }
          }

          continue;
        }
        State x = {len, conf, start};
        int pos = toC(x);
        int mask = (conf & ((1 << (start + 1)) - 1));
        int mask2 = ((conf - mask) >> (start + 1));
        for (int i = start; i >= 0; i--)
          if (mask & (1 << i)) {
            mask2 = mask2 * 2 + 1;
            mask = (conf & ((1 << i) - 1));
            State p;
            p.len = len - (start - i + 1) + 1;
            p.conf = (mask2 << i) + mask;
            p.start = i - 1;
            int aux = toC(p);
            /*if(DP[aux] == 0)
                Q.push(aux);*/
            DP[aux] += DP[pos];
            break;
          }
        mask = (conf & ((1 << (start + 1)) - 1));
        mask2 = ((conf - mask) >> (start + 1));
        for (int i = start; i >= 0; i--)
          if ((mask & (1 << i)) == 0) {
            mask2 = mask2 * 2;
            mask = (conf & ((1 << i) - 1));
            State p;
            p.len = len - (start - i + 1) + 1;
            p.conf = (mask2 << i) + mask;
            p.start = i - 1;
            int aux = toC(p);
            /*if(DP[aux] == 0)
                Q.push(aux);*/
            DP[aux] += DP[pos];
            break;
          }
        mask2 = ((conf - mask) >> (start + 1));
        State a;
        a.len = len - (start + 1);
        a.conf = mask2;
        a.start = -1;
        int aux = toC(a);
        DP[aux] += DP[pos];
      }
    }
  }
  for (int j = ans.first - 1; j >= 0; j--) {
    if ((ans.second & (1 << j)) != 0)
      cout << "1";
    else
      cout << "0";
  }
  cout << "\n";
}
int main() {
  cin >> N >> K;
  cin.get();
  precalcSum();
  precalcNodes();
  Solve();
  return 0;
}