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

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

typedef long long ll;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define each(itr, c)                                                           \\
  for (__typeof(c.begin()) itr = c.begin(); itr != c.end(); ++itr)
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second

const int N = 1000000;
double dp[N], dp2[N];

int main() {
  int n, m, k;
  while (cin >> n >> m >> k, n | m | k) {
    memset(dp, 0, sizeof(dp));
    dp[0] = 1;

    rep(i, n) {
      memset(dp2, 0, sizeof(dp2));

      for (int j = i; j <= m * i; ++j) {
        for (int k = 1; k <= m; ++k) {
          if (j + k >= N)
            break;
          dp2[j + k] += dp[j] / m;
        }
      }

      rep(j, N) dp[j] = dp2[j];
    }

    double ans = 0;
    rep(i, N) ans += max(1, i - k) * dp[i];

    printf("%.10lf\\n", ans);
  }
  return 0;
}

Error: MLE

Fixed submission:

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

typedef long long ll;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define each(itr, c)                                                           \\
  for (__typeof(c.begin()) itr = c.begin(); itr != c.end(); ++itr)
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second

const int N = 5000000;
double dp[N], dp2[N];

int main() {
  int n, m, k;
  while (cin >> n >> m >> k, n | m | k) {
    memset(dp, 0, sizeof(dp));
    dp[0] = 1;

    rep(i, n) {
      memset(dp2, 0, sizeof(dp2));

      for (int j = i; j <= m * i; ++j) {
        for (int k = 1; k <= m; ++k) {
          if (j + k >= N)
            break;
          dp2[j + k] += dp[j] / m;
        }
      }

      rep(j, N) dp[j] = dp2[j];
    }

    double ans = 0;
    rep(i, N) ans += max(1, i - k) * dp[i];

    printf("%.10lf\\n", ans);
  }
  return 0;
}
Problem p00865 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define INF 100000000
#define MAXN 100
#define MAXM 10000

typedef long long ll;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

double dp[MAXN][MAXM]; // dp[n][m]はn回サイコロをふったとき合計値がmになる確率

int n, m, k;

int main(void) {
  while (cin >> n >> m >> k) {
    if (n == 0 && m == 0 && k == 0)
      break;
    for (int i = 0; i < MAXN; i++)
      for (int j = 0; j < MAXM; j++)
        dp[i][j] = 0;

    dp[0][0] = 1;
    int ma = n * m;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= ma; j++) {
        for (int k = 1; k <= m; k++) {
          if (j - k >= 0) {
            dp[i][j] += dp[i - 1][j - k] / m;
          }
        }
      }
    }
    // for (int i = 0; i <= ma; i++) cout << dp[n][i] << endl;
    double ans = 0;
    for (int i = 1; i <= ma; i++) {
      if (i <= k)
        ans += 1.0 * dp[n][i];
      else
        ans += (i - k) * dp[n][i];
    }
    printf("%.10f\\n", ans);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define INF 100000000
#define MAXN 1000
#define MAXM 10000

typedef long long ll;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

double dp[MAXN][MAXM]; // dp[n][m]はn回サイコロをふったとき合計値がmになる確率

int n, m, k;

int main(void) {
  while (cin >> n >> m >> k) {
    if (n == 0 && m == 0 && k == 0)
      break;
    for (int i = 0; i < MAXN; i++)
      for (int j = 0; j < MAXM; j++)
        dp[i][j] = 0;

    dp[0][0] = 1;
    int ma = n * m;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= ma; j++) {
        for (int k = 1; k <= m; k++) {
          if (j - k >= 0) {
            dp[i][j] += dp[i - 1][j - k] / m;
          }
        }
      }
    }
    // for (int i = 0; i <= ma; i++) cout << dp[n][i] << endl;
    double ans = 0;
    for (int i = 1; i <= ma; i++) {
      if (i <= k)
        ans += 1.0 * dp[n][i];
      else
        ans += (i - k) * dp[n][i];
    }
    printf("%.10f\\n", ans);
  }
  return 0;
}
Problem p00865 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const int INF = 100000000;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
using namespace std;
typedef pair<int, int> P;
typedef long long ll;

const int MAX_SIZE = 100005;
int n, m, k;
double p[2][MAX_SIZE];
void solve() {
  rep(i, MAX_SIZE) p[0][i] = p[1][i] = 0.0;

  rep(i, m) p[0][i + 1] = 1.0 / m;
  rep(i, n - 1) {
    rep(j, MAX_SIZE) {
      rep(k, m) {
        if (k + j + 1 >= MAX_SIZE)
          continue;
        p[1][k + j + 1] += p[0][j] / m;
      }
    }
    rep(j, MAX_SIZE) {
      p[0][j] = p[1][j];
      p[1][j] = 0.0;
    }
  }
  double ans = 0.0;

  rep(i, m * n) {
    int b = max(1, i - k + 1);
    // cout<<i+1<<" : "<<p[0][i+1]<<","<<b<<endl;
    ans += p[0][i + 1] * b;
  }

  printf("%.8f\\n", ans);
}

int main() {
  while (cin >> n >> m >> k) {
    if (n + m + k == 0)
      break;
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const int INF = 100000000;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
using namespace std;
typedef pair<int, int> P;
typedef long long ll;

const int MAX_SIZE = 1000006;
int n, m, k;
double p[2][MAX_SIZE];
void solve() {
  rep(i, MAX_SIZE) p[0][i] = p[1][i] = 0.0;

  rep(i, m) p[0][i + 1] = 1.0 / m;
  rep(i, n - 1) {
    rep(j, MAX_SIZE) {
      rep(k, m) {
        if (k + j + 1 >= MAX_SIZE)
          continue;
        p[1][k + j + 1] += p[0][j] / m;
      }
    }
    rep(j, MAX_SIZE) {
      p[0][j] = p[1][j];
      p[1][j] = 0.0;
    }
  }
  double ans = 0.0;

  rep(i, m * n) {
    int b = max(1, i - k + 1);
    // cout<<i+1<<" : "<<p[0][i+1]<<","<<b<<endl;
    ans += p[0][i + 1] * b;
  }

  printf("%.8f\\n", ans);
}

int main() {
  while (cin >> n >> m >> k) {
    if (n + m + k == 0)
      break;
    solve();
  }
  return 0;
}
Problem p00870 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

void KMP_build(const char *pttn, int *fail) {
  int n = strlen(pttn);
  fail[0] = -1;
  for (int i = 1, j = -1; i <= n; i++) {
    while (j >= 0 && pttn[j] != pttn[i - 1])
      j = fail[j];
    fail[i] = ++j;
  }
}

void KMP_search(const char *s, const char *pttn, const int *fail, bool *res) {
  int m = strlen(s), n = strlen(pttn), j = 0;
  rep(i, m) {
    while (j >= 0 && s[i] != pttn[j])
      j = fail[j];
    j++;
    if (j == n) {
      res[i - n + 1] = true;
      j = fail[j];
    }
  }
}

int L; // s の長さ

int n, len[12];
bool check[12][5000]; // check[i][j] := ( s[j..j+len[i]) == key[i] )

char dp[5000][1 << 12]; // メモリ節約のため char
bool dfs(int x, int S) {
  if (S == (1 << n) - 1)
    return true;
  if (x >= L)
    return false;

  if (dp[x][S] != -1)
    return dp[x][S];

  rep(i, n) if (check[i][x] && (S >> i & 1) == 0 &&
                dfs(x + len[i], S | 1 << i)) return dp[x][S] = true;
  return dp[x][S] = false;
}

int main() {
  for (int m; scanf("%d%d", &n, &m), n;) {
    char key[12][32];
    rep(i, n) scanf("%s", key[i]), len[i] = strlen(key[i]);

    L = 0;
    char s[5001];
    rep(i, m) {
      scanf("%s", s + L);
      L += strlen(s + L);
    }

    rep(i, n) {
      rep(x, L) check[i][x] = false;
      int fail[5001];
      KMP_build(key[i], fail);
      KMP_search(s, key[i], fail, check[i]);
    }

    int ans = 0;
    memset(dp, -1, sizeof dp);
    rep(x, L) if (dfs(x, 0)) ans++;
    printf("%d\\n", ans);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

void KMP_build(const char *pttn, int *fail) {
  int n = strlen(pttn);
  fail[0] = -1;
  for (int i = 1, j = -1; i <= n; i++) {
    while (j >= 0 && pttn[j] != pttn[i - 1])
      j = fail[j];
    fail[i] = ++j;
  }
}

void KMP_search(const char *s, const char *pttn, const int *fail, bool *res) {
  int m = strlen(s), n = strlen(pttn), j = 0;
  rep(i, m) {
    while (j >= 0 && s[i] != pttn[j])
      j = fail[j];
    j++;
    if (j == n) {
      res[i - n + 1] = true;
      j = fail[j];
    }
  }
}

int L; // s の長さ

int n, len[12];
bool check[12][5000]; // check[i][j] := ( s[j..j+len[i]) == key[i] )

bool dfs(int x, int S) {
  if (S == (1 << n) - 1)
    return true;
  if (x >= L)
    return false;

  rep(i, n) if (check[i][x] && (S >> i & 1) == 0 &&
                dfs(x + len[i], S | 1 << i)) return true;
  return false;
}

int main() {
  for (int m; scanf("%d%d", &n, &m), n;) {
    char key[12][32];
    rep(i, n) scanf("%s", key[i]), len[i] = strlen(key[i]);

    L = 0;
    char s[5001];
    rep(i, m) {
      scanf("%s", s + L);
      L += strlen(s + L);
    }

    rep(i, n) {
      rep(x, L) check[i][x] = false;
      int fail[5001];
      KMP_build(key[i], fail);
      KMP_search(s, key[i], fail, check[i]);
    }

    int ans = 0;
    rep(x, L) if (dfs(x, 0)) ans++;
    printf("%d\\n", ans);
  }

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

#include <bits/stdc++.h>
#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)

using namespace std;

int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (!n)
      break;
    vector<string> vs(n);
    REP(i, n) cin >> vs[i];
    int sum = 0;
    string tx;
    REP(cnt, m) {
      string line;
      cin >> line;
      tx += line;
    }
    int l = tx.size();
    vector<vector<bool>> dp(1 << n, vector<bool>(l + 1, false));
    vector<vector<bool>> match(l + 1, vector<bool>(n, false));
    REP(i, l + 1) dp[0][i] = true;
    REP(i, l + 1) REP(j, n) {
      if (j + vs[j].size() > tx.size())
        continue;
      if (tx.substr(i, vs[j].size()) == vs[j])
        match[i][j] = true;
    }
    REP(i, 1 << n) REP(j, n) {
      if ((i >> j) & 1)
        continue;
      int ls = vs[j].size();
      REP(k, l + 1 - ls) {
        if (dp[i][k] && match[k][j]) {
          dp[i | (1 << j)][k + ls] = true;
        }
      }
    }
    REP(i, l + 1) {
      if (dp[(1 << n) - 1][i])
        ++sum;
    }
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)

using namespace std;

int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (!n)
      break;
    vector<string> vs(n);
    REP(i, n) cin >> vs[i];
    int sum = 0;
    string tx;
    REP(cnt, m) {
      string line;
      cin >> line;
      tx += line;
    }
    int l = tx.size();
    vector<vector<bool>> dp(1 << n, vector<bool>(l + 1, false));
    REP(i, l + 1) dp[0][i] = true;
    REP(i, 1 << n) REP(j, n) {
      if ((i >> j) & 1)
        continue;
      int ls = vs[j].size();
      REP(k, l + 1 - ls) {
        if (dp[i][k] && tx.substr(k, ls) == vs[j]) {
          dp[i | (1 << j)][k + ls] = true;
        }
      }
    }
    REP(i, l + 1) {
      if (dp[(1 << n) - 1][i])
        ++sum;
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p00870 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int N;
vector<string> elem;
string text;
bool dp[5050][1 << 12];
bool flg[5050][1 << 12];

bool check(int idx, int n) {
  for (int i = 0; i < elem[n].size(); i++) {
    if (idx + i >= text.size())
      return false;
    if (text[idx + i] != elem[n][i])
      return false;
  }
  return true;
}

bool dfs(int idx, int bit) {
  if (bit == 0)
    return true;
  if (idx >= text.size())
    return false;
  if (flg[idx][bit])
    return dp[idx][bit];
  flg[idx][bit] = true;
  for (int i = 0; i < N; i++) {
    if ((1 << i) & bit) {
      if (check(idx, i)) {
        if (dfs(idx + elem[i].size(), bit & ~(1 << i))) {
          return dp[idx][bit] = true;
        }
      }
    }
  }
  return dp[idx][bit] = false;
}

int main() {
  int m;
  while (cin >> N >> m, N) {
    elem.resize(N);
    for (int i = 0; i < N; i++) {
      cin >> elem[i];
    }
    text = "";
    for (int i = 0; i < m; i++) {
      string str;
      cin >> str;
      text += str;
    }
    for (int i = 0; i <= text.size(); i++) {
      for (int j = 0; j < (1 << N); j++) {
        flg[i][j] = false;
      }
    }
    int ans = 0;
    for (int i = 0; i < text.size(); i++) {
      ans += dfs(i, (1 << N) - 1);
    }
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

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

int N;
vector<string> elem;
string text;
int dp[5050][1 << 12];

bool check(int idx, int n) {
  for (int i = 0; i < elem[n].size(); i++) {
    if (idx + i >= text.size())
      return false;
    if (text[idx + i] != elem[n][i])
      return false;
  }
  return true;
}

int dfs(int idx, int bit) {
  if (bit == 0)
    return 1;
  if (idx >= text.size())
    return 0;
  if (dp[idx][bit] != -1)
    return dp[idx][bit];
  for (int i = 0; i < N; i++) {
    if ((1 << i) & bit) {
      if (check(idx, i)) {
        if (dfs(idx + elem[i].size(), bit & ~(1 << i))) {
          return dp[idx][bit] = 1;
        }
      }
    }
  }
  return dp[idx][bit] = 0;
}

int main() {
  int m;
  while (cin >> N >> m, N) {
    elem.resize(N);
    for (int i = 0; i < N; i++) {
      cin >> elem[i];
    }
    text = "";
    for (int i = 0; i < m; i++) {
      string str;
      cin >> str;
      text += str;
    }
    for (int i = 0; i <= text.size(); i++) {
      for (int j = 0; j < (1 << N); j++) {
        dp[i][j] = -1;
      }
    }
    int ans = 0;
    for (int i = 0; i < text.size(); i++) {
      ans += dfs(i, (1 << N) - 1);
    }
    cout << ans << endl;
  }
}
Problem p00870 (C++) — buggy submission, verdict: Runtime Error

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

bool pos[12][5010] = {};
bool dp[5010][1 << 12];
bool dn[5010][1 << 12];
string p[12];
string s;

int n, m;
bool dfs(int x, int bit) {
  if (bit == 0)
    return 1;
  if (dn[x][bit])
    return dp[x][bit];
  bool ans = 0;
  for (int i = 0; i < n; i++) {
    if ((bit >> i & 1) && pos[i][x]) {
      ans |= dfs(x + p[i].size(), bit ^ (1 << i));
    }
  }
  dn[x][bit] = true;
  return dp[x][bit] = ans;
}

int main() {
  while (cin >> n >> m && n) {
    memset(dp, 0, sizeof(dp));
    memset(dn, 0, sizeof(dn));
    memset(pos, 0, sizeof(pos));

    for (int i = 0; i < n; i++)
      cin >> p[i];
    s = "";
    for (int i = 0; i < m; i++) {
      string c;
      cin >> c;
      s += c;
    }
    for (int j = 0; j < n; j++) {
      for (int i = 0; i + p[j].size() <= s.size(); i++) {
        int f = 1;
        for (int k = 0; k < p[j].size(); k++)
          if (p[j][k] != s[i + k]) {
            f = 0;
            break;
          }
        if (f)
          pos[j][i] = 1;
      }
    }
    int ans = 0;
    for (int i = 0; i < s.size(); i++) {
      if (dfs(i, (1 << n) - 1)) {
        ans++;
      }
    }
    cout << ans << endl;
  }
}

Error: WA

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

int l;
int n, m;

char c[20][30];
char s[5050];
int A[128], B[128];
bool D[20];

int check() {
  int remain = false;
  for (int i = 'a'; i <= 'z'; i++) {
    if (B[i] != A[i]) {
      return false;
    } else {
      remain |= B[i];
    }
  }

  if (!remain)
    return true;

  for (int i = 0; i < n; i++) {
    if (!D[i]) {
      D[i] = true;
      for (int j = 0; c[i][j]; j++)
        B[c[i][j]]--, A[c[i][j]]--;
      int res = check();
      D[i] = false;
      for (int j = 0; c[i][j]; j++)
        B[c[i][j]]++, A[c[i][j]]++;
      if (res)
        return true;
    }
  }
  return false;
}

int main() {
  while (scanf("%d%d", &n, &m) && n) {
    for (int i = 'a'; i <= 'z'; i++)
      B[i] = 0;
    l = 0;
    for (int i = 0; i < n; i++) {
      scanf("%s", c[i]);
      int L = strlen(c[i]);
      for (int j = 0; j < L; j++)
        B[c[i][j]]++;
      l += L;
    }
    int ans = 0;
    int len = 0;

    for (int i = 0; i < m; i++) {
      scanf("%s", s + len);
      if (s[len - 1] == '\\r')
        while (1) {
        }
      len += strlen(s);
    }

    if (len >= l) {
      for (int j = 'a'; j <= 'z'; j++)
        A[j] = 0;
      for (int j = 0; j < l; j++)
        A[s[j]]++;
      for (int j = l; j <= len; j++) {
        if (check()) {
          ans++;
        }
        A[s[j - l]]--;
        A[s[j]]++;
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00870 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int N, M;
vector<string> elms;
string text;
vector<vector<int>> cors;
bool memo[5010][1 << 12];

bool dfs(int si, int s, int l) {
  if (s == (1 << N) - 1)
    return true;
  if (memo[si][s])
    return false;

  for (int i = 0; i < N; i++)
    if ((s >> i & 1) == 0) {
      if (binary_search(cors[i].begin(), cors[i].end(), si))
        if (dfs(si + (int)elms[i].size(), s | (1 << i),
                l + (int)elms[i].size()))
          return true;
    }
  memo[si][s] = true;
  return false;
}

void solve() {
  cors.clear();
  cors.resize(N);
  memset(memo, 0, sizeof(memo));
  for (int i = 0; i < N; i++) {
    string elm = elms[i];
    for (int j = 0; j < text.size(); j++) {
      if (text.substr(j, elm.size()) == elm)
        cors[i].push_back(j);
    }
  }
  int ans = 0;
  for (int i = 0; i < text.size(); i++) {
    ans += dfs(i, 0, 0);
  }
  cout << ans << endl;
}

int main() {
  while (cin >> N >> M, N | M) {
    elms.resize(N);
    for (auto &s : elms)
      cin >> s;
    text = "";
    for (int i = 0; i < M; i++) {
      string t;
      cin >> t;
      text += t;
    }
    solve();
  }

  return 0;
}

Error: MLE

Fixed submission:

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

int N, M;
vector<string> elms;
string text;
vector<vector<int>> cors;
set<pair<int, int>> memo;

bool dfs(int si, int s, int l) {
  if (s == (1 << N) - 1)
    return true;
  if (memo.count(make_pair(si, s)))
    return false;

  for (int i = 0; i < N; i++)
    if ((s >> i & 1) == 0) {
      if (binary_search(cors[i].begin(), cors[i].end(), si))
        if (dfs(si + (int)elms[i].size(), s | (1 << i),
                l + (int)elms[i].size()))
          return true;
    }
  memo.insert(make_pair(si, s));
  return false;
}

void solve() {
  cors.clear();
  cors.resize(N);
  memo.clear();
  for (int i = 0; i < N; i++) {
    string elm = elms[i];
    for (int j = 0; j < text.size(); j++) {
      if (text.substr(j, elm.size()) == elm)
        cors[i].push_back(j);
    }
  }
  int ans = 0;
  for (int i = 0; i < text.size(); i++) {
    ans += dfs(i, 0, 0);
  }
  cout << ans << endl;
}

int main() {
  while (cin >> N >> M, N | M) {
    elms.resize(N);
    for (auto &s : elms)
      cin >> s;
    text = "";
    for (int i = 0; i < M; i++) {
      string t;
      cin >> t;
      text += t;
    }
    solve();
  }

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

#include <bits/stdc++.h>

using namespace std;

int main() {
  int N, M;
  while (cin >> N >> M, N) {
    string S[12], T;
    int sum = 0;
    for (int i = 0; i < N; i++)
      cin >> S[i], sum += S[i].size();
    for (int i = 0; i < M; i++) {
      string q;
      cin >> q;
      T += q;
    }

    vector<int> pos(1 << N, 0);
    for (int i = 0; i < 1 << N; i++) {
      for (int j = 0; j < N; j++)
        if ((i >> j) & 1)
          pos[i] += S[j].size();
    }

    long long ret = 0LL;
    for (int i = 0; i <= (int)T.size() - sum; i++) {
      bitset<1 << 12> dp;
      dp[0] = true;
      for (int j = 0; j < 1 << N; j++) {
        if (!dp[j])
          continue;
        for (int k = 0; k < N; k++) {
          if ((j >> k) & 1)
            continue;
          if (dp[j | (1 << k)])
            continue;
          if ([&]() {
                for (int l = 0; l < S[k].size(); l++) {
                  if (S[k][l] != T[i + l + pos[j]])
                    return (false);
                }
                return (true);
              }())
            dp[j | (1 << k)] = true;
        }
      }
      ret += dp[(1 << N) - 1];
    }
    cout << ret << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int N, M;
  while (cin >> N >> M, N) {
    string S[12], T;
    int sum = 0;
    for (int i = 0; i < N; i++)
      cin >> S[i], sum += S[i].size();
    for (int i = 0; i < M; i++) {
      string q;
      cin >> q;
      T += q;
    }

    int pos[1 << 12] = {};
    for (int i = 0; i < 1 << N; i++) {
      for (int j = 0; j < N; j++)
        if ((i >> j) & 1)
          pos[i] += S[j].size();
    }

    long long ret = 0LL;
    for (int i = 0; i <= (int)T.size() - sum; i++) {
      bool dp[1 << 12] = {};
      dp[0] = true;
      for (int j = 0; j < 1 << N; j++) {
        if (!dp[j])
          continue;
        for (int k = 0; k < N; k++) {
          if ((j >> k) & 1)
            continue;
          dp[j | (1 << k)] |= [&]() {
            for (int l = 0; l < S[k].size(); l++) {
              if (S[k][l] != T[i + l + pos[j]])
                return (false);
            }
            return (true);
          }();
        }
      }
      ret += dp[(1 << N) - 1];
    }
    cout << ret << endl;
  }
}