Problem p00784 (C++) — buggy submission, verdict: Time Limit Exceeded

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

//// < "d:\\d_download\\visual studio
///2015\\projects\\programing_contest_c++\\debug\\a.txt" > "d:\\d_download\\visual
///studio 2015\\projects\\programing_contest_c++\\debug\\b.txt"

int getans(const bitset<19> &bs, int rest, vector<vector<int>> &memo,
           vector<int> &confs) {
  if (!rest)
    return 0;
  else {
    if (memo[rest][bs.to_ulong()] != -1)
      return memo[rest][bs.to_ulong()];
    else {
      int ans = 10000;
      for (int bet = 1; bet < 19; ++bet) {
        bool ok = true;
        for (int i = bet; i < 19; ++i) {
          if (bs[i] && confs[bet + 18 - i]) {
            ok = false;
          }
        }
        if (ok) {
          bitset<19> nextbs;
          for (int i = 0; i < 19 - bet; ++i) {
            nextbs[i] = bs[i + bet];
          }
          nextbs[18] = true;
          ans = min(ans, bet + getans(nextbs, rest - 1, memo, confs));
        }
      }
      return memo[rest][bs.to_ulong()] = ans;
    }
  }
}

int main() {
  while (1) {
    int N;
    cin >> N;
    if (!N)
      break;
    vector<vector<int>> tables(5, vector<int>(N));
    for (int i = 0; i < 5; ++i) {
      string st;
      cin >> st;
      for (int j = 0; j < N; ++j) {
        tables[i][j] = (st[j] == 'X');
      }
    }
    vector<int> conf(41);
    for (int i = 1; i < N; ++i) {
      for (int y = 0; y < 5; ++y) {
        for (int l = 0; l < N - i; ++l) {
          int r = l + i;
          if (tables[y][l] && tables[y][r])
            conf[i] = true;
        }
      }
    }
    bitset<19> bs;
    vector<vector<int>> memo(11, vector<int>(1 << 19, -1));
    int ans = getans(bs, 10, memo, conf);
    cout << ans + N - 1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

//// < "d:\\d_download\\visual studio
///2015\\projects\\programing_contest_c++\\debug\\a.txt" > "d:\\d_download\\visual
///studio 2015\\projects\\programing_contest_c++\\debug\\b.txt"

int getans(const bitset<20> &bs, int rest, vector<vector<int>> &memo,
           vector<int> &confs) {
  if (!rest)
    return 0;
  else {
    if (memo[rest][bs.to_ulong()] != -1)
      return memo[rest][bs.to_ulong()];
    else {
      int ans = 10000;
      for (int bet = 1; bet < 20; ++bet) {
        bool ok = true;
        for (int i = bet; i < 20; ++i) {
          if (bs[i] && confs[bet + 19 - i]) {
            ok = false;
          }
        }
        if (ok) {
          bitset<20> nextbs;
          for (int i = 0; i < 20 - bet; ++i) {
            nextbs[i] = bs[i + bet];
          }
          nextbs[19] = true;
          ans = min(ans, bet + getans(nextbs, rest - 1, memo, confs));
        }
      }
      return memo[rest][bs.to_ulong()] = ans;
    }
  }
}

int main() {
  while (1) {
    int N;
    cin >> N;
    vector<vector<int>> tables(5, vector<int>(N));
    for (int i = 0; i < 5; ++i) {
      string st;
      cin >> st;
      for (int j = 0; j < N; ++j) {
        tables[i][j] = (st[j] == 'X');
      }
    }
    vector<int> conf(41);
    for (int i = 1; i < N; ++i) {
      for (int y = 0; y < 5; ++y) {
        for (int l = 0; l < N - i; ++l) {
          int r = l + i;
          if (tables[y][l] && tables[y][r])
            conf[i] = true;
        }
      }
    }
    bitset<20> bs;
    vector<vector<int>> memo(11, vector<int>(1 << 20, -1));
    int ans = getans(bs, 10, memo, conf);
    cout << ans + N - 1 << endl;
  }
  return 0;
}