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

#include <iostream>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

string a[11];
string dp[1 << 10][11];
int n;

string g(string a, string b) {
  // cout << a << " " << b << endl;
  int m = min(a.size(), b.size());
  for (int i = m; i >= 0; --i) {
    // cout << i << endl;
    bool ok = true;
    for (int j = 0; j < i; ++j) {
      if (a[a.size() - i + j] != b[j]) {
        ok = false;
        break;
      }
    }
    if (ok) {
      // cout << i << endl;
      // cout << a << " " << b <<" " << a + b.substr(i)<< endl;;
      return a + b.substr(i);
    }
  }
}

void f(int bit, int k) {
  if (dp[bit][k] != "")
    return;
  if (n == 0)
    return;
  rep(i, n) if (bit & (1 << i)) {
    f(bit ^ (1 << i), k - 1);
    string s = "";
    string ss;
    if (k == 1) {
      dp[bit][i] = a[i];
      continue;
    }
    rep(j, n) if ((bit ^ (1 << i)) & (1 << j)) {
      ss = g(dp[bit ^ (1 << i)][j], a[i]);
      if (s == "" || ss.size() < s.size() || (ss.size() == s.size() && ss < s))
        s = ss;
    }
    dp[bit][i] = s;
  }
}

int main() {
  for (;;) {
    cin >> n;
    if (n == 0)
      break;
    int k = 0;
    rep(i, n) {
      cin >> a[k];
      rep(i, k) {
        if (string::npos != a[i].find(a[k])) {
          --k;
          break;
        }
        if (string::npos != a[k].find(a[i])) {
          a[i] = a[k];
          --k;
          break;
        }
      }
      ++k;
    }
    n = k;
    rep(i, 1 << n) rep(j, 11) dp[i][j] = "";
    f((1 << n) - 1, n);
    string res = "";
    rep(i, n) if (res == "" || dp[(1 << n) - 1][i].size() < res.size() ||
                  (dp[(1 << n) - 1][i].size() == res.size() &&
                   dp[(1 << n) - 1][i] < res)) res = dp[(1 << n) - 1][i];
    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<string> vstring;
typedef vector<pint> vpint;

struct Edge {
  int to, from, cost;
};

#ifdef DEBUG
#define debug cout
#else
stringstream __ss__;
#define debug __ss__
#endif

template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    debug << *i << " ";
  debug << endl;
}
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repd(i, n) for (int i = (n)-1; i >= 0; i--)
#define repn(i, m, n) for (int i = (m); i <= (n); ++i)
#define repnd(i, m, n) for (int i = (n)-1; i >= (m); i--)
#define rep0(i, n) for (i = 0; i < (n); ++i)
#define repd0(i, n) for (i = (n)-1; i >= 0; i--)
#define repn0(i, m, n) for (i = (m); i <= (n); ++i)
#define repnd0(i, m, n) for (i = (n)-1; i >= (m); i--)
#define all(n) n.begin(), n.end()
#define sz(n) ((int)(n).size())
#define IL for (;;)
#define MP make_pair
#define PB push_back
#define SS stringstream
#define X second
#define Y first
#define PUTLINE debug << "LINE:" << __LINE__ << endl;

const int INF = 2147483647 / 3;
const double EPS = 1e-10;
const double PI = acos(-1.0);

const int dx[] = {1, -1, 0, 0, 1, -1, 1, -1, 0};
const int dy[] = {0, 0, 1, -1, 1, -1, -1, 1, 0};

string pls(string a, string b) {
  repnd(i, 1, sz(b)) {
    if (sz(a) - i < 0)
      continue;
    if (a.substr(sz(a) - i, i) == b.substr(0, i)) {
      return a + b.substr(i);
    }
  }
  return a + b;
}

string f(vstring v) {
  if (sz(v) == 1)
    return v[0];
  vstring next;
  next.PB(pls(v[0], v[1]));
  repn(i, 2, sz(v) - 1) { next.PB(v[i]); }
  return f(next);
}

bool g(string a, string b) {
  if (sz(a) > sz(b))
    return true;
  if (sz(a) < sz(b))
    return false;
  return a > b;
}

bool conc(string a, string b) {
  rep(i, sz(a) - sz(b) + 1) {
    if (a.substr(i, sz(b)) == b)
      return true;
  }
  return false;
}

int main() {
  int n;
  IL {
    cin >> n;
    if (n == 0)
      break;
    vstring vs;
    string res;
    rep(i, n) {
      string s;
      cin >> s;
      res += s;
      vs.PB(s);
    }
    rep(i, n) rep(j, n) if (i != j) {
      if (conc(vs[i], vs[j]))
        vs[j] = "";
    }
    vint per;
    rep(i, n) per.PB(i);
    do {
      vstring v;
      rep(i, n) v.PB(vs[per[i]]);
      string ss = f(v);
      if (g(res, ss))
        res = ss;
    } while (next_permutation(all(per)));
    cout << res << endl;
  }
  return 0;
}