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

#define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
// #define int ll
// #define endl "\\n"
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (ll i = a; i < ll(b); i++)
#define rep(i, b) loop(i, 0, b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template <class T> ostream &operator<<(ostream &os, vector<T> const &);
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
                                                  tuple<T...> const &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
                                                 tuple<T...> const &t) {
  os << (n == 0 ? "" : " ") << get<n>(t);
  _ot<n + 1>(os, t);
}
template <class... T> ostream &operator<<(ostream &os, tuple<T...> const &t) {
  _ot<0>(os, t);
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
  return os << "(" << p.first << ", " << p.second << ") ";
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  rep(i, v.size()) os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
  return os;
}
template <class T> inline bool chmax(T &x, T const &y) {
  return x < y ? x = y, true : false;
}
template <class T> inline bool chmin(T &x, T const &y) {
  return x > y ? x = y, true : false;
}
#ifdef DEBUG
#define dump(...)                                                              \\
  (cerr << #__VA_ARGS__ << " = " << mt(__VA_ARGS__) << " [" << __LINE__ << "]" \\
        << endl)
#else
#define dump(...)
#endif
// ll const mod = 1000000007;
// ll const inf = 1LL<<60;

bool dp[6000][1 << 13];

ll const B = 1000000007;

int solve(vector<string> &ss, string &t) {
  int n = ss.size();
  int len = t.size();

  vector<ull> shash(n);
  rep(i, n) {
    ull x = 0;
    rep(j, ss[i].size()) { x = x * B + ss[i][j]; }
    shash[i] = x;
  }
  vector<ull> pow(len + 1), thash(len + 1);
  pow[0] = 1;
  thash[0] = 0;
  rep(i, len) {
    pow[i + 1] = pow[i] * B;
    thash[i + 1] = thash[i] * B + t[i];
  }

  rep(i, len + 1) rep(j, 1 << n) dp[i][j] = false;
  rep(i, len + 1) dp[i][0] = true;
  rep(pos, len) {
    rep(mask, 1 << n) {
      if (!dp[pos][mask])
        continue;
      rep(i, n) {
        if (mask >> i & 1)
          continue;
        if (pos + ss[i].size() > t.size())
          continue;
        ull h1 = shash[i];
        ull h2 = thash[pos + ss[i].size()] - thash[pos] * pow[ss[i].size()];
        // if(h1 != h2) continue;
        dp[pos + ss[i].size()][mask | 1 << i] |=
            h1 == h2; // t.substr(pos,ss[i].size()) == ss[i];
      }
    }

    // dump(pos);
    // rep(i,1<<n){
    //     dump(bitset<3>(i),dp[pos][i]);
    // }
  }

  int ans = 0;
  rep(i, len + 1) {
    if (dp[i][(1 << n) - 1]) {
      ++ans;
    }
  }
  return ans;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  while (cin >> n >> m && n) {
    vector<string> s(n);
    rep(i, n) cin >> s[i];
    string t;
    rep(i, m) {
      string x;
      cin >> x;
      t += x;
    }
    cout << solve(s, t) << endl;
  }
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
// #define int ll
// #define endl "\\n"
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (ll i = a; i < ll(b); i++)
#define rep(i, b) loop(i, 0, b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template <class T> ostream &operator<<(ostream &os, vector<T> const &);
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
                                                  tuple<T...> const &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
                                                 tuple<T...> const &t) {
  os << (n == 0 ? "" : " ") << get<n>(t);
  _ot<n + 1>(os, t);
}
template <class... T> ostream &operator<<(ostream &os, tuple<T...> const &t) {
  _ot<0>(os, t);
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
  return os << "(" << p.first << ", " << p.second << ") ";
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  rep(i, v.size()) os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
  return os;
}
template <class T> inline bool chmax(T &x, T const &y) {
  return x < y ? x = y, true : false;
}
template <class T> inline bool chmin(T &x, T const &y) {
  return x > y ? x = y, true : false;
}
#ifdef DEBUG
#define dump(...)                                                              \\
  (cerr << #__VA_ARGS__ << " = " << mt(__VA_ARGS__) << " [" << __LINE__ << "]" \\
        << endl)
#else
#define dump(...)
#endif
// ll const mod = 1000000007;
// ll const inf = 1LL<<60;

bool dp[6000][1 << 13];

ll const B = 1000000007;

int solve(vector<string> &ss, string &t) {
  int n = ss.size();
  int len = t.size();

  vector<ull> shash(n);
  rep(i, n) {
    ull x = 0;
    rep(j, ss[i].size()) { x = x * B + ss[i][j]; }
    shash[i] = x;
  }
  vector<ull> pow(len + 1), thash(len + 1);
  pow[0] = 1;
  thash[0] = 0;
  rep(i, len) {
    pow[i + 1] = pow[i] * B;
    thash[i + 1] = thash[i] * B + t[i];
  }

  rep(i, len + 1) rep(j, 1 << n) dp[i][j] = false;
  rep(i, len + 1) dp[i][0] = true;
  rep(pos, len) {
    rep(mask, 1 << n) {
      if (!dp[pos][mask])
        continue;
      rep(i, n) {
        if (mask >> i & 1)
          continue;
        if (pos + ss[i].size() > t.size())
          continue;
        ull h1 = shash[i];
        ull h2 = thash[pos + ss[i].size()] - thash[pos] * pow[ss[i].size()];
        if (h1 != h2)
          continue;
        dp[pos + ss[i].size()][mask | 1 << i] =
            t.substr(pos, ss[i].size()) == ss[i];
      }
    }

    // dump(pos);
    // rep(i,1<<n){
    //     dump(bitset<3>(i),dp[pos][i]);
    // }
  }

  int ans = 0;
  rep(i, len + 1) {
    if (dp[i][(1 << n) - 1]) {
      ++ans;
    }
  }
  return ans;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  while (cin >> n >> m && n) {
    vector<string> s(n);
    rep(i, n) cin >> s[i];
    string t;
    rep(i, m) {
      string x;
      cin >> x;
      t += x;
    }
    cout << solve(s, t) << endl;
  }
}