Problem p03350 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

// 入力
int N, K;
int dp[1 << 21][22];
string S[1 << 18];

// 遷移
int LEN[1 << 21], BASE[1 << 21];
pair<int, int> G[1 << 21][22];
bool H[1 << 21][22];

void init() {
  for (int i = 0; i < 21; i++) {
    for (int j = (1 << i); j < (2 << i); j++)
      LEN[j] = i;
  }
  for (int i = 0; i < (1 << 21); i++)
    BASE[i] = i - (1 << LEN[i]);

  for (int i = 1; i < (2 << N); i++) {
    int bit[22], base = BASE[i];
    for (int j = 0; j < LEN[i]; j++) {
      if ((base & (1 << j)) == 0)
        bit[j] = 0;
      else
        bit[j] = 1;
    }

    // 部分消し
    for (int j = 0; j < LEN[i]; j++) {
      int lims = j;
      for (int k = j; k < LEN[i]; k++) {
        if (k + 1 == LEN[i] || bit[k] != bit[k + 1])
          break;
        lims = k + 1;
      }

      int r1 = (base & ((1 << j) - 1));
      int r2 = (base >> (lims + 1));
      int cx = r1 + (r2 << j);
      int cp = j + (LEN[i] - lims - 1);
      G[i][j] = make_pair((1 << cp) + cx, min(cp, j + 1));
      if (lims + 1 == LEN[i])
        H[i][j] = true;
    }
  }
}

int main() {
  cin >> N >> K;
  for (int i = 0; i <= N; i++)
    cin >> S[i];
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j < S[i].size(); j++) {
      if (S[i][j] == '1')
        dp[(1 << i) + j][0] = 1;
    }
  }
  /*string T; cin >> T;
  int r = 0;
  for (int i = 0; i < T.size(); i++) {
          if (T[T.size() - 1 - i] == '1') r += (1 << i);
  }
  dp[(1 << T.size()) + r][0] = 1;*/

  init();

  for (int i = (2 << N) - 1; i >= 1; i--) {
    int cv[22];
    for (int j = 0; j < N; j++)
      cv[j] = (1 << j) + (BASE[i] & ((1 << j) - 1));
    for (int j = 0; j < LEN[i]; j++) {
      for (int k = j; k < LEN[i]; k++) {
        dp[G[i][k].first][G[i][k].second] += dp[i][j];
        if (H[i][k] == false)
          dp[cv[k]][k] += dp[i][j];
      }
    }
  }

  for (int i = N; i >= 0; i--) {
    for (int j = (1 << i); j < (2 << i); j++) {
      int ret = 0;
      for (int k = 0; k <= N; k++)
        ret += dp[j][k];
      if (ret < K)
        continue;

      string str = "";
      for (int k = i - 1; k >= 0; k--) {
        if (((j - (1 << i)) / (1 << k)) % 2 == 1)
          str += "1";
        else
          str += "0";
      }
      cout << str << endl;
      return 0;
    }
  }
  cout << "-1" << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

// 入力
int N, K;
int dp[1 << 21][22];
string S[1 << 18];

// 遷移
int LEN[1 << 21], BASE[1 << 21];
pair<int, int> G[1 << 21][22];
bool H[1 << 21][22];

void init() {
  for (int i = 0; i < 21; i++) {
    for (int j = (1 << i); j < (2 << i); j++)
      LEN[j] = i;
  }
  for (int i = 0; i < (1 << 21); i++)
    BASE[i] = i - (1 << LEN[i]);

  for (int i = 1; i < (2 << N); i++) {
    int bit[22], base = BASE[i];
    for (int j = 0; j < LEN[i]; j++) {
      if ((base & (1 << j)) == 0)
        bit[j] = 0;
      else
        bit[j] = 1;
    }

    // 部分消し
    for (int j = 0; j < LEN[i]; j++) {
      int lims = j;
      for (int k = j; k < LEN[i]; k++) {
        if (k + 1 == LEN[i] || bit[k] != bit[k + 1])
          break;
        lims = k + 1;
      }

      int r1 = (base & ((1 << j) - 1));
      int r2 = (base >> (lims + 1));
      int cx = r1 + (r2 << j);
      int cp = j + (LEN[i] - lims - 1);
      G[i][j] = make_pair((1 << cp) + cx, min(cp, j + 1));
      if (lims + 1 == LEN[i])
        H[i][j] = true;
    }
  }
}

int main() {
  cin >> N >> K;
  for (int i = 0; i <= N; i++)
    cin >> S[i];
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j < S[i].size(); j++) {
      if (S[i][j] == '1')
        dp[(1 << i) + j][0] = 1;
    }
  }
  /*string T; cin >> T;
  int r = 0;
  for (int i = 0; i < T.size(); i++) {
          if (T[T.size() - 1 - i] == '1') r += (1 << i);
  }
  dp[(1 << T.size()) + r][0] = 1;*/

  init();

  for (int i = (2 << N) - 1; i >= 1; i--) {
    int cv[22];
    for (int j = 0; j < N; j++)
      cv[j] = (1 << j) + (BASE[i] & ((1 << j) - 1));
    for (int j = 0; j < LEN[i]; j++) {
      if (dp[i][j] == 0)
        continue;
      for (int k = j; k < LEN[i]; k++) {
        dp[G[i][k].first][G[i][k].second] += dp[i][j];
        if (H[i][k] == false)
          dp[cv[k]][k] += dp[i][j];
      }
    }
  }

  for (int i = N; i >= 0; i--) {
    for (int j = (1 << i); j < (2 << i); j++) {
      int ret = 0;
      for (int k = 0; k <= N; k++)
        ret += dp[j][k];
      if (ret < K)
        continue;

      string str = "";
      for (int k = i - 1; k >= 0; k--) {
        if (((j - (1 << i)) / (1 << k)) % 2 == 1)
          str += "1";
        else
          str += "0";
      }
      cout << str << endl;
      return 0;
    }
  }
  cout << "-1" << endl;
  return 0;
}