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

#include <algorithm>
#include <climits>
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define IINF (INT_MAX)
#define MAX_E 13

using namespace std;

int n, m;
string e[MAX_E];
string text;
bool dp[5001][(1 << 12)];
bool isAble[5001][12];

int main() {
  int tmp;
  while (tmp = scanf("%d %d", &n, &m), n | m) {
    rep(i, n) { cin >> e[i]; }
    text = "";
    rep(i, m) {
      string content;
      cin >> content;
      text += content;
    }

    int len = text.size();
    rep(i, len + 1) rep(j, (1 << n)) dp[i][j] = false;
    int ans = 0;

    rep(i, len) {
      rep(j, n) {
        isAble[i][j] = false;
        int e_len = e[j].size();
        if (i + e_len > text.size())
          continue;
        if (text[i] != e[j][0])
          continue;
        if (text.substr(i, e_len) == e[j]) {
          isAble[i][j] = true;
          dp[i + e_len][(1 << j)] = true;
        }
      }
    }

    rep(cur, len + 1) {
      rep(state, (1 << n)) {
        if (dp[cur][state] == false)
          continue;
        rep(k, n) {
          if ((state >> k) & 1)
            continue;
          if (!isAble[cur][k])
            continue;
          int e_len = e[k].size();
          if (e[k][0] != text[cur])
            continue;
          if (text.size() < cur + e_len)
            continue;
          // if(text.substr(cur,e_len) == e[k])
          //{
          dp[cur + e_len][state | (1 << k)] = true;
          //}
        }
      }
      if (dp[cur][(1 << n) - 1])
        ans++;
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cstdio>
#include <iostream>
#include <set>
#include <vector>

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define IINF (INT_MAX)
#define MAX_E 13

using namespace std;

int n, m;
string e[MAX_E];
string text;
bool dp[5001][(1 << 12)];

int main() {
  while (scanf("%d %d", &n, &m), n | m) {
    rep(i, n) { cin >> e[i]; }
    text = "";
    rep(i, m) {
      string content;
      cin >> content;
      text += content;
    }

    int len = text.size();
    rep(i, len + 1) rep(j, (1 << n)) dp[i][j] = false;
    int ans = 0;

    rep(i, len) {
      rep(j, n) {
        int e_len = e[j].size();
        if (i + e_len > text.size())
          continue;
        if (text[i] != e[j][0])
          continue;
        if (text.substr(i, e_len) == e[j]) {
          dp[i + e_len][(1 << j)] = true;
        }
      }
    }

    rep(cur, len + 1) {
      rep(state, (1 << n)) {
        if (dp[cur][state] == false)
          continue;
        rep(k, n) {
          if ((state >> k) & 1)
            continue;
          int e_len = e[k].size();
          if (e[k][0] != text[cur])
            continue;
          if (text.size() < cur + e_len)
            continue;
          if (text.substr(cur, e_len) == e[k]) {
            dp[cur + e_len][state | (1 << k)] = true;
          }
        }
      }
      if (dp[cur][(1 << n) - 1])
        ans++;
    }
    printf("%d\\n", ans);
  }
  return 0;
}