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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
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();
}
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 EXIST(s, e) ((s).find(e) != (s).end())
#define EXIST2(s, e) (find(ALL(s), (e)) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i)
const double EPS = 1e-10;
const double PI = acos(-1.0);

string concat(const string &l, const string &r, int common) {
  return l.substr(0, l.size() - common) + r;
}

typedef vector<vs> vvs;
typedef vector<vvs> vvvs;

string smaller(const string &l, const string &r) {
  if (l.size() == r.size()) {
    return min(l, r);
  } else {
    return (l.size() < r.size() ? l : r);
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    vs s(n);
    REP(i, n) { cin >> s[i]; }

    vi unused(n);
    REP(i, n) {
      REP(j, n) {
        if (s[i] == s[j]) {
          if (i < j)
            unused[j] = 1;
        } else if (i != j && s[i].find(s[j]) != string::npos) {
          unused[j] = 1;
        }
      }
    }
    for (int i = n - 1; i >= 0; i--) {
      if (unused[i]) {
        s.erase(s.begin() + i);
      }
    }
    n = s.size();

    vvi common(n, vi(n));
    REP(i, n) {
      REP(j, n) {
        if (i == j)
          continue;
        int c = 0;
        for (int k = min(s[i].size(), s[j].size()); k > 0; k--) {
          bool flag = true;
          REP(l, k) {
            if (s[j][l] != s[i][s[i].size() - k + l]) {
              flag = false;
              break;
            }
          }
          if (flag) {
            c = k;
            break;
          }
        }
        common[i][j] = c;
      }
    }

    vvs dp(1 << n, vs(n, string(200, 'z')));
    REP(i, n) { dp[1 << i][i] = s[i]; }
    REP(mask, 1 << n) {
      REP(i, n) {
        if ((1 << i) & mask) {
          REP(j, n) {
            if (((1 << j) & mask) == 0) {
              dp[mask | (1 << j)][j] =
                  smaller(dp[mask | (1 << j)][j],
                          concat(dp[mask][i], s[j], common[i][j]));
            }
          }
        }
      }
    }

    string ans(200, 'z');
    REP(i, n) { ans = smaller(ans, dp[(1 << n) - 1][i]); }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
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();
}
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 EXIST(s, e) ((s).find(e) != (s).end())
#define EXIST2(s, e) (find(ALL(s), (e)) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i)
const double EPS = 1e-10;
const double PI = acos(-1.0);

int main() {
  int n;
  while (cin >> n, n) {
    vs s(n);
    REP(i, n) { cin >> s[i]; }

    vvi common(n, vi(n));
    REP(i, n) {
      REP(j, n) {
        if (i == j)
          continue;
        int c = 0;
        for (int k = min(s[i].size(), s[j].size()); k > 0; k--) {
          bool flag = true;
          REP(l, k) {
            if (s[j][l] != s[i][s[i].size() - k + l]) {
              flag = false;
              break;
            }
          }
          if (flag) {
            c = k;
            break;
          }
        }
        common[i][j] = c;
      }
    }

    vi order;
    REP(i, n) { order.push_back(i); }
    string best(200, 'z');
    do {
      int len = s[n - 1].size();
      REP(i, n - 1) {
        len += s[order[i]].size() - common[order[i]][order[i + 1]];
      }
      if (best.size() >= len) {
        string ss;
        REP(i, n - 1) {
          ss += s[order[i]].substr(0, s[order[i]].size() -
                                          common[order[i]][order[i + 1]]);
        }
        ss += s[order[n - 1]];
        if (best.size() > len) {
          best = ss;
        } else {
          if (best > ss)
            best = ss;
        }
      }
    } while (next_permutation(ALL(order)));

    cout << best << endl;
  }
}