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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

struct edge {
  int to, cost;
};
typedef pair<int, int> P;
const int INF = 1 << 15;

string make_table(string x, string y) {
  string res = y;
  string y2 = y;
  for (int i = 0; i <= y.size(); i++) {
    y2.erase(0, 1);
    string tmp = x + y2;
    if (tmp.find(x) != string::npos && tmp.find(y) != string::npos)
      res = y2;
  }

  return res;
}

int main(void) {

  // cout<<make_table("abc","xyz")<<endl;
  // cout<<make_table("abc","abc")<<endl;
  // cout<<make_table("abc","bcd")<<endl;

  int n;
  while (cin >> n, n) {
    vector<string> vs;
    string ans = "";
    for (int i = 0; i < n; i++) {
      string tmp;
      cin >> tmp;
      bool ok = true;
      for (int j = 0; j < vs.size() && ok; j++)
        ok = (vs[j].find(tmp) == string::npos);
      if (ok) {
        vs.push_back(tmp);
        ans += tmp;
      }
    }

    vector<int> order(vs.size());
    for (int i = 0; i < order.size(); i++)
      order[i] = i;

    vector<vector<string>> table(vs.size(), vector<string>(vs.size()));

    for (int i = 0; i < vs.size(); i++) {
      for (int j = 0; j < vs.size(); j++)
        table[i][j] = make_table(vs[i], vs[j]);
    }

    // int cnt=0;
    do {
      // cout<<cnt++<<endl;
      string res = vs[order[0]];
      for (int i = 1; i < order.size(); i++) {
        res += table[order[i - 1]][order[i]];
      }
      /*
                              if(ans=="") ans=res;
                              else
         if(ans.size()>res.size()){cout<<ans<<"->"<<res<<"(size)"<<endl;
         ans=res;} else if(ans.size()==res.size() &&
         ans>res){cout<<ans<<"->"<<res<<"(order)"<<endl; ans=res;}
      */
      if (ans.size() > res.size() || ans.size() == res.size() && ans > res)
        ans = res;
    } while (next_permutation(order.begin(), order.end()));

    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

struct edge {
  int to, cost;
};
typedef pair<int, int> P;
const int INF = 1 << 15;

inline string add(string &x, string &y) {
  string res;
  for (int i = 0; i < min(x.size(), y.size()); i++) {
    string x2 = x, y2 = y;
    for (int j = 0; j < i; j++) {
      x2.erase(x2.end() - 1);
    }
    string tmp = x2 + y2;
    if (tmp.find(x) != string::npos && tmp.find(y) != string::npos)
      res = tmp;
  }
  return res;
}

int main(void) {
  int n;
  while (cin >> n, n) {
    vector<string> vs;
    string ans = "";
    for (int i = 0; i < n; i++) {
      string tmp;
      cin >> tmp;
      bool ok = true;
      for (int j = 0; j < vs.size() && ok; j++)
        ok = (vs[j].find(tmp) == string::npos);
      if (ok) {
        vs.push_back(tmp);
        ans += tmp;
      }
    }

    vector<int> order(vs.size());
    for (int i = 0; i < order.size(); i++)
      order[i] = i;

    do {
      string res = vs[order[0]];
      for (int i = 1; i < order.size(); i++) {
        res = add(res, vs[order[i]]);
      }
      /*
                              if(ans=="") ans=res;
                              else
         if(ans.size()>res.size()){cout<<ans<<"->"<<res<<"(size)"<<endl;
         ans=res;} else if(ans.size()==res.size() &&
         ans>res){cout<<ans<<"->"<<res<<"(order)"<<endl; ans=res;}
      */
      if (ans.size() > res.size() || ans.size() == res.size() && ans > res)
        ans = res;
    } while (next_permutation(order.begin(), order.end()));

    cout << ans << endl;
  }

  return 0;
}