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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e9;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
int add(string a, string b) { // a+b
  int t = min(a.size(), b.size());
  for (int i = t; i >= 0; i--) {
    bool h = true;
    rep(j, i) if (a[a.size() - i + j] != b[j]) h = false;
    if (h)
      return b.size() - i;
  }
}
int main() {
  int n;
  while (cin >> n, n) {
    vector<string> in(n);
    rep(i, n) cin >> in[i];
    sort(all(in));
    n = unique(all(in)) - in.begin();
    vector<bool> ha(n, true);
    rep(i, n) rep(j, n) if (i != j && in[i].find(in[j]) != string::npos) ha[j] =
        false;
    vector<string> tmp;
    rep(i, n) if (ha[i]) tmp.pb(in[i]);
    in = tmp;
    n = in.size();
    sort(all(in));
    vvi c(n, vi(n));
    rep(i, n) rep(j, n) c[i][j] = add(in[i], in[j]);
    vvi dp(1 << n, vi(n, inf));
    rep(i, n) dp[1 << i][i] = in[i].size();
    loop(i, 1, 1 << n) rep(j, n) if (i & 1 << j)
        rep(k, n) if ((i & 1 << k) == 0) {
      int t = i ^ 1 << k;
      dp[t][k] = min(dp[t][k], dp[i][j] + c[j][k]);
    }
    int w = (1 << n) - 1;
    int out = inf;
    rep(i, n) out = min(out, dp[w][i]);
    cout << out << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e9;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
int add(string a, string b) { // a+b
  int t = min(a.size(), b.size());
  for (int i = t; i >= 0; i--) {
    bool h = true;
    rep(j, i) if (a[a.size() - i + j] != b[j]) h = false;
    if (h)
      return b.size() - i;
  }
}
int main() {
  int n;
  while (cin >> n, n) {
    vector<string> in(n);
    rep(i, n) cin >> in[i];
    sort(all(in));
    n = unique(all(in)) - in.begin();
    vector<bool> ha(n, true);
    rep(i, n) rep(j, n) if (i != j && in[i].find(in[j]) != string::npos) ha[j] =
        false;
    vector<string> tmp;
    rep(i, n) if (ha[i]) tmp.pb(in[i]);
    in = tmp;
    n = in.size();
    sort(all(in));

    vvi dp(1 << n, vi(n, inf));
    rep(i, n) dp[1 << i][i] = in[i].size();
    loop(i, 1, 1 << n) rep(j, n) if (i & 1 << j)
        rep(k, n) if ((i & 1 << k) == 0) {
      int t = i ^ 1 << k;
      dp[t][k] = min(dp[t][k], dp[i][j] + add(in[j], in[k]));
    }
    int w = (1 << n) - 1;
    int out = inf;
    rep(i, n) out = min(out, dp[w][i]);
    cout << out << endl;
  }
}