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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

bool table[12][5000];
int n;
string text;
string str[12];
bool visited[1 << 12];

bool dfs(int S, int now) {
  if (visited[S])
    return 0;
  visited[S] = 1;
  if (S == (1 << n) - 1)
    return 1;
  if (now >= text.size())
    return 0;
  REP(i, n) {
    if (S >> i & 1)
      continue;
    if (table[i][now] && dfs(S | 1 << i, now + str[i].size()))
      return 1;
  }
  return 0;
}

int main() {
  int m;
  while (cin >> n >> m, n || m) {
    int len = 0;
    REP(i, n) {
      cin >> str[i];
      len += str[i].size();
    }
    text.clear();
    REP(i, m) {
      string tmp;
      cin >> tmp;
      text += tmp;
    }
    memset(table, 0, sizeof(table));
    REP(i, n) {
      REP(j, text.size() - str[i].size() + 1) {
        if (str[i] == text.substr(j, str[i].size()))
          table[i][j] = 1;
      }
    }
    int ans = 0;
    REP(i, text.size() - len + 1) {
      memset(visited, 0, sizeof(visited));
      ans += dfs(0, i);
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

bool table[12][5000];
int n;
string text;
string str[12];

bool dfs(int S, int now) {
  if (S == (1 << n) - 1)
    return 1;
  if (now >= text.size())
    return 0;
  REP(i, n) {
    if (S >> i & 1)
      continue;
    if (table[i][now] && dfs(S | 1 << i, now + str[i].size()))
      return 1;
  }
  return 0;
}

int main() {
  int m;
  while (cin >> n >> m, n || m) {
    int len = 0;
    REP(i, n) {
      cin >> str[i];
      len += str[i].size();
    }
    text.clear();
    REP(i, m) {
      string tmp;
      cin >> tmp;
      text += tmp;
    }
    memset(table, 0, sizeof(table));
    REP(i, n) {
      REP(j, text.size() - str[i].size() + 1) {
        if (str[i] == text.substr(j, str[i].size()))
          table[i][j] = 1;
      }
    }
    int ans = 0;
    map<string, bool> mp;
    REP(i, text.size() - len + 1) {
      if (mp.count(text.substr(i, len))) {
        if (mp[text.substr(i, len)])
          ans++;
      } else {
        if (dfs(0, i)) {
          mp[text.substr(i, len)] = 1;
          ans++;
        } else
          mp[text.substr(i, len)] = 0;
      }
    }
    cout << ans << endl;
  }
}