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

#include <bits/stdc++.h>
using namespace std;

vector<string> v;
int cost[10][10];
int dp[10][1 << 10];
int N;
int dfs(int p, int bit) {
  if (bit == (1 << N) - 1)
    return 0;
  if (dp[p][bit] != -1)
    return dp[p][bit];
  int res = 1e9;
  for (int i = 0; i < N; i++) {
    if (bit >> i & 1)
      continue;
    res = min(dfs(i, bit | (1 << i)) + cost[p][i], res);
  }
  return dp[p][bit] = res;
}
string rec(int p, int bit) {
  if (bit == (1 << N) - 1)
    return "";
  string res = "~";
  for (int i = 0; i < N; i++) {
    if (bit >> i & 1)
      continue;
    if (dfs(p, bit) == dfs(i, bit | (1 << i)) + cost[p][i]) {
      res = min(v[i].substr(v[i].size() - cost[p][i]) + rec(i, bit | (1 << i)),
                res);
    }
  }
  return res;
}
int main() {
  while (cin >> N && N) {
    memset(dp, -1, sizeof(dp));
    v.clear();
    v.resize(N);

    for (int i = 0; i < N; i++)
      cin >> v[i];
    while (true) {
      int flag = 0;
      vector<string> t;
      for (int i = 0; i < v.size(); i++) {
        for (int j = 0; j < v.size(); j++) {
          if (i != j) {
            if (v[i].find(v[j]) != -1) {
              for (int k = 0; k < v.size(); k++)
                if (j != k)
                  t.push_back(v[k]);
              v = t;
              goto end;
            }
          }
        }
      }
      break;
    end:;
    }
    for (int i = 0; i < v.size(); i++) {
      for (int j = 0; j < v.size(); j++) {
        if (i != j) {
          int m = min(v[i].size(), v[j].size());
          cost[i][j] = v[j].size();
          for (int k = m; k > 0; k--) {
            if (v[i].substr(v[i].size() - k) == v[j].substr(0, k)) {
              cost[i][j] = v[j].size() - k;
              break;
            }
          }
          // cout << v[i] << " " << v[j] << " " << cost[i][j] << endl;
        }
      }
    }
    N = v.size();
    int res = 1e9;
    for (int i = 0; i < v.size(); i++) {
      res = min<int>(dfs(i, (1 << i)) + v[i].size(), res);
    }
    string rres = "~";
    for (int i = 0; i < v.size(); i++) {
      if (dfs(i, (1 << i)) + v[i].size() == res) {
        rres = min(v[i] + rec(i, (1 << i)), rres);
      }
    }
    // cout << res << endl;
    cout << rres << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
using namespace std;

int n;
string v[10];

string merge(string a, string b) {
  if (~a.find(b))
    return a;
  for (int i = 0; i < a.size(); i++) {
    bool f = true;
    for (int j = 0; i + j < a.size(); j++) {
      if (a[i + j] != b[j]) {
        f = false;
        break;
      }
    }
    if (f)
      return a.substr(0, i) + b;
  }
  return a + b;
}
string result;
void f(int bit, string s) {
  if (s.size() > result.size())
    return;
  if (bit == 0) {
    if (result.length() > s.length() ||
        (result.length() == s.length() && s < result))
      result = s;
    return;
  }
  for (int i = 0; i < n; i++) {
    if (bit >> i & 1)
      f(bit & ~(1 << i), merge(s, v[i]));
  }
}
int main() {
  while (cin >> n && n) {
    for (int i = 0; i < n; i++)
      cin >> v[i];
    sort(v, v + n);
    result = string(101, '~');
    f((1 << n) - 1, "");
    cout << result << endl;
  }
}