Problem p00870 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
#include <fstream>
#include <sys/timeb.h>

using namespace std;

#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define replrev(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define reprev(i, n) replrev(i, 0, n)
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
#define all(a) a.begin(), a.end()
#define mp make_pair
#define mt make_tuple
#define INF 2000000000
#define INFL 1000000000000000000LL
#define EPS (1e-10)
#define MOD 1000000007
#define PI 3.1415926536
#define RMAX 4294967295

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<P> vP;
typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;
typedef vector<vector<ll>> vvll;
typedef vector<vector<char>> vvc;
typedef vector<vector<string>> vvs;
typedef vector<vector<double>> vvd;
typedef vector<vector<P>> vvP;
typedef priority_queue<int, vector<int>, greater<int>> pqli;
typedef priority_queue<ll, vector<ll>, greater<ll>> pqlll;
typedef priority_queue<P, vector<P>, greater<P>> pqlP;
struct Edge {
  int from, to, cost;
  bool operator<(Edge e) { return cost < e.cost; }
};

int main() {
  while (true) {
    int N, M;
    cin >> N >> M;
    if (N == 0)
      break;
    vs pat(N);
    string text = "";
    rep(i, N) { cin >> pat[i]; }
    rep(i, M) {
      string str;
      cin >> str;
      text += str;
    }
    int L = text.size();
    vvb dp(1 << N, vb(L, false));

    rep(j, N) {
      rep(k, L - pat[j].size() + 1) {
        bool flag = true;
        rep(l, pat[j].size()) {
          if (text[k + l] != pat[j][l]) {
            flag = false;
            break;
          }
        }
        if (flag) {
          dp[1 << j][k + pat[j].size() - 1] = true;
        }
      }
    }

    repl(i, 1, 1 << N) {
      rep(j, N) {
        if ((i & (1 << j)) != 0) {
          if ((i ^ (1 << j)) == 0)
            continue;

          repl(k, 1, L - pat[j].size() + 1) {
            if (dp[(i ^ (1 << j))][k - 1]) {
              bool flag = true;
              rep(l, pat[j].size()) {
                if (text[k + l] != pat[j][l]) {
                  flag = false;
                  break;
                }
              }
              if (flag) {
                dp[i][k + pat[j].size() - 1] = true;
              }
            }
          }
        }
      }
    }
    int ans = 0;
    rep(i, L) {
      if (dp[(1 << N) - 1][i])
        ans++;
    }
    /*
    rep(i, 1 << N) {
            rep(j, L) {
                    cout << "xo"[dp[i][j]];
            }
            cout << static_cast<std::bitset<3>>(i) << endl;
    }
    */
    cout << ans << endl;
  }

  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#include <fstream>
#include <sys/timeb.h>

using namespace std;

#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define replrev(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define reprev(i, n) replrev(i, 0, n)
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
#define all(a) a.begin(), a.end()
#define mp make_pair
#define mt make_tuple
#define INF 2000000000
#define INFL 1000000000000000000LL
#define EPS (1e-10)
#define MOD 1000000007
#define PI 3.1415926536
#define RMAX 4294967295

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<P> vP;
typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;
typedef vector<vector<ll>> vvll;
typedef vector<vector<char>> vvc;
typedef vector<vector<string>> vvs;
typedef vector<vector<double>> vvd;
typedef vector<vector<P>> vvP;
typedef priority_queue<int, vector<int>, greater<int>> pqli;
typedef priority_queue<ll, vector<ll>, greater<ll>> pqlll;
typedef priority_queue<P, vector<P>, greater<P>> pqlP;
struct Edge {
  int from, to, cost;
  bool operator<(Edge e) { return cost < e.cost; }
};

int main() {
  while (true) {
    int N, M;
    cin >> N >> M;
    if (N == 0)
      break;
    vs pat(N);
    string text = "";
    rep(i, N) { cin >> pat[i]; }
    rep(i, M) {
      string str;
      cin >> str;
      text += str;
    }
    int L = text.size();
    vvb dp(1 << N, vb(L, false));

    rep(j, N) {
      rep(k, L - pat[j].size() + 1) {
        bool flag = true;
        rep(l, pat[j].size()) {
          if (text[k + l] != pat[j][l]) {
            flag = false;
            break;
          }
        }
        if (flag) {
          dp[1 << j][k + pat[j].size() - 1] = true;
        }
      }
    }

    repl(i, 1, 1 << N) {
      rep(j, N) {
        if ((i & (1 << j)) != 0) {
          if ((i ^ (1 << j)) == 0)
            continue;

          repl(k, 1, L - pat[j].size() + 1) {
            if (dp[(i ^ (1 << j))][k - 1]) {
              if (dp[1 << j][k + pat[j].size() - 1]) {
                dp[i][k + pat[j].size() - 1] = true;
              }
            }
          }
        }
      }
    }
    int ans = 0;
    rep(i, L) {
      if (dp[(1 << N) - 1][i])
        ans++;
    }
    /*
    rep(i, 1 << N) {
            rep(j, L) {
                    cout << "xo"[dp[i][j]];
            }
            cout << static_cast<std::bitset<3>>(i) << endl;
    }
    */
    cout << ans << endl;
  }

  return 0;
}