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

#include <bits/stdc++.h>

using namespace std;

string dp[11][1 << 10];
bool made[11][1 << 10] = {0};

void proc(string s, int i, int j) {
  if (!made[i][j] || s.size() < dp[i][j].size() ||
      (s.size() == dp[i][j].size() && s < dp[i][j])) {
    dp[i][j] = s;
  }
  made[i][j] = true;
}
string cat(string s1, string s2) {
  if (s1.size() >= s2.size()) {
    for (int i = 0; i <= s1.size() - s2.size(); ++i) {
      bool f = true;
      for (int j = 0; j < s2.size(); ++j) {
        if (s1[i + j] != s2[j]) {
          f = false;
          break;
        }
      }
      if (f) {
        return s1;
      }
    }
  }

  int len = 0;
  for (int k = min(s1.size(), s2.size()); k >= 0; --k) {
    bool f = true;
    for (int i = 0; i < k; ++i) {
      if (s1[s1.size() - k + i] != s2[i]) {
        f = false;
        break;
      }
    }
    if (f) {
      len = k;
      break;
    }
  }
  return s1 + s2.substr(len);
}

int N;
string s[10];
bool used[10] = {0};
string ans;
void dfs(int n, string str) {
  if (str.size() < ans.size() || (str.size() == ans.size() && str < ans)) {
    if (n == N) {
      ans = str;
    } else {
      if (str <= ans.substr(0, str.size())) {
        for (int i = 0; i < N; ++i) {
          if (!used[i]) {
            used[i] = true;
            dfs(n + 1, cat(str, s[i]));
            used[i] = false;
          }
        }
      }
    }
  }
}
int main() {
  while (cin >> N, N) {
    fill(&made[0][0], &made[0][0] + 11 * (1 << 10), false);

    for (int i = 0; i < N; ++i) {
      cin >> s[i];
    }

    made[0][0] = true;
    dp[0][0] = "";
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < (1 << N); ++j) {
        if (made[i][j]) {
          for (int k = 0; k < N; ++k) {
            if ((j & (1 << k)) == 0) {
              int nj = j | (1 << k);
              proc(cat(dp[i][j], s[k]), i + 1, nj);
              proc(cat(s[k], dp[i][j]), i + 1, nj);
            }
          }
        }
      }
    }
    ans = dp[N][(1 << N) - 1];

    //		cout << ans << endl;
    dfs(0, "");
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

string dp[11][1 << 10];
bool made[11][1 << 10] = {0};

void proc(string s, int i, int j) {
  if (!made[i][j] || s.size() < dp[i][j].size() ||
      (s.size() == dp[i][j].size() && s < dp[i][j])) {
    dp[i][j] = s;
  }
  made[i][j] = true;
}
string cat(string s1, string s2) {
  if (s1.size() >= s2.size()) {
    for (int i = 0; i <= s1.size() - s2.size(); ++i) {
      bool f = true;
      for (int j = 0; j < s2.size(); ++j) {
        if (s1[i + j] != s2[j]) {
          f = false;
          break;
        }
      }
      if (f) {
        return s1;
      }
    }
  }

  int len = 0;
  for (int k = min(s1.size(), s2.size()); k >= 0; --k) {
    bool f = true;
    for (int i = 0; i < k; ++i) {
      if (s1[s1.size() - k + i] != s2[i]) {
        f = false;
        break;
      }
    }
    if (f) {
      len = k;
      break;
    }
  }
  return s1 + s2.substr(len);
}

int N;
string s[10];
bool used[10] = {0};
string ans;
void dfs(int n, string str) {
  if (str.size() < ans.size() || (str.size() == ans.size() && str < ans)) {
    if (n == N) {
      ans = str;
    } else {
      for (int i = 0; i < N; ++i) {
        if (!used[i]) {
          used[i] = true;
          dfs(n + 1, cat(str, s[i]));
          used[i] = false;
        }
      }
    }
  }
}
int main() {
  while (cin >> N, N) {
    fill(&made[0][0], &made[0][0] + 11 * (1 << 10), false);

    for (int i = 0; i < N; ++i) {
      cin >> s[i];
    }

    made[0][0] = true;
    dp[0][0] = "";
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < (1 << N); ++j) {
        if (made[i][j]) {
          for (int k = 0; k < N; ++k) {
            if ((j & (1 << k)) == 0) {
              int nj = j | (1 << k);
              proc(cat(dp[i][j], s[k]), i + 1, nj);
              proc(cat(s[k], dp[i][j]), i + 1, nj);
            }
          }
        }
      }
    }
    ans = dp[N][(1 << N) - 1];

    dfs(0, "");
    cout << ans << endl;
  }
}