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

#include <bits/stdc++.h>

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();
}
template <class T> inline T sqr(T x) { return x * x; }

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 pb push_back
#define mp make_pair
#define each(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF = 1LL << 62;
const int inf = 1 << 29;

int n;
string s[15];

inline string cmp(string a, string b) {
  if (a == "")
    return b;
  if (b == "")
    return a;
  if (a.size() < b.size())
    return a;
  if (b.size() < a.size())
    return b;
  return min(a, b);
}

int main(void) {
  while (cin >> n, n) {
    rep(i, n) cin >> s[i];

    bool rm[15];
    int rel[15][15];

    rep(i, n) rm[i] = false;
    rep(i, n) rep(j, n) rel[i][j] = 0;

    rep(i, n) rep(j, n) {
      if (i >= j)
        continue;
      if (s[i].find(s[j]) != string::npos)
        rm[j] = true;
      if (s[j].find(s[i]) != string::npos)
        rm[i] = true;
      if (s[i] == s[j])
        rm[i] = false;
    }
    rep(i, n) rep(j, n) {
      if (i == j)
        continue;
      if (rm[i] || rm[j])
        rel[i][j] = 0;
      else {
        int m = min(s[i].size(), s[j].size());
        for (int k = m - 1; k >= 1; --k) {
          if (s[i].substr(s[i].size() - k, k) == s[j].substr(0, k))
            rel[i][j] = k, k = 0;
        }
      }
    }

    vi a;
    rep(i, n) if (!rm[i]) a.pb(i);
    n = a.size();
    string dp[1 << 10][10];
    rep(i, 1 << n) rep(j, n) dp[i][j] = "";
    rep(i, n) dp[1 << i][i] = s[a[i]];

    range(mask, 1, 1 << n) rep(i, n) {
      if (!(mask & (1 << i)))
        continue;
      rep(j, n) {
        if (mask & (1 << j))
          continue;
        int prev = a[i], cur = a[j];
        string res = dp[mask][i];
        int k = rel[prev][cur];
        res += s[cur].substr(k, s[cur].size() - k);
        dp[mask | (1 << j)][j] = cmp(dp[mask | (1 << j)][j], res);
      }
    }
    string ans = "";
    rep(i, n) ans = cmp(ans, dp[(1 << n) - 1][i]);
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

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();
}
template <class T> inline T sqr(T x) { return x * x; }

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 pb push_back
#define mp make_pair
#define each(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF = 1LL << 62;
const int inf = 1 << 29;

int n;
string s[15];

inline string cmp(string a, string b) {
  if (a == "")
    return b;
  if (b == "")
    return a;
  if (a.size() < b.size())
    return a;
  if (b.size() < a.size())
    return b;
  return min(a, b);
}

int main(void) {
  while (cin >> n, n) {
    rep(i, n) cin >> s[i];

    bool rm[15];
    int rel[15][15];

    rep(i, n) rm[i] = false;
    rep(i, n) rep(j, n) rel[i][j] = 0;

    rep(i, n) rep(j, n) if (i != j && s[i].find(s[j]) != string::npos) rm[j] =
        true;

    rep(i, n) rep(j, n) {
      if (rm[i] || rm[j])
        rel[i][j] = 0;
      else {
        int m = min(s[i].size(), s[j].size());
        for (int k = m - 1; k >= 1; --k) {
          if (s[i].substr(s[i].size() - k, k) == s[j].substr(0, k))
            rel[i][j] = k, k = 0;
        }
      }
    }

    vi a;
    rep(i, n) if (!rm[i]) a.pb(i);
    string ans = "";
    do {
      string res = "";
      int prev = -1;
      rep(i, a.size()) {
        int cur = a[i];

        if (prev == -1)
          res += s[cur];
        else {
          int k = rel[prev][cur];
          res += s[cur].substr(k, s[cur].size() - k);
        }
        prev = cur;
      }
      ans = cmp(ans, res);
    } while (next_permutation(a.begin(), a.end()));
    cout << ans << endl;
  }
  return 0;
}