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

#include <bits/stdc++.h>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF = 1LL << 62;
const int inf = 1 << 29;

int bit[15];
int com[15][15];
string in[15];
int dp[15][1 << 15];

int main(void) {
  int n;
  while (cin >> n, n) {
    rep(i, n) cin >> in[i];
    rep(i, n) rep(j, 1 << n) dp[i][j] = inf;
    rep(i, n) bit[i] = (1 << i);

    rep(i, n) rep(j, n) {
      if (i == j)
        continue;
      if (in[i].find(in[j]) != string::npos) {
        bit[i] |= bit[j];
        bit[j] = 0;
      }
    }

    rep(i, n) dp[i][bit[i]] = in[i].size();

    rep(i, n) rep(j, n) {
      if (i == j)
        continue;
      int cs = in[i].size();
      int ns = in[j].size();
      for (int a = min(cs, ns); a >= 0; --a) {
        if (in[i].substr(cs - a, a) == in[j].substr(0, a)) {
          com[i][j] = ns - a;
          break;
        }
      }
    }

    rep(mask, 1 << n) {
      rep(i, n) rep(j, n) {
        dp[j][mask | bit[j]] =
            min(dp[j][mask | bit[j]], dp[i][mask] + com[i][j]);
      }
    }
    int ans = dp[0][(1 << n) - 1];
    rep(i, n) ans = min(ans, dp[i][(1 << n) - 1]);
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF = 1LL << 62;
const int inf = 1 << 29;

int com[15][15];
string in[15];
string dp[15][1 << 15];

int main(void) {
  int n;
  while (cin >> n, n) {
    rep(i, n) cin >> in[i];
    rep(i, n) rep(j, 1 << n) dp[i][j] = "";
    rep(i, n) dp[i][1 << i] = in[i];

    rep(i, n) rep(j, n) {
      if (i == j)
        continue;
      if (in[i].find(in[j]) != string::npos)
        com[i][j] = 0;
      else {
        int cs = in[i].size();
        int ns = in[j].size();
        for (int a = ns - 1; a >= 0; --a) {
          if (cs - a >= 0 && in[i].substr(cs - a, a) == in[j].substr(0, a)) {
            com[i][j] = ns - a;
            break;
          }
        }
      }
    }

    rep(mask, 1 << n) {
      if (mask == 0)
        continue;
      rep(i, n) rep(j, n) {
        if ((mask & (1 << i)) == 0)
          continue;
        if (mask & (1 << j))
          continue;

        bool exist = false;
        if (dp[i][mask].find(in[j]) != string::npos)
          exist = true;
        string nexts = "";
        int nj = in[j].size();
        if (exist)
          nexts = dp[i][mask];
        else
          nexts = dp[i][mask] + in[j].substr(nj - com[i][j], com[i][j]);
        int m = dp[j][mask | (1 << j)].size();
        if (m == 0 || m > nexts.size())
          dp[j][mask | (1 << j)] = nexts;
      }
    }
    int ans = dp[0][(1 << n) - 1].size();
    rep(i, n) ans = min(ans, (int)dp[i][(1 << n) - 1].size());
    cout << ans << endl;
  }
  return 0;
}