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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1 << 29)
#define EPS 1e-10
#define MOD 1000000007

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

#define MAX 14

int dp[1 << MAX][MAX];

int N;
vector<string> s;

int dfs(int S, int u) {
  if (S == (1 << N) - 1)
    return 0;
  if (u == -1) {
    int res = INF;
    rep(i, N) res = min(res, dfs(S | (1 << i), i) + (int)s[i].size());
    return res;
  }
  int &res = dp[S][u];
  if (res >= 0)
    return res;
  res = INF;
  //  cout << S << " " << u << endl;
  string us = s[u];
  for (int i = 0; i < N; i++) {
    if ((S >> i) & 1)
      continue;
    string c = s[i];
    for (int p = us.size() - min(us.size(), c.size()) + 1; p <= us.size();
         p++) {
      int f = 1;
      for (int j = p; j < us.size(); j++)
        if (us[j] != c[j - p]) {
          f = 0;
          break;
        }
      if (f)
        res =
            min(res, dfs(S | (1 << i), i) + (int)c.size() - (int)us.size() + p);

      /*      if(us.substr(p) == c.substr(0, us.size()-p)){
        res = min(res, dfs(S | (1<<i), i) + (int)c.size()-(int)us.size()+p);
        break;
        }*/
    }
  }
  return res;
}

int main() {
  while (cin >> N, N) {
    s.clear();
    rep(i, N) {
      string in;
      cin >> in;
      s.push_back(in);
    }
    rep(i, s.size()) {
      rep(j, s.size()) if (i != j && s[j].find(s[i], 0) != string::npos) {
        s.erase(s.begin() + i);
        i--;
        break;
      }
    }
    N = s.size();
    memset(dp, -1, sizeof(dp));
    cout << dfs(0, -1) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1 << 29)
#define EPS 1e-10
#define MOD 1000000007

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

#define MAX 15

int dp[1 << MAX][MAX];

int N;
vector<string> s;

int dfs(int S, int u) {
  //  cout << S << " " << (1<<s.size())-1 << " " << u << endl;
  if (S == (1 << s.size()) - 1)
    return 0;

  int &res = dp[S][u];
  if (res >= 0)
    return res;
  res = INF;
  string us = s[u];
  for (int i = 0; i < s.size(); i++) {
    if ((S >> i) & 1)
      continue;
    string c = s[i];
    //    cout << us << " " << c << endl;
    //    cout << us.size()-min(us.size(),c.size())+1 << endl;
    for (int p = us.size() - min(us.size(), c.size()) + 1; p <= us.size();
         p++) {
      //      cout << p << " " << us.substr(p)<< " " << c.substr(0, us.size()-p)
      //      << endl;
      if (us.substr(p) == c.substr(0, us.size() - p)) {
        res =
            min(res, dfs(S | (1 << i), i) + (int)c.size() - (int)us.size() + p);
        break;
      }
    }
  }
  //  cout << res << endl;
  return res;
}

int main() {
  while (cin >> N, N) {
    s.clear();
    rep(i, N) {
      string in;
      cin >> in;
      s.push_back(in);
    }
    rep(i, s.size()) {
      rep(j, s.size()) if (i != j && s[j].find(s[i], 0) != string::npos) {
        s.erase(s.begin() + i);
        i--;
        break;
      }
      //          cout << s.size() << endl;
      //    rep(i,s.size()) cout << s[i] << endl;
    }

    s.push_back(".");
    memset(dp, -1, sizeof(dp));
    cout << dfs(1 << (s.size() - 1), s.size() - 1) << endl;
  }
  return 0;
}