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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#define REP(i, a, b) for (i = a; i < b; ++i)
#define rep(i, n) REP(i, 0, n)

int table[15][15];

int dup(string s, string t) {
  int i, j, k = min(s.length(), t.length());
  for (int i = k; i >= 0; --i) {
    if (s.substr(s.length() - i) == t.substr(0, i)) {
      return i;
    }
  }
  return 0;
}

string conc(vector<int> &perm, vector<string> &input) {
  string res = input[perm[0]];
  for (int i = 0; i < perm.size() - 1; ++i) {
    res += input[perm[i + 1]].substr(table[perm[i]][perm[i + 1]]);
  }
  return res;
}

bool issubstr(string s, string t) {
  if (s.length() > t.length())
    return false;
  for (int i = 0; i < t.length() - s.length(); ++i) {
    if (t.substr(i, s.length()) == s)
      return true;
  }
  return false;
}

int main() {
  int i, j, k, tt, n;
  while (cin >> n, n) {
    memset(table, 0, sizeof(table));

    vector<string> input(n), tmp;
    vector<int> dd;
    rep(i, n) cin >> input[i];

    rep(i, n) rep(j, n) if (issubstr(input[i], input[j])) dd.push_back(i);

    rep(i, n) if (find(dd.begin(), dd.end(), i) == dd.end())
        tmp.push_back(input[i]);
    input.clear();
    rep(i, tmp.size()) input.push_back(tmp[i]);

    sort(input.begin(), input.end());
    rep(i, input.size()) rep(j, input.size()) table[i][j] =
        dup(input[i], input[j]);

    vector<int> perm;
    int total = 0;
    rep(i, input.size()) {
      perm.push_back(i);
      total += input[i].length();
    }

    int ans = 1 << 29;
    string sa = "";
    do {
      tt = 0;
      rep(i, input.size() - 1) { tt += table[perm[i]][perm[i + 1]]; }

      if (ans > total - tt) {
        ans = total - tt;
        sa = conc(perm, input);
      } else if (ans == total - tt) {
        string t = conc(perm, input);
        if (sa > t)
          sa = t;
      }
    } while (next_permutation(perm.begin(), perm.end()));

    cout << sa << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#define REP(i, a, b) for (i = a; i < b; ++i)
#define rep(i, n) REP(i, 0, n)

int table[10][10], n;
const int inf = 1 << 29;
string res = "";

int dup(string s, string t) {
  int i, j, k = min(s.length(), t.length());
  for (int i = k; i >= 0; --i) {
    if (s.substr(s.length() - i) == t.substr(0, i)) {
      return i;
    }
  }
  return 0;
}

string conc(vector<int> &perm, vector<string> &input) {
  string res = input[perm[0]];
  for (int i = 0; i < perm.size() - 1; ++i) {
    res += input[perm[i + 1]].substr(table[perm[i]][perm[i + 1]]);
  }
  return res;
}

void dfs(int used, int lastused, string s, vector<string> &input) {
  if (used == (1 << n) - 1) {
    if (res == "")
      res = s;
    else if (res.length() > s.length())
      res = s;
    else if (res.length() == s.length() && res > s)
      res = s;
    return;
  }
  for (int i = 0; i < n; ++i) {
    if ((used >> i) & 1)
      continue;
    dfs((used | (1 << i)), i, s + input[i].substr(table[lastused][i]), input);
  }
  return;
}

int main() {
  int i, j, k;
  while (cin >> n, n) {
    memset(table, 0, sizeof(table));
    res = "";
    vector<string> input(n);
    rep(i, n) cin >> input[i];

    rep(i, n) rep(j, n) table[i][j] = dup(input[i], input[j]);

    vector<int> perm;
    rep(i, n) perm.push_back(i);

    rep(i, n) { dfs((1 << i), i, input[i], input); }

    cout << res << endl;
  }
}