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

#include <algorithm>
#include <set>
#include <stdio.h>
#include <vector>
using namespace std;
char str[22][22];
int has[12];
char p[12][22][22];
int bit[12][4][22];
int h[12];
int w[12];
int r;
int com[12];
vector<int> now;
int H, W;

// set<vector<int> >S;
int solve(int a) {
  if (a == r)
    return 1;
  //	if(S.count(now))return 0;
  //	S.insert(now);
  int tmp[22];
  for (int i = 0; i < H; i++)
    tmp[i] = now[i];
  for (int i = a; i < r; i++) {
    int v = com[i];
    for (int j = 0; j <= H - h[v]; j++)
      for (int k = 0; k <= W - w[v]; k++) {
        bool ok = true;
        for (int l = 0; l < h[v]; l++)
          if ((bit[v][0][l] << k) & now[j + l]) {
            ok = false;
            break;
          }
        if (ok) {
          for (int l = 0; l < h[v]; l++)
            tmp[j + l] |= (bit[v][0][l] << k);
        }
      }
    for (int j = 0; j <= H - h[v]; j++)
      for (int k = 0; k <= W - w[v]; k++) {
        bool ok = true;
        for (int l = 0; l < h[v]; l++)
          if ((bit[v][2][l] << k) & now[j + l]) {
            ok = false;
            break;
          }
        if (ok) {
          for (int l = 0; l < h[v]; l++)
            tmp[j + l] |= (bit[v][2][l] << k);
        }
      }
    for (int j = 0; j <= H - w[v]; j++)
      for (int k = 0; k <= W - h[v]; k++) {
        bool ok = true;
        for (int l = 0; l < w[v]; l++)
          if ((bit[v][1][l] << k) & now[j + l]) {
            ok = false;
            break;
          }
        if (ok) {
          for (int l = 0; l < w[v]; l++)
            tmp[j + l] |= (bit[v][1][l] << k);
        }
      }
    for (int j = 0; j <= H - w[v]; j++)
      for (int k = 0; k <= W - h[v]; k++) {
        bool ok = true;
        for (int l = 0; l < w[v]; l++)
          if ((bit[v][3][l] << k) & now[j + l]) {
            ok = false;
            break;
          }
        if (ok) {
          for (int l = 0; l < w[v]; l++)
            tmp[j + l] |= (bit[v][3][l] << k);
        }
      }
  }
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      if (!(tmp[i] & (1 << j)))
        return 0;
  int f = com[a];
  for (int i = 0; i <= H - h[f]; i++) {
    for (int j = 0; j <= W - w[f]; j++) {
      bool ok = true;
      for (int k = 0; k < h[f]; k++)
        if ((bit[f][0][k] << j) & now[i + k]) {
          ok = false;
          break;
        }
      if (ok) {
        for (int k = 0; k < h[f]; k++)
          now[i + k] ^= (bit[f][0][k] << j);
        if (solve(a + 1))
          return 1;
        for (int k = 0; k < h[f]; k++)
          now[i + k] ^= (bit[f][0][k] << j);
      }
    }
  }
  for (int i = 0; i <= H - h[f]; i++) {
    for (int j = 0; j <= W - w[f]; j++) {
      bool ok = true;
      for (int k = 0; k < h[f]; k++)
        if ((bit[f][2][k] << j) & now[i + k]) {
          ok = false;
          break;
        }
      if (ok) {
        for (int k = 0; k < h[f]; k++)
          now[i + k] ^= (bit[f][2][k] << j);
        if (solve(a + 1))
          return 1;
        for (int k = 0; k < h[f]; k++)
          now[i + k] ^= (bit[f][2][k] << j);
      }
    }
  }
  for (int i = 0; i <= H - w[f]; i++) {
    for (int j = 0; j <= W - h[f]; j++) {
      bool ok = true;
      for (int k = 0; k < w[f]; k++)
        if ((bit[f][1][k] << j) & now[i + k]) {
          ok = false;
          break;
        }
      if (ok) {
        for (int k = 0; k < w[f]; k++)
          now[i + k] ^= (bit[f][1][k] << j);
        if (solve(a + 1))
          return 1;
        for (int k = 0; k < w[f]; k++)
          now[i + k] ^= (bit[f][1][k] << j);
      }
    }
  }
  for (int i = 0; i <= H - w[f]; i++) {
    for (int j = 0; j <= W - h[f]; j++) {
      bool ok = true;
      for (int k = 0; k < w[f]; k++)
        if ((bit[f][3][k] << j) & now[i + k]) {
          ok = false;
          break;
        }
      if (ok) {
        for (int k = 0; k < w[f]; k++)
          now[i + k] ^= (bit[f][3][k] << j);
        if (solve(a + 1))
          return 1;
        for (int k = 0; k < w[f]; k++)
          now[i + k] ^= (bit[f][3][k] << j);
      }
    }
  }
  return 0;
}
int sts[12];
int main() {
  int a, b;
  while (scanf("%d%d", &a, &b), a) {
    H = a;
    W = b;
    for (int i = 0; i < a; i++)
      scanf("%s", str[i]);
    int cnt = 0;
    now = vector<int>(a);
    for (int i = 0; i < a; i++)
      now[i] = 0;
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++)
        if (str[i][j] == '#')
          now[i] += (1 << j);
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++)
        if (str[i][j] == '.')
          cnt++;
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
      for (int j = 0; j < 4; j++)
        for (int k = 0; k < 22; k++)
          bit[i][j][k] = 0;
    for (int i = 0; i < n; i++) {
      has[i] = 0;
      scanf("%d%d", h + i, w + i);
      sts[i] =
          max((H - h[i] + 1) * (W - w[i] + 1), (H - w[i] + 1) * (W - h[i] + 1));
      for (int j = 0; j < h[i]; j++)
        scanf("%s", p[i][j]);
      for (int j = 0; j < h[i]; j++) {
        for (int k = 0; k < w[i]; k++) {
          if (p[i][j][k] == '#') {
            has[i]++;
            bit[i][0][j] += (1 << k);
            bit[i][1][w[i] - 1 - k] += (1 << j);
            bit[i][2][h[i] - 1 - j] += (1 << (w[i] - 1 - k));
            bit[i][3][k] += (1 << (h[i] - 1 - j));
          }
        }
      }
    }
    int m;
    scanf("%d", &m);
    while (m--) {
      //		S.clear();
      for (int i = 0; i < a; i++)
        now[i] = 0;
      for (int i = 0; i < a; i++)
        for (int j = 0; j < b; j++)
          if (str[i][j] == '#')
            now[i] += (1 << j);
      int sum = 0;
      scanf("%d", &r);
      for (int i = 0; i < r; i++) {
        scanf("%d", com + i);
        com[i]--;
        sum += has[com[i]];
      }
      if (sum != cnt) {
        printf("NO\\n");
        continue;
      }
      for (int i = 0; i < r; i++) {
        for (int j = 0; j < r - 1; j++) {
          if (sts[com[j]] > sts[com[j + 1]]) {
            swap(com[j], com[j + 1]);
          }
        }
      }
      int res = solve(0);
      if (res)
        printf("YES\\n");
      else
        printf("NO\\n");
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <set>
#include <stdio.h>
#include <vector>
using namespace std;
char str[22][22];
int has[12];
char p[12][22][22];
int bit[12][4][22];
int h[12];
int w[12];
int r;
int com[12];
vector<int> now;
int H, W;
// set<vector<int> >S;
int solve(int a) {
  if (a == r)
    return 1;
  //	if(S.count(now))return 0;
  //	S.insert(now);
  int f = com[a];
  for (int i = 0; i <= H - h[f]; i++) {
    for (int j = 0; j <= W - w[f]; j++) {
      bool ok = true;
      for (int k = 0; k < h[f]; k++)
        if ((bit[f][0][k] << j) & now[i + k]) {
          ok = false;
          break;
        }
      if (ok) {
        for (int k = 0; k < h[f]; k++)
          now[i + k] ^= (bit[f][0][k] << j);
        if (solve(a + 1))
          return 1;
        for (int k = 0; k < h[f]; k++)
          now[i + k] ^= (bit[f][0][k] << j);
      }
    }
  }
  for (int i = 0; i <= H - h[f]; i++) {
    for (int j = 0; j <= W - w[f]; j++) {
      bool ok = true;
      for (int k = 0; k < h[f]; k++)
        if ((bit[f][2][k] << j) & now[i + k]) {
          ok = false;
          break;
        }
      if (ok) {
        for (int k = 0; k < h[f]; k++)
          now[i + k] ^= (bit[f][2][k] << j);
        if (solve(a + 1))
          return 1;
        for (int k = 0; k < h[f]; k++)
          now[i + k] ^= (bit[f][2][k] << j);
      }
    }
  }
  for (int i = 0; i <= H - w[f]; i++) {
    for (int j = 0; j <= W - h[f]; j++) {
      bool ok = true;
      for (int k = 0; k < w[f]; k++)
        if ((bit[f][1][k] << j) & now[i + k]) {
          ok = false;
          break;
        }
      if (ok) {
        for (int k = 0; k < w[f]; k++)
          now[i + k] ^= (bit[f][1][k] << j);
        if (solve(a + 1))
          return 1;
        for (int k = 0; k < w[f]; k++)
          now[i + k] ^= (bit[f][1][k] << j);
      }
    }
  }
  for (int i = 0; i <= H - w[f]; i++) {
    for (int j = 0; j <= W - h[f]; j++) {
      bool ok = true;
      for (int k = 0; k < w[f]; k++)
        if ((bit[f][3][k] << j) & now[i + k]) {
          ok = false;
          break;
        }
      if (ok) {
        for (int k = 0; k < w[f]; k++)
          now[i + k] ^= (bit[f][3][k] << j);
        if (solve(a + 1))
          return 1;
        for (int k = 0; k < w[f]; k++)
          now[i + k] ^= (bit[f][3][k] << j);
      }
    }
  }
  return 0;
}
int sts[12];
int main() {
  int a, b;
  while (scanf("%d%d", &a, &b), a) {
    H = a;
    W = b;
    for (int i = 0; i < a; i++)
      scanf("%s", str[i]);
    int cnt = 0;
    now = vector<int>(a);
    for (int i = 0; i < a; i++)
      now[i] = 0;
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++)
        if (str[i][j] == '#')
          now[i] += (1 << j);
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++)
        if (str[i][j] == '.')
          cnt++;
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
      for (int j = 0; j < 4; j++)
        for (int k = 0; k < 22; k++)
          bit[i][j][k] = 0;
    for (int i = 0; i < n; i++) {
      has[i] = 0;
      scanf("%d%d", h + i, w + i);
      sts[i] =
          max((H - h[i] + 1) * (W - w[i] + 1), (H - w[i] + 1) * (W - h[i] + 1));
      for (int j = 0; j < h[i]; j++)
        scanf("%s", p[i][j]);
      for (int j = 0; j < h[i]; j++) {
        for (int k = 0; k < w[i]; k++) {
          if (p[i][j][k] == '#') {
            has[i]++;
            bit[i][0][j] += (1 << k);
            bit[i][1][w[i] - 1 - k] += (1 << j);
            bit[i][2][h[i] - 1 - j] += (1 << (w[i] - 1 - k));
            bit[i][3][k] += (1 << (h[i] - 1 - j));
          }
        }
      }
    }
    int m;
    scanf("%d", &m);
    while (m--) {
      //		S.clear();
      for (int i = 0; i < a; i++)
        now[i] = 0;
      for (int i = 0; i < a; i++)
        for (int j = 0; j < b; j++)
          if (str[i][j] == '#')
            now[i] += (1 << j);
      int sum = 0;
      scanf("%d", &r);
      for (int i = 0; i < r; i++) {
        scanf("%d", com + i);
        com[i]--;
        sum += has[com[i]];
      }
      if (sum != cnt) {
        printf("NO\\n");
        continue;
      }
      for (int i = 0; i < r; i++) {
        for (int j = 0; j < r - 1; j++) {
          if (sts[com[j]] > sts[com[j + 1]]) {
            swap(com[j], com[j + 1]);
          }
        }
      }
      int res = solve(0);
      if (res)
        printf("YES\\n");
      else
        printf("NO\\n");
    }
  }
}