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

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

bool sb[10];
int n;
string ans;
string s[10];
int N[10][10];

void make(int m, int num, string S, int before) {
  if (before != -1) {
    if (N[before][m] != 0)
      S += s[m].substr(N[before][m]);
    else
      S += s[m];
  }
  if (n == num) {
    // cout<<S<<endl;
    if (ans == "") {
      ans = S;
      return;
    } else {
      if (ans.length() > S.length())
        ans = S;
      else if (ans.length() == S.length() && ans > S)
        ans = S;
    }
    return;
  } else {
    for (int i = 0; i < n; i++) {
      if (sb[i])
        continue;
      sb[i] = true;
      make(i, num + 1, S, m);
      sb[i] = false;
    }
  }
}

int main() {
  string S;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> s[i];
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        N[i][j] = 0;

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 1; k <= s[i].length() && k <= s[j].length(); k++) {
          if (s[i].substr(s[i].length() - k, k) == s[j].substr(0, k) &&
              i != j) {
            N[i][j] = k;
          }
        }
      }
    }
    for (int i = 0; i < n; i++)
      sb[i] = false;
    int sum = 1;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (s[i].length() == s[j].length()) {
          if (s[i] == s[j]) {
            if (sb[i])
              continue;
            sb[i] = true;
            sum++;
          }
        } else if (s[i].length() > s[j].length()) {
          for (int k = 0; k <= s[i].length() - s[j].length(); k++) {
            if (s[i].substr(k, s[j].length()) == s[j]) {
              if (sb[j])
                continue;
              sb[j] = true;
              sum++;
              break;
            }
          }
        } else {
          for (int k = 0; k <= s[j].length() - s[i].length(); k++) {
            if (s[j].substr(k, s[i].length()) == s[i]) {
              if (sb[i])
                continue;
              sb[i] = true;
              sum++;
              break;
            }
          }
        }
      }
    }
    ans = "";
    for (int i = 0; i < n; i++) {
      if (sb[i])
        continue;
      sb[i] = true;
      make(i, sum, s[i], -1);
      sb[i] = false;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

bool sb[10];
int n;
string ans;
string s[10];

string Chain(string S, string ms) {
  string a = "";
  for (int i = 1; i < S.length() && i < ms.length(); i++) {
    if (S.substr(S.length() - i, i) == ms.substr(0, i))
      a = S + ms.substr(i);
  }
  if (a != "")
    return a;
  return S + ms;
}

void make(int m, int num, string S) {
  if (S == s[m])
    S = s[m];
  else
    S = Chain(S, s[m]);
  if (S == "")
    return;
  if (n == num) {
    if (ans == "") {
      ans = S;
      return;
    } else {
      if (ans.length() > S.length())
        ans = S;
      else if (ans.length() == S.length() && ans > S)
        ans = S;
    }
    return;
  } else {
    for (int i = 0; i < n; i++) {
      if (sb[i])
        continue;
      sb[i] = true;
      make(i, num + 1, S);
      sb[i] = false;
    }
  }
}

int main() {
  string S;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    // s.clear();
    for (int i = 0; i < n; i++) {
      cin >> s[i];
      // s.push_back(S);
    }
    for (int i = 0; i < n; i++)
      sb[i] = false;
    int sum = 1;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (sb[j])
          continue;
        if (s[i].length() == s[j].length()) {
          if (s[i] == s[j]) {
            sb[j] = true;
            sum++;
          }
        } else if (s[i].length() > s[j].length()) {
          for (int k = 0; k <= s[i].length() - s[j].length(); k++) {
            if (s[i].substr(k, s[j].length()) == s[j]) {
              sb[j] = true;
              sum++;
              break;
            }
          }
        } else {
          for (int k = 0; k <= s[j].length() - s[i].length(); k++) {
            if (s[j].substr(k, s[i].length()) == s[i]) {
              sb[i] = true;
              sum++;
              break;
            }
          }
        }
      }
    }
    ans = "";
    for (int i = 0; i < n; i++) {
      if (sb[i])
        continue;
      sb[i] = true;
      make(i, sum, s[i]);
      sb[i] = false;
    }
    cout << ans << endl;
  }
  return 0;
}