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

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
// #include<cctype>
#include <climits>
#include <iostream>
#include <map>
#include <string>
#include <vector>
// #include<list>
#include <algorithm>
#include <deque>
#include <queue>
// #include<numeric>
#include <utility>
// #include<memory>
#include <cassert>
#include <functional>
#include <random>
#include <set>
#include <stack>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

const int MAXN = 16;
const int INF = 100000;
string S[MAXN];
int dp[MAXN][1 << MAXN];
int memo[MAXN][MAXN][22];
int N;

int dfs(int now, int state) {
  int &ret = dp[now][state];
  if (ret >= 0)
    return ret;
  if (state == (1 << N) - 1)
    return ret = 0;
  ret = INF;
  int sz = S[now].size();
  for (int i = 0; i < N; i++)
    if (((state >> i) & 1) == 0) {
      int isz = S[i].size();
      for (int j = max(0, sz + 1 - isz); j <= sz; j++) {
        int ok = memo[now][i][j];
        if (ok) {
          int nstate = state | ok;
          ret = min<int>(ret, (isz - (sz - j)) + dfs(i, nstate));
        }
      }
    }
  return ret;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (cin >> N) {
    if (N == 0)
      break;
    for (int i = 0; i < N; i++)
      cin >> S[i];
    int ans = INF;
    memset(dp, -1, sizeof(dp));
    memset(memo, 0, sizeof(memo));
    for (int n = 0; n < N; n++) {
      int sz = S[n].size();
      for (int i = 0; i < N; i++) {
        int isz = S[i].size();
        for (int j = max(0, sz + 1 - isz); j <= sz; j++) {
          const string s = S[n].substr(j);
          const string t = S[i].substr(0, s.size());
          if (s == t) {
            const string u = s + S[i].substr(t.size());
            int &ok = memo[n][i][j];
            for (int k = 0; k < N; k++) {
              if (u.find(S[k]) != string::npos)
                ok |= 1 << k;
            }
          }
        }
      }
    }
    for (int i = 0; i < N; i++) {
      int state = 0;
      const string u = S[i];
      for (int j = 0; j < N; j++) {
        if (u.find(S[j]) != string::npos)
          state |= 1 << j;
      }
      ans = min<int>(ans, S[i].size() + dfs(i, state));
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
// #include<cctype>
#include <climits>
#include <iostream>
#include <map>
#include <string>
#include <vector>
// #include<list>
#include <algorithm>
#include <deque>
#include <queue>
// #include<numeric>
#include <utility>
// #include<memory>
#include <cassert>
#include <functional>
#include <random>
#include <set>
#include <stack>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

const int MAXN = 16;
const int INF = 100000;
string S[MAXN];
int dp[MAXN][1 << MAXN];
int N;

int dfs(int now, int state) {
  int &ret = dp[now][state];
  if (ret >= 0)
    return ret;
  if (state == (1 << N) - 1)
    return ret = 0;
  ret = INF;
  int sz = S[now].size();
  for (int i = 0; i < N; i++)
    if (((state >> i) & 1) == 0) {
      int isz = S[i].size();
      for (int j = max(0, sz + 1 - isz); j <= sz; j++) {
        const string s = S[now].substr(j);
        const string t = S[i].substr(0, s.size());
        if (s == t) {
          const string u = s + S[i].substr(t.size());
          int nstate = state | (1 << i);
          for (int j = 0; j < N; j++)
            if (((state >> j) & 1) == 0) {
              if (u.find(S[j]) != string::npos)
                nstate |= 1 << j;
            }
          ret = min<int>(ret, S[i].size() - t.size() + dfs(i, nstate));
        }
      }
    }
  return ret;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (cin >> N) {
    if (N == 0)
      break;
    for (int i = 0; i < N; i++)
      cin >> S[i];
    int ans = INF;
    memset(dp, -1, sizeof(dp));
    for (int i = 0; i < N; i++) {
      int state = 0;
      const string u = S[i];
      for (int j = 0; j < N; j++) {
        if (u.find(S[j]) != string::npos)
          state |= 1 << j;
      }
      ans = min<int>(ans, S[i].size() + dfs(i, state));
    }
    cout << ans << endl;
  }
  return 0;
}