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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl;
string s[10], st[10], add[10][10];
int sz[10][10];
bool iran[10];
int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    rep(i, n) iran[i] = 0;
    rep(i, n) cin >> s[i];
    rep(i, n) {
      rep(j, n) {
        if (i == j)
          continue;
        string x = s[i], y = s[j];
        if (x == y) {
          iran[min(i, j)] = true;
          continue;
        }
        if (x.size() > y.size())
          continue;
        int xs = x.size(), ys = y.size();
        rep(k, ys - xs + 1) if (x == y.substr(k, xs)) iran[i] = true;
      }
    }
    int it = 0;
    rep(i, n) if (!iran[i]) st[it] = s[i], it++;
    n = it;
    int a[10];
    rep(i, n) a[i] = i;
    string mn;
    rep(i, n) rep(j, n) {
      if (i == j)
        continue;
      string x = st[i], y = st[j];
      int xs = x.size(), ys = y.size(), mx = min(xs, ys);
      bool ok = 0;
      for (int m = mx; m > 0; m--) {
        if (x.substr(xs - m, m) == y.substr(0, m)) {
          add[i][j] = y.substr(m, ys - m);
          ok = true;
          break;
        }
      }
      if (!ok)
        add[i][j] = y;
      sz[i][j] = add[i][j].size();
    }
    int ms = 1000000;
    do {
      int sum = st[a[0]].size();
      rep(i, n - 1) sum += sz[a[i]][a[i + 1]];
      if (sum > ms)
        continue;
      string now = st[a[0]];
      rep(i, n - 1) now += add[a[i]][a[i + 1]];
      if (sum < ms)
        mn = now, ms = sum;
      else if (now < mn)
        mn = now;
    } while (next_permutation(a, a + n));
    cout << mn << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl;
string s[10], st[10];
bool iran[10];
int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    rep(i, n) iran[i] = 0;
    rep(i, n) cin >> s[i];
    rep(i, n) {
      rep(j, i) {
        if (i == j)
          continue;
        string x = s[i], y = s[j];
        if (x.size() > y.size())
          continue;
        int xs = x.size(), ys = y.size();
        rep(k, ys - xs + 1) if (x == y.substr(k, xs)) iran[i] = true;
      }
    }
    int it = 0;
    rep(i, n) if (!iran[i]) st[it] = s[i], it++;
    n = it;
    int a[10];
    rep(i, n) a[i] = i;
    string mn;
    do {
      string now = st[a[0]];
      rep(i, n - 1) {
        string x = st[a[i]], y = st[a[i + 1]];
        int xs = x.size(), ys = y.size(), mx = min(xs, ys);
        bool ok = 0;
        for (int j = mx; j > 0; j--) {
          if (x.substr(xs - j, j) == y.substr(0, j)) {
            now += y.substr(j, ys - j);
            ok = true;
            break;
          }
        }
        if (!ok)
          now += y;
      }
      if (mn.size() == 0 || now.size() < mn.size() ||
          (now.size() == mn.size() && now < mn))
        mn = now;
    } while (next_permutation(a, a + n));
    cout << mn << endl;
  }
}