alexjercan/bugnet huggingface dataset:
Problem p00870 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define pb push_back
#define fi first
#define sc second
#define mp make_pair
#define is insert

bool dp[4096][5001];

void solve(int n, int m) {
  string e[12], T;
  int ans = 0, mn = 20;
  for (int i = 0; i < n; ++i) {
    cin >> e[i];
    mn = min(mn, (int)e[i].size());
  }
  for (int j = 0; j < m; ++j) {
    string t;
    cin >> t;
    T += t;
  }
  bool f[12][5000];
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= T.size() - mn; ++j) {
      if (T.substr(j, e[i].size()) == e[i])
        f[i][j] = true;
      else
        f[i][j] = false;
    }
  }
  fill(dp[0], dp[0] + T.size() + 1, true);
  for (int s = 1; s < (1 << n); ++s)
    fill(dp[s], dp[s] + T.size() + 1, false);
  for (int s = 0; s < (1 << n) - 1; ++s) {
    for (int j = 0; j <= T.size() - mn; ++j) {
      if (dp[s][j]) {
        for (int i = 0; i < n; ++i) {
          if (!((s >> i) & 1) && f[i][j])
            dp[s + (1 << i)][j + e[i].size()] = true;
        }
      }
    }
  }
  for (int j = 1; j <= T.size(); ++j)
    ans += dp[(1 << n) - 1][j];
  printf("%d\\n", ans);
}

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0)
      break;
    solve(n, m);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define pb push_back
#define fi first
#define sc second
#define mp make_pair
#define is insert

bool dp[4096][5001];

void solve(int n, int m) {
  string e[12], T;
  int ans = 0, mn = 20;
  for (int i = 0; i < n; ++i) {
    cin >> e[i];
    mn = min(mn, (int)e[i].size());
  }
  for (int j = 0; j < m; ++j) {
    string t;
    cin >> t;
    T += t;
  }
  fill(dp[0], dp[0] + T.size() + 1, true);
  for (int s = 1; s < (1 << n); ++s) {
    fill(dp[s], dp[s] + T.size() + 1, false);
  }
  for (int s = 0; s < (1 << n) - 1; ++s) {
    for (int j = 0; j <= T.size() - mn; ++j) {
      if (dp[s][j]) {
        for (int i = 0; i < n; ++i) {
          if (!((s >> i) & 1) && T.substr(j, e[i].size()) == e[i]) {
            dp[s + (1 << i)][j + e[i].size()] = true;
          }
        }
      }
    }
  }
  for (int j = 1; j <= T.size(); ++j) {
    ans += dp[(1 << n) - 1][j];
  }
  printf("%d\\n", ans);
}

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0)
      break;
    solve(n, m);
  }
}
Problem p00870 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define N 12
#define S 5005
#define M 1777771
using namespace std;
typedef unsigned long long ull;

bool dp[(1 << N)][S];
int n, m;
string s[N], t;

ull d1[N], d2[S], A[30];

int main() {

  while (1) {

    cin >> n >> m;
    if (!n && !m)
      break;

    for (int i = 0; i < n; i++)
      cin >> s[i];

    t = "";

    for (int i = 0; i < m; i++) {
      string tmp;
      cin >> tmp;
      t += tmp;
    }

    for (int i = 0; i < n; i++) {
      ull hash = s[i][0];
      for (int j = 1; j < s[i].size(); j++)
        hash = hash * M + s[i][j];
      d1[i] = hash;
    }

    d2[0] = t[0];
    for (int i = 1; i < t.size(); i++)
      d2[i] = d2[i - 1] * M + t[i];

    memset(dp, 0, sizeof(dp));

    for (int i = 0; i < t.size(); i++)
      dp[0][i] = true;

    A[0] = 1;
    for (int i = 1; i < 30; i++)
      A[i] = A[i - 1] * M;

    for (int i = 0; i < (1 << n); i++) {

      for (int j = 0; j < t.size(); j++) {

        if (!dp[i][j])
          continue;

        for (int k = 0; k < n; k++) {

          if (i & (1 << k))
            continue;

          int slen = s[k].size();

          if (t.size() < j + slen)
            continue;

          ull x = d2[j + slen - 1];
          if (j)
            x -= d2[j - 1] * A[slen];

          if (d1[k] != x)
            continue;

          dp[i | (1 << k)][j + slen] = true;
        }
      }
    }

    int ans = 0;

    for (int i = 0; i <= t.size(); i++)
      ans += dp[(1 << n) - 1][i];

    cout << ans << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define N 12
#define S 5005
#define M 1777771
using namespace std;
typedef unsigned long long ull;

bool dp[(1 << N)][S];
int n, m;
string s[N], t;

ull d1[N], d2[S], A[30];

int main() {

  while (1) {

    cin >> n >> m;
    if (!n && !m)
      break;

    for (int i = 0; i < n; i++)
      cin >> s[i];

    t = "";

    for (int i = 0; i < m; i++) {
      string tmp;
      cin >> tmp;
      t += tmp;
    }

    for (int i = 0; i < n; i++) {
      ull hash = s[i][0];
      for (int j = 1; j < s[i].size(); j++)
        hash = hash * M + s[i][j];
      d1[i] = hash;
    }

    d2[0] = t[0];
    for (int i = 1; i < t.size(); i++)
      d2[i] = d2[i - 1] * M + t[i];

    memset(dp, 0, sizeof(dp));

    for (int i = 0; i < t.size(); i++)
      dp[0][i] = true;

    A[0] = 1;
    for (int i = 1; i < 30; i++)
      A[i] = A[i - 1] * M;

    for (int i = 0; i < (1 << n); i++) {

      for (int j = 0; j < t.size(); j++) {

        if (!dp[i][j])
          continue;

        for (int k = 0; k < n; k++) {

          if (i & (1 << k))
            continue;

          int slen = s[k].size();

          if (t.size() < j + slen)
            continue;

          ull x = d2[j + A[slen] - 1];
          if (j)
            x -= d2[j - 1] * A[slen];

          if (d1[k] != x)
            continue;

          dp[i | (1 << k)][j + slen] = true;
        }
      }
    }

    int ans = 0;

    for (int i = 0; i <= t.size(); i++)
      ans += dp[(1 << n) - 1][i];

    cout << ans << endl;
  }

  return 0;
}
Problem p00870 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const ull B = 1e9 + 7;

ull StoH(string a) {
  ull res = 0;
  for (int i = 0; i < a.size(); i++)
    res = res * B + a[i];
  return res;
}

vector<bool> search(string &a, string &key) {
  vector<bool> res(a.size(), 0);
  ull klen = key.size(), h = StoH(a.substr(0, klen)), target = StoH(key), t = 1;
  for (int i = 0; i < klen; i++)
    t *= B;
  for (int i = klen; i < a.size(); i++) {
    if (h == target)
      res[i - klen] = 1;
    h = h * B + a[i] - a[i - klen] * t;
  }
  if (h == target)
    res[a.size() - klen] = 1;
  return res;
}

int n, m, len;
string e[12];
vector<bool> es[12];
bool used[12], mem[1 << 12];

bool check(int idx, int sta) {
  if (sta == (1 << n) - 1)
    return 1;
  if (mem[sta])
    return 0;
  mem[sta] = 1;

  for (int i = 0; i < n; i++) {
    if (used[i] || !es[i][idx])
      continue;
    used[i] = 1;
    if (check(idx + e[i].size(), sta + (1 << i)))
      return 1;
    used[i] = 0;
  }
  return 0;
}

int main() {
  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;

    len = 0;
    for (int i = 0; i < n; i++)
      cin >> e[i], len += e[i].size();

    string mp;
    for (int i = 0; i < m; i++) {
      string tmp;
      cin >> tmp;
      mp += tmp;
    }

    for (int i = 0; i < n; i++)
      es[i] = search(mp, e[i]);
    int ans = 0;
    for (int i = 0; i < mp.size() - len + 1; i++) {
      memset(used, 0, sizeof(used));
      memset(mem, 0, sizeof(mem));
      if (check(i, 0))
        ans++;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const ull B = 1e9 + 7;

ull StoH(string a) {
  ull res = 0;
  for (int i = 0; i < a.size(); i++)
    res = res * B + a[i];
  return res;
}

vector<bool> search(string &a, string &key) {
  vector<bool> res(a.size(), 0);
  ull klen = key.size(), h = StoH(a.substr(0, klen)), target = StoH(key), t = 1;
  for (int i = 0; i < klen; i++)
    t *= B;
  for (int i = klen; i < a.size(); i++) {
    if (h == target)
      res[i - klen] = 1;
    h = h * B + a[i] - a[i - klen] * t;
  }
  if (h == target)
    res[a.size() - klen] = 1;
  return res;
}

int n, m, len;
string e[12];
vector<bool> es[12];
bool used[12];
bool check(int idx, int cnt) {
  if (cnt == n)
    return 1;
  for (int i = 0; i < n; i++) {
    if (used[i] || !es[i][idx])
      continue;
    used[i] = 1;
    if (check(idx + e[i].size(), cnt + 1))
      return 1;
    used[i] = 0;
  }
  return 0;
}

int main() {
  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;
    len = 0;
    for (int i = 0; i < n; i++)
      cin >> e[i], len += e[i].size();

    string mp;
    for (int i = 0; i < m; i++) {
      string tmp;
      cin >> tmp;
      mp += tmp;
    }
    for (int i = 0; i < n; i++)
      es[i] = search(mp, e[i]);

    int ans = 0;
    for (int i = 0; i < mp.size() - len + 1; i++) {
      memset(used, 0, sizeof(used));
      if (check(i, 0))
        ans++;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00870 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define MAX 5001
typedef unsigned long long ull;
ull B = 9999973;

int n, m;
string a[12];
ull b[12];

vector<int> G[MAX];
bool dp[MAX][(1 << 12)];
string s, tmp;

int main() {
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < MAX; i++)
      G[i].clear();
    memset(b, 0, sizeof(b));
    memset(dp, 0, sizeof(dp));
    s = "";
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      for (int j = 0; j < (int)a[i].size(); j++)
        b[i] = b[i] * B + a[i][j];
    }
    for (int i = 0; i < m; i++)
      cin >> tmp, s += tmp;

    for (int i = 0; i < n; i++) {
      int size = a[i].size();
      ull C = 1, key = 0;
      for (int j = 0; j < (int)s.size(); j++) {
        key = key * B + s[j];
        if (j - size >= 0)
          key -= s[j - size] * C;
        else
          C *= B;
        if (key == b[i])
          G[j - size + 1].push_back(i);
      }
    }

    ull ans = 0;
    for (int i = 0; i < (int)s.size(); i++) {
      dp[i][0] = true;
      for (int S = 0; S < (1 << n); S++) {
        if (dp[i][S] == false)
          continue;
        for (int j = 0; j < (int)G[i].size(); j++) {
          int id = G[i][j];
          int size = a[id].size();
          if (S >> id & 1)
            continue;
          dp[i + size][S | (1 << id)] = true;
        }
      }
      ans += dp[i][(1 << n) - 1];
    }
    if (dp[s.size()][(1 << n) - 1])
      ans++;
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MAX 5001
typedef unsigned long long ull;
ull B = 9999973;

int n, m;
string a[12];
ull b[12];

vector<int> G[MAX];
ull dp[MAX][(1 << 12)];
string s, tmp;

int main() {
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < MAX; i++)
      G[i].clear();
    memset(b, 0, sizeof(b));
    memset(dp, 0, sizeof(dp));
    s = "";
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      for (int j = 0; j < (int)a[i].size(); j++)
        b[i] = b[i] * B + a[i][j];
    }
    for (int i = 0; i < m; i++)
      cin >> tmp, s += tmp;

    for (int i = 0; i < n; i++) {
      int size = a[i].size();
      ull C = 1, key = 0;
      for (int j = 0; j < (int)s.size(); j++) {
        key = key * B + s[j];
        if (j - size >= 0)
          key -= s[j - size] * C;
        else
          C *= B;
        if (key == b[i])
          G[j - size + 1].push_back(i);
      }
    }

    ull ans = 0;
    for (int i = 0; i < (int)s.size(); i++) {
      dp[i][0] = 1;
      for (int S = 0; S < (1 << n); S++) {
        for (int j = 0; j < (int)G[i].size(); j++) {
          int id = G[i][j];
          int size = a[id].size();
          if (S >> id & 1)
            continue;
          dp[i + size][S | (1 << id)] += dp[i][S];
        }
      }
      ans += (dp[i][(1 << n) - 1] > 0 ? 1 : 0);
    }
    if (dp[s.size()][(1 << n) - 1] > 0)
      ans++;
    cout << ans << endl;
  }
  return 0;
}
Problem p00870 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int n;
int m;
string t;
char buf[114514];
string des[114514];
bool dp[5001][1 << 12];

int main() {
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      return 0;

    for (int i = 0; i < n; i++) {
      scanf(" %s", buf);
      des[i] = buf;
    }

    t = "";
    for (int i = 0; i < m; i++) {
      scanf(" %s", buf);
      t += buf;
    }

    int lim = 1 << n;
    fill(dp[0], dp[t.size() + 1], false);
    for (int i = 0; i < t.size(); i++) {
      for (int g = 0; g < n; g++) {
        int prev = i + 1 - des[g].size();
        if (prev < 0)
          continue;
        if (t.substr(prev, des[g].size()) != des[g])
          continue;

        for (int st = 0; st < lim; st++) {
          if (st >> g & 1)
            continue;
          dp[i + 1][st | (1 << g)] |= dp[prev][st];
        }
        dp[i + 1][1 << g] = true;
      }
    }

    int ans = 0;
    for (int i = 0; i <= t.size(); i++) {
      if (dp[i][lim - 1])
        ans++;
    }
    printf("%d\\n", ans);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int n;
int m;
string t;
char buf[114514];
string des[114514];
bool dp[1 << 12][5001];

int main() {
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      return 0;

    for (int i = 0; i < n; i++) {
      scanf(" %s", buf);
      des[i] = buf;
    }

    t = "";
    for (int i = 0; i < m; i++) {
      scanf(" %s", buf);
      t += buf;
    }

    int lim = 1 << n;
    fill(dp[0], dp[lim], false);
    for (int i = 0; i < t.size(); i++) {
      for (int g = 0; g < n; g++) {
        int prev = i + 1 - des[g].size();
        if (prev < 0)
          continue;
        if (t.substr(prev, des[g].size()) != des[g])
          continue;

        for (int st = 0; st < lim; st++) {
          if (st >> g & 1)
            continue;
          dp[st | (1 << g)][i + 1] |= dp[st][prev];
        }
        dp[1 << g][i + 1] = true;
      }
    }

    int ans = 0;
    for (int i = 0; i <= t.size(); i++) {
      if (dp[lim - 1][i])
        ans++;
    }
    printf("%d\\n", ans);
  }
}
Problem p00870 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define fs first
#define sc second
#define pb push_back
#define show(x) cout << #x << " " << x << endl
int N, M;
bool dp[5001][1 << 12];
bool b[5001][12];
string a[12];
int main() {
  while (true) {
    cin >> N >> M;
    if (N == 0)
      break;
    rep(i, N) cin >> a[i];
    string s;
    rep(i, M) {
      string o;
      cin >> o;
      s += o;
    }
    M = s.size();
    rep(i, M + 1) rep(j, 1 << N) dp[i][j] = 0;
    rep(i, M + 1) dp[i][0] = 1;
    rep(i, M + 1) rep(j, N) {
      b[i][j] = 0;
      if (i + a[j].size() <= M && s.substr(i, a[j].size()) == a[j])
        b[i][j] = 1;
    }
    rep(i, M) {
      rep(j, 1 << N) {
        if (!dp[i][j])
          continue;
        bool is[12];
        rep(k, N) {
          if ((j >> k) & 1)
            continue;
          if (b[i][k])
            dp[i + a[k].size()][j + (1 << k)] = 1;
        }
      }
    }
    int ans = 0;
    rep(i, M + 1) if (dp[i][(1 << N) - 1]) ans++;
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define fs first
#define sc second
#define pb push_back
#define show(x) cout << #x << " " << x << endl
int N, M;
bool dp[5001][1 << 12];
string a[12];
int main() {
  while (true) {
    cin >> N >> M;
    if (N == 0)
      break;
    rep(i, N) cin >> a[i];
    string s;
    rep(i, M) {
      string o;
      cin >> o;
      s += o;
    }
    M = s.size();
    rep(i, M + 1) rep(j, 1 << N) dp[i][j] = 0;
    rep(i, M + 1) dp[i][0] = 1;
    rep(i, M) {
      rep(j, 1 << N) {
        if (!dp[i][j])
          continue;
        bool is[12];
        rep(k, N) is[k] = (j >> k) & 1;
        rep(k, N) {
          if (is[k])
            continue;
          if (i + a[k].size() > M)
            continue;
          if (s.substr(i, a[k].size()) == a[k])
            dp[i + a[k].size()][j + (1 << k)] = 1;
        }
      }
    }
    int ans = 0;
    rep(i, M + 1) if (dp[i][(1 << N) - 1]) ans++;
    cout << ans << endl;
  }
}
Problem p00870 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

string e[12];
int idx[5010];
bool dp[5010][1 << 12];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
#ifdef LOCAL
  std::ifstream in("in");
  std::cin.rdbuf(in.rdbuf());
#endif

  int n, m;
  while (cin >> n >> m, n) {
    map<string, int> sm;
    for (int i = 0; i < n; i++) {
      cin >> e[i];
      sm[e[i]]++;
    }
    string s;
    for (int i = 0; i < m; i++) {
      string t;
      cin >> t;
      s += t;
    }
    int L = s.size();
    for (int i = 0; i < L; i++) {
      idx[i] = 0;
    }
    for (int i = 0; i < n; i++) {
      int len = e[i].length();
      for (int j = 0; j + len <= L; j++) {
        if (e[i] == s.substr(j, len))
          idx[j] |= 1 << i;
      }
    }

    memset(dp, 0, sizeof dp);
    for (int i = 0; i < L + 1; i++) {
      dp[i][0] = 1;
    }
    for (int i = 0; i < L; i++) {
      for (int j = 0; j < 1 << n; j++) {
        if (dp[i][j] == 0)
          continue;
        for (int k = 0; k < n; k++) {
          if (idx[i] >> k & 1) {
            if (j >> k & 1)
              continue;
            int len = e[k].length();
            dp[i + len][j | (1 << k)] = true;
          }
        }
      }
    }

    ll ans = 0;
    for (int i = 0; i <= L; i++) {
      ans += dp[i][(1 << n) - 1];
    }
    /*for(auto v : sm){
            if(v.second >= 2){
                    for(int i = 2; i <= v.second; i++){
                            ans /= i;
                    }
            }
    }*/
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

string e[12];
int idx[5010];
ll dp[5010][1 << 12];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
#ifdef LOCAL
  std::ifstream in("in");
  std::cin.rdbuf(in.rdbuf());
#endif

  int n, m;
  while (cin >> n >> m, n) {
    map<string, int> sm;
    for (int i = 0; i < n; i++) {
      cin >> e[i];
      sm[e[i]]++;
    }
    string s;
    for (int i = 0; i < m; i++) {
      string t;
      cin >> t;
      s += t;
    }
    int L = s.size();
    for (int i = 0; i < L; i++) {
      idx[i] = 0;
    }
    for (int i = 0; i < n; i++) {
      int len = e[i].length();
      for (int j = 0; j + len <= L; j++) {
        if (e[i] == s.substr(j, len))
          idx[j] |= 1 << i;
      }
    }

    memset(dp, 0, sizeof dp);
    for (int i = 0; i < L + 1; i++) {
      dp[i][0] = 1;
    }
    for (int i = 0; i < L; i++) {
      for (int j = 0; j < 1 << n; j++) {
        if (dp[i][j] == 0)
          continue;
        for (int k = 0; k < n; k++) {
          if (idx[i] >> k & 1) {
            if (j >> k & 1)
              continue;
            int len = e[k].length();
            dp[i + len][j | (1 << k)] += dp[i][j];
          }
        }
      }
    }

    ll ans = 0;
    for (int i = 0; i <= L; i++) {
      ans += dp[i][(1 << n) - 1];
    }
    for (auto v : sm) {
      if (v.second >= 2) {
        for (int i = 2; i <= v.second; i++) {
          ans /= i;
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p00870 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
ll dp[32][1 << 12];

int main() {
  ios_base::sync_with_stdio(false);
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }
    vector<string> patterns(n);
    for (int i = 0; i < n; ++i) {
      cin >> patterns[i];
    }
    string text;
    for (int i = 0; i < m; ++i) {
      string line;
      cin >> line;
      text += line;
    }
    const int length = text.size();
    vector<int> match(length);
    for (int i = 0; i < n; ++i) {
      const int p = patterns[i].size();
      for (int j = 0; j + p <= length; ++j) {
        int flag = 1;
        for (int k = 0; flag && k < p; ++k) {
          if (text[j + k] != patterns[i][k]) {
            flag = 0;
          }
        }
        match[j] |= (flag << i);
      }
    }
    memset(dp, 0, sizeof(dp));
    ll answer = 0;
    for (int i = 0; i < length; ++i) {
      const int cur = i & 31, next = (i - 1) & 31;
      for (int j = 0; j < (1 << n); ++j) {
        dp[next][j] = 0;
      }
      dp[cur][0] = 1;
      for (int j = 0; j + 1 < (1 << n); ++j) {
        int mask = match[i] & ~j;
        while (mask != 0) {
          const int shift = __builtin_ctz(mask);
          mask &= mask - 1;
          const int a = (i + patterns[shift].size()) & 31;
          const int b = j | (1 << shift);
          dp[a][b] += dp[cur][j];
        }
      }
      if (dp[(cur + 1) & 31][(1 << n) - 1]) {
        ++answer;
      }
    }
    cout << answer << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
ll dp[5001][1 << 12];

int main() {
  ios_base::sync_with_stdio(false);
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }
    vector<string> patterns(n);
    for (int i = 0; i < n; ++i) {
      cin >> patterns[i];
    }
    string text;
    for (int i = 0; i < m; ++i) {
      string line;
      cin >> line;
      text += line;
    }
    const int length = text.size();
    vector<int> match(length);
    for (int i = 0; i < n; ++i) {
      const int p = patterns[i].size();
      for (int j = 0; j + p <= length; ++j) {
        int flag = 1;
        for (int k = 0; flag && k < p; ++k) {
          if (text[j + k] != patterns[i][k]) {
            flag = 0;
          }
        }
        match[j] |= (flag << i);
      }
    }
    memset(dp, 0, sizeof(dp));
    ll answer = 0;
    for (int i = 0; i < length; ++i) {
      dp[i][0] = 1;
      for (int j = 0; j + 1 < (1 << n); ++j) {
        int mask = match[i] & ~j;
        while (mask != 0) {
          const int shift = __builtin_ctz(mask);
          mask &= mask - 1;
          const int a = i + patterns[shift].size();
          const int b = j | (1 << shift);
          dp[a][b] += dp[i][j];
        }
      }
      if (dp[i + 1][(1 << n) - 1]) {
        ++answer;
      }
    }
    cout << answer << endl;
  }
  return 0;
}