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

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

int main() {
  int n, len;
  string tmp, ans;
  vector<string> s;
  bool f;
  int a[10], g[10][10], hoge;

  for (int i = 0; i < 10; i++)
    a[i] = i;

  while (cin >> n, n) {
    s.resize(n);
    for (int i = 0; i < n; i++)
      cin >> s[i];

    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (s[i].size() < s[j].size())
          swap(s[i], s[j]);
      }
    }

    for (int i = 0; i < s.size(); i++) {
      for (int j = i + 1; j < s.size(); j++) {
        if (i == j)
          continue;
        for (int k = 0; k < s[i].size() - s[j].size(); k++) {
          if (s[i].substr(k, s[j].size()) == s[j]) {
            s.erase(s.begin() + j);
            j--;
            break;
          }
        }
      }
    }

    n = s.size();
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == j)
          g[i][j] = 0;
        else {
          len = min(s[i].size(), s[j].size());
          for (int k = len; k >= 0; k--) {
            f = true;
            for (int l = 0; l < k; l++) {
              if (s[i][(int)s[i].size() - k + l] != s[j][l]) {
                f = false;
                break;
              }
            }
            if (f) {
              g[i][j] = k;
              break;
            }
          }
        }
      }
    }

    len = -1;
    ans = "";
    sort(a, a + n);

    do {
      hoge = 0;
      for (int i = 1; i < n; i++)
        hoge += g[a[i - 1]][a[i]];

      if (len < hoge) {
        len = hoge;
        tmp = s[a[0]];
        for (int i = 1; i < n; i++) {
          hoge = g[a[i - 1]][a[i]];
          tmp += s[a[i]].substr(hoge, (int)s[a[i]].size() - hoge);
        }
        ans = tmp;
      } else if (hoge == len) {
        tmp = s[a[0]];
        for (int i = 1; i < n; i++) {
          hoge = g[a[i - 1]][a[i]];
          tmp += s[a[i]].substr(hoge, (int)s[a[i]].size() - hoge);
        }
        if (tmp < ans)
          ans = tmp;
      }
    } while (next_permutation(a, a + n));
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int n, len;
  string s[10], tmp, ans;
  bool f;
  int a[10], g[10][10], hoge;

  for (int i = 0; i < 10; i++)
    a[i] = i;

  while (cin >> n, n) {
    for (int i = 0; i < n; i++)
      cin >> s[i];

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == j)
          g[i][j] = 0;
        else {
          len = min(s[i].size(), s[j].size());
          for (int k = len; k >= 0; k--) {
            f = true;
            for (int l = 0; l < k; l++) {
              if (s[i][(int)s[i].size() - k + l] != s[j][l]) {
                f = false;
                break;
              }
            }
            if (f) {
              g[i][j] = k;
              break;
            }
          }
        }
      }
    }

    ans = "";
    sort(a, a + n);

    do {
      tmp = s[a[0]];
      for (int i = 1; i < n; i++) {
        hoge = g[a[i - 1]][a[i]];
        tmp += s[a[i]].substr(hoge, (int)s[a[i]].size() - hoge);
      }
      if (ans == "" || tmp.size() < ans.size() ||
          (tmp.size() == ans.size() && tmp < ans)) {
        ans = tmp;
      }
    } while (next_permutation(a, a + n));
    cout << ans << endl;
  }
}