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

#include <algorithm>
#include <bitset>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 998244353;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop                                                                   \\
  char nyaa;                                                                   \\
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<ld, ld> LDP;
int n, m;
int bs;
string b[12];
string in, s;

queue<int> q;
bool used[1 << 12];
int len[1 << 12];
bool ok[12][5000];

bool valid(int le) {
  rep(i, (1 << n)) { used[i] = false; }
  q.push(0);
  used[0] = true;
  while (!q.empty()) {
    int p = q.front();
    q.pop();
    rep(i, n) {
      if (p & (1 << i))
        continue;
      if (ok[i][le + len[p]]) {
        int np = p ^ (1 << i);
        if (!used[np]) {
          used[np] = true;
          q.push(np);
        }
      }
    }
  }
  return used[(1 << n) - 1];
}
void init() {
  bs = 0;
  s.clear();
  rep(i, 1 << n) { len[i] = 0; }
}
int main() {
  while (cin >> n >> m, n) {
    init();
    rep(i, n) {
      cin >> in;
      bs += in.length();
      b[i] = in;
    }
    rep(i, (1 << n)) {
      rep(j, n) {
        if (i & (1 << j)) {
          len[i] += b[j].length();
        }
      }
    }
    rep(i, m) {
      cin >> in;
      s += in;
    }
    rep(i, n) {
      rep(j, s.length() - b[i].length() + 1) {
        ok[i][j] = (b[i] == s.substr(j, b[i].length()));
      }
    }
    int ans = 0;
    rep(i, s.length() - bs + 1) {
      if (valid(i))
        ans++;
    }
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 998244353;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop                                                                   \\
  char nyaa;                                                                   \\
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<ld, ld> LDP;
int n, m;
int bs;
string b[12];
string in, s;

queue<int> q;
bool used[1 << 12];
int len[1 << 12];
bool valid(int le) {
  rep(i, (1 << n)) { used[i] = false; }
  q.push(0);
  used[0] = true;
  while (!q.empty()) {
    int p = q.front();
    q.pop();
    rep(i, n) {
      if (p & (1 << i))
        continue;
      string u = s.substr(le + len[p], b[i].length());
      if (u == b[i]) {
        int np = p ^ (1 << i);
        if (!used[np]) {
          used[np] = true;
          q.push(np);
        }
      }
    }
  }
  return used[(1 << n) - 1];
}
void init() {
  bs = 0;
  s.clear();
  rep(i, 1 << n) { len[i] = 0; }
}
int main() {
  while (cin >> n >> m, n) {
    init();
    rep(i, n) {
      cin >> in;
      bs += in.length();
      b[i] = in;
    }
    rep(i, (1 << n)) {
      rep(j, n) {
        if (i & (1 << j)) {
          len[i] += b[j].length();
        }
      }
    }
    rep(i, m) {
      cin >> in;
      s += in;
    }
    int ans = 0;
    rep(i, s.length() - bs + 1) {
      if (valid(i))
        ans++;
    }
    cout << ans << endl;
  }
  return 0;
}