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

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
// #include<cctype>
#include <climits>
#include <iostream>
#include <map>
#include <string>
#include <vector>
// #include<list>
#include <algorithm>
#include <deque>
#include <queue>
// #include<numeric>
#include <utility>
// #include<memory>
#include <cassert>
#include <functional>
#include <random>
#include <set>
#include <stack>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

pii same[15][15];
char ans[200], tmp[200];
int asize = 0;

// s < t ????????????
bool comp(const char *s, int ssize, const char *t, int tsize) {
  if (ssize < tsize)
    return true;
  if (ssize > tsize)
    return false;
  for (int i = 0; i < ssize; i++) {
    if (s[i] < t[i])
      return true;
    if (s[i] > t[i])
      return false;
  }
  return true;
}

int main() {
  int N;
  while (cin >> N) {
    if (N == 0)
      break;
    vector<string> ss(N);
    for (int i = 0; i < N; i++)
      cin >> ss[i];
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++) {
        int sz = min(ss[i].size(), ss[j].size());
        int maxi = 0;
        for (int k = 1; k <= sz; k++) {
          if (ss[i].substr(ss[i].size() - k, k) == ss[j].substr(0, k))
            maxi = k;
        }
        same[i][j] = pii(maxi, j);
        for (int k = 0; k < ss[i].size(); k++) {
          if (ss[i].substr(k, ss[j].size()) == ss[j])
            same[i][j] = pii(ss[j].size(), i);
        }
      }
    vector<int> perm(N);
    for (int i = 0; i < N; i++)
      perm[i] = i;
    bool first = true;
    do {
      int sz = 0;
      for (int i = 0; i < ss[perm[0]].size(); i++)
        tmp[i] = ss[perm[0]][i];
      sz = ss[perm[0]].size();
      int now = perm[0];
      for (int i = 0; i < N - 1; i++) {
        int next = perm[i + 1];
        pii p = same[now][next];
        int len = p.first;
        for (int j = len; j < ss[next].size(); j++)
          tmp[sz++] = ss[next][j];
        now = p.second;
      }
      if (first || !comp(ans, asize, tmp, sz)) {
        asize = sz;
        for (int i = 0; i < sz; i++)
          ans[i] = tmp[i];
      }
      first = false;
    } while (next_permutation(perm.begin(), perm.end()));
    for (int i = 0; i < asize; i++)
      printf("%c", ans[i]);
    printf("\\n");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
// #include<cctype>
#include <climits>
#include <iostream>
#include <map>
#include <string>
#include <vector>
// #include<list>
#include <algorithm>
#include <deque>
#include <queue>
// #include<numeric>
#include <utility>
// #include<memory>
#include <cassert>
#include <functional>
#include <random>
#include <set>
#include <stack>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

int same[15][15];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  while (cin >> N) {
    if (N == 0)
      break;
    vector<string> ss(N);
    for (int i = 0; i < N; i++)
      cin >> ss[i];
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++) {
        int sz = min(ss[i].size(), ss[j].size());
        int maxi = 0;
        for (int k = 1; k <= sz; k++) {
          if (ss[i].substr(ss[i].size() - k, k) == ss[j].substr(0, k))
            maxi = k;
        }
        same[i][j] = maxi;
      }
    vector<int> perm(N);
    for (int i = 0; i < N; i++)
      perm[i] = i;
    string ans;
    do {
      string tmp = ss[perm[0]];
      for (int i = 0; i < N - 1; i++) {
        int now = perm[i], next = perm[i + 1];
        int len = same[now][next];
        tmp += ss[next].substr(len, ss[next].size() - len);
      }
      if (ans == "" || ans.size() > tmp.size() ||
          (ans.size() == tmp.size() && ans > tmp)) {
        ans = tmp;
      }
    } while (next_permutation(perm.begin(), perm.end()));
    cout << ans << endl;
  }
  return 0;
}