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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
const int INF = 114514810;
const int MOD = 1000000007;
const double EPS = 1e-10;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

bool comp(string &a, string &b) {
  if (a.size() == b.size())
    return a < b;
  else
    return a.size() < b.size();
}

int main() {
  int n;
  while (cin >> n, n) {
    vs v, t(n);
    REP(i, n) cin >> t[i];
    sort(ALL(t));
    t.erase(unique(ALL(t)), t.end());
    REP(i, t.size()) {
      bool f = true;
      REP(j, t.size()) {
        if (i == j)
          continue;
        if (t[j].find(t[i]) != string::npos)
          f = false;
      }
      if (f)
        v.push_back(t[i]);
    }
    string mp[11][11];
    REP(i, v.size()) REP(j, v.size()) {
      if (i == j)
        continue;
      for (int k = min(v[i].size(), v[j].size()); k >= 0; k--) {
        string x = v[i].substr(v[i].size() - k);
        string y = v[j].substr(0, k);
        if (x == y) {
          mp[i][j] = v[j].substr(k);
          break;
        }
      }
    }
    n = v.size();
    int index[20];
    REP(i, n) index[i] = i;
    string ans(111, 'z');
    do {
      string tmp = v[index[0]];
      FOR(i, 1, v.size()) { tmp += mp[index[i - 1]][index[i]]; }
      if (ans.size() > tmp.size())
        ans = tmp;
      else if (ans.size() == tmp.size())
        chmin(ans, tmp);
    } while (next_permutation(index, index + n));
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
const int INF = 114514810;
const int MOD = 1000000007;
const double EPS = 1e-10;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

int main() {
  int n;
  while (cin >> n, n) {
    vs v, t(n);
    REP(i, n) cin >> t[i];
    REP(i, n) {
      bool f = true;
      REP(j, n) {
        if (i == j)
          continue;
        if (t[j].find(t[i]) != string::npos)
          f = false;
      }
      if (f)
        v.push_back(t[i]);
    }
    map<pair<string, string>, int> mp;
    REP(i, v.size()) REP(j, v.size()) {
      if (i == j)
        continue;
      for (int k = min(v[i].size(), v[j].size()); k >= 0; k--) {
        string x = v[i].substr(v[i].size() - k);
        string y = v[j].substr(0, k);
        if (x == y) {
          mp[pair<string, string>(v[i], v[j])] = k;
          break;
        }
      }
    }
    n = v.size();
    int index[20];
    REP(i, n) index[i] = i;
    string ans(111, 'z');
    do {
      string tmp = v[index[0]];
      FOR(i, 1, v.size()) {
        tmp += v[index[i]].substr(mp[make_pair(v[index[i - 1]], v[index[i]])]);
      }
      if (ans.size() > tmp.size())
        ans = tmp;
      else if (ans.size() == tmp.size())
        chmin(ans, tmp);
    } while (next_permutation(index, index + n));
    cout << ans << endl;
  }
  return 0;
}